mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 15:26:29 +00:00
Avoid asking for terminal size on each rendering
This commit is contained in:
parent
bcc2a136c8
commit
9faa5d3aa4
3 changed files with 27 additions and 11 deletions
|
@ -29,7 +29,7 @@ mod terminal_event;
|
||||||
enum WatchEvent {
|
enum WatchEvent {
|
||||||
Input(InputEvent),
|
Input(InputEvent),
|
||||||
FileChange { exercise_ind: usize },
|
FileChange { exercise_ind: usize },
|
||||||
TerminalResize,
|
TerminalResize { width: u16 },
|
||||||
NotifyErr(notify::Error),
|
NotifyErr(notify::Error),
|
||||||
TerminalEventErr(io::Error),
|
TerminalEventErr(io::Error),
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ fn run_watch(
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut watch_state = WatchState::new(app_state, manual_run);
|
let mut watch_state = WatchState::build(app_state, manual_run)?;
|
||||||
|
|
||||||
let mut stdout = io::stdout().lock();
|
let mut stdout = io::stdout().lock();
|
||||||
watch_state.run_current_exercise(&mut stdout)?;
|
watch_state.run_current_exercise(&mut stdout)?;
|
||||||
|
@ -96,7 +96,9 @@ fn run_watch(
|
||||||
WatchEvent::FileChange { exercise_ind } => {
|
WatchEvent::FileChange { exercise_ind } => {
|
||||||
watch_state.handle_file_change(exercise_ind, &mut stdout)?;
|
watch_state.handle_file_change(exercise_ind, &mut stdout)?;
|
||||||
}
|
}
|
||||||
WatchEvent::TerminalResize => watch_state.render(&mut stdout)?,
|
WatchEvent::TerminalResize { width } => {
|
||||||
|
watch_state.update_term_width(width, &mut stdout)?;
|
||||||
|
}
|
||||||
WatchEvent::NotifyErr(e) => return Err(Error::from(e).context(NOTIFY_ERR)),
|
WatchEvent::NotifyErr(e) => return Err(Error::from(e).context(NOTIFY_ERR)),
|
||||||
WatchEvent::TerminalEventErr(e) => {
|
WatchEvent::TerminalEventErr(e) => {
|
||||||
return Err(Error::from(e).context("Terminal event listener failed"));
|
return Err(Error::from(e).context("Terminal event listener failed"));
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use anyhow::Result;
|
use anyhow::{Context, Result};
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
style::{
|
style::{
|
||||||
Attribute, Attributes, Color, ResetColor, SetAttribute, SetAttributes, SetForegroundColor,
|
Attribute, Attributes, Color, ResetColor, SetAttribute, SetAttributes, SetForegroundColor,
|
||||||
|
@ -27,17 +27,23 @@ pub struct WatchState<'a> {
|
||||||
show_hint: bool,
|
show_hint: bool,
|
||||||
done_status: DoneStatus,
|
done_status: DoneStatus,
|
||||||
manual_run: bool,
|
manual_run: bool,
|
||||||
|
term_width: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> WatchState<'a> {
|
impl<'a> WatchState<'a> {
|
||||||
pub fn new(app_state: &'a mut AppState, manual_run: bool) -> Self {
|
pub fn build(app_state: &'a mut AppState, manual_run: bool) -> Result<Self> {
|
||||||
Self {
|
let term_width = terminal::size()
|
||||||
|
.context("Failed to get the terminal size")?
|
||||||
|
.0;
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
app_state,
|
app_state,
|
||||||
output: Vec::with_capacity(OUTPUT_CAPACITY),
|
output: Vec::with_capacity(OUTPUT_CAPACITY),
|
||||||
show_hint: false,
|
show_hint: false,
|
||||||
done_status: DoneStatus::Pending,
|
done_status: DoneStatus::Pending,
|
||||||
manual_run,
|
manual_run,
|
||||||
}
|
term_width,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_current_exercise(&mut self, stdout: &mut StdoutLock) -> Result<()> {
|
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(
|
progress_bar(
|
||||||
stdout,
|
stdout,
|
||||||
self.app_state.n_done(),
|
self.app_state.n_done(),
|
||||||
self.app_state.exercises().len() as u16,
|
self.app_state.exercises().len() as u16,
|
||||||
line_width,
|
self.term_width,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
stdout.write_all(b"\nCurrent exercise: ")?;
|
stdout.write_all(b"\nCurrent exercise: ")?;
|
||||||
|
@ -202,4 +207,13 @@ impl<'a> WatchState<'a> {
|
||||||
|
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,8 +43,8 @@ pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Resize(_, _) => {
|
Event::Resize(width, _) => {
|
||||||
if tx.send(WatchEvent::TerminalResize).is_err() {
|
if tx.send(WatchEvent::TerminalResize { width }).is_err() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue