-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ANSI escape codes provided in input from being stripped when rend…
…ering Fixes #248 > A recent commit (possibly 8e515d1#diff-546b6385118f60f64674170f786acf59f0ccce53d5d6ad4400409fc8363cfce1R74) has made it so that ANSI escape codes are now stripped. This makes it impossible to have colorised text inside prompt messages, e.g. if you use Confirm with a string that contains color text the color won't show up. This is a regression as this was possible with older versions (at least with 0.5.0).
- Loading branch information
1 parent
7fe8e5e
commit bf40860
Showing
4 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
use std::{collections::VecDeque, fmt::Display}; | ||
|
||
use crate::ui::{Key, Styled}; | ||
|
||
use super::{Terminal, TerminalSize}; | ||
|
||
pub struct MockTerminal { | ||
pub size: TerminalSize, | ||
pub input: VecDeque<Key>, | ||
pub output: VecDeque<MockTerminalToken>, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum MockTerminalToken { | ||
Text(Styled<String>), | ||
ClearLine, | ||
ClearUntilNewLine, | ||
CursorHide, | ||
CursorShow, | ||
CursorUp(u16), | ||
CursorDown(u16), | ||
CursorLeft(u16), | ||
CursorRight(u16), | ||
CursorMoveToColumn(u16), | ||
} | ||
|
||
impl<T> From<T> for MockTerminalToken | ||
where | ||
T: Display, | ||
{ | ||
fn from(val: T) -> Self { | ||
MockTerminalToken::Text(Styled::new(val.to_string())) | ||
} | ||
} | ||
|
||
impl MockTerminal { | ||
pub fn new() -> Self { | ||
Self { | ||
size: TerminalSize::new(80, 40), | ||
input: VecDeque::new(), | ||
output: VecDeque::new(), | ||
} | ||
} | ||
|
||
pub fn with_size(mut self, size: TerminalSize) -> Self { | ||
self.size = size; | ||
self | ||
} | ||
|
||
pub fn find_and_expect_token(&mut self, token: MockTerminalToken) { | ||
while let Some(actual) = self.output.pop_front() { | ||
if actual == token { | ||
return; | ||
} | ||
} | ||
|
||
panic!("Expected token not found: {:?}", token); | ||
} | ||
} | ||
|
||
impl Terminal for MockTerminal { | ||
fn get_size(&self) -> std::io::Result<TerminalSize> { | ||
Ok(self.size) | ||
} | ||
|
||
fn write<T: Display>(&mut self, val: T) -> std::io::Result<()> { | ||
let styled = Styled::new(format!("{}", val)); | ||
let token = MockTerminalToken::Text(styled); | ||
self.output.push_back(token); | ||
Ok(()) | ||
} | ||
|
||
fn write_styled<T: Display>(&mut self, val: &Styled<T>) -> std::io::Result<()> { | ||
let styled = Styled::new(format!("{}", val.content)).with_style_sheet(val.style); | ||
let token = MockTerminalToken::Text(styled); | ||
self.output.push_back(token); | ||
Ok(()) | ||
} | ||
|
||
fn clear_line(&mut self) -> std::io::Result<()> { | ||
let token = MockTerminalToken::ClearLine; | ||
self.output.push_back(token); | ||
Ok(()) | ||
} | ||
|
||
fn clear_until_new_line(&mut self) -> std::io::Result<()> { | ||
let token = MockTerminalToken::ClearUntilNewLine; | ||
self.output.push_back(token); | ||
Ok(()) | ||
} | ||
|
||
fn cursor_hide(&mut self) -> std::io::Result<()> { | ||
let token = MockTerminalToken::CursorHide; | ||
self.output.push_back(token); | ||
Ok(()) | ||
} | ||
|
||
fn cursor_show(&mut self) -> std::io::Result<()> { | ||
let token = MockTerminalToken::CursorShow; | ||
self.output.push_back(token); | ||
Ok(()) | ||
} | ||
|
||
fn cursor_up(&mut self, cnt: u16) -> std::io::Result<()> { | ||
let token = MockTerminalToken::CursorUp(cnt); | ||
self.output.push_back(token); | ||
Ok(()) | ||
} | ||
|
||
fn cursor_down(&mut self, cnt: u16) -> std::io::Result<()> { | ||
let token = MockTerminalToken::CursorDown(cnt); | ||
self.output.push_back(token); | ||
Ok(()) | ||
} | ||
|
||
fn cursor_left(&mut self, cnt: u16) -> std::io::Result<()> { | ||
let token = MockTerminalToken::CursorLeft(cnt); | ||
self.output.push_back(token); | ||
Ok(()) | ||
} | ||
|
||
fn cursor_right(&mut self, cnt: u16) -> std::io::Result<()> { | ||
let token = MockTerminalToken::CursorRight(cnt); | ||
self.output.push_back(token); | ||
Ok(()) | ||
} | ||
|
||
fn cursor_move_to_column(&mut self, idx: u16) -> std::io::Result<()> { | ||
let token = MockTerminalToken::CursorMoveToColumn(idx); | ||
self.output.push_back(token); | ||
Ok(()) | ||
} | ||
|
||
fn flush(&mut self) -> std::io::Result<()> { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters