Files
LLeMbas-TTY/term/src/lib.rs
T
2018-02-10 18:55:43 -08:00

1500 lines
53 KiB
Rust

//! Terminal model
#![feature(slice_patterns)]
#[allow(unused_imports)]
#[macro_use]
extern crate failure;
#[macro_use]
extern crate bitflags;
extern crate palette;
extern crate unicode_width;
extern crate unicode_segmentation;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate vte;
#[cfg(test)]
#[macro_use]
extern crate maplit;
use failure::Error;
use std::ops::{Deref, DerefMut, Range};
use std::rc::Rc;
use std::str;
use std::time::{Duration, Instant};
#[macro_use]
mod debug;
pub mod input;
pub use input::*;
pub mod cell;
pub use cell::*;
pub mod line;
pub use line::*;
pub mod selection;
use selection::{SelectionCoordinate, SelectionRange};
pub mod hyperlink;
use hyperlink::Hyperlink;
/// Represents the index into screen.lines. Index 0 is the top of
/// the scrollback (if any). The index of the top of the visible screen
/// depends on the terminal dimensions and the scrollback size.
pub type PhysRowIndex = usize;
/// Represents an index into the visible portion of the screen.
/// Value 0 is the first visible row. VisibleRowIndex needs to be
/// resolved into a PhysRowIndex to obtain an actual row. It is not
/// valid to have a negative VisibleRowIndex value so this type logically
/// should be unsigned, however, having a different sign is helpful to
/// have the compiler catch accidental arithmetic performed between
/// PhysRowIndex and VisibleRowIndex. We could define our own type with
/// its own Add and Sub operators, but then we'd not be able to iterate
/// over Ranges of these types without also laboriously implementing an
/// iterator Skip trait that is currently only in unstable rust.
pub type VisibleRowIndex = i64;
/// Like VisibleRowIndex above, but can index backwards into scrollback.
/// This is deliberately a differently sized signed type to catch
/// accidentally blending together the wrong types of indices.
/// This is explicitly 32-bit rather than 64-bit as it seems unreasonable
/// to want to scroll back or select more than ~2billion lines of scrollback.
pub type ScrollbackOrVisibleRowIndex = i32;
/// range.contains(), but that is unstable
pub fn in_range<T: PartialOrd>(value: T, range: &Range<T>) -> bool {
value >= range.start && value < range.end
}
/// Position allows referring to an absolute visible row number
/// or a position relative to some existing row number (typically
/// where the cursor is located). Both of the cases are represented
/// as signed numbers so that the math and error checking for out
/// of range values can be deferred to the point where we execute
/// the request.
#[derive(Debug)]
pub enum Position {
Absolute(VisibleRowIndex),
Relative(i64),
}
/// Describes the location of the cursor in the visible portion
/// of the screen.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct CursorPosition {
pub x: usize,
pub y: VisibleRowIndex,
}
pub mod color;
mod csi;
use self::csi::*;
#[cfg(test)]
mod test;
/// The response we given when queries for device attributes.
/// This particular string says "we are a VT102".
/// TODO: Consider VT220 extended response which can advertise
/// certain feature sets.
pub const DEVICE_IDENT: &[u8] = b"\x1b[?6c";
#[allow(dead_code)]
pub const CSI: &[u8] = b"\x1b[";
#[allow(dead_code)]
pub const OSC: &[u8] = b"\x1b]";
#[allow(dead_code)]
pub const ST: &[u8] = b"\x1b\\";
#[allow(dead_code)]
pub const DCS: &[u8] = b"\x1bP";
/// Holds the model of a screen. This can either be the primary screen
/// which includes lines of scrollback text, or the alternate screen
/// which holds no scrollback. The intent is to have one instance of
/// Screen for each of these things.
#[derive(Debug, Clone)]
pub struct Screen {
/// Holds the line data that comprises the screen contents.
/// This is allocated with capacity for the entire scrollback.
/// The last N lines are the visible lines, with those prior being
/// the lines that have scrolled off the top of the screen.
/// Index 0 is the topmost line of the screen/scrollback (depending
/// on the current window size) and will be the first line to be
/// popped off the front of the screen when a new line is added that
/// would otherwise have exceeded the line capacity
lines: Vec<Line>,
/// Maximum number of lines of scrollback
scrollback_size: usize,
/// Physical, visible height of the screen (not including scrollback)
physical_rows: usize,
/// Physical, visible width of the screen
physical_cols: usize,
}
impl Screen {
/// Create a new Screen with the specified dimensions.
/// The Cells in the viewable portion of the screen are set to the
/// default cell attributes.
pub fn new(physical_rows: usize, physical_cols: usize, scrollback_size: usize) -> Screen {
let mut lines = Vec::with_capacity(physical_rows + scrollback_size);
for _ in 0..physical_rows {
lines.push(Line::new(physical_cols));
}
Screen {
lines,
scrollback_size,
physical_rows,
physical_cols,
}
}
/// Resize the physical, viewable portion of the screen
pub fn resize(&mut self, physical_rows: usize, physical_cols: usize) {
let capacity = physical_rows + self.scrollback_size;
let current_capacity = self.lines.capacity();
if capacity > current_capacity {
self.lines.reserve(capacity - current_capacity);
}
if physical_rows > self.physical_rows {
// Enlarging the viewable portion? Add more lines at the bottom
for _ in self.physical_rows..physical_rows {
self.lines.push(Line::new(physical_cols));
}
}
self.physical_rows = physical_rows;
self.physical_cols = physical_cols;
}
/// Get mutable reference to a line, relative to start of scrollback.
/// Sets the line dirty.
fn line_mut(&mut self, idx: PhysRowIndex) -> &mut Line {
let line = &mut self.lines[idx];
line.set_dirty();
line
}
/// Sets a line dirty. The line is relative to the visible origin.
#[inline]
fn dirty_line(&mut self, idx: VisibleRowIndex) {
let line_idx = self.phys_row(idx);
if line_idx < self.lines.len() {
self.lines[line_idx].set_dirty();
}
}
/// Clears the dirty flag for a line. The line is relative to the visible origin.
#[inline]
#[allow(dead_code)]
fn clean_line(&mut self, idx: VisibleRowIndex) {
let line_idx = self.phys_row(idx);
self.lines[line_idx].set_clean();
}
/// Returns a slice over the visible lines in the screen (no scrollback)
#[cfg(test)]
fn visible_lines(&self) -> &[Line] {
let line_idx = self.lines.len() - self.physical_rows;
&self.lines[line_idx..line_idx + self.physical_rows]
}
/// Set a cell. the x and y coordinates are relative to the visible screeen
/// origin. 0,0 is the top left.
pub fn set_cell(
&mut self,
x: usize,
y: VisibleRowIndex,
c: char,
attr: &CellAttributes,
) -> &Cell {
let line_idx = self.phys_row(y);
debug!(
"set_cell x,y {},{}, line_idx = {} {} {:?}",
x,
y,
line_idx,
c,
attr
);
let cells = &mut self.line_mut(line_idx).cells;
let width = cells.len();
// if the line isn't wide enough, pad it out with the default attributes
if x >= width {
cells.resize(x + 1, Cell::default());
}
cells[x] = Cell::from_char(c, attr);
&cells[x]
}
pub fn clear_line(&mut self, y: VisibleRowIndex, cols: std::ops::Range<usize>) {
let blank = Cell::default();
let line_idx = self.phys_row(y);
let line = self.line_mut(line_idx);
let max_col = line.cells.len();
for x in cols {
if x >= max_col {
break;
}
line.cells[x] = blank.clone();
}
}
/// Translate a VisibleRowIndex into a PhysRowIndex. The resultant index
/// will be invalidated by inserting or removing rows!
#[inline]
fn phys_row(&self, row: VisibleRowIndex) -> PhysRowIndex {
assert!(row >= 0);
(self.lines.len() - self.physical_rows) + row as usize
}
/// Given a possibly negative row number, return the corresponding physical
/// row. This is similar to phys_row() but allows indexing backwards into
/// the scrollback.
#[inline]
fn scrollback_or_visible_row(&self, row: ScrollbackOrVisibleRowIndex) -> PhysRowIndex {
((self.lines.len() - self.physical_rows) as ScrollbackOrVisibleRowIndex + row).max(0) as
usize
}
#[inline]
fn scrollback_or_visible_range(
&self,
range: &Range<ScrollbackOrVisibleRowIndex>,
) -> Range<PhysRowIndex> {
self.scrollback_or_visible_row(range.start)..self.scrollback_or_visible_row(range.end)
}
/// Translate a range of VisibleRowIndex to a range of PhysRowIndex.
/// The resultant range will be invalidated by inserting or removing rows!
#[inline]
fn phys_range(&self, range: &Range<VisibleRowIndex>) -> Range<PhysRowIndex> {
self.phys_row(range.start)..self.phys_row(range.end)
}
/// ---------
/// |
/// |--- top
/// |
/// |--- bottom
///
/// scroll the region up by num_rows. Any rows that would be scrolled
/// beyond the top get removed from the screen.
/// In other words, we remove (top..top+num_rows) and then insert num_rows
/// at bottom.
/// If the top of the region is the top of the visible display, rather than
/// removing the lines we let them go into the scrollback.
fn scroll_up(&mut self, scroll_region: &Range<VisibleRowIndex>, num_rows: usize) {
let phys_scroll = self.phys_range(&scroll_region);
assert!(num_rows <= phys_scroll.end - phys_scroll.start);
// Invalidate the lines that will move before they move so that
// the indices of the lines are stable (we may remove lines below)
for y in phys_scroll.clone() {
self.line_mut(y).set_dirty();
}
// if we're going to remove lines due to lack of scrollback capacity,
// remember how many so that we can adjust our insertion point later.
let lines_removed = if scroll_region.start > 0 {
// No scrollback available for these;
// Remove the scrolled lines
num_rows
} else {
let max_allowed = self.physical_rows + self.scrollback_size;
if self.lines.len() + num_rows >= max_allowed {
(self.lines.len() + num_rows) - max_allowed
} else {
0
}
};
// Perform the removal
for _ in 0..lines_removed {
self.lines.remove(phys_scroll.start);
}
if scroll_region.start == 0 {
// All of the lines above the top are now effectively dirty because
// they were moved into scrollback by the scroll operation.
for y in 0..phys_scroll.start {
self.line_mut(y).set_dirty();
}
}
for _ in 0..num_rows {
self.lines.insert(
phys_scroll.end - lines_removed,
Line::new(self.physical_cols),
);
}
}
/// ---------
/// |
/// |--- top
/// |
/// |--- bottom
///
/// scroll the region down by num_rows. Any rows that would be scrolled
/// beyond the bottom get removed from the screen.
/// In other words, we remove (bottom-num_rows..bottom) and then insert num_rows
/// at scroll_top.
fn scroll_down(&mut self, scroll_region: &Range<VisibleRowIndex>, num_rows: usize) {
let phys_scroll = self.phys_range(&scroll_region);
assert!(num_rows <= phys_scroll.end - phys_scroll.start);
let middle = phys_scroll.end - num_rows;
// dirty the rows in the region
for y in phys_scroll.start..middle {
self.line_mut(y).set_dirty();
}
for _ in 0..num_rows {
self.lines.remove(middle);
}
for _ in 0..num_rows {
self.lines.insert(
phys_scroll.start,
Line::new(self.physical_cols),
);
}
}
}
/// This is a little helper that keeps track of the "click streak",
/// which is the number of successive clicks of the same mouse button
/// within the CLICK_INTERVAL. The streak is reset to 1 each time
/// the mouse button differs from the last click, or when the elapsed
/// time exceeds CLICK_INTERVAL.
#[derive(Debug)]
struct LastMouseClick {
button: MouseButton,
time: Instant,
streak: usize,
}
/// The multi-click interval, measured in milliseconds
const CLICK_INTERVAL: u64 = 500;
impl LastMouseClick {
fn new(button: MouseButton) -> Self {
Self {
button,
time: Instant::now(),
streak: 1,
}
}
fn add(&self, button: MouseButton) -> Self {
let now = Instant::now();
let streak = if button == self.button &&
now.duration_since(self.time) <= Duration::from_millis(CLICK_INTERVAL)
{
self.streak + 1
} else {
1
};
Self {
button,
time: now,
streak,
}
}
}
pub struct TerminalState {
/// The primary screen + scrollback
screen: Screen,
/// The alternate screen; no scrollback
alt_screen: Screen,
/// Tells us which screen is active
alt_screen_is_active: bool,
/// The current set of attributes in effect for the next
/// attempt to print to the display
pen: CellAttributes,
/// The current cursor position, relative to the top left
/// of the screen. 0-based index.
cursor: CursorPosition,
saved_cursor: CursorPosition,
/// if true, implicitly move to the next line on the next
/// printed character
wrap_next: bool,
/// Some parsing operations may yield responses that need
/// to be returned to the client. They are collected here
/// and this is used as the result of the advance_bytes()
/// method.
answerback: Vec<AnswerBack>,
/// The scroll region
scroll_region: Range<VisibleRowIndex>,
/// When set, modifies the sequence of bytes sent for keys
/// designated as cursor keys. This includes various navigation
/// keys. The code in key_down() is responsible for interpreting this.
application_cursor_keys: bool,
/// When set, modifies the sequence of bytes sent for keys
/// in the numeric keypad portion of the keyboard.
application_keypad: bool,
/// When set, pasting the clipboard should bracket the data with
/// designated marker characters.
bracketed_paste: bool,
sgr_mouse: bool,
button_event_mouse: bool,
current_mouse_button: MouseButton,
cursor_visible: bool,
/// Keeps track of double and triple clicks
last_mouse_click: Option<LastMouseClick>,
/// Used to compute the offset to the top of the viewport.
/// This is used to display the scrollback of the terminal.
/// It is distinct from the scroll_region in that the scroll region
/// afects how the terminal output is scrolled as data is output,
/// and the viewport_offset is used to index into the scrollback
/// purely for display purposes.
/// The offset is measured from the top of the physical viewable
/// screen with larger numbers going backwards.
viewport_offset: VisibleRowIndex,
/// Remembers the starting coordinate of the selection prior to
/// dragging.
selection_start: Option<SelectionCoordinate>,
/// Holds the not-normalized selection range.
selection_range: Option<SelectionRange>,
}
impl TerminalState {
pub fn new(
physical_rows: usize,
physical_cols: usize,
scrollback_size: usize,
) -> TerminalState {
let screen = Screen::new(physical_rows, physical_cols, scrollback_size);
let alt_screen = Screen::new(physical_rows, physical_cols, 0);
TerminalState {
screen,
alt_screen,
alt_screen_is_active: false,
pen: CellAttributes::default(),
cursor: CursorPosition::default(),
saved_cursor: CursorPosition::default(),
answerback: Vec::new(),
scroll_region: 0..physical_rows as VisibleRowIndex,
wrap_next: false,
application_cursor_keys: false,
application_keypad: false,
bracketed_paste: false,
sgr_mouse: false,
button_event_mouse: false,
cursor_visible: true,
current_mouse_button: MouseButton::None,
last_mouse_click: None,
viewport_offset: 0,
selection_range: None,
selection_start: None,
}
}
fn screen(&self) -> &Screen {
if self.alt_screen_is_active {
&self.alt_screen
} else {
&self.screen
}
}
fn screen_mut(&mut self) -> &mut Screen {
if self.alt_screen_is_active {
&mut self.alt_screen
} else {
&mut self.screen
}
}
pub fn get_selection_text(&self) -> String {
let mut s = String::new();
if let Some(sel) = self.selection_range.as_ref().map(|r| r.normalize()) {
let screen = self.screen();
for y in sel.rows() {
let idx = screen.scrollback_or_visible_row(y);
let line = screen.lines[idx].as_str();
let cols = sel.cols_for_row(y);
let end = cols.end.min(line.len());
let col_range = cols.start..end;
let selected = &line[col_range].trim_right();
if s.len() > 0 {
s.push('\n');
}
s.push_str(selected);
}
}
s
}
/// Dirty the lines in the current selection range
fn dirty_selection_lines(&mut self) {
if let Some(sel) = self.selection_range.as_ref().map(|r| r.normalize()) {
let screen = self.screen_mut();
for y in screen.scrollback_or_visible_range(&sel.rows()) {
screen.line_mut(y).set_dirty();
}
}
}
pub fn clear_selection(&mut self) {
self.dirty_selection_lines();
self.selection_range = None;
self.selection_start = None;
}
pub fn mouse_event(&mut self, event: MouseEvent, host: &mut TerminalHost) -> Result<(), Error> {
// First pass to figure out if we're messing with the selection
let send_event = self.sgr_mouse && !event.modifiers.contains(KeyModifiers::SHIFT);
// Perform click counting
if event.kind == MouseEventKind::Press {
let click = match self.last_mouse_click.take() {
None => LastMouseClick::new(event.button),
Some(click) => click.add(event.button),
};
self.last_mouse_click = Some(click);
}
if !send_event {
match (event, self.current_mouse_button) {
(MouseEvent {
kind: MouseEventKind::Press,
button: MouseButton::Left,
..
},
_) => {
self.current_mouse_button = MouseButton::Left;
self.dirty_selection_lines();
match self.last_mouse_click.as_ref() {
// Single click prepares the start of a new selection
Some(&LastMouseClick { streak: 1, .. }) => {
// Prepare to start a new selection.
// We don't form the selection until the mouse drags.
self.selection_range = None;
self.selection_start = Some(SelectionCoordinate {
x: event.x,
y: event.y as ScrollbackOrVisibleRowIndex,
});
host.set_clipboard(None)?;
}
// Double click to select a word on the current line
Some(&LastMouseClick { streak: 2, .. }) => {
let y = event.y as ScrollbackOrVisibleRowIndex;
let idx = self.screen().scrollback_or_visible_row(y);
let line = self.screen().lines[idx].as_str();
use unicode_segmentation::UnicodeSegmentation;
self.selection_start = None;
self.selection_range = None;
// TODO: allow user to configure the word boundary rules.
// Also consider making the default work with URLs?
for (x, word) in line.split_word_bound_indices() {
if event.x < x {
break;
}
if event.x <= x + word.len() {
// this is our word
let start = SelectionCoordinate { x, y };
let end = SelectionCoordinate {
x: x + word.len() - 1,
y,
};
self.selection_start = Some(start.clone());
self.selection_range = Some(SelectionRange { start, end });
self.dirty_selection_lines();
let text = self.get_selection_text();
debug!(
"finish 2click selection {:?} '{}'",
self.selection_range,
text
);
host.set_clipboard(Some(text))?;
return Ok(());
}
}
host.set_clipboard(None)?;
}
// triple click to select the current line
Some(&LastMouseClick { streak: 3, .. }) => {
self.selection_start = Some(SelectionCoordinate {
x: event.x,
y: event.y as ScrollbackOrVisibleRowIndex,
});
self.selection_range = Some(SelectionRange {
start: SelectionCoordinate {
x: 0,
y: event.y as ScrollbackOrVisibleRowIndex,
},
end: SelectionCoordinate {
x: usize::max_value(),
y: event.y as ScrollbackOrVisibleRowIndex,
},
});
self.dirty_selection_lines();
let text = self.get_selection_text();
debug!(
"finish 3click selection {:?} '{}'",
self.selection_range,
text
);
host.set_clipboard(Some(text))?;
}
// otherwise, clear out the selection
_ => {
self.selection_range = None;
self.selection_start = None;
host.set_clipboard(None)?;
}
}
return Ok(());
}
(MouseEvent {
kind: MouseEventKind::Release,
button: MouseButton::Left,
..
},
_) => {
// Finish selecting a region, update clipboard
self.current_mouse_button = MouseButton::None;
match self.last_mouse_click.as_ref() {
// Only consider a drag selection if we have a streak==1.
// The double/triple click cases are handled above.
Some(&LastMouseClick { streak: 1, .. }) => {
let text = self.get_selection_text();
debug!(
"finish drag selection {:?} '{}'",
self.selection_range,
text
);
host.set_clipboard(Some(text))?;
return Ok(());
}
_ => {}
}
}
(MouseEvent { kind: MouseEventKind::Move, .. }, MouseButton::Left) => {
// dragging out the selection region
// TODO: may drag and change the viewport
self.dirty_selection_lines();
let end = SelectionCoordinate {
x: event.x,
y: event.y as ScrollbackOrVisibleRowIndex,
};
let sel = match self.selection_range.take() {
None => {
SelectionRange::start(self.selection_start.unwrap_or(end.clone()))
.extend(end)
}
Some(sel) => sel.extend(end),
};
self.selection_range = Some(sel);
// Dirty lines again to reflect new range
self.dirty_selection_lines();
return Ok(());
}
_ => {}
}
}
match event {
MouseEvent {
kind: MouseEventKind::Press,
button: MouseButton::WheelUp,
..
} |
MouseEvent {
kind: MouseEventKind::Press,
button: MouseButton::WheelDown,
..
} => {
let (report_button, scroll_delta, key) = if event.button == MouseButton::WheelUp {
(64, -1, KeyCode::Up)
} else {
(65, 1, KeyCode::Down)
};
if self.sgr_mouse {
write!(
host.writer(),
"\x1b[<{};{};{}M",
report_button,
event.x + 1,
event.y + 1
)?;
} else if self.alt_screen_is_active {
// Send cursor keys instead (equivalent to xterm's alternateScroll mode)
self.key_down(key, KeyModifiers::default(), host)?;
} else {
self.scroll_viewport(scroll_delta)
}
}
MouseEvent { kind: MouseEventKind::Press, .. } => {
self.current_mouse_button = event.button;
if let Some(button) = match event.button {
MouseButton::Left => Some(0),
MouseButton::Middle => Some(1),
MouseButton::Right => Some(2),
_ => None,
}
{
if self.sgr_mouse {
write!(
host.writer(),
"\x1b[<{};{};{}M",
button,
event.x + 1,
event.y + 1
)?;
} else if event.button == MouseButton::Middle {
let clip = host.get_clipboard()?;
if self.bracketed_paste {
write!(host.writer(), "\x1b[200~{}\x1b[201~", clip)?;
} else {
write!(host.writer(), "{}", clip)?;
}
}
}
}
MouseEvent { kind: MouseEventKind::Release, .. } => {
self.current_mouse_button = MouseButton::None;
if self.sgr_mouse {
write!(host.writer(), "\x1b[<3;{};{}m", event.x + 1, event.y + 1)?;
}
}
MouseEvent { kind: MouseEventKind::Move, .. } => {
if let Some(button) = match (self.current_mouse_button, self.button_event_mouse) {
(_, false) => None,
(MouseButton::Left, true) => Some(32),
(MouseButton::Middle, true) => Some(33),
(MouseButton::Right, true) => Some(34),
(..) => None,
}
{
if self.sgr_mouse {
write!(
host.writer(),
"\x1b[<{};{};{}M",
button,
event.x + 1,
event.y + 1
)?;
}
}
}
}
Ok(())
}
/// Processes a key_down event generated by the gui/render layer
/// that is embedding the Terminal. This method translates the
/// keycode into a sequence of bytes to send to the slave end
/// of the pty via the `Write`-able object provided by the caller.
pub fn key_down(
&mut self,
key: KeyCode,
mods: KeyModifiers,
host: &mut TerminalHost,
) -> Result<(), Error> {
const CTRL: KeyModifiers = KeyModifiers::CTRL;
const SHIFT: KeyModifiers = KeyModifiers::SHIFT;
const ALT: KeyModifiers = KeyModifiers::ALT;
const APPCURSOR: bool = true;
use KeyCode::*;
let ctrl = mods & CTRL;
let shift = mods & SHIFT;
let alt = mods & ALT;
let mut buf = String::new();
// TODO: also respect self.application_keypad
let to_send = match (key, ctrl, alt, shift, self.application_cursor_keys) {
(Char(c), CTRL, _, SHIFT, _) if c <= 0xff as char => {
// If shift is held we have C == 0x43 and want to translate
// that into 0x03
buf.push((c as u8 - 0x40) as char);
buf.as_str()
}
(Char(c), CTRL, ..) if c <= 0xff as char => {
// If shift is not held we have C == 0x63 and want to translate
// that into 0x03
buf.push((c as u8 - 0x60) as char);
buf.as_str()
}
(Char(c), _, ALT, ..) if c <= 0xff as char => {
// TODO: add config option to select 8-bit vs. escape behavior?
//buf.push((c as u8 | 0x80) as char);
buf.push(0x1b as char);
buf.push(c);
buf.as_str()
}
(Char(c), ..) => {
buf.push(c);
buf.as_str()
}
(Insert, _, _, SHIFT, _) => {
let clip = host.get_clipboard()?;
if self.bracketed_paste {
use std::fmt::Write;
write!(buf, "\x1b[200~{}\x1b[201~", clip)?;
} else {
buf = clip;
}
buf.as_str()
}
(Up, _, _, _, APPCURSOR) => "\x1bOA",
(Down, _, _, _, APPCURSOR) => "\x1bOB",
(Right, _, _, _, APPCURSOR) => "\x1bOC",
(Left, _, _, _, APPCURSOR) => "\x1bOD",
(Home, _, _, _, APPCURSOR) => "\x1bOH",
(End, _, _, _, APPCURSOR) => "\x1bOF",
(Up, ..) => "\x1b[A",
(Down, ..) => "\x1b[B",
(Right, ..) => "\x1b[C",
(Left, ..) => "\x1b[D",
(PageUp, _, _, SHIFT, _) => {
let rows = self.screen().physical_rows as i64;
self.scroll_viewport(-rows);
""
}
(PageDown, _, _, SHIFT, _) => {
let rows = self.screen().physical_rows as i64;
self.scroll_viewport(rows);
""
}
(PageUp, ..) => "\x1b[5~",
(PageDown, ..) => "\x1b[6~",
(Home, ..) => "\x1b[H",
(End, ..) => "\x1b[F",
(Insert, ..) => "\x1b[2~",
// Modifier keys pressed on their own and unmappable keys don't expand to anything
(Control, ..) | (Alt, ..) | (Meta, ..) | (Super, ..) | (Hyper, ..) | (Shift, ..) |
(Unknown, ..) => "",
};
host.writer().write(&to_send.as_bytes())?;
// Reset the viewport if we sent data to the parser
if to_send.len() > 0 && self.viewport_offset != 0 {
// TODO: some folks like to configure this behavior.
self.set_scroll_viewport(0);
}
Ok(())
}
pub fn key_up(
&mut self,
_: KeyCode,
_: KeyModifiers,
_: &mut TerminalHost,
) -> Result<(), Error> {
Ok(())
}
pub fn resize(&mut self, physical_rows: usize, physical_cols: usize) {
self.screen.resize(physical_rows, physical_cols);
self.alt_screen.resize(physical_rows, physical_cols);
self.scroll_region = 0..physical_rows as i64;
self.set_scroll_viewport(0);
}
/// Returns true if any of the visible lines are marked dirty
pub fn has_dirty_lines(&self) -> bool {
let screen = self.screen();
let height = screen.physical_rows;
let len = screen.lines.len() - self.viewport_offset as usize;
for line in screen.lines.iter().skip(len - height) {
if line.is_dirty() {
return true;
}
}
false
}
/// Returns the set of visible lines that are dirty.
/// The return value is a Vec<(line_idx, line, selrange)>, where
/// line_idx is relative to the top of the viewport.
/// The selrange value is the column range representing the selected
/// columns on this line.
pub fn get_dirty_lines(&self) -> Vec<(usize, &Line, Range<usize>)> {
let mut res = Vec::new();
let screen = self.screen();
let height = screen.physical_rows;
let len = screen.lines.len() - self.viewport_offset as usize;
let selection = self.selection_range.map(|r| r.normalize());
for (i, mut line) in screen.lines.iter().skip(len - height).enumerate() {
if line.is_dirty() {
let selrange = match selection {
None => 0..0,
Some(sel) => {
// i is relative to the viewport, convert it back to
// something we can relate to the selection
let row = (i as ScrollbackOrVisibleRowIndex) -
self.viewport_offset as ScrollbackOrVisibleRowIndex;
sel.cols_for_row(row)
}
};
res.push((i, &*line, selrange));
}
}
res
}
pub fn get_viewport_offset(&self) -> VisibleRowIndex {
self.viewport_offset
}
/// Clear the dirty flag for all dirty lines
pub fn clean_dirty_lines(&mut self) {
let screen = self.screen_mut();
for line in screen.lines.iter_mut() {
line.set_clean();
}
}
/// When dealing with selection, mark a range of lines as dirty
pub fn make_all_lines_dirty(&mut self) {
let screen = self.screen_mut();
for line in screen.lines.iter_mut() {
line.set_dirty();
}
}
/// Returns the 0-based cursor position relative to the top left of
/// the visible screen
pub fn cursor_pos(&self) -> CursorPosition {
// TODO: figure out how to expose cursor visibility; Option<CursorPosition>?
CursorPosition {
x: self.cursor.x,
y: self.cursor.y + self.viewport_offset,
}
}
/// Sets the cursor position. x and y are 0-based and relative to the
/// top left of the visible screen.
/// TODO: DEC origin mode impacts the interpreation of these
fn set_cursor_pos(&mut self, x: &Position, y: &Position) {
let x = match x {
&Position::Relative(x) => (self.cursor.x as i64 + x).max(0),
&Position::Absolute(x) => x,
};
let y = match y {
&Position::Relative(y) => (self.cursor.y + y).max(0),
&Position::Absolute(y) => y,
};
let rows = self.screen().physical_rows;
let cols = self.screen().physical_cols;
let old_y = self.cursor.y;
let new_y = y.min(rows as i64 - 1);
self.cursor.x = x.min(cols as i64 - 1) as usize;
self.cursor.y = new_y;
self.wrap_next = false;
let screen = self.screen_mut();
screen.dirty_line(old_y);
screen.dirty_line(new_y);
}
fn set_scroll_viewport(&mut self, position: VisibleRowIndex) {
self.clear_selection();
let position = position.max(0);
let rows = self.screen().physical_rows;
let avail_scrollback = self.screen().lines.len() - rows;
let position = position.min(avail_scrollback as i64);
self.viewport_offset = position;
let top = self.screen().lines.len() - (rows + position as usize);
let screen = self.screen_mut();
for y in top..top + rows {
screen.line_mut(y).set_dirty();
}
}
/// Adjust the scroll position of the viewport by delta.
/// Dirties the lines that are now in view.
pub fn scroll_viewport(&mut self, delta: VisibleRowIndex) {
let position = self.viewport_offset - delta;
self.set_scroll_viewport(position);
}
fn scroll_up(&mut self, num_rows: usize) {
self.clear_selection();
let scroll_region = self.scroll_region.clone();
self.screen_mut().scroll_up(&scroll_region, num_rows)
}
fn scroll_down(&mut self, num_rows: usize) {
self.clear_selection();
let scroll_region = self.scroll_region.clone();
self.screen_mut().scroll_down(&scroll_region, num_rows)
}
fn new_line(&mut self, move_to_first_column: bool) {
let x = if move_to_first_column {
0
} else {
self.cursor.x
};
let y = self.cursor.y;
let y = if y == self.scroll_region.end - 1 {
self.scroll_up(1);
y
} else {
y + 1
};
self.set_cursor_pos(&Position::Absolute(x as i64), &Position::Absolute(y as i64));
}
fn push_answerback(&mut self, buf: &[u8]) {
self.answerback.push(AnswerBack::WriteToPty(buf.to_vec()));
}
/// Move the cursor up 1 line. If the position is at the top scroll margin,
/// scroll the region down.
fn reverse_index(&mut self) {
let y = self.cursor.y;
let y = if y == self.scroll_region.start {
self.scroll_down(1);
y
} else {
y - 1
};
self.set_cursor_pos(&Position::Relative(0), &Position::Absolute(y as i64));
}
fn set_hyperlink(&mut self, link: Option<Hyperlink>) {
self.pen.hyperlink = match link {
Some(hyperlink) => Some(Rc::new(hyperlink)),
None => None,
}
}
fn perform_csi(&mut self, act: CSIAction) {
debug!("{:?}", act);
match act {
CSIAction::SetPen(pen) => {
self.pen = pen;
}
CSIAction::SetForegroundColor(color) => {
self.pen.foreground = color;
}
CSIAction::SetBackgroundColor(color) => {
self.pen.background = color;
}
CSIAction::SetIntensity(level) => {
self.pen.set_intensity(level);
}
CSIAction::SetUnderline(level) => {
self.pen.set_underline(level);
}
CSIAction::SetItalic(on) => {
self.pen.set_italic(on);
}
CSIAction::SetBlink(on) => {
self.pen.set_blink(on);
}
CSIAction::SetReverse(on) => {
self.pen.set_reverse(on);
}
CSIAction::SetStrikethrough(on) => {
self.pen.set_strikethrough(on);
}
CSIAction::SetInvisible(on) => {
self.pen.set_invisible(on);
}
CSIAction::SetCursorXY { x, y } => {
self.set_cursor_pos(&x, &y);
}
CSIAction::EraseInLine(erase) => {
let cx = self.cursor.x;
let cy = self.cursor.y;
let mut screen = self.screen_mut();
let cols = screen.physical_cols;
match erase {
LineErase::ToRight => {
screen.clear_line(cy, cx..cols);
}
LineErase::ToLeft => {
screen.clear_line(cy, 0..cx);
}
LineErase::All => {
screen.clear_line(cy, 0..cols);
}
}
}
CSIAction::EraseInDisplay(erase) => {
let cy = self.cursor.y;
let mut screen = self.screen_mut();
let cols = screen.physical_cols;
let rows = screen.physical_rows as VisibleRowIndex;
match erase {
DisplayErase::Below => {
for y in cy..rows {
screen.clear_line(y, 0..cols);
}
}
DisplayErase::Above => {
for y in 0..cy {
screen.clear_line(y, 0..cols);
}
}
DisplayErase::All => {
for y in 0..rows {
screen.clear_line(y, 0..cols);
}
}
DisplayErase::SavedLines => {
println!("ed: no support for xterm Erase Saved Lines yet");
}
}
}
CSIAction::SetDecPrivateMode(DecPrivateMode::StartBlinkingCursor, _) => {
// ignored
}
CSIAction::SetDecPrivateMode(DecPrivateMode::ShowCursor, on) => {
self.cursor_visible = on;
}
CSIAction::SetDecPrivateMode(DecPrivateMode::ButtonEventMouse, on) => {
self.button_event_mouse = on;
}
CSIAction::SetDecPrivateMode(DecPrivateMode::SGRMouse, on) => {
self.sgr_mouse = on;
}
CSIAction::SetDecPrivateMode(DecPrivateMode::ClearAndEnableAlternateScreen, on) => {
// TODO: some folks like to disable alt screen
match (on, self.alt_screen_is_active) {
(true, false) => {
self.perform_csi(CSIAction::SaveCursor);
self.alt_screen_is_active = true;
self.set_cursor_pos(&Position::Absolute(0), &Position::Absolute(0));
self.perform_csi(CSIAction::EraseInDisplay(DisplayErase::All));
self.set_scroll_viewport(0);
}
(false, true) => {
self.alt_screen_is_active = false;
self.perform_csi(CSIAction::RestoreCursor);
self.set_scroll_viewport(0);
}
_ => {}
}
}
CSIAction::SetDecPrivateMode(DecPrivateMode::ApplicationCursorKeys, on) => {
self.application_cursor_keys = on;
}
CSIAction::SetDecPrivateMode(DecPrivateMode::BrackedPaste, on) => {
self.bracketed_paste = on;
}
CSIAction::DeviceStatusReport => {
// "OK"
self.push_answerback(b"\x1b[0n");
}
CSIAction::ReportCursorPosition => {
let row = self.cursor.y + 1;
let col = self.cursor.x + 1;
self.push_answerback(format!("\x1b[{};{}R", row, col).as_bytes());
}
CSIAction::SetScrollingRegion { top, bottom } => {
let rows = self.screen().physical_rows;
let mut top = top.min(rows as i64 - 1);
let mut bottom = bottom.min(rows as i64 - 1);
if top > bottom {
std::mem::swap(&mut top, &mut bottom);
}
self.scroll_region = top..bottom + 1;
}
CSIAction::RequestDeviceAttributes => {
self.push_answerback(DEVICE_IDENT);
}
CSIAction::DeleteLines(n) => {
if in_range(self.cursor.y, &self.scroll_region) {
let scroll_region = self.cursor.y..self.scroll_region.end;
self.screen_mut().scroll_up(&scroll_region, n as usize);
}
}
CSIAction::InsertLines(n) => {
if in_range(self.cursor.y, &self.scroll_region) {
let scroll_region = self.cursor.y..self.scroll_region.end;
self.screen_mut().scroll_down(&scroll_region, n as usize);
}
}
CSIAction::SaveCursor => {
self.saved_cursor = self.cursor;
}
CSIAction::RestoreCursor => {
let x = self.saved_cursor.x;
let y = self.saved_cursor.y;
self.set_cursor_pos(&Position::Absolute(x as i64), &Position::Absolute(y));
}
CSIAction::LinePosition(row) => {
self.set_cursor_pos(&Position::Relative(0), &row);
}
CSIAction::ScrollLines(amount) => {
if amount > 0 {
self.scroll_down(amount as usize);
} else {
self.scroll_up((-amount) as usize);
}
}
}
}
}
pub struct Terminal {
/// The terminal model/state
state: TerminalState,
/// Baseline terminal escape sequence parser
parser: vte::Parser,
}
impl Deref for Terminal {
type Target = TerminalState;
fn deref(&self) -> &TerminalState {
&self.state
}
}
impl DerefMut for Terminal {
fn deref_mut(&mut self) -> &mut TerminalState {
&mut self.state
}
}
/// When the terminal parser needs to convey a response
/// back to the caller, this enum holds that response
#[derive(Debug, Clone)]
pub enum AnswerBack {
/// Some data to send back to the application on
/// the slave end of the pty.
WriteToPty(Vec<u8>),
/// The application has requested that we change
/// the terminal title, and here it is.
TitleChanged(String),
}
impl Terminal {
pub fn new(physical_rows: usize, physical_cols: usize, scrollback_size: usize) -> Terminal {
Terminal {
state: TerminalState::new(physical_rows, physical_cols, scrollback_size),
parser: vte::Parser::new(),
}
}
/// Feed the terminal parser a slice of bytes of input.
/// The return value is a (likely empty most of the time)
/// sequence of AnswerBack objects that may need to be rendered
/// in the UI or sent back to the client on the slave side of
/// the pty.
pub fn advance_bytes<B: AsRef<[u8]>>(&mut self, bytes: B) -> Vec<AnswerBack> {
let bytes = bytes.as_ref();
for b in bytes.iter() {
self.parser.advance(&mut self.state, *b);
}
self.answerback.drain(0..).collect()
}
}
/// Represents the host of the terminal.
/// Provides a means for sending data to the connected pty,
/// and for operating on the clipboard
pub trait TerminalHost {
/// Returns an object that can be used to send data to the
/// slave end of the associated pty.
fn writer(&mut self) -> &mut std::io::Write;
/// Returns the current clipboard contents
fn get_clipboard(&mut self) -> Result<String, Error>;
/// Adjust the contents of the clipboard
fn set_clipboard(&mut self, clip: Option<String>) -> Result<(), Error>;
}
impl vte::Perform for TerminalState {
/// Draw a character to the screen
fn print(&mut self, c: char) {
if self.wrap_next {
self.new_line(true);
}
let x = self.cursor.x;
let y = self.cursor.y;
let width = self.screen().physical_cols;
let pen = self.pen.clone();
// Assign the cell and extract its printable width
let print_width = {
let cell = self.screen_mut().set_cell(x, y, c, &pen);
cell.width()
};
// for double- or triple-wide cells, the client of the terminal
// expects the cursor to move by the visible width, which means that
// we need to generate non-printing cells to pad out the gap. They
// need to be non-printing rather than space so that that renderer
// doesn't render an actual space between the glyphs.
for non_print_x in 1..print_width {
self.screen_mut().set_cell(
x + non_print_x,
y,
0 as char, // non-printable
&pen,
);
}
if x + print_width < width {
self.set_cursor_pos(
&Position::Relative(print_width as i64),
&Position::Relative(0),
);
} else {
self.wrap_next = true;
}
}
fn execute(&mut self, byte: u8) {
debug!("execute {:02x}", byte);
match byte {
b'\n' | 0x0b /* VT */ | 0x0c /* FF */ => {
self.new_line(true /* TODO: depend on terminal mode */)
}
b'\r' => /* CR */ {
self.set_cursor_pos(&Position::Absolute(0), &Position::Relative(0));
}
0x08 /* BS */ => {
self.set_cursor_pos(&Position::Relative(-1), &Position::Relative(0));
}
_ => println!("unhandled vte execute {}", byte),
}
}
fn hook(&mut self, _: &[i64], _: &[u8], _: bool) {}
fn put(&mut self, _: u8) {}
fn unhook(&mut self) {}
fn osc_dispatch(&mut self, osc: &[&[u8]]) {
match osc {
&[b"0", title] => {
if let Ok(title) = str::from_utf8(title) {
self.answerback.push(
AnswerBack::TitleChanged(title.to_string()),
);
} else {
println!("OSC: failed to decode utf for {:?}", title);
}
}
&[b"8", params, url] => {
match (str::from_utf8(params), str::from_utf8(url)) {
(Ok(params), Ok(url)) => {
let params = hyperlink::parse_link_params(params);
if url.len() > 0 {
self.set_hyperlink(Some(Hyperlink::new(url, &params)));
} else {
self.set_hyperlink(None);
}
}
_ => {
eprintln!("problem decoding URL/params {:?}, {:?}", url, params);
self.set_hyperlink(None)
}
}
}
_ => {
println!("OSC unhandled: {:?}", osc);
}
}
}
fn csi_dispatch(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, byte: char) {
for act in CSIParser::new(params, intermediates, ignore, byte) {
self.perform_csi(act);
}
}
fn esc_dispatch(&mut self, params: &[i64], intermediates: &[u8], _ignore: bool, byte: u8) {
debug!(
"ESC params={:?}, intermediates={:?} b={:02x} {}",
params,
intermediates,
byte,
byte as char
);
// Sequences from both of these sections show up in this handler:
// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-C1-_8-Bit_-Control-Characters
// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Controls-beginning-with-ESC
match (byte, intermediates, params) {
// String Terminator (ST); explicitly has nothing to do here, as its purpose is
// handled by vte::Parser
(b'\\', &[], &[]) => {}
// Application Keypad (DECKPAM)
(b'=', &[], &[]) => {
self.application_keypad = true;
}
// Normal Keypad (DECKPAM)
(b'>', &[], &[]) => {
self.application_keypad = false;
}
// Reverse Index (RI)
(b'M', &[], &[]) => self.reverse_index(),
// Enable alternate character set mode (smacs)
(b'0', &[b'('], &[]) => {}
// Exit alternate character set mode (rmacs)
(b'B', &[b'('], &[]) => {}
// DECSC - Save Cursor
(b'7', &[], &[]) => self.perform_csi(CSIAction::SaveCursor),
// DECRC - Restore Cursor
(b'8', &[], &[]) => self.perform_csi(CSIAction::RestoreCursor),
(..) => {
println!(
"ESC unhandled params={:?}, intermediates={:?} b={:02x} {}",
params,
intermediates,
byte,
byte as char
);
}
}
}
}