mirror of
https://github.com/TECHNOFAB11/tmux-copyrat.git
synced 2025-12-12 16:10:07 +01:00
refactor: rename state -> model
This commit is contained in:
parent
809cdb21f4
commit
8464c451e3
3 changed files with 48 additions and 47 deletions
|
|
@ -6,9 +6,9 @@ use std::str::FromStr;
|
|||
pub mod alphabets;
|
||||
pub mod colors;
|
||||
pub mod error;
|
||||
pub mod model;
|
||||
pub mod process;
|
||||
pub mod regexes;
|
||||
pub mod state;
|
||||
pub mod view;
|
||||
|
||||
/// Run copyrat on an input string `buffer`, configured by `Opt`.
|
||||
|
|
@ -19,7 +19,7 @@ pub mod view;
|
|||
pub fn run(buffer: String, opt: &CliOpt) -> Option<(String, bool)> {
|
||||
let lines: Vec<&str> = buffer.split('\n').collect();
|
||||
|
||||
let mut state = state::State::new(
|
||||
let mut model = model::Model::new(
|
||||
&lines,
|
||||
&opt.alphabet,
|
||||
&opt.named_pattern,
|
||||
|
|
@ -42,7 +42,7 @@ pub fn run(buffer: String, opt: &CliOpt) -> Option<(String, bool)> {
|
|||
|
||||
let selection: Option<(String, bool)> = {
|
||||
let mut viewbox = view::View::new(
|
||||
&mut state,
|
||||
&mut model,
|
||||
opt.unique_hint,
|
||||
&opt.hint_alignment,
|
||||
&opt.colors,
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ impl<'a> fmt::Debug for RawMatch<'a> {
|
|||
}
|
||||
|
||||
/// Holds data for the `View`.
|
||||
pub struct State<'a> {
|
||||
pub struct Model<'a> {
|
||||
pub lines: &'a Vec<&'a str>,
|
||||
alphabet: &'a Alphabet,
|
||||
named_patterns: &'a Vec<NamedPattern>,
|
||||
|
|
@ -54,15 +54,15 @@ pub struct State<'a> {
|
|||
pub reverse: bool,
|
||||
}
|
||||
|
||||
impl<'a> State<'a> {
|
||||
impl<'a> Model<'a> {
|
||||
pub fn new(
|
||||
lines: &'a Vec<&'a str>,
|
||||
alphabet: &'a Alphabet,
|
||||
named_patterns: &'a Vec<NamedPattern>,
|
||||
custom_regexes: &'a Vec<String>,
|
||||
reverse: bool,
|
||||
) -> State<'a> {
|
||||
State {
|
||||
) -> Model<'a> {
|
||||
Model {
|
||||
lines,
|
||||
alphabet,
|
||||
named_patterns,
|
||||
|
|
@ -89,7 +89,7 @@ impl<'a> State<'a> {
|
|||
matches
|
||||
}
|
||||
|
||||
/// Internal function that searches the state lines for pattern matches.
|
||||
/// Internal function that searches the model's lines for pattern matches.
|
||||
/// Returns a vector of `RawMatch`es (text, location, pattern id) without
|
||||
/// an associated hint. The hint is attached to `Match`, not to `RawMatch`.
|
||||
///
|
||||
|
|
@ -274,7 +274,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.first().unwrap().hint, "a");
|
||||
|
|
@ -287,7 +287,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(true);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(true);
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.first().unwrap().hint, "a");
|
||||
|
|
@ -300,7 +300,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(
|
||||
|
|
@ -315,7 +315,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.get(0).unwrap().text, "/var/log/nginx.log");
|
||||
|
|
@ -329,7 +329,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.get(0).unwrap().text, "/tmp/foo/bar_lol");
|
||||
|
|
@ -343,7 +343,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(results.get(0).unwrap().text, "~/.gnu/.config.txt");
|
||||
|
|
@ -356,7 +356,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
}
|
||||
|
|
@ -367,7 +367,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 4);
|
||||
assert_eq!(results.get(0).unwrap().text, "fd70b5695");
|
||||
|
|
@ -386,7 +386,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.get(0).unwrap().text, "127.0.0.1");
|
||||
|
|
@ -400,7 +400,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 4);
|
||||
assert_eq!(results.get(0).unwrap().text, "fe80::2:202:fe4");
|
||||
|
|
@ -420,7 +420,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 2);
|
||||
assert_eq!(results.get(0).unwrap().pattern, "markdown_url");
|
||||
|
|
@ -435,7 +435,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 4);
|
||||
assert_eq!(
|
||||
|
|
@ -457,7 +457,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.get(0).unwrap().text, "0xfd70b5695");
|
||||
|
|
@ -472,7 +472,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 4);
|
||||
assert_eq!(results.get(0).unwrap().text, "#fd7b56");
|
||||
|
|
@ -487,7 +487,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(
|
||||
|
|
@ -503,7 +503,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 8);
|
||||
}
|
||||
|
|
@ -514,7 +514,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(results.get(0).unwrap().text, "src/main.rs");
|
||||
|
|
@ -526,7 +526,7 @@ mod tests {
|
|||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(results.get(0).unwrap().text, "src/main.rs");
|
||||
|
|
@ -542,7 +542,7 @@ mod tests {
|
|||
.map(|&s| s.to_string())
|
||||
.collect();
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 9);
|
||||
assert_eq!(results.get(0).unwrap().text, "http://foo.bar");
|
||||
|
|
@ -571,7 +571,7 @@ mod tests {
|
|||
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = State::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 2);
|
||||
assert_eq!(results.get(0).unwrap().text, "http://foo.bar");
|
||||
39
src/view.rs
39
src/view.rs
|
|
@ -1,16 +1,17 @@
|
|||
use clap::Clap;
|
||||
use sequence_trie::SequenceTrie;
|
||||
use std::char;
|
||||
use std::io;
|
||||
use std::str::FromStr;
|
||||
|
||||
use clap::Clap;
|
||||
use sequence_trie::SequenceTrie;
|
||||
use termion::{self, color, cursor, event, style};
|
||||
|
||||
use crate::error::ParseError;
|
||||
use crate::{colors, state};
|
||||
use crate::{colors, model};
|
||||
|
||||
pub struct View<'a> {
|
||||
state: &'a mut state::State<'a>,
|
||||
matches: Vec<state::Match<'a>>,
|
||||
model: &'a mut model::Model<'a>,
|
||||
matches: Vec<model::Match<'a>>,
|
||||
lookup_trie: SequenceTrie<char, usize>,
|
||||
focus_index: usize,
|
||||
hint_alignment: &'a HintAlignment,
|
||||
|
|
@ -112,18 +113,18 @@ enum Event {
|
|||
|
||||
impl<'a> View<'a> {
|
||||
pub fn new(
|
||||
state: &'a mut state::State<'a>,
|
||||
model: &'a mut model::Model<'a>,
|
||||
unique_hint: bool,
|
||||
hint_alignment: &'a HintAlignment,
|
||||
rendering_colors: &'a ViewColors,
|
||||
hint_style: Option<HintStyle>,
|
||||
) -> View<'a> {
|
||||
let matches = state.matches(unique_hint);
|
||||
let lookup_trie = state::State::build_lookup_trie(&matches);
|
||||
let focus_index = if state.reverse { matches.len() - 1 } else { 0 };
|
||||
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 };
|
||||
|
||||
View {
|
||||
state,
|
||||
model,
|
||||
matches,
|
||||
lookup_trie,
|
||||
focus_index,
|
||||
|
|
@ -147,7 +148,7 @@ impl<'a> View<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Render entire state lines on provided writer.
|
||||
/// Render entire model lines on provided writer.
|
||||
///
|
||||
/// This renders the basic content on which matches and hints can be rendered.
|
||||
///
|
||||
|
|
@ -325,7 +326,7 @@ impl<'a> View<'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.
|
||||
View::render_base_text(stdout, self.state.lines, &self.rendering_colors);
|
||||
View::render_base_text(stdout, self.model.lines, &self.rendering_colors);
|
||||
|
||||
for (index, mat) in self.matches.iter().enumerate() {
|
||||
// 2. Render the match's text.
|
||||
|
|
@ -335,7 +336,7 @@ impl<'a> View<'a> {
|
|||
// instance ´ + e = é. Consequently the hint offset has to be adjusted
|
||||
// to the left.
|
||||
let offset_x = {
|
||||
let line = &self.state.lines[mat.y as usize];
|
||||
let line = &self.model.lines[mat.y as usize];
|
||||
let prefix = &line[0..mat.x as usize];
|
||||
let adjust = prefix.len() - prefix.chars().count();
|
||||
(mat.x as usize) - (adjust)
|
||||
|
|
@ -419,14 +420,14 @@ impl<'a> View<'a> {
|
|||
event::Key::Right => self.next(),
|
||||
|
||||
event::Key::Char(_ch @ 'n') => {
|
||||
if self.state.reverse {
|
||||
if self.model.reverse {
|
||||
self.prev()
|
||||
} else {
|
||||
self.next()
|
||||
}
|
||||
}
|
||||
event::Key::Char(_ch @ 'N') => {
|
||||
if self.state.reverse {
|
||||
if self.model.reverse {
|
||||
self.next()
|
||||
} else {
|
||||
self.prev()
|
||||
|
|
@ -763,7 +764,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
|
|||
let named_pat = vec![];
|
||||
let custom_regexes = vec![];
|
||||
let alphabet = alphabets::Alphabet("abcd".to_string());
|
||||
let mut state = state::State::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 {
|
||||
text_fg: Box::new(color::Black),
|
||||
text_bg: Box::new(color::White),
|
||||
|
|
@ -778,7 +779,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
|
|||
|
||||
// create a view without any match
|
||||
let view = View {
|
||||
state: &mut state,
|
||||
model: &mut model,
|
||||
matches: vec![], // no matches
|
||||
lookup_trie: SequenceTrie::new(),
|
||||
focus_index: 0,
|
||||
|
|
@ -826,7 +827,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
|
|||
let custom_regexes = vec![];
|
||||
let alphabet = alphabets::Alphabet("abcd".to_string());
|
||||
let reverse = true;
|
||||
let mut state = state::State::new(&lines, &alphabet, &named_pat, &custom_regexes, reverse);
|
||||
let mut model = model::Model::new(&lines, &alphabet, &named_pat, &custom_regexes, reverse);
|
||||
let unique_hint = false;
|
||||
|
||||
let rendering_colors = ViewColors {
|
||||
|
|
@ -843,7 +844,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
|
|||
let hint_style = None;
|
||||
|
||||
let view = View::new(
|
||||
&mut state,
|
||||
&mut model,
|
||||
unique_hint,
|
||||
&hint_alignment,
|
||||
&rendering_colors,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue