refactor: unique_hint -> textbuf::Model

This commit is contained in:
graelo 2021-03-22 07:17:54 +01:00
parent 9611b8a36d
commit 893f80a1e2
4 changed files with 74 additions and 28 deletions

View file

@ -17,6 +17,7 @@ pub fn run(buffer: String, opt: &config::basic::Config) -> Option<ui::Selection>
&opt.named_patterns,
&opt.custom_patterns,
opt.reverse,
opt.unique_hint,
);
let hint_style = match &opt.hint_style {
@ -37,7 +38,6 @@ pub fn run(buffer: String, opt: &config::basic::Config) -> Option<ui::Selection>
let selection: Option<ui::Selection> = {
let mut ui = ui::ViewController::new(
&mut model,
opt.unique_hint,
opt.focus_wrap_around,
default_output_destination,
&opt.colors,

View file

@ -20,6 +20,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -27,8 +28,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 3);
assert_eq!(results.first().unwrap().hint, "a");
@ -43,6 +45,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = true;
let results = Model::new(
buffer,
&alphabet,
@ -50,8 +53,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(true);
.matches();
assert_eq!(results.len(), 3);
assert_eq!(results.first().unwrap().hint, "a");
@ -66,6 +70,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -73,8 +78,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 1);
assert_eq!(
@ -92,6 +98,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = true;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -99,8 +106,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 3);
assert_eq!(results.get(0).unwrap().text, "/var/log/nginx.log");
@ -117,6 +125,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -124,8 +133,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 3);
assert_eq!(results.get(0).unwrap().text, "/tmp/foo/bar_lol");
@ -141,6 +151,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -148,8 +159,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 1);
assert_eq!(results.get(0).unwrap().text, "~/.gnu/.config.txt");
@ -163,6 +175,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -170,8 +183,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 1);
}
@ -184,6 +198,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -191,8 +206,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 4);
assert_eq!(results.get(0).unwrap().text, "fd70b5695");
@ -212,6 +228,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -219,8 +236,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 3);
assert_eq!(results.get(0).unwrap().pattern, "ipv4");
@ -239,6 +257,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -246,8 +265,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 4);
assert_eq!(results.get(0).unwrap().text, "fe80::2:202:fe4");
@ -268,6 +288,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -275,8 +296,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 2);
assert_eq!(results.get(0).unwrap().pattern, "markdown-url");
@ -293,6 +315,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -300,8 +323,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 4);
assert_eq!(
@ -326,6 +350,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -333,8 +358,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 2);
assert_eq!(results.get(0).unwrap().pattern, "email");
@ -357,6 +383,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -364,8 +391,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 3);
assert_eq!(results.get(0).unwrap().pattern, "mem-address");
@ -384,6 +412,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -391,8 +420,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 4);
assert_eq!(results.get(0).unwrap().text, "#fd7b56");
@ -409,6 +439,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -416,8 +447,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 1);
assert_eq!(
@ -434,6 +466,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -441,8 +474,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 8);
}
@ -455,6 +489,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -462,8 +497,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 1);
assert_eq!(results.get(0).unwrap().pattern, "diff-a");
@ -478,6 +514,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -485,8 +522,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 1);
assert_eq!(results.get(0).unwrap().pattern, "diff-b");
@ -504,6 +542,7 @@ mod tests {
.collect();
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -511,8 +550,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 9);
assert_eq!(results.get(0).unwrap().text, "http://foo.bar");
@ -543,6 +583,7 @@ mod tests {
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let results = Model::new(
buffer,
&alphabet,
@ -550,8 +591,9 @@ mod tests {
&named_pat,
&custom,
reverse,
unique_hint,
)
.matches(false);
.matches();
assert_eq!(results.len(), 2);
assert_eq!(results.get(0).unwrap().text, "http://foo.bar");

View file

@ -17,6 +17,7 @@ pub struct Model<'a> {
named_patterns: &'a [NamedPattern],
custom_patterns: &'a [String],
pub reverse: bool,
unique_hint: bool,
}
impl<'a> Model<'a> {
@ -27,6 +28,7 @@ impl<'a> Model<'a> {
named_patterns: &'a [NamedPattern],
custom_patterns: &'a [String],
reverse: bool,
unique_hint: bool,
) -> Model<'a> {
let lines = buffer.split('\n').collect();
@ -38,19 +40,20 @@ impl<'a> Model<'a> {
named_patterns,
custom_patterns,
reverse,
unique_hint,
}
}
/// Returns a vector of `Match`es, each corresponding to a pattern match
/// in the lines, its location (x, y), and associated hint.
pub fn matches(&self, unique: bool) -> Vec<Match<'a>> {
pub fn matches(&self) -> Vec<Match<'a>> {
let mut raw_matches = self.raw_matches();
if self.reverse {
raw_matches.reverse();
}
let mut matches = self.associate_hints(&raw_matches, unique);
let mut matches = self.associate_hints(&raw_matches, self.unique_hint);
if self.reverse {
matches.reverse();

View file

@ -29,14 +29,13 @@ impl<'a> ViewController<'a> {
pub fn new(
model: &'a mut textbuf::Model<'a>,
unique_hint: bool,
focus_wrap_around: bool,
default_output_destination: OutputDestination,
rendering_colors: &'a UiColors,
hint_alignment: &'a HintAlignment,
hint_style: Option<HintStyle>,
) -> ViewController<'a> {
let matches = model.matches(unique_hint);
let matches = model.matches();
let lookup_trie = textbuf::Model::build_lookup_trie(&matches);
let focus_index = if model.reverse { matches.len() - 1 } else { 0 };
@ -890,6 +889,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
let custom_patterns = vec![];
let alphabet = alphabet::Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let mut model = textbuf::Model::new(
content,
&alphabet,
@ -897,6 +897,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
&named_pat,
&custom_patterns,
reverse,
unique_hint,
);
let term_width: u16 = 80;
let line_offsets = get_line_offsets(&model.lines, term_width);
@ -965,6 +966,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
let custom_patterns = vec![];
let alphabet = alphabet::Alphabet("abcd".to_string());
let reverse = true;
let unique_hint = false;
let mut model = textbuf::Model::new(
content,
&alphabet,
@ -972,8 +974,8 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
&named_pat,
&custom_patterns,
reverse,
unique_hint,
);
let unique_hint = false;
let wrap_around = false;
let default_output_destination = OutputDestination::Tmux;
@ -992,7 +994,6 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
let ui = ViewController::new(
&mut model,
unique_hint,
wrap_around,
default_output_destination,
&rendering_colors,