From 2eefee4412d8035fd9a9e9463d5611aa4ce862fe Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 28 Jan 2018 11:35:36 -0800 Subject: [PATCH] move csi parsing to its own file --- src/term/csi.rs | 138 +++++++++++++++++++++++++++++++++++++++++++++++ src/term/mod.rs | 139 +----------------------------------------------- 2 files changed, 140 insertions(+), 137 deletions(-) create mode 100644 src/term/csi.rs diff --git a/src/term/csi.rs b/src/term/csi.rs new file mode 100644 index 000000000..1151d0470 --- /dev/null +++ b/src/term/csi.rs @@ -0,0 +1,138 @@ +use super::*; + +#[derive(Debug)] +pub enum CSIAction { + SetPen(CellAttributes), + SetForegroundColor(color::ColorAttribute), + SetBackgroundColor(color::ColorAttribute), + SetIntensity(Intensity), + SetUnderline(Underline), + SetItalic(bool), + SetBlink(bool), + SetReverse(bool), + SetStrikethrough(bool), + SetInvisible(bool), +} + +impl CSIAction { + /// Parses out a "Set Graphics Rendition" action. + /// Returns the decoded action plus the unparsed remainder of the + /// parameter stream. Returns None if we couldn't decode one of + /// the parameter elements. + pub fn parse_sgr(params: &[i64]) -> Option<(CSIAction, &[i64])> { + if params.len() > 5 { + // ISO-8613-6 foreground and background color specification + // using full RGB color codes. + if params[0] == 38 && params[1] == 2 { + return Some(( + CSIAction::SetForegroundColor( + color::ColorAttribute::Rgb(color::RgbColor { + red: params[3] as u8, + green: params[4] as u8, + blue: params[5] as u8, + }), + ), + ¶ms[6..], + )); + } + if params[0] == 48 && params[1] == 2 { + return Some(( + CSIAction::SetBackgroundColor( + color::ColorAttribute::Rgb(color::RgbColor { + red: params[3] as u8, + green: params[4] as u8, + blue: params[5] as u8, + }), + ), + ¶ms[6..], + )); + } + } + if params.len() > 2 { + // Some special look-ahead cases for 88 and 256 color support + if params[0] == 38 && params[1] == 5 { + // 38;5;IDX -> foreground color + let color = color::ColorAttribute::PaletteIndex(params[2] as u8); + return Some((CSIAction::SetForegroundColor(color), ¶ms[3..])); + } + + if params[0] == 48 && params[1] == 5 { + // 48;5;IDX -> background color + let color = color::ColorAttribute::PaletteIndex(params[2] as u8); + return Some((CSIAction::SetBackgroundColor(color), ¶ms[3..])); + } + } + + let p = params[0]; + match p { + 0 => Some((CSIAction::SetPen(CellAttributes::default()), ¶ms[1..])), + 1 => Some((CSIAction::SetIntensity(Intensity::Bold), ¶ms[1..])), + 2 => Some((CSIAction::SetIntensity(Intensity::Half), ¶ms[1..])), + 3 => Some((CSIAction::SetItalic(true), ¶ms[1..])), + 4 => Some((CSIAction::SetUnderline(Underline::Single), ¶ms[1..])), + 5 => Some((CSIAction::SetBlink(true), ¶ms[1..])), + 7 => Some((CSIAction::SetReverse(true), ¶ms[1..])), + 8 => Some((CSIAction::SetInvisible(true), ¶ms[1..])), + 9 => Some((CSIAction::SetStrikethrough(true), ¶ms[1..])), + 21 => Some((CSIAction::SetUnderline(Underline::Double), ¶ms[1..])), + 22 => Some((CSIAction::SetIntensity(Intensity::Normal), ¶ms[1..])), + 23 => Some((CSIAction::SetItalic(false), ¶ms[1..])), + 24 => Some((CSIAction::SetUnderline(Underline::None), ¶ms[1..])), + 25 => Some((CSIAction::SetBlink(false), ¶ms[1..])), + 27 => Some((CSIAction::SetReverse(false), ¶ms[1..])), + 28 => Some((CSIAction::SetInvisible(false), ¶ms[1..])), + 29 => Some((CSIAction::SetStrikethrough(false), ¶ms[1..])), + 30...37 => { + Some(( + CSIAction::SetForegroundColor( + color::ColorAttribute::PaletteIndex(p as u8 - 30), + ), + ¶ms[1..], + )) + } + 39 => { + Some(( + CSIAction::SetForegroundColor( + color::ColorAttribute::Foreground, + ), + ¶ms[1..], + )) + } + 90...97 => { + // Bright foreground colors + Some(( + CSIAction::SetForegroundColor( + color::ColorAttribute::PaletteIndex(p as u8 - 90 + 8), + ), + ¶ms[1..], + )) + } + 40...47 => { + Some(( + CSIAction::SetBackgroundColor( + color::ColorAttribute::PaletteIndex(p as u8 - 40), + ), + ¶ms[1..], + )) + } + 49 => { + Some(( + CSIAction::SetBackgroundColor( + color::ColorAttribute::Background, + ), + ¶ms[1..], + )) + } + 100...107 => { + // Bright background colors + Some(( + CSIAction::SetBackgroundColor( + color::ColorAttribute::PaletteIndex(p as u8 - 100 + 8), + ), + ¶ms[1..], + )) + } + _ => None, + } + } +} diff --git a/src/term/mod.rs b/src/term/mod.rs index 5cba7e1b3..52b799bea 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -7,6 +7,8 @@ use unicode_segmentation; use vte; pub mod color; +mod csi; +use self::csi::*; bitflags! { #[derive(Default)] @@ -473,143 +475,6 @@ impl Terminal { } } -#[derive(Debug)] -enum CSIAction { - SetPen(CellAttributes), - SetForegroundColor(color::ColorAttribute), - SetBackgroundColor(color::ColorAttribute), - SetIntensity(Intensity), - SetUnderline(Underline), - SetItalic(bool), - SetBlink(bool), - SetReverse(bool), - SetStrikethrough(bool), - SetInvisible(bool), -} - -impl CSIAction { - /// Parses out a "Set Graphics Rendition" action. - /// Returns the decoded action plus the unparsed remainder of the - /// parameter stream. Returns None if we couldn't decode one of - /// the parameter elements. - fn parse_sgr(params: &[i64]) -> Option<(CSIAction, &[i64])> { - if params.len() > 5 { - // ISO-8613-6 foreground and background color specification - // using full RGB color codes. - if params[0] == 38 && params[1] == 2 { - return Some(( - CSIAction::SetForegroundColor( - color::ColorAttribute::Rgb(color::RgbColor { - red: params[3] as u8, - green: params[4] as u8, - blue: params[5] as u8, - }), - ), - ¶ms[6..], - )); - } - if params[0] == 48 && params[1] == 2 { - return Some(( - CSIAction::SetBackgroundColor( - color::ColorAttribute::Rgb(color::RgbColor { - red: params[3] as u8, - green: params[4] as u8, - blue: params[5] as u8, - }), - ), - ¶ms[6..], - )); - } - } - if params.len() > 2 { - // Some special look-ahead cases for 88 and 256 color support - if params[0] == 38 && params[1] == 5 { - // 38;5;IDX -> foreground color - let color = color::ColorAttribute::PaletteIndex(params[2] as u8); - return Some((CSIAction::SetForegroundColor(color), ¶ms[3..])); - } - - if params[0] == 48 && params[1] == 5 { - // 48;5;IDX -> background color - let color = color::ColorAttribute::PaletteIndex(params[2] as u8); - return Some((CSIAction::SetBackgroundColor(color), ¶ms[3..])); - } - } - - let p = params[0]; - match p { - 0 => Some((CSIAction::SetPen(CellAttributes::default()), ¶ms[1..])), - 1 => Some((CSIAction::SetIntensity(Intensity::Bold), ¶ms[1..])), - 2 => Some((CSIAction::SetIntensity(Intensity::Half), ¶ms[1..])), - 3 => Some((CSIAction::SetItalic(true), ¶ms[1..])), - 4 => Some((CSIAction::SetUnderline(Underline::Single), ¶ms[1..])), - 5 => Some((CSIAction::SetBlink(true), ¶ms[1..])), - 7 => Some((CSIAction::SetReverse(true), ¶ms[1..])), - 8 => Some((CSIAction::SetInvisible(true), ¶ms[1..])), - 9 => Some((CSIAction::SetStrikethrough(true), ¶ms[1..])), - 21 => Some((CSIAction::SetUnderline(Underline::Double), ¶ms[1..])), - 22 => Some((CSIAction::SetIntensity(Intensity::Normal), ¶ms[1..])), - 23 => Some((CSIAction::SetItalic(false), ¶ms[1..])), - 24 => Some((CSIAction::SetUnderline(Underline::None), ¶ms[1..])), - 25 => Some((CSIAction::SetBlink(false), ¶ms[1..])), - 27 => Some((CSIAction::SetReverse(false), ¶ms[1..])), - 28 => Some((CSIAction::SetInvisible(false), ¶ms[1..])), - 29 => Some((CSIAction::SetStrikethrough(false), ¶ms[1..])), - 30...37 => { - Some(( - CSIAction::SetForegroundColor( - color::ColorAttribute::PaletteIndex(p as u8 - 30), - ), - ¶ms[1..], - )) - } - 39 => { - Some(( - CSIAction::SetForegroundColor( - color::ColorAttribute::Foreground, - ), - ¶ms[1..], - )) - } - 90...97 => { - // Bright foreground colors - Some(( - CSIAction::SetForegroundColor( - color::ColorAttribute::PaletteIndex(p as u8 - 90 + 8), - ), - ¶ms[1..], - )) - } - 40...47 => { - Some(( - CSIAction::SetBackgroundColor( - color::ColorAttribute::PaletteIndex(p as u8 - 40), - ), - ¶ms[1..], - )) - } - 49 => { - Some(( - CSIAction::SetBackgroundColor( - color::ColorAttribute::Background, - ), - ¶ms[1..], - )) - } - 100...107 => { - // Bright background colors - Some(( - CSIAction::SetBackgroundColor( - color::ColorAttribute::PaletteIndex(p as u8 - 100 + 8), - ), - ¶ms[1..], - )) - } - _ => None, - } - } -} - impl vte::Perform for TerminalState { /// Draw a character to the screen