From de1aa3889cdc0a295dc7abc93a51abd332c31087 Mon Sep 17 00:00:00 2001 From: graelo Date: Fri, 19 Mar 2021 09:00:42 +0100 Subject: [PATCH] refactor: refactor ui -> vc --- src/lib.rs | 2 +- src/ui/mod.rs | 22 ++++++++++++++++++++++ src/{ui.rs => ui/vc.rs} | 30 +++++++++++++++--------------- 3 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 src/ui/mod.rs rename src/{ui.rs => ui/vc.rs} (98%) diff --git a/src/lib.rs b/src/lib.rs index c51ebb7..5dcc402 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,7 @@ pub fn run(buffer: String, opt: &CliOpt) -> Option { let default_output_destination = output_destination::OutputDestination::Tmux; let selection: Option = { - let mut ui = ui::Ui::new( + let mut ui = ui::ViewController::new( &mut model, opt.unique_hint, opt.focus_wrap_around, diff --git a/src/ui/mod.rs b/src/ui/mod.rs new file mode 100644 index 0000000..1dcac7f --- /dev/null +++ b/src/ui/mod.rs @@ -0,0 +1,22 @@ +//! The `ui` module is responsible for presenting information to the user and +//! handling keypresses. +//! +//! In particular, the `Ui` struct +//! +//! - renders text, matched text and hints from the structured buffer content +//! to the screen, +//! - listens for keypress events, +//! - and returns the user selection in the form of a `Selection` struct. +//! +//! Via keypresses the user can +//! +//! - navigate the buffer (in case it is larger than the number of lines in +//! the terminal) +//! - move the focus from one match to another +//! - select one of the matches +//! - toggle the output destination (tmux buffer or clipboard) +//! + +mod vc; +pub use vc::ViewController; +pub use vc::{HintAlignment, HintStyle, UiColors}; diff --git a/src/ui.rs b/src/ui/vc.rs similarity index 98% rename from src/ui.rs rename to src/ui/vc.rs index bcc0d6d..391e848 100644 --- a/src/ui.rs +++ b/src/ui/vc.rs @@ -10,7 +10,7 @@ use termion::{self, color, cursor, event, style}; use crate::error::ParseError; use crate::{colors, model, output_destination::OutputDestination, selection::Selection}; -pub struct Ui<'a> { +pub struct ViewController<'a> { model: &'a mut model::Model<'a>, term_width: u16, line_offsets: Vec, @@ -24,7 +24,7 @@ pub struct Ui<'a> { hint_style: Option, } -impl<'a> Ui<'a> { +impl<'a> ViewController<'a> { pub fn new( model: &'a mut model::Model<'a>, unique_hint: bool, @@ -33,7 +33,7 @@ impl<'a> Ui<'a> { rendering_colors: &'a UiColors, hint_alignment: &'a HintAlignment, hint_style: Option, - ) -> Ui<'a> { + ) -> ViewController<'a> { let matches = model.matches(unique_hint); let lookup_trie = model::Model::build_lookup_trie(&matches); let focus_index = if model.reverse { matches.len() - 1 } else { 0 }; @@ -41,7 +41,7 @@ impl<'a> Ui<'a> { let (term_width, _) = termion::terminal_size().unwrap_or((80u16, 30u16)); // .expect("Cannot read the terminal size."); let line_offsets = get_line_offsets(&model.lines, term_width); - Ui { + ViewController { model, term_width, line_offsets, @@ -319,7 +319,7 @@ impl<'a> Ui<'a> { let (offset_x, offset_y) = self.match_offsets(mat); let (offset_x, offset_y) = self.map_coords_to_wrapped_space(offset_x, offset_y); - Ui::render_matched_text( + ViewController::render_matched_text( stdout, text, focused, @@ -336,7 +336,7 @@ impl<'a> Ui<'a> { HintAlignment::Trailing => text.len() - mat.hint.len(), }; - Ui::render_matched_hint( + ViewController::render_matched_hint( stdout, &mat.hint, (offset_x + extra_offset, offset_y), @@ -362,7 +362,7 @@ impl<'a> Ui<'a> { /// and `hint` are rendered in their proper position. fn full_render(&self, stdout: &mut dyn io::Write) { // 1. Trim all lines and render non-empty ones. - Ui::render_base_text( + ViewController::render_base_text( stdout, &self.model.lines, &self.line_offsets, @@ -679,7 +679,7 @@ path: /usr/local/bin/cargo"; }; let mut writer = vec![]; - Ui::render_base_text(&mut writer, &lines, &line_offsets, &colors); + ViewController::render_base_text(&mut writer, &lines, &line_offsets, &colors); let goto1 = cursor::Goto(1, 1); let goto2 = cursor::Goto(1, 2); @@ -716,7 +716,7 @@ path: /usr/local/bin/cargo"; hint_bg: Box::new(color::Cyan), }; - Ui::render_matched_text(&mut writer, text, focused, offset, &colors); + ViewController::render_matched_text(&mut writer, text, focused, offset, &colors); assert_eq!( writer, @@ -750,7 +750,7 @@ path: /usr/local/bin/cargo"; hint_bg: Box::new(color::Cyan), }; - Ui::render_matched_text(&mut writer, text, focused, offset, &colors); + ViewController::render_matched_text(&mut writer, text, focused, offset, &colors); assert_eq!( writer, @@ -786,7 +786,7 @@ path: /usr/local/bin/cargo"; let extra_offset = 0; let hint_style = None; - Ui::render_matched_hint( + ViewController::render_matched_hint( &mut writer, hint_text, (offset.0 + extra_offset, offset.1), @@ -828,7 +828,7 @@ path: /usr/local/bin/cargo"; let extra_offset = 0; let hint_style = Some(HintStyle::Underline); - Ui::render_matched_hint( + ViewController::render_matched_hint( &mut writer, hint_text, (offset.0 + extra_offset, offset.1), @@ -872,7 +872,7 @@ path: /usr/local/bin/cargo"; let extra_offset = 0; let hint_style = Some(HintStyle::Surround('{', '}')); - Ui::render_matched_hint( + ViewController::render_matched_hint( &mut writer, hint_text, (offset.0 + extra_offset, offset.1), @@ -932,7 +932,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - "; let hint_alignment = HintAlignment::Leading; // create a Ui without any match - let ui = Ui { + let ui = ViewController { model: &mut model, term_width, line_offsets, @@ -1009,7 +1009,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - "; let hint_alignment = HintAlignment::Leading; let hint_style = None; - let ui = Ui::new( + let ui = ViewController::new( &mut model, unique_hint, wrap_around,