From 0ab8cf1eec64a600091c8ec638432ff658fe23f4 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 27 Jan 2018 22:17:31 -0800 Subject: [PATCH] Render the cursor position, fixup colors I thought it would be nice to use the default xterm palette, but I really hate the contrast of its default blue on black, so switch to the palette from my xterm config. --- src/term/color.rs | 40 ++++++++++++++++++++++++++-------------- src/term/mod.rs | 6 ++++++ src/xwin.rs | 15 +++++++++++++-- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/term/color.rs b/src/term/color.rs index a70fa1914..0d72bc1e5 100644 --- a/src/term/color.rs +++ b/src/term/color.rs @@ -46,6 +46,7 @@ pub struct ColorPalette { colors: [RgbColor; 256], foreground: RgbColor, background: RgbColor, + cursor: RgbColor, } impl ColorPalette { @@ -57,6 +58,10 @@ impl ColorPalette { &ColorAttribute::Rgb(color) => color, } } + + pub fn cursor(&self) -> RgbColor { + self.cursor + } } impl Default for ColorPalette { @@ -67,20 +72,20 @@ impl Default for ColorPalette { // The XTerm ansi color set static ANSI: [RgbColor; 16] = [ RgbColor{ red: 0x00, green: 0x00, blue: 0x00}, // Black - RgbColor{ red: 0x80, green: 0x00, blue: 0x00}, // Maroon - RgbColor{ red: 0x00, green: 0x80, blue: 0x00}, // Green - RgbColor{ red: 0x80, green: 0x80, blue: 0x00}, // Olive - RgbColor{ red: 0x00, green: 0x00, blue: 0x80}, // Navy - RgbColor{ red: 0x80, green: 0x00, blue: 0x80}, // Purple - RgbColor{ red: 0x00, green: 0x80, blue: 0x80}, // Teal - RgbColor{ red: 0xc0, green: 0xc0, blue: 0xc0}, // Silver - RgbColor{ red: 0x80, green: 0x80, blue: 0x80}, // Grey - RgbColor{ red: 0xff, green: 0x00, blue: 0x00}, // Red - RgbColor{ red: 0x00, green: 0xff, blue: 0x00}, // Lime - RgbColor{ red: 0xff, green: 0xff, blue: 0x00}, // Yellow - RgbColor{ red: 0x00, green: 0x00, blue: 0xff}, // Blue - RgbColor{ red: 0xff, green: 0x00, blue: 0xff}, // Fuschia - RgbColor{ red: 0x00, green: 0xff, blue: 0xff}, // Aqua + RgbColor{ red: 0xcc, green: 0x55, blue: 0x55}, // Maroon + RgbColor{ red: 0x55, green: 0xcc, blue: 0x55}, // Green + RgbColor{ red: 0xcd, green: 0xcd, blue: 0x55}, // Olive + RgbColor{ red: 0x54, green: 0x55, blue: 0xcb}, // Navy + RgbColor{ red: 0xcc, green: 0x55, blue: 0xcc}, // Purple + RgbColor{ red: 0x7a, green: 0xca, blue: 0xca}, // Teal + RgbColor{ red: 0xcc, green: 0xcc, blue: 0xcc}, // Silver + RgbColor{ red: 0x55, green: 0x55, blue: 0x55}, // Grey + RgbColor{ red: 0xff, green: 0x55, blue: 0x55}, // Red + RgbColor{ red: 0x55, green: 0xff, blue: 0x55}, // Lime + RgbColor{ red: 0xff, green: 0xff, blue: 0x55}, // Yellow + RgbColor{ red: 0x55, green: 0x55, blue: 0xff}, // Blue + RgbColor{ red: 0xff, green: 0x55, blue: 0xff}, // Fuschia + RgbColor{ red: 0x55, green: 0xff, blue: 0xff}, // Aqua RgbColor{ red: 0xff, green: 0xff, blue: 0xff}, // White ]; @@ -136,10 +141,17 @@ impl Default for ColorPalette { let foreground = colors[249]; // Grey70 let background = colors[AnsiColor::Black as usize]; + let cursor = RgbColor { + red: 0x52, + green: 0xad, + blue: 0x70, + }; + ColorPalette { colors, foreground, background, + cursor, } } } diff --git a/src/term/mod.rs b/src/term/mod.rs index dc821f57f..f3bd74315 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -427,6 +427,12 @@ impl TerminalState { let len = screen.lines.len(); (width, &screen.lines[len - height..len]) } + + /// Returns the 0-based cursor position relative to the top left of + /// the visible screen + pub fn cursor_pos(&self) -> (usize, usize) { + (self.cursor_x, self.cursor_y) + } } pub struct Terminal { diff --git a/src/xwin.rs b/src/xwin.rs index b993204ef..a83cdf240 100644 --- a/src/xwin.rs +++ b/src/xwin.rs @@ -137,7 +137,9 @@ impl<'a> TerminalWindow<'a> { let (phys_cols, lines) = self.terminal.visible_cells(); - for line in lines.iter() { + let (cursor_x, cursor_y) = self.terminal.cursor_pos(); + + for (line_idx, line) in lines.iter().enumerate() { let mut x = 0 as isize; y += cell_height as isize; @@ -146,6 +148,11 @@ impl<'a> TerminalWindow<'a> { if cell_idx > phys_cols { break; } + let is_cursor_cell = if cell_idx == cursor_x && line_idx == cursor_y { + true + } else { + false + }; let has_color = self.font.has_color(info.font_idx)?; let ft_glyph = self.font.load_glyph(info.font_idx, info.glyph_pos)?; @@ -169,7 +176,11 @@ impl<'a> TerminalWindow<'a> { y - cell_height as isize, info.num_cells as usize * self.cell_width as usize, cell_height, - bg_color.into(), + if is_cursor_cell { + palette.cursor() + } else { + bg_color + }.into(), ); let scale = if (info.x_advance / info.num_cells as f64).floor() > self.cell_width {