mirror of
https://github.com/notohh/rustlings.git
synced 2024-12-17 22:58:08 -05:00
Merge branch 'rust-lang:main' into main
This commit is contained in:
commit
e96623588c
2 changed files with 9 additions and 21 deletions
|
@ -43,7 +43,6 @@ pub struct ListState<'a> {
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
term_width: u16,
|
term_width: u16,
|
||||||
term_height: u16,
|
term_height: u16,
|
||||||
separator_line: Vec<u8>,
|
|
||||||
show_footer: bool,
|
show_footer: bool,
|
||||||
pub search_query: String,
|
pub search_query: String,
|
||||||
}
|
}
|
||||||
|
@ -77,7 +76,6 @@ impl<'a> ListState<'a> {
|
||||||
// Set by `set_term_size`
|
// Set by `set_term_size`
|
||||||
term_width: 0,
|
term_width: 0,
|
||||||
term_height: 0,
|
term_height: 0,
|
||||||
separator_line: Vec::new(),
|
|
||||||
show_footer: true,
|
show_footer: true,
|
||||||
search_query: String::new(),
|
search_query: String::new(),
|
||||||
};
|
};
|
||||||
|
@ -97,14 +95,10 @@ impl<'a> ListState<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let header_height = 1;
|
let header_height = 1;
|
||||||
// 2 separators, 1 progress bar, 2 footer message lines.
|
// 1 progress bar, 2 footer message lines.
|
||||||
let footer_height = 5;
|
let footer_height = 3;
|
||||||
self.show_footer = height > header_height + footer_height;
|
self.show_footer = height > header_height + footer_height;
|
||||||
|
|
||||||
if self.show_footer {
|
|
||||||
self.separator_line = "─".as_bytes().repeat(width as usize);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.scroll_state.set_max_n_rows_to_display(
|
self.scroll_state.set_max_n_rows_to_display(
|
||||||
height.saturating_sub(header_height + u16::from(self.show_footer) * footer_height)
|
height.saturating_sub(header_height + u16::from(self.show_footer) * footer_height)
|
||||||
as usize,
|
as usize,
|
||||||
|
@ -204,9 +198,6 @@ impl<'a> ListState<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.show_footer {
|
if self.show_footer {
|
||||||
stdout.write_all(&self.separator_line)?;
|
|
||||||
next_ln(stdout)?;
|
|
||||||
|
|
||||||
progress_bar(
|
progress_bar(
|
||||||
&mut MaxLenWriter::new(stdout, self.term_width as usize),
|
&mut MaxLenWriter::new(stdout, self.term_width as usize),
|
||||||
self.app_state.n_done(),
|
self.app_state.n_done(),
|
||||||
|
@ -215,9 +206,6 @@ impl<'a> ListState<'a> {
|
||||||
)?;
|
)?;
|
||||||
next_ln(stdout)?;
|
next_ln(stdout)?;
|
||||||
|
|
||||||
stdout.write_all(&self.separator_line)?;
|
|
||||||
next_ln(stdout)?;
|
|
||||||
|
|
||||||
let mut writer = MaxLenWriter::new(stdout, self.term_width as usize);
|
let mut writer = MaxLenWriter::new(stdout, self.term_width as usize);
|
||||||
if self.message.is_empty() {
|
if self.message.is_empty() {
|
||||||
// Help footer message
|
// Help footer message
|
||||||
|
|
14
src/term.rs
14
src/term.rs
|
@ -5,7 +5,7 @@ use std::{
|
||||||
|
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
cursor::MoveTo,
|
cursor::MoveTo,
|
||||||
style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor},
|
style::{Attribute, Color, SetAttribute, SetForegroundColor},
|
||||||
terminal::{Clear, ClearType},
|
terminal::{Clear, ClearType},
|
||||||
Command, QueueableCommand,
|
Command, QueueableCommand,
|
||||||
};
|
};
|
||||||
|
@ -93,20 +93,19 @@ pub fn progress_bar<'a>(
|
||||||
total: u16,
|
total: u16,
|
||||||
line_width: u16,
|
line_width: u16,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
|
debug_assert!(total < 1000);
|
||||||
debug_assert!(progress <= total);
|
debug_assert!(progress <= total);
|
||||||
|
|
||||||
const PREFIX: &[u8] = b"Progress: [";
|
const PREFIX: &[u8] = b"Progress: [";
|
||||||
const PREFIX_WIDTH: u16 = PREFIX.len() as u16;
|
const PREFIX_WIDTH: u16 = PREFIX.len() as u16;
|
||||||
// Leaving the last char empty (_) for `total` > 99.
|
const POSTFIX_WIDTH: u16 = "] xxx/xxx".len() as u16;
|
||||||
const POSTFIX_WIDTH: u16 = "] xxx/xx exercises_".len() as u16;
|
|
||||||
const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH;
|
const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH;
|
||||||
const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4;
|
const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4;
|
||||||
|
|
||||||
if line_width < MIN_LINE_WIDTH {
|
if line_width < MIN_LINE_WIDTH {
|
||||||
writer.write_ascii(b"Progress: ")?;
|
writer.write_ascii(b"Progress: ")?;
|
||||||
// Integers are in ASCII.
|
// Integers are in ASCII.
|
||||||
writer.write_ascii(format!("{progress}/{total}").as_bytes())?;
|
return writer.write_ascii(format!("{progress}/{total}").as_bytes());
|
||||||
return writer.write_ascii(b" exercises");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let stdout = writer.stdout();
|
let stdout = writer.stdout();
|
||||||
|
@ -133,8 +132,9 @@ pub fn progress_bar<'a>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stdout.queue(ResetColor)?;
|
stdout.queue(SetForegroundColor(Color::Reset))?;
|
||||||
write!(stdout, "] {progress:>3}/{total} exercises")
|
|
||||||
|
write!(stdout, "] {progress:>3}/{total}")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_terminal(stdout: &mut StdoutLock) -> io::Result<()> {
|
pub fn clear_terminal(stdout: &mut StdoutLock) -> io::Result<()> {
|
||||||
|
|
Loading…
Reference in a new issue