Skip to content

Commit 9faa5d3

Browse files
committed
Avoid asking for terminal size on each rendering
1 parent bcc2a13 commit 9faa5d3

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

src/watch.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mod terminal_event;
2929
enum WatchEvent {
3030
Input(InputEvent),
3131
FileChange { exercise_ind: usize },
32-
TerminalResize,
32+
TerminalResize { width: u16 },
3333
NotifyErr(notify::Error),
3434
TerminalEventErr(io::Error),
3535
}
@@ -72,7 +72,7 @@ fn run_watch(
7272
None
7373
};
7474

75-
let mut watch_state = WatchState::new(app_state, manual_run);
75+
let mut watch_state = WatchState::build(app_state, manual_run)?;
7676

7777
let mut stdout = io::stdout().lock();
7878
watch_state.run_current_exercise(&mut stdout)?;
@@ -96,7 +96,9 @@ fn run_watch(
9696
WatchEvent::FileChange { exercise_ind } => {
9797
watch_state.handle_file_change(exercise_ind, &mut stdout)?;
9898
}
99-
WatchEvent::TerminalResize => watch_state.render(&mut stdout)?,
99+
WatchEvent::TerminalResize { width } => {
100+
watch_state.update_term_width(width, &mut stdout)?;
101+
}
100102
WatchEvent::NotifyErr(e) => return Err(Error::from(e).context(NOTIFY_ERR)),
101103
WatchEvent::TerminalEventErr(e) => {
102104
return Err(Error::from(e).context("Terminal event listener failed"));

src/watch/state.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::Result;
1+
use anyhow::{Context, Result};
22
use crossterm::{
33
style::{
44
Attribute, Attributes, Color, ResetColor, SetAttribute, SetAttributes, SetForegroundColor,
@@ -27,17 +27,23 @@ pub struct WatchState<'a> {
2727
show_hint: bool,
2828
done_status: DoneStatus,
2929
manual_run: bool,
30+
term_width: u16,
3031
}
3132

3233
impl<'a> WatchState<'a> {
33-
pub fn new(app_state: &'a mut AppState, manual_run: bool) -> Self {
34-
Self {
34+
pub fn build(app_state: &'a mut AppState, manual_run: bool) -> Result<Self> {
35+
let term_width = terminal::size()
36+
.context("Failed to get the terminal size")?
37+
.0;
38+
39+
Ok(Self {
3540
app_state,
3641
output: Vec::with_capacity(OUTPUT_CAPACITY),
3742
show_hint: false,
3843
done_status: DoneStatus::Pending,
3944
manual_run,
40-
}
45+
term_width,
46+
})
4147
}
4248

4349
pub fn run_current_exercise(&mut self, stdout: &mut StdoutLock) -> Result<()> {
@@ -175,12 +181,11 @@ impl<'a> WatchState<'a> {
175181
)?;
176182
}
177183

178-
let line_width = terminal::size()?.0;
179184
progress_bar(
180185
stdout,
181186
self.app_state.n_done(),
182187
self.app_state.exercises().len() as u16,
183-
line_width,
188+
self.term_width,
184189
)?;
185190

186191
stdout.write_all(b"\nCurrent exercise: ")?;
@@ -202,4 +207,13 @@ impl<'a> WatchState<'a> {
202207

203208
Ok(())
204209
}
210+
211+
pub fn update_term_width(&mut self, width: u16, stdout: &mut StdoutLock) -> io::Result<()> {
212+
if self.term_width != width {
213+
self.term_width = width;
214+
self.render(stdout)?;
215+
}
216+
217+
Ok(())
218+
}
205219
}

src/watch/terminal_event.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
4343
return;
4444
}
4545
}
46-
Event::Resize(_, _) => {
47-
if tx.send(WatchEvent::TerminalResize).is_err() {
46+
Event::Resize(width, _) => {
47+
if tx.send(WatchEvent::TerminalResize { width }).is_err() {
4848
return;
4949
}
5050
}

0 commit comments

Comments
 (0)