termwiz: line editor: add ChangeSequence helper

This helps us keep track of the extent and cursor position that
we render for the line editor, making it easier to make the editor
rendering more fancy.
This commit is contained in:
Wez Furlong committed 2020-04-10 20:42:02 -07:00
1 parent 8963fb76aa
commit d4df39f793
2 files changed
+216 -74

No files matched your search

+82 -73
View File
@@ -37,11 +37,11 @@
//! Alt-b, Alt-Left | Move the cursor backwards one word
//! Alt-f, Alt-Right | Move the cursor forwards one word
use crate::caps::{Capabilities, ProbeHints};
use crate::cell::unicode_column_width;
use crate::input::{InputEvent, KeyCode, KeyEvent, Modifiers};
use crate::surface::change::ChangeSequence;
use crate::surface::{Change, Position};
use crate::terminal::{new_terminal, Terminal};
use unicode_segmentation::{GraphemeCursor, UnicodeSegmentation};
use unicode_segmentation::GraphemeCursor;
mod actions;
mod history;
@@ -79,12 +79,8 @@ pub struct LineEditor<'term> {
completion: Option<CompletionState>,
/// How many rows of text we last output
last_render_height: usize,
/// on which row of the last render we think the cursor is
last_render_cursor_y: usize,
/// For a multi-line prompt, how many new lines it contains
prompt_height: usize,
move_to_editor_start: Option<Change>,
move_to_editor_end: Option<Change>,
state: EditorState,
}
@@ -162,9 +158,8 @@ impl<'term> LineEditor<'term> {
history_pos: None,
bottom_line: None,
completion: None,
last_render_height: 0,
last_render_cursor_y: 0,
prompt_height: 0,
move_to_editor_start: None,
move_to_editor_end: None,
state: EditorState::Inactive,
}
}
@@ -172,35 +167,14 @@ impl<'term> LineEditor<'term> {
fn render(&mut self, host: &mut dyn LineEditorHost) -> anyhow::Result<()> {
let screen_size = self.terminal.get_screen_size()?;
let mut changes = vec![
Change::CursorPosition {
x: Position::Absolute(0),
y: Position::Relative(-1 * self.last_render_cursor_y as isize),
},
Change::ClearToEndOfScreen(Default::default()),
Change::AllAttributes(Default::default()),
];
let mut prompt_width = 0;
self.prompt_height = 0;
let mut changes = ChangeSequence::new(screen_size.rows, screen_size.cols);
changes.add(Change::ClearToEndOfScreen(Default::default()));
changes.add(Change::AllAttributes(Default::default()));
for ele in host.render_prompt(&self.prompt) {
if let OutputElement::Text(ref t) = ele {
// To accomodate multi-line prompts we need to understand
// both the horizontal and vertical position of the end of
// the prompt so we break it down here.
for g in t.as_str().graphemes(true) {
if g == "\n" || g == "\r\n" {
prompt_width = 0;
self.prompt_height += 1;
} else {
prompt_width += unicode_column_width(g);
}
}
}
changes.push(ele.into());
changes.add(ele);
}
changes.push(Change::AllAttributes(Default::default()));
changes.add(Change::AllAttributes(Default::default()));
// If we're searching, the input area shows the match rather than the input,
// and the cursor moves to the first matching character
@@ -213,22 +187,47 @@ impl<'term> LineEditor<'term> {
_ => (&self.line, self.cursor),
};
let cursor_position_after_printing_prompt = changes.current_cursor_position();
let (elements, cursor_x_pos) = host.highlight_line(line_to_display, cursor);
let mut output_width = 0;
// Calculate what the cursor position would be after printing X columns
// of text from the specified location.
// Returns (x, y) of the resultant cursor position.
fn compute_cursor_after_printing_x_columns(
cursor_x: usize,
cursor_y: isize,
delta: usize,
screen_cols: usize,
) -> (usize, isize) {
let y = (cursor_x + delta) / screen_cols;
let x = (cursor_x + delta) % screen_cols;
let row = cursor_y + y as isize;
let col = x.max(0) as usize;
(col, row)
}
let cursor_position = compute_cursor_after_printing_x_columns(
cursor_position_after_printing_prompt.0,
cursor_position_after_printing_prompt.1,
cursor_x_pos,
screen_size.cols,
);
for ele in elements {
if let OutputElement::Text(ref t) = ele {
output_width += unicode_column_width(t.as_str());
}
changes.push(ele.into());
changes.add(ele);
}
let visible_cursor_column = (prompt_width + cursor_x_pos) % screen_size.cols;
if visible_cursor_column == 0 && output_width == cursor_x_pos {
changes.push("\n".into());
let cursor_after_line_render = changes.current_cursor_position();
if cursor_after_line_render.0 == screen_size.cols {
// If the cursor position remains in the first column
// then the renderer may still consider itself to be on
// the prior line; force out an additional character to force
// it to apply wrapping/flush.
changes.add(" ");
}
let mut searching = false;
if let EditorState::Searching {
style, direction, ..
} = &self.state
@@ -238,29 +237,42 @@ impl<'term> LineEditor<'term> {
(SearchStyle::Substring, SearchDirection::Backwards) => "bck-i-search",
(SearchStyle::Substring, SearchDirection::Forwards) => "fwd-i-search",
};
changes.push(format!("\r\n{}: {}_", label, self.line).into());
searching = true;
// We position the actual cursor on the matching portion of
// the text in the line editing area, but since the input
// is drawn here, we render an `_` to indicate where the input
// position really is.
changes.add(format!("\r\n{}: {}_", label, self.line));
}
self.last_render_height = self.prompt_height
+ ((prompt_width + output_width) as f32 / screen_size.cols as f32).ceil() as usize
+ if visible_cursor_column == 0 && output_width == cursor_x_pos {
1
} else {
0
}
+ if searching { 1 } else { 0 };
self.last_render_cursor_y =
self.prompt_height + (prompt_width + cursor_x_pos) / screen_size.cols;
// Add some debugging status at the bottom
/*
changes.add(format!(
"\r\n{:?} {:?}",
cursor_position,
(changes.cursor_x, changes.cursor_y)
));
*/
changes.push(Change::CursorPosition {
x: Position::Absolute(visible_cursor_column),
y: Position::Relative(
(1 + self.last_render_cursor_y as isize) - (self.last_render_height as isize),
),
let render_height = changes.render_height();
changes.move_to(cursor_position);
let mut changes = changes.consume();
if let Some(start) = self.move_to_editor_start.take() {
changes.insert(0, start);
}
self.terminal.render(&changes)?;
self.move_to_editor_start.replace(Change::CursorPosition {
x: Position::Absolute(0),
y: Position::Relative(-1 * cursor_position.1),
});
self.move_to_editor_end.replace(Change::CursorPosition {
x: Position::Absolute(0),
y: Position::Relative(1 + render_height as isize - cursor_position.1),
});
self.terminal.render(&changes)?;
Ok(())
}
@@ -280,20 +292,17 @@ impl<'term> LineEditor<'term> {
// Clear out the last render info so that we don't over-compensate
// on the first call to render().
self.last_render_height = 0;
self.last_render_cursor_y = 0;
self.move_to_editor_end.take();
self.move_to_editor_start.take();
self.terminal.set_raw_mode()?;
self.state = EditorState::Editing;
let res = self.read_line_impl(host);
self.state = EditorState::Inactive;
self.terminal.render(&[Change::CursorPosition {
x: Position::Absolute(0),
y: Position::Relative(
(self.last_render_height as isize) - (self.last_render_cursor_y as isize),
),
}])?;
if let Some(move_end) = self.move_to_editor_end.take() {
self.terminal.render(&[move_end])?;
}
self.terminal.flush()?;
self.terminal.set_cooked_mode()?;
+134 -1
View File
@@ -1,9 +1,10 @@
use crate::cell::{AttributeChange, CellAttributes};
use crate::cell::{unicode_column_width, AttributeChange, CellAttributes};
use crate::color::ColorAttribute;
pub use crate::image::{ImageData, TextureCoordinate};
use crate::surface::{CursorShape, Position};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use unicode_segmentation::UnicodeSegmentation;
/// `Change` describes an update operation to be applied to a `Surface`.
/// Changes to the active attributes (color, style), moving the cursor
@@ -115,6 +116,138 @@ impl From<AttributeChange> for Change {
}
}
/// Keeps track of a run of changes and allows reasoning about the cursor
/// position and the extent of the screen that the sequence will affect.
/// This is useful for example when implementing something like a LineEditor
/// where you don't want to take control over the entire surface but do want
/// to be able to emit a dynamically sized output relative to the cursor
/// position at the time that the editor is invoked.
pub struct ChangeSequence {
changes: Vec<Change>,
screen_rows: usize,
screen_cols: usize,
pub(crate) cursor_x: usize,
pub(crate) cursor_y: isize,
render_y_max: isize,
render_y_min: isize,
}
impl ChangeSequence {
pub fn new(rows: usize, cols: usize) -> Self {
Self {
changes: vec![],
screen_rows: rows,
screen_cols: cols,
cursor_x: 0,
cursor_y: 0,
render_y_max: 0,
render_y_min: 0,
}
}
pub fn consume(self) -> Vec<Change> {
self.changes
}
/// Returns the cursor position, (x, y).
pub fn current_cursor_position(&self) -> (usize, isize) {
(self.cursor_x, self.cursor_y)
}
pub fn move_to(&mut self, (cursor_x, cursor_y): (usize, isize)) {
self.add(Change::CursorPosition {
x: Position::Relative(cursor_x as isize - self.cursor_x as isize),
y: Position::Relative(cursor_y - self.cursor_y),
});
}
/// Returns the total number of rows affected
pub fn render_height(&self) -> usize {
(self.render_y_max - self.render_y_min).max(0).abs() as usize
}
fn update_render_height(&mut self) {
self.render_y_max = self.render_y_max.max(self.cursor_y);
self.render_y_min = self.render_y_min.min(self.cursor_y);
}
pub fn add_changes(&mut self, changes: Vec<Change>) {
for change in changes {
self.add(change);
}
}
pub fn add<C: Into<Change>>(&mut self, change: C) {
let change = change.into();
match &change {
Change::AllAttributes(_)
| Change::Attribute(_)
| Change::CursorColor(_)
| Change::CursorShape(_)
| Change::ClearToEndOfLine(_)
| Change::Title(_)
| Change::ClearToEndOfScreen(_) => {}
Change::Text(t) => {
for g in t.as_str().graphemes(true) {
if self.cursor_x == self.screen_cols {
self.cursor_y += 1;
self.cursor_x = 0;
}
if g == "\n" {
self.cursor_y += 1;
} else if g == "\r" {
self.cursor_x = 0;
} else if g == "\r\n" {
self.cursor_y += 1;
self.cursor_x = 0;
} else {
let len = unicode_column_width(g);
self.cursor_x += len;
}
}
self.update_render_height();
}
Change::Image(im) => {
self.cursor_x += im.width;
self.render_y_max = self.render_y_max.max(self.cursor_y + im.height as isize);
}
Change::ClearScreen(_) => {
self.cursor_x = 0;
self.cursor_y = 0;
}
Change::CursorPosition { x, y } => {
self.cursor_x = match x {
Position::Relative(x) => {
((self.cursor_x as isize + x) % self.screen_cols as isize) as usize
}
Position::Absolute(x) => x % self.screen_cols,
Position::EndRelative(x) => (self.screen_cols - x) % self.screen_cols,
};
self.cursor_y = match y {
Position::Relative(y) => {
(self.cursor_y as isize + y) % self.screen_rows as isize
}
Position::Absolute(y) => (y % self.screen_rows) as isize,
Position::EndRelative(y) => {
((self.screen_rows - y) % self.screen_rows) as isize
}
};
self.update_render_height();
}
Change::ScrollRegionUp { .. } | Change::ScrollRegionDown { .. } => {
// The resultant cursor position is undefined by
// the renderer!
// We just pick something.
self.cursor_x = 0;
self.cursor_y = 0;
}
}
self.changes.push(change);
}
}
/// The `Image` `Change` needs to support adding an image that spans multiple
/// rows and columns, as well as model the content for just one of those cells.
/// For instance, if some of the cells inside an image are replaced by textual