From 4d61bb6defb56c9c3b4910bff9357b1efcfd829b Mon Sep 17 00:00:00 2001 From: A-Walrus Date: Fri, 23 Aug 2024 14:48:42 +0300 Subject: [PATCH] Fill in cursor support Also added underline --- src/terminal.rs | 65 +++++++++++++++++++-------------------------- src/terminal_box.rs | 27 ++++++++++++++----- 2 files changed, 48 insertions(+), 44 deletions(-) diff --git a/src/terminal.rs b/src/terminal.rs index 4087c31..8d0433e 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -12,7 +12,7 @@ use alacritty_terminal::{ viewport_to_point, Config, TermDamage, TermMode, }, tty::{self, Options}, - vte::ansi::{Color, NamedColor, Rgb}, + vte::ansi::{Color, CursorShape, NamedColor, Rgb}, Term, }; use cosmic::{ @@ -757,43 +757,34 @@ impl Terminal { } // Change color if cursor - if indexed.point == grid.cursor.point { - //TODO: better handling of cursor - if term.mode().contains(TermMode::SHOW_CURSOR) { - //Use specific cursor color if requested - if term.colors()[NamedColor::Cursor].is_some() { - fg = bg; - bg = convert_color(term.colors(), Color::Named(NamedColor::Cursor)); - } else if self.colors[NamedColor::Cursor].is_some() { - //Use specific theme cursor color if exists - fg = bg; - bg = convert_color(&self.colors, Color::Named(NamedColor::Cursor)); - } else { - mem::swap(&mut fg, &mut bg); - } - let fg_rgb = Rgb { - r: fg.r(), - g: fg.g(), - b: fg.b(), - }; - let bg_rgb = Rgb { - r: bg.r(), - g: bg.g(), - b: bg.b(), - }; - let contrast = fg_rgb.contrast(bg_rgb); - if contrast < MIN_CURSOR_CONTRAST { - fg = convert_color( - &self.colors, - Color::Named(NamedColor::Background), - ); - bg = convert_color( - &self.colors, - Color::Named(NamedColor::Foreground), - ); - } - } else { + if indexed.point == grid.cursor.point + && term.cursor_style().shape == CursorShape::Block + { + //Use specific cursor color if requested + if term.colors()[NamedColor::Cursor].is_some() { fg = bg; + bg = convert_color(term.colors(), Color::Named(NamedColor::Cursor)); + } else if self.colors[NamedColor::Cursor].is_some() { + //Use specific theme cursor color if exists + fg = bg; + bg = convert_color(&self.colors, Color::Named(NamedColor::Cursor)); + } else { + mem::swap(&mut fg, &mut bg); + } + let fg_rgb = Rgb { + r: fg.r(), + g: fg.g(), + b: fg.b(), + }; + let bg_rgb = Rgb { + r: bg.r(), + g: bg.g(), + b: bg.b(), + }; + let contrast = fg_rgb.contrast(bg_rgb); + if contrast < MIN_CURSOR_CONTRAST { + fg = convert_color(&self.colors, Color::Named(NamedColor::Background)); + bg = convert_color(&self.colors, Color::Named(NamedColor::Foreground)); } } diff --git a/src/terminal_box.rs b/src/terminal_box.rs index a446a27..431f49e 100644 --- a/src/terminal_box.rs +++ b/src/terminal_box.rs @@ -589,28 +589,41 @@ where } // Draw cursor - terminal.with_buffer(|buffer| { + { let cursor = terminal.term.lock().renderable_content().cursor; let col = cursor.point.column.0; let line = cursor.point.line.0; + let color = Color::WHITE; // TODO + let width = terminal.size().cell_width; + let height = terminal.size().cell_height; + let top_left = view_position + + Vector::new((col as f32 * width).floor(), (line as f32 * height).floor()); match cursor.shape { CursorShape::Beam => { + let quad = Quad { + bounds: Rectangle::new(top_left, Size::new(1.0, height)), + ..Default::default() + }; + renderer.fill_quad(quad, color); + } + CursorShape::Underline => { let quad = Quad { bounds: Rectangle::new( view_position + Vector::new( - (col as f32 * terminal.size().cell_width).floor(), - line as f32 * terminal.size().cell_height, + (col as f32 * width).floor(), + ((line + 1) as f32 * height).floor(), ), - Size::new(1.0, buffer.metrics().line_height), + Size::new(width, 1.0), ), ..Default::default() }; - renderer.fill_quad(quad, Color::WHITE); + renderer.fill_quad(quad, color); } - _ => {} + CursorShape::HollowBlock => {} // TODO not sure when this would even be activated + CursorShape::Block | CursorShape::Hidden => {} // Block is handled seperately } - }); + } let duration = instant.elapsed(); log::trace!("redraw {}, {}: {:?}", view_w, view_h, duration);