Skip to content

Commit

Permalink
Improve incremental renders and handling of terminal window resizing (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelmello authored Jan 4, 2024
1 parent 75bf421 commit 8e515d1
Show file tree
Hide file tree
Showing 12 changed files with 537 additions and 235 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- Added 'without_filtering' to both Select and MultiSelect, useful when you want to simplify the UX if the filter does not add any value, such as when the list is already short.
- Added 'with_answered_prompt_prefix' to RenderConfig to allow customization of answered prompt prefix.
- Revamped keybindings for DateSelect.
- Improved rendering, with optimizations on incremental rendering and terminal resizing.

### Fixes

Expand Down
1 change: 1 addition & 0 deletions inquire/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ newline-converter = "0.3"
once_cell = "1.18.0"
unicode-segmentation = "1"
unicode-width = "0.1"
fxhash = "0.2"

[dev-dependencies]
rstest = "0.18.2"
Expand Down
1 change: 0 additions & 1 deletion inquire/src/prompts/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ where
{
fn read_key(&mut self) -> crate::error::InquireResult<Key> {
let key = self.next();
println!("key: {:?}", key);

match key {
Some(key) => Ok(key),
Expand Down
12 changes: 0 additions & 12 deletions inquire/src/terminal/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ use super::Terminal;
#[derive(Clone)]
pub struct ConsoleTerminal {
term: Term,
in_memory_content: String,
}

impl ConsoleTerminal {
#[allow(unused)]
pub fn new() -> Self {
Self {
term: Term::stderr(),
in_memory_content: String::new(),
}
}
}
Expand Down Expand Up @@ -80,12 +78,10 @@ impl Terminal for ConsoleTerminal {
}

fn write<T: std::fmt::Display>(&mut self, val: T) -> Result<()> {
self.in_memory_content.push_str(&val.to_string());
write!(self.term, "{}", val)
}

fn write_styled<T: std::fmt::Display>(&mut self, val: &Styled<T>) -> Result<()> {
self.in_memory_content.push_str(&val.content.to_string());
let styled_object = Style::from(val.style).apply_to(&val.content);
write!(self.term, "{}", styled_object)
}
Expand All @@ -105,14 +101,6 @@ impl Terminal for ConsoleTerminal {
fn cursor_show(&mut self) -> Result<()> {
self.term.show_cursor()
}

fn get_in_memory_content(&self) -> &str {
&self.in_memory_content
}

fn clear_in_memory_content(&mut self) {
self.in_memory_content.clear();
}
}

impl Drop for ConsoleTerminal {
Expand Down
12 changes: 0 additions & 12 deletions inquire/src/terminal/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ enum IO {

pub struct CrosstermTerminal {
io: IO,
in_memory_content: String,
}

pub struct CrosstermKeyReader;
Expand All @@ -51,7 +50,6 @@ impl CrosstermTerminal {

Ok(Self {
io: IO::Std(stderr()),
in_memory_content: String::new(),
})
}

Expand Down Expand Up @@ -140,7 +138,6 @@ impl Terminal for CrosstermTerminal {
}

fn write<T: std::fmt::Display>(&mut self, val: T) -> Result<()> {
self.in_memory_content.push_str(&val.to_string());
self.write_command(Print(val))
}

Expand Down Expand Up @@ -185,14 +182,6 @@ impl Terminal for CrosstermTerminal {
fn cursor_show(&mut self) -> Result<()> {
self.write_command(cursor::Show)
}

fn get_in_memory_content(&self) -> &str {
&self.in_memory_content
}

fn clear_in_memory_content(&mut self) {
self.in_memory_content.clear();
}
}

impl Drop for CrosstermTerminal {
Expand Down Expand Up @@ -345,7 +334,6 @@ mod test {
pub fn new_in_memory_output() -> Self {
Self {
io: IO::Test(Vec::new()),
in_memory_content: String::new(),
}
}

Expand Down
3 changes: 0 additions & 3 deletions inquire/src/terminal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ pub trait Terminal: Sized {
fn clear_line(&mut self) -> Result<()>;
fn clear_until_new_line(&mut self) -> Result<()>;

fn get_in_memory_content(&self) -> &str;
fn clear_in_memory_content(&mut self);

fn cursor_hide(&mut self) -> Result<()>;
fn cursor_show(&mut self) -> Result<()>;
fn cursor_up(&mut self, cnt: u16) -> Result<()>;
Expand Down
12 changes: 0 additions & 12 deletions inquire/src/terminal/termion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ impl InputReader for TermionKeyReader {

pub struct TermionTerminal<'a> {
io: IO<'a>,
in_memory_content: String,
}

impl<'a> TermionTerminal<'a> {
Expand All @@ -65,7 +64,6 @@ impl<'a> TermionTerminal<'a> {

Ok(Self {
io: IO::TTY(raw_terminal),
in_memory_content: String::new(),
})
}

Expand All @@ -76,7 +74,6 @@ impl<'a> TermionTerminal<'a> {
pub fn new_with_writer<W: 'a + Write>(writer: &'a mut W) -> Self {
Self {
io: IO::Custom(writer),
in_memory_content: String::new(),
}
}

Expand Down Expand Up @@ -161,7 +158,6 @@ impl<'a> Terminal for TermionTerminal<'a> {
}

fn write<T: std::fmt::Display>(&mut self, val: T) -> Result<()> {
self.in_memory_content.push_str(&val.to_string());
write!(self.get_writer(), "{}", val)
}

Expand Down Expand Up @@ -206,14 +202,6 @@ impl<'a> Terminal for TermionTerminal<'a> {
fn cursor_show(&mut self) -> Result<()> {
write!(self.get_writer(), "{}", termion::cursor::Show)
}

fn get_in_memory_content(&self) -> &str {
self.in_memory_content.as_ref()
}

fn clear_in_memory_content(&mut self) {
self.in_memory_content.clear();
}
}

impl<'a> Drop for TermionTerminal<'a> {
Expand Down
Loading

0 comments on commit 8e515d1

Please sign in to comment.