summaryrefslogtreecommitdiff
path: root/src/watch
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-09-05 17:45:27 +0200
committermo8it <mo8it@proton.me>2024-09-05 17:45:27 +0200
commit9faa5d3aa48f7a94ed87e61ad6ea659579f1311a (patch)
treec841dada045258cfec968e664f848c879d1713b2 /src/watch
parentbcc2a136c8b086a660b8e656c2cd9398f47435f4 (diff)
Avoid asking for terminal size on each rendering
Diffstat (limited to 'src/watch')
-rw-r--r--src/watch/state.rs26
-rw-r--r--src/watch/terminal_event.rs4
2 files changed, 22 insertions, 8 deletions
diff --git a/src/watch/state.rs b/src/watch/state.rs
index 75a0c9e..e66cbee 100644
--- a/src/watch/state.rs
+++ b/src/watch/state.rs
@@ -1,4 +1,4 @@
-use anyhow::Result;
+use anyhow::{Context, Result};
use crossterm::{
style::{
Attribute, Attributes, Color, ResetColor, SetAttribute, SetAttributes, SetForegroundColor,
@@ -27,17 +27,23 @@ pub struct WatchState<'a> {
show_hint: bool,
done_status: DoneStatus,
manual_run: bool,
+ term_width: u16,
}
impl<'a> WatchState<'a> {
- pub fn new(app_state: &'a mut AppState, manual_run: bool) -> Self {
- Self {
+ pub fn build(app_state: &'a mut AppState, manual_run: bool) -> Result<Self> {
+ let term_width = terminal::size()
+ .context("Failed to get the terminal size")?
+ .0;
+
+ Ok(Self {
app_state,
output: Vec::with_capacity(OUTPUT_CAPACITY),
show_hint: false,
done_status: DoneStatus::Pending,
manual_run,
- }
+ term_width,
+ })
}
pub fn run_current_exercise(&mut self, stdout: &mut StdoutLock) -> Result<()> {
@@ -175,12 +181,11 @@ impl<'a> WatchState<'a> {
)?;
}
- let line_width = terminal::size()?.0;
progress_bar(
stdout,
self.app_state.n_done(),
self.app_state.exercises().len() as u16,
- line_width,
+ self.term_width,
)?;
stdout.write_all(b"\nCurrent exercise: ")?;
@@ -202,4 +207,13 @@ impl<'a> WatchState<'a> {
Ok(())
}
+
+ pub fn update_term_width(&mut self, width: u16, stdout: &mut StdoutLock) -> io::Result<()> {
+ if self.term_width != width {
+ self.term_width = width;
+ self.render(stdout)?;
+ }
+
+ Ok(())
+ }
}
diff --git a/src/watch/terminal_event.rs b/src/watch/terminal_event.rs
index 0762151..ca3a846 100644
--- a/src/watch/terminal_event.rs
+++ b/src/watch/terminal_event.rs
@@ -43,8 +43,8 @@ pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
return;
}
}
- Event::Resize(_, _) => {
- if tx.send(WatchEvent::TerminalResize).is_err() {
+ Event::Resize(width, _) => {
+ if tx.send(WatchEvent::TerminalResize { width }).is_err() {
return;
}
}