From a11581748ca9def473246f4e99673e56eafdb997 Mon Sep 17 00:00:00 2001 From: daa Date: Tue, 3 May 2016 20:25:52 +0300 Subject: [PATCH] Add cursor --- build.rs | 2 ++ src/ui.rs | 22 ++++++++++++++++++++-- src/ui_model.rs | 6 +++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index ccd7a9d..1a7232a 100644 --- a/build.rs +++ b/build.rs @@ -15,6 +15,8 @@ fn main() { write!(&mut file, "static KEYVAL_MAP: phf::Map<&'static str, &'static str> = ").unwrap(); phf_codegen::Map::new() + .entry("Left", "\"Left\"") + .entry("Right", "\"Right\"") .entry("Up", "\"Up\"") .entry("Down", "\"Down\"") .entry("BackSpace", "\"BS\"") diff --git a/src/ui.rs b/src/ui.rs index 4aa0447..b9e6a72 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -187,11 +187,12 @@ fn draw(ui: &Ui, ctx: &cairo::Context) { let font_extents = ctx.font_extents(); let line_height = ui.line_height.unwrap(); let char_width = ui.char_width.unwrap(); + let (row, col) = ui.model.get_cursor(); let mut line_y = line_height; - for line in ui.model.model() { + for (line_idx, line) in ui.model.model().iter().enumerate() { ctx.move_to(0.0, line_y - font_extents.descent); - for cell in line { + for (col_idx, cell) in line.iter().enumerate() { let slant = if cell.attrs.italic { FontSlant::Italic } else { @@ -220,11 +221,28 @@ fn draw(ui: &Ui, ctx: &cairo::Context) { ctx.move_to(current_point.0, current_point.1); } + let fg = if let Some(ref fg) = cell.attrs.foreground { fg } else { &ui.fg_color }; + + let bg = if let Some(ref bg) = cell.attrs.background { + bg + } else { + &ui.bg_color + }; + + if row == line_idx && col == col_idx { + ctx.set_source_rgba(1.0 - bg.0, 1.0 - bg.1, 1.0 - bg.2, 0.5); + ctx.rectangle(current_point.0, + line_y - line_height, + char_width, + line_height); + ctx.fill(); + ctx.move_to(current_point.0, current_point.1); + } ctx.set_source_rgb(fg.0, fg.1, fg.2); ctx.show_text(&cell.ch.to_string()); ctx.move_to(current_point.0 + char_width, current_point.1); diff --git a/src/ui_model.rs b/src/ui_model.rs index 4f023e8..6726e68 100644 --- a/src/ui_model.rs +++ b/src/ui_model.rs @@ -95,8 +95,12 @@ impl UiModel { } pub fn set_cursor(&mut self, row: u64, col: u64) { - self.cur_col = col as usize; self.cur_row = row as usize; + self.cur_col = col as usize; + } + + pub fn get_cursor(&self) -> (usize, usize) { + (self.cur_row, self.cur_col) } pub fn put(&mut self, text: &str, attrs: &Option) {