diff --git a/src/term/csi.rs b/src/term/csi.rs index 992260dde..d0be51c3d 100644 --- a/src/term/csi.rs +++ b/src/term/csi.rs @@ -1,6 +1,14 @@ //! Parsing CSI escape sequences + use super::*; +#[derive(Debug)] +pub enum LineErase { + ToRight, + ToLeft, + All, +} + #[derive(Debug)] pub enum CSIAction { SetPen(CellAttributes), @@ -13,6 +21,8 @@ pub enum CSIAction { SetReverse(bool), SetStrikethrough(bool), SetInvisible(bool), + SetCursorXY(usize, usize), + EraseInLine(LineErase), } pub struct CSIParser<'a> { @@ -44,7 +54,50 @@ impl<'a> CSIParser<'a> { } } - fn next_sgr(&mut self, params: &'a [i64]) -> Option { + /// Erase in Line (EL) + fn el(&mut self, params: &'a [i64]) -> Option { + match params { + &[] => Some(CSIAction::EraseInLine(LineErase::ToRight)), + &[i] => { + self.advance_by(1, params); + match i { + 0 => Some(CSIAction::EraseInLine(LineErase::ToRight)), + 1 => Some(CSIAction::EraseInLine(LineErase::ToLeft)), + 2 => Some(CSIAction::EraseInLine(LineErase::All)), + _ => { + println!("el: unknown parameter {:?}", params); + None + } + } + } + _ => { + println!("el: unhandled csi sequence {:?}", params); + None + } + } + } + + /// Cursor Position (CUP) + fn cup(&mut self, params: &'a [i64]) -> Option { + match params { + &[] => { + // With no parameters, home the cursor + Some(CSIAction::SetCursorXY(0, 0)) + } + &[x, y] => { + self.advance_by(2, params); + // Co-ordinates are 1-based, but we want 0-based + Some(CSIAction::SetCursorXY((x - 1) as usize, (y - 1) as usize)) + } + _ => { + println!("CUP: unhandled csi sequence {:?}", params); + None + } + } + } + + /// Set Graphics Rendition (SGR) + fn sgr(&mut self, params: &'a [i64]) -> Option { match params { &[] => { // With no parameters, reset to default pen. @@ -208,7 +261,9 @@ impl<'a> Iterator for CSIParser<'a> { let params = self.params.take(); match (self.byte, params) { (_, None) => None, - ('m', Some(params)) => self.next_sgr(params), + ('H', Some(params)) => self.cup(params), + ('K', Some(params)) => self.el(params), + ('m', Some(params)) => self.sgr(params), (b, Some(p)) => { println!("unhandled {} {:?}", b, p); None diff --git a/src/term/mod.rs b/src/term/mod.rs index 8202f7ed3..76a167857 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -512,102 +512,60 @@ impl vte::Perform for TerminalState { fn unhook(&mut self) {} fn osc_dispatch(&mut self, _: &[&[u8]]) {} fn csi_dispatch(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, byte: char) { - match byte { - 'm' => { - for act in CSIParser::new(params, intermediates, ignore, byte) { - match act { - CSIAction::SetPen(pen) => { - self.pen = pen; + for act in CSIParser::new(params, intermediates, ignore, byte) { + 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.cursor_x = x; + self.cursor_y = 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); } - CSIAction::SetForegroundColor(color) => { - self.pen.foreground = color; + LineErase::ToLeft => { + screen.clear_line(cy, 0..cx); } - 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); + LineErase::All => { + screen.clear_line(cy, 0..cols); } } } } - 'H' => { - // Cursor Position (CUP) - if params.len() == 2 { - // coordinates are 1-based; convert to 0-based - self.cursor_y = (params[0] - 1) as usize; - self.cursor_x = (params[1] - 1) as usize; - } else { - // no parameters -> home the cursor - self.cursor_x = 0; - self.cursor_y = 0; - } - } - 'K' => { - // Erase in line (EL) - #[derive(Debug)] - enum Erase { - ToRight, - ToLeft, - All, - Unknown, - } - let what = if params.len() == 0 { - Erase::ToRight - } else { - match params[0] { - 0 => Erase::ToRight, - 1 => Erase::ToLeft, - 2 => Erase::All, - _ => Erase::Unknown, - } - }; - - let cx = self.cursor_x; - let cy = self.cursor_y; - let mut screen = self.screen_mut(); - let cols = screen.physical_cols; - match what { - Erase::ToRight => { - screen.clear_line(cy, cx..cols); - } - Erase::ToLeft => { - screen.clear_line(cy, 0..cx); - } - Erase::All => { - screen.clear_line(cy, 0..cols); - } - Erase::Unknown => {} - } - - } - _ => { - println!( - "CSI: unhandled params {:?} intermediates {:?} ignore={} byte={}", - params, - intermediates, - ignore, - byte - ); - } } } fn esc_dispatch(&mut self, _: &[i64], _: &[u8], _: bool, _: u8) {}