refactor: view -> ui

This commit is contained in:
graelo 2020-06-01 15:25:54 +02:00
parent 57f032af7c
commit 0accc9bc6b
3 changed files with 49 additions and 49 deletions

View file

@ -9,7 +9,7 @@ pub mod error;
pub mod model; pub mod model;
pub mod process; pub mod process;
pub mod regexes; pub mod regexes;
pub mod view; pub mod ui;
/// Run copyrat on an input string `buffer`, configured by `Opt`. /// Run copyrat on an input string `buffer`, configured by `Opt`.
/// ///
@ -30,18 +30,18 @@ pub fn run(buffer: String, opt: &CliOpt) -> Option<(String, bool)> {
let hint_style = match &opt.hint_style { let hint_style = match &opt.hint_style {
None => None, None => None,
Some(style) => match style { Some(style) => match style {
HintStyleCli::Bold => Some(view::HintStyle::Bold), HintStyleCli::Bold => Some(ui::HintStyle::Bold),
HintStyleCli::Italic => Some(view::HintStyle::Italic), HintStyleCli::Italic => Some(ui::HintStyle::Italic),
HintStyleCli::Underline => Some(view::HintStyle::Underline), HintStyleCli::Underline => Some(ui::HintStyle::Underline),
HintStyleCli::Surround => { HintStyleCli::Surround => {
let (open, close) = opt.hint_surroundings; let (open, close) = opt.hint_surroundings;
Some(view::HintStyle::Surround(open, close)) Some(ui::HintStyle::Surround(open, close))
} }
}, },
}; };
let selection: Option<(String, bool)> = { let selection: Option<(String, bool)> = {
let mut viewbox = view::View::new( let mut ui = ui::Ui::new(
&mut model, &mut model,
opt.unique_hint, opt.unique_hint,
opt.focus_wrap_around, opt.focus_wrap_around,
@ -50,7 +50,7 @@ pub fn run(buffer: String, opt: &CliOpt) -> Option<(String, bool)> {
hint_style, hint_style,
); );
viewbox.present() ui.present()
}; };
selection selection
@ -90,11 +90,11 @@ pub struct CliOpt {
unique_hint: bool, unique_hint: bool,
#[clap(flatten)] #[clap(flatten)]
colors: view::ViewColors, colors: ui::UiColors,
/// Align hint with its match. /// Align hint with its match.
#[clap(short = "a", long, arg_enum, default_value = "leading")] #[clap(short = "a", long, arg_enum, default_value = "leading")]
hint_alignment: view::HintAlignment, hint_alignment: ui::HintAlignment,
/// Move focus back to first/last match. /// Move focus back to first/last match.
#[clap(long)] #[clap(long)]
@ -124,7 +124,7 @@ pub struct CliOpt {
} }
/// Type introduced due to parsing limitation, /// Type introduced due to parsing limitation,
/// as we cannot directly parse into view::HintStyle. /// as we cannot directly parse into ui::HintStyle.
#[derive(Debug, Clap)] #[derive(Debug, Clap)]
enum HintStyleCli { enum HintStyleCli {
Bold, Bold,
@ -185,7 +185,7 @@ impl CliOpt {
"@copyrat-hint-bg" => self.colors.hint_bg = colors::parse_color(value)?, "@copyrat-hint-bg" => self.colors.hint_bg = colors::parse_color(value)?,
"@copyrat-hint-alignment" => { "@copyrat-hint-alignment" => {
self.hint_alignment = view::HintAlignment::from_str(&value)? self.hint_alignment = ui::HintAlignment::from_str(&value)?
} }
"@copyrat-hint-style" => self.hint_style = Some(HintStyleCli::from_str(&value)?), "@copyrat-hint-style" => self.hint_style = Some(HintStyleCli::from_str(&value)?),

View file

@ -45,7 +45,7 @@ impl<'a> fmt::Debug for RawMatch<'a> {
} }
} }
/// Holds data for the `View`. /// Holds data for the `Ui`.
pub struct Model<'a> { pub struct Model<'a> {
pub lines: &'a Vec<&'a str>, pub lines: &'a Vec<&'a str>,
alphabet: &'a Alphabet, alphabet: &'a Alphabet,

View file

@ -9,31 +9,31 @@ use termion::{self, color, cursor, event, style};
use crate::error::ParseError; use crate::error::ParseError;
use crate::{colors, model}; use crate::{colors, model};
pub struct View<'a> { pub struct Ui<'a> {
model: &'a mut model::Model<'a>, model: &'a mut model::Model<'a>,
matches: Vec<model::Match<'a>>, matches: Vec<model::Match<'a>>,
lookup_trie: SequenceTrie<char, usize>, lookup_trie: SequenceTrie<char, usize>,
focus_index: usize, focus_index: usize,
focus_wrap_around: bool, focus_wrap_around: bool,
hint_alignment: &'a HintAlignment, hint_alignment: &'a HintAlignment,
rendering_colors: &'a ViewColors, rendering_colors: &'a UiColors,
hint_style: Option<HintStyle>, hint_style: Option<HintStyle>,
} }
impl<'a> View<'a> { impl<'a> Ui<'a> {
pub fn new( pub fn new(
model: &'a mut model::Model<'a>, model: &'a mut model::Model<'a>,
unique_hint: bool, unique_hint: bool,
focus_wrap_around: bool, focus_wrap_around: bool,
hint_alignment: &'a HintAlignment, hint_alignment: &'a HintAlignment,
rendering_colors: &'a ViewColors, rendering_colors: &'a UiColors,
hint_style: Option<HintStyle>, hint_style: Option<HintStyle>,
) -> View<'a> { ) -> Ui<'a> {
let matches = model.matches(unique_hint); let matches = model.matches(unique_hint);
let lookup_trie = model::Model::build_lookup_trie(&matches); let lookup_trie = model::Model::build_lookup_trie(&matches);
let focus_index = if model.reverse { matches.len() - 1 } else { 0 }; let focus_index = if model.reverse { matches.len() - 1 } else { 0 };
View { Ui {
model, model,
matches, matches,
lookup_trie, lookup_trie,
@ -108,7 +108,7 @@ impl<'a> View<'a> {
/// # Notes /// # Notes
/// - All trailing whitespaces are trimmed, empty lines are skipped. /// - All trailing whitespaces are trimmed, empty lines are skipped.
/// - This writes directly on the writer, avoiding extra allocation. /// - This writes directly on the writer, avoiding extra allocation.
fn render_base_text(stdout: &mut dyn io::Write, lines: &Vec<&str>, colors: &ViewColors) -> () { fn render_base_text(stdout: &mut dyn io::Write, lines: &Vec<&str>, colors: &UiColors) -> () {
for (index, line) in lines.iter().enumerate() { for (index, line) in lines.iter().enumerate() {
let trimmed_line = line.trim_end(); let trimmed_line = line.trim_end();
@ -140,7 +140,7 @@ impl<'a> View<'a> {
text: &str, text: &str,
focused: bool, focused: bool,
offset: (usize, usize), offset: (usize, usize),
colors: &ViewColors, colors: &UiColors,
) { ) {
// To help identify it, the match thas has focus is rendered with a dedicated color. // To help identify it, the match thas has focus is rendered with a dedicated color.
let (fg_color, bg_color) = if focused { let (fg_color, bg_color) = if focused {
@ -177,7 +177,7 @@ impl<'a> View<'a> {
stdout: &mut dyn io::Write, stdout: &mut dyn io::Write,
hint_text: &str, hint_text: &str,
offset: (usize, usize), offset: (usize, usize),
colors: &ViewColors, colors: &UiColors,
hint_style: &Option<HintStyle>, hint_style: &Option<HintStyle>,
) { ) {
let fg_color = color::Fg(colors.hint_fg.as_ref()); let fg_color = color::Fg(colors.hint_fg.as_ref());
@ -271,7 +271,7 @@ impl<'a> View<'a> {
let (offset_x, offset_y) = self.match_offsets(mat); let (offset_x, offset_y) = self.match_offsets(mat);
View::render_matched_text( Ui::render_matched_text(
stdout, stdout,
text, text,
focused, focused,
@ -288,7 +288,7 @@ impl<'a> View<'a> {
HintAlignment::Trailing => text.len() - mat.hint.len(), HintAlignment::Trailing => text.len() - mat.hint.len(),
}; };
View::render_matched_hint( Ui::render_matched_hint(
stdout, stdout,
&mat.hint, &mat.hint,
(offset_x + extra_offset, offset_y), (offset_x + extra_offset, offset_y),
@ -298,7 +298,7 @@ impl<'a> View<'a> {
} }
} }
/// Full nender the view on the provided writer. /// Full nender the Ui on the provided writer.
/// ///
/// This renders in 3 phases: /// This renders in 3 phases:
/// - all lines are rendered verbatim /// - all lines are rendered verbatim
@ -314,7 +314,7 @@ impl<'a> View<'a> {
/// and `hint` are rendered in their proper position. /// and `hint` are rendered in their proper position.
fn full_render(&self, stdout: &mut dyn io::Write) -> () { fn full_render(&self, stdout: &mut dyn io::Write) -> () {
// 1. Trim all lines and render non-empty ones. // 1. Trim all lines and render non-empty ones.
View::render_base_text(stdout, self.model.lines, &self.rendering_colors); Ui::render_base_text(stdout, self.model.lines, &self.rendering_colors);
for (index, mat) in self.matches.iter().enumerate() { for (index, mat) in self.matches.iter().enumerate() {
let focused = index == self.focus_index; let focused = index == self.focus_index;
@ -472,7 +472,7 @@ impl<'a> View<'a> {
Event::Exit Event::Exit
} }
/// Configure the terminal and display the `View`. /// Configure the terminal and display the `Ui`.
/// ///
/// - Setup steps: switch to alternate screen, switch to raw mode, hide the cursor. /// - Setup steps: switch to alternate screen, switch to raw mode, hide the cursor.
/// - Teardown steps: show cursor, back to main screen. /// - Teardown steps: show cursor, back to main screen.
@ -539,7 +539,7 @@ pub enum HintStyle {
Surround(char, char), Surround(char, char),
} }
/// Returned value after the `View` has finished listening to events. /// Returned value after the `Ui` has finished listening to events.
enum Event { enum Event {
/// Exit with no selected matches, /// Exit with no selected matches,
Exit, Exit,
@ -561,7 +561,7 @@ path: /usr/local/bin/git
path: /usr/local/bin/cargo"; path: /usr/local/bin/cargo";
let lines: Vec<&str> = content.split('\n').collect(); let lines: Vec<&str> = content.split('\n').collect();
let colors = ViewColors { let colors = UiColors {
text_fg: Box::new(color::Black), text_fg: Box::new(color::Black),
text_bg: Box::new(color::White), text_bg: Box::new(color::White),
focused_fg: Box::new(color::Red), focused_fg: Box::new(color::Red),
@ -573,7 +573,7 @@ path: /usr/local/bin/cargo";
}; };
let mut writer = vec![]; let mut writer = vec![];
View::render_base_text(&mut writer, &lines, &colors); Ui::render_base_text(&mut writer, &lines, &colors);
let goto1 = cursor::Goto(1, 1); let goto1 = cursor::Goto(1, 1);
let goto2 = cursor::Goto(1, 2); let goto2 = cursor::Goto(1, 2);
@ -599,7 +599,7 @@ path: /usr/local/bin/cargo";
let text = "https://en.wikipedia.org/wiki/Barcelona"; let text = "https://en.wikipedia.org/wiki/Barcelona";
let focused = true; let focused = true;
let offset: (usize, usize) = (3, 1); let offset: (usize, usize) = (3, 1);
let colors = ViewColors { let colors = UiColors {
text_fg: Box::new(color::Black), text_fg: Box::new(color::Black),
text_bg: Box::new(color::White), text_bg: Box::new(color::White),
focused_fg: Box::new(color::Red), focused_fg: Box::new(color::Red),
@ -610,7 +610,7 @@ path: /usr/local/bin/cargo";
hint_bg: Box::new(color::Cyan), hint_bg: Box::new(color::Cyan),
}; };
View::render_matched_text(&mut writer, text, focused, offset, &colors); Ui::render_matched_text(&mut writer, text, focused, offset, &colors);
assert_eq!( assert_eq!(
writer, writer,
@ -633,7 +633,7 @@ path: /usr/local/bin/cargo";
let text = "https://en.wikipedia.org/wiki/Barcelona"; let text = "https://en.wikipedia.org/wiki/Barcelona";
let focused = false; let focused = false;
let offset: (usize, usize) = (3, 1); let offset: (usize, usize) = (3, 1);
let colors = ViewColors { let colors = UiColors {
text_fg: Box::new(color::Black), text_fg: Box::new(color::Black),
text_bg: Box::new(color::White), text_bg: Box::new(color::White),
focused_fg: Box::new(color::Red), focused_fg: Box::new(color::Red),
@ -644,7 +644,7 @@ path: /usr/local/bin/cargo";
hint_bg: Box::new(color::Cyan), hint_bg: Box::new(color::Cyan),
}; };
View::render_matched_text(&mut writer, text, focused, offset, &colors); Ui::render_matched_text(&mut writer, text, focused, offset, &colors);
assert_eq!( assert_eq!(
writer, writer,
@ -666,7 +666,7 @@ path: /usr/local/bin/cargo";
let mut writer = vec![]; let mut writer = vec![];
let hint_text = "eo"; let hint_text = "eo";
let offset: (usize, usize) = (3, 1); let offset: (usize, usize) = (3, 1);
let colors = ViewColors { let colors = UiColors {
text_fg: Box::new(color::Black), text_fg: Box::new(color::Black),
text_bg: Box::new(color::White), text_bg: Box::new(color::White),
focused_fg: Box::new(color::Red), focused_fg: Box::new(color::Red),
@ -680,7 +680,7 @@ path: /usr/local/bin/cargo";
let extra_offset = 0; let extra_offset = 0;
let hint_style = None; let hint_style = None;
View::render_matched_hint( Ui::render_matched_hint(
&mut writer, &mut writer,
hint_text, hint_text,
(offset.0 + extra_offset, offset.1), (offset.0 + extra_offset, offset.1),
@ -708,7 +708,7 @@ path: /usr/local/bin/cargo";
let mut writer = vec![]; let mut writer = vec![];
let hint_text = "eo"; let hint_text = "eo";
let offset: (usize, usize) = (3, 1); let offset: (usize, usize) = (3, 1);
let colors = ViewColors { let colors = UiColors {
text_fg: Box::new(color::Black), text_fg: Box::new(color::Black),
text_bg: Box::new(color::White), text_bg: Box::new(color::White),
focused_fg: Box::new(color::Red), focused_fg: Box::new(color::Red),
@ -722,7 +722,7 @@ path: /usr/local/bin/cargo";
let extra_offset = 0; let extra_offset = 0;
let hint_style = Some(HintStyle::Underline); let hint_style = Some(HintStyle::Underline);
View::render_matched_hint( Ui::render_matched_hint(
&mut writer, &mut writer,
hint_text, hint_text,
(offset.0 + extra_offset, offset.1), (offset.0 + extra_offset, offset.1),
@ -752,7 +752,7 @@ path: /usr/local/bin/cargo";
let mut writer = vec![]; let mut writer = vec![];
let hint_text = "eo"; let hint_text = "eo";
let offset: (usize, usize) = (3, 1); let offset: (usize, usize) = (3, 1);
let colors = ViewColors { let colors = UiColors {
text_fg: Box::new(color::Black), text_fg: Box::new(color::Black),
text_bg: Box::new(color::White), text_bg: Box::new(color::White),
focused_fg: Box::new(color::Red), focused_fg: Box::new(color::Red),
@ -766,7 +766,7 @@ path: /usr/local/bin/cargo";
let extra_offset = 0; let extra_offset = 0;
let hint_style = Some(HintStyle::Surround('{', '}')); let hint_style = Some(HintStyle::Surround('{', '}'));
View::render_matched_hint( Ui::render_matched_hint(
&mut writer, &mut writer,
hint_text, hint_text,
(offset.0 + extra_offset, offset.1), (offset.0 + extra_offset, offset.1),
@ -804,7 +804,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
let custom_regexes = vec![]; let custom_regexes = vec![];
let alphabet = alphabets::Alphabet("abcd".to_string()); let alphabet = alphabets::Alphabet("abcd".to_string());
let mut model = model::Model::new(&lines, &alphabet, &named_pat, &custom_regexes, false); let mut model = model::Model::new(&lines, &alphabet, &named_pat, &custom_regexes, false);
let rendering_colors = ViewColors { let rendering_colors = UiColors {
text_fg: Box::new(color::Black), text_fg: Box::new(color::Black),
text_bg: Box::new(color::White), text_bg: Box::new(color::White),
focused_fg: Box::new(color::Red), focused_fg: Box::new(color::Red),
@ -816,8 +816,8 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
}; };
let hint_alignment = HintAlignment::Leading; let hint_alignment = HintAlignment::Leading;
// create a view without any match // create a Ui without any match
let view = View { let ui = Ui {
model: &mut model, model: &mut model,
matches: vec![], // no matches matches: vec![], // no matches
lookup_trie: SequenceTrie::new(), lookup_trie: SequenceTrie::new(),
@ -829,7 +829,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
}; };
let mut writer = vec![]; let mut writer = vec![];
view.full_render(&mut writer); ui.full_render(&mut writer);
let goto1 = cursor::Goto(1, 1); let goto1 = cursor::Goto(1, 1);
let goto3 = cursor::Goto(1, 3); let goto3 = cursor::Goto(1, 3);
@ -848,7 +848,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
// println!("{:?}", writer); // println!("{:?}", writer);
// println!("{:?}", expected.as_bytes()); // println!("{:?}", expected.as_bytes());
// println!("matches: {}", view.matches.len()); // println!("matches: {}", ui.matches.len());
// println!("lines: {}", lines.len()); // println!("lines: {}", lines.len());
assert_eq!(writer, expected.as_bytes()); assert_eq!(writer, expected.as_bytes());
@ -871,7 +871,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
let unique_hint = false; let unique_hint = false;
let wrap_around = false; let wrap_around = false;
let rendering_colors = ViewColors { let rendering_colors = UiColors {
text_fg: Box::new(color::Black), text_fg: Box::new(color::Black),
text_bg: Box::new(color::White), text_bg: Box::new(color::White),
focused_fg: Box::new(color::Red), focused_fg: Box::new(color::Red),
@ -884,7 +884,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
let hint_alignment = HintAlignment::Leading; let hint_alignment = HintAlignment::Leading;
let hint_style = None; let hint_style = None;
let view = View::new( let ui = Ui::new(
&mut model, &mut model,
unique_hint, unique_hint,
wrap_around, wrap_around,
@ -894,7 +894,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
); );
let mut writer = vec![]; let mut writer = vec![];
view.full_render(&mut writer); ui.full_render(&mut writer);
let expected_content = { let expected_content = {
let goto1 = cursor::Goto(1, 1); let goto1 = cursor::Goto(1, 1);
@ -984,7 +984,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
// .find(|(_idx, (&l, &r))| l != r); // .find(|(_idx, (&l, &r))| l != r);
// println!("{:?}", diff_point); // println!("{:?}", diff_point);
assert_eq!(2, view.matches.len()); assert_eq!(2, ui.matches.len());
assert_eq!(writer, expected.as_bytes()); assert_eq!(writer, expected.as_bytes());
} }
@ -996,7 +996,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
/// - `normal_*` colors are used to render other matched text. /// - `normal_*` colors are used to render other matched text.
/// - `hint_*` colors are used to render the hints. /// - `hint_*` colors are used to render the hints.
#[derive(Clap, Debug)] #[derive(Clap, Debug)]
pub struct ViewColors { pub struct UiColors {
/// Foreground color for base text. /// Foreground color for base text.
#[clap(long, default_value = "bright-cyan", parse(try_from_str = colors::parse_color))] #[clap(long, default_value = "bright-cyan", parse(try_from_str = colors::parse_color))]
pub text_fg: Box<dyn color::Color>, pub text_fg: Box<dyn color::Color>,