mirror of
https://github.com/TECHNOFAB11/tmux-copyrat.git
synced 2025-12-13 00:20:08 +01:00
refactor: refactor ui -> vc
This commit is contained in:
parent
ec03a71bfd
commit
de1aa3889c
3 changed files with 38 additions and 16 deletions
|
|
@ -44,7 +44,7 @@ pub fn run(buffer: String, opt: &CliOpt) -> Option<selection::Selection> {
|
||||||
let default_output_destination = output_destination::OutputDestination::Tmux;
|
let default_output_destination = output_destination::OutputDestination::Tmux;
|
||||||
|
|
||||||
let selection: Option<selection::Selection> = {
|
let selection: Option<selection::Selection> = {
|
||||||
let mut ui = ui::Ui::new(
|
let mut ui = ui::ViewController::new(
|
||||||
&mut model,
|
&mut model,
|
||||||
opt.unique_hint,
|
opt.unique_hint,
|
||||||
opt.focus_wrap_around,
|
opt.focus_wrap_around,
|
||||||
|
|
|
||||||
22
src/ui/mod.rs
Normal file
22
src/ui/mod.rs
Normal file
|
|
@ -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};
|
||||||
|
|
@ -10,7 +10,7 @@ use termion::{self, color, cursor, event, style};
|
||||||
use crate::error::ParseError;
|
use crate::error::ParseError;
|
||||||
use crate::{colors, model, output_destination::OutputDestination, selection::Selection};
|
use crate::{colors, model, output_destination::OutputDestination, selection::Selection};
|
||||||
|
|
||||||
pub struct Ui<'a> {
|
pub struct ViewController<'a> {
|
||||||
model: &'a mut model::Model<'a>,
|
model: &'a mut model::Model<'a>,
|
||||||
term_width: u16,
|
term_width: u16,
|
||||||
line_offsets: Vec<usize>,
|
line_offsets: Vec<usize>,
|
||||||
|
|
@ -24,7 +24,7 @@ pub struct Ui<'a> {
|
||||||
hint_style: Option<HintStyle>,
|
hint_style: Option<HintStyle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Ui<'a> {
|
impl<'a> ViewController<'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,
|
||||||
|
|
@ -33,7 +33,7 @@ impl<'a> Ui<'a> {
|
||||||
rendering_colors: &'a UiColors,
|
rendering_colors: &'a UiColors,
|
||||||
hint_alignment: &'a HintAlignment,
|
hint_alignment: &'a HintAlignment,
|
||||||
hint_style: Option<HintStyle>,
|
hint_style: Option<HintStyle>,
|
||||||
) -> Ui<'a> {
|
) -> ViewController<'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 };
|
||||||
|
|
@ -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 (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);
|
let line_offsets = get_line_offsets(&model.lines, term_width);
|
||||||
|
|
||||||
Ui {
|
ViewController {
|
||||||
model,
|
model,
|
||||||
term_width,
|
term_width,
|
||||||
line_offsets,
|
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.match_offsets(mat);
|
||||||
let (offset_x, offset_y) = self.map_coords_to_wrapped_space(offset_x, offset_y);
|
let (offset_x, offset_y) = self.map_coords_to_wrapped_space(offset_x, offset_y);
|
||||||
|
|
||||||
Ui::render_matched_text(
|
ViewController::render_matched_text(
|
||||||
stdout,
|
stdout,
|
||||||
text,
|
text,
|
||||||
focused,
|
focused,
|
||||||
|
|
@ -336,7 +336,7 @@ impl<'a> Ui<'a> {
|
||||||
HintAlignment::Trailing => text.len() - mat.hint.len(),
|
HintAlignment::Trailing => text.len() - mat.hint.len(),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ui::render_matched_hint(
|
ViewController::render_matched_hint(
|
||||||
stdout,
|
stdout,
|
||||||
&mat.hint,
|
&mat.hint,
|
||||||
(offset_x + extra_offset, offset_y),
|
(offset_x + extra_offset, offset_y),
|
||||||
|
|
@ -362,7 +362,7 @@ impl<'a> Ui<'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.
|
||||||
Ui::render_base_text(
|
ViewController::render_base_text(
|
||||||
stdout,
|
stdout,
|
||||||
&self.model.lines,
|
&self.model.lines,
|
||||||
&self.line_offsets,
|
&self.line_offsets,
|
||||||
|
|
@ -679,7 +679,7 @@ path: /usr/local/bin/cargo";
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut writer = vec![];
|
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 goto1 = cursor::Goto(1, 1);
|
||||||
let goto2 = cursor::Goto(1, 2);
|
let goto2 = cursor::Goto(1, 2);
|
||||||
|
|
@ -716,7 +716,7 @@ path: /usr/local/bin/cargo";
|
||||||
hint_bg: Box::new(color::Cyan),
|
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!(
|
assert_eq!(
|
||||||
writer,
|
writer,
|
||||||
|
|
@ -750,7 +750,7 @@ path: /usr/local/bin/cargo";
|
||||||
hint_bg: Box::new(color::Cyan),
|
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!(
|
assert_eq!(
|
||||||
writer,
|
writer,
|
||||||
|
|
@ -786,7 +786,7 @@ path: /usr/local/bin/cargo";
|
||||||
let extra_offset = 0;
|
let extra_offset = 0;
|
||||||
let hint_style = None;
|
let hint_style = None;
|
||||||
|
|
||||||
Ui::render_matched_hint(
|
ViewController::render_matched_hint(
|
||||||
&mut writer,
|
&mut writer,
|
||||||
hint_text,
|
hint_text,
|
||||||
(offset.0 + extra_offset, offset.1),
|
(offset.0 + extra_offset, offset.1),
|
||||||
|
|
@ -828,7 +828,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);
|
||||||
|
|
||||||
Ui::render_matched_hint(
|
ViewController::render_matched_hint(
|
||||||
&mut writer,
|
&mut writer,
|
||||||
hint_text,
|
hint_text,
|
||||||
(offset.0 + extra_offset, offset.1),
|
(offset.0 + extra_offset, offset.1),
|
||||||
|
|
@ -872,7 +872,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('{', '}'));
|
||||||
|
|
||||||
Ui::render_matched_hint(
|
ViewController::render_matched_hint(
|
||||||
&mut writer,
|
&mut writer,
|
||||||
hint_text,
|
hint_text,
|
||||||
(offset.0 + extra_offset, offset.1),
|
(offset.0 + extra_offset, offset.1),
|
||||||
|
|
@ -932,7 +932,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
|
||||||
let hint_alignment = HintAlignment::Leading;
|
let hint_alignment = HintAlignment::Leading;
|
||||||
|
|
||||||
// create a Ui without any match
|
// create a Ui without any match
|
||||||
let ui = Ui {
|
let ui = ViewController {
|
||||||
model: &mut model,
|
model: &mut model,
|
||||||
term_width,
|
term_width,
|
||||||
line_offsets,
|
line_offsets,
|
||||||
|
|
@ -1009,7 +1009,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 ui = Ui::new(
|
let ui = ViewController::new(
|
||||||
&mut model,
|
&mut model,
|
||||||
unique_hint,
|
unique_hint,
|
||||||
wrap_around,
|
wrap_around,
|
||||||
Loading…
Add table
Add a link
Reference in a new issue