termwiz: introduce host concept, prompt and coloring

The LineEditorHost trait allows colorizing the line editor.
This commit is contained in:
Wez Furlong committed 2019-05-27 12:42:19 -07:00
1 parent fb5d0464fb
commit 2b85d5dca4
3 files changed
+140 -26

No files matched your search

+24 -2
View File
@@ -1,10 +1,32 @@
use failure::Fallible;
use termwiz::lineedit::line_editor;
use termwiz::cell::AttributeChange;
use termwiz::color::{AnsiColor, ColorAttribute, RgbColor};
use termwiz::lineedit::{line_editor, LineEditorHost, OutputElement};
struct Host {}
impl LineEditorHost for Host {
// Render the prompt with a darkslateblue background color if
// the terminal supports true color, otherwise render it with
// a navy blue ansi color.
fn render_prompt(&self, prompt: &str) -> Vec<OutputElement> {
vec![
OutputElement::Attribute(AttributeChange::Background(
ColorAttribute::TrueColorWithPaletteFallback(
RgbColor::from_named("darkslateblue").unwrap(),
AnsiColor::Navy.into(),
),
)),
OutputElement::Text(prompt.to_owned()),
]
}
}
fn main() -> Fallible<()> {
let mut editor = line_editor()?;
let line = editor.read_line()?;
let mut host = Host {};
let line = editor.read_line(&mut host)?;
println!("read line: {:?}", line);
Ok(())
+66
View File
@@ -0,0 +1,66 @@
use crate::cell::{AttributeChange, CellAttributes};
use crate::surface::Change;
/// The `OutputElement` type allows returning graphic attribute changes
/// as well as textual output.
pub enum OutputElement {
/// Change a single attribute
Attribute(AttributeChange),
/// Change all possible attributes to the given set of values
AllAttributes(CellAttributes),
/// Printable text.
/// Control characters are rendered inert by transforming them
/// to space. CR and LF characters are interpreted by moving
/// the cursor position. CR moves the cursor to the start of
/// the line and LF moves the cursor down to the next line.
/// You typically want to use both together when sending in
/// a line break.
Text(String),
}
impl Into<Change> for OutputElement {
fn into(self) -> Change {
match self {
OutputElement::Attribute(a) => Change::Attribute(a),
OutputElement::AllAttributes(a) => Change::AllAttributes(a),
OutputElement::Text(t) => Change::Text(t),
}
}
}
/// The `LineEditorHost` trait allows an embedding application to influence
/// how the line editor functions.
/// A concrete implementation of the host with neutral defaults is provided
/// as `NopLineEditorHost`.
pub trait LineEditorHost {
/// Given a prompt string, return the rendered form of the prompt as
/// a sequence of `OutputElement` instances.
/// The implementation is free to interpret the prompt string however
/// it chooses; for instance, the application can opt to expand its own
/// application specific escape sequences as it sees fit.
/// The `OutputElement` type allows returning graphic attribute changes
/// as well as textual output.
/// The default implementation returns the prompt as-is with no coloring
/// and no textual transformation.
fn render_prompt(&self, prompt: &str) -> Vec<OutputElement> {
vec![OutputElement::Text(prompt.to_owned())]
}
/// Given a reference to the current line being edited and the position
/// of the cursor, return the rendered form of the line as a sequence
/// of `OutputElement` instances.
/// While this interface technically allows returning arbitrary Text sequences,
/// the application should preserve the column positions of the graphemes,
/// otherwise the terminal cursor position won't match up to the correct
/// location.
/// The `OutputElement` type allows returning graphic attribute changes
/// as well as textual output.
/// The default implementation returns the line as-is with no coloring.
fn highlight_line(&self, line: &str, _cursor_position: usize) -> Vec<OutputElement> {
vec![OutputElement::Text(line.to_owned())]
}
}
/// A concrete implementation of `LineEditorHost` that uses the default behaviors.
pub struct NopLineEditorHost {}
impl LineEditorHost for NopLineEditorHost {}
+50 -24
View File
@@ -3,13 +3,14 @@
//!
//! ```no_run
//! use failure::Fallible;
//! use termwiz::lineedit::line_editor;
//! use termwiz::lineedit::{line_editor,NopLineEditorHost};
//!
//! fn main() -> Fallible<()> {
//! let mut editor = line_editor()?;
//! let mut host = NopLineEditorHost{};
//!
//! let line = editor.read_line()?;
//! println!("read line: {}", line);
//! let line = editor.read_line(&mut host)?;
//! println!("read line: {:?}", line);
//!
//! Ok(())
//! }
@@ -42,25 +43,29 @@ use unicode_segmentation::GraphemeCursor;
use unicode_width::UnicodeWidthStr;
mod actions;
mod host;
pub use actions::{Action, Movement, RepeatCount};
pub use host::*;
/// The `LineEditor` struct provides line editing facilities similar
/// to those in the unix shell.
/// ```no_run
/// use failure::Fallible;
/// use termwiz::lineedit::line_editor;
/// use termwiz::lineedit::{line_editor,NopLineEditorHost};
///
/// fn main() -> Fallible<()> {
/// let mut editor = line_editor()?;
/// let mut host = NopLineEditorHost{};
///
/// let line = editor.read_line()?;
/// println!("read line: {}", line);
/// let line = editor.read_line(&mut host)?;
/// println!("read line: {:?}", line);
///
/// Ok(())
/// }
/// ```
pub struct LineEditor<T: Terminal> {
terminal: T,
prompt: String,
line: String,
/// byte index into the UTF-8 string data of the insertion
/// point. This is NOT the number of graphemes!
@@ -91,12 +96,35 @@ impl<T: Terminal> LineEditor<T> {
pub fn new(terminal: T) -> Self {
Self {
terminal,
prompt: "> ".to_owned(),
line: String::new(),
cursor: 0,
}
}
fn render(&mut self) -> Fallible<()> {
fn render(&mut self, host: &mut LineEditorHost) -> Fallible<()> {
let mut changes = vec![
Change::CursorPosition {
x: Position::Absolute(0),
y: Position::NoChange,
},
Change::ClearToEndOfScreen(Default::default()),
Change::AllAttributes(Default::default()),
];
let mut prompt_width = 0;
for ele in host.render_prompt(&self.prompt) {
if let OutputElement::Text(ref t) = ele {
prompt_width += UnicodeWidthStr::width(t.as_str());
}
changes.push(ele.into());
}
changes.push(Change::AllAttributes(Default::default()));
for ele in host.highlight_line(&self.line, self.cursor) {
changes.push(ele.into());
}
// In order to position the terminal cursor at the right spot,
// we need to compute how many graphemes away from the start of
// the line the current insertion point is. We can do this by
@@ -105,28 +133,26 @@ impl<T: Terminal> LineEditor<T> {
// the string, but this doesn't render correctly for glyphs that
// are double-width. Nothing about unicode is easy :-/
let grapheme_count = UnicodeWidthStr::width(&self.line[0..self.cursor]);
self.terminal.render(&[
Change::CursorPosition {
x: Position::Absolute(0),
y: Position::NoChange,
},
Change::ClearToEndOfScreen(Default::default()),
Change::Text(self.line.clone()),
Change::CursorPosition {
x: Position::Absolute(grapheme_count),
y: Position::NoChange,
},
])?;
changes.push(Change::CursorPosition {
x: Position::Absolute(prompt_width + grapheme_count),
y: Position::NoChange,
});
self.terminal.render(&changes)?;
Ok(())
}
pub fn set_prompt(&mut self, prompt: &str) {
self.prompt = prompt.to_owned();
}
/// Enter line editing mode.
/// Control is not returned to the caller until a line has been
/// accepted, or until an error is detected.
/// Returns Ok(None) if the editor was cancelled eg: via CTRL-C.
pub fn read_line(&mut self) -> Fallible<Option<String>> {
pub fn read_line(&mut self, host: &mut LineEditorHost) -> Fallible<Option<String>> {
self.terminal.set_raw_mode()?;
let res = self.read_line_impl();
let res = self.read_line_impl(host);
self.terminal.set_cooked_mode()?;
println!();
res
@@ -346,11 +372,11 @@ impl<T: Terminal> LineEditor<T> {
self.cursor = new_cursor.min(self.line.len());
}
fn read_line_impl(&mut self) -> Fallible<Option<String>> {
fn read_line_impl(&mut self, host: &mut LineEditorHost) -> Fallible<Option<String>> {
self.line.clear();
self.cursor = 0;
self.render()?;
self.render(host)?;
while let Some(event) = self.terminal.poll_input(None)? {
match self.resolve_action(&event) {
Some(Action::Cancel) => return Ok(None),
@@ -378,7 +404,7 @@ impl<T: Terminal> LineEditor<T> {
}
_ => {}
}
self.render()?;
self.render(host)?;
}
Ok(Some(self.line.clone()))
}