mirror of
https://github.com/TECHNOFAB11/tmux-copyrat.git
synced 2025-12-12 16:10:07 +01:00
refactor: move line creation inside model
This commit is contained in:
parent
005fa7890f
commit
aeca7223a6
3 changed files with 51 additions and 62 deletions
|
|
@ -17,10 +17,8 @@ pub mod ui;
|
|||
///
|
||||
/// Maybe the decision to take ownership of the buffer is a bit bold.
|
||||
pub fn run(buffer: String, opt: &CliOpt) -> Option<(String, bool)> {
|
||||
let lines: Vec<&str> = buffer.split('\n').collect();
|
||||
|
||||
let mut model = model::Model::new(
|
||||
&lines,
|
||||
&buffer,
|
||||
&opt.alphabet,
|
||||
&opt.named_pattern,
|
||||
&opt.custom_regex,
|
||||
|
|
|
|||
99
src/model.rs
99
src/model.rs
|
|
@ -9,7 +9,8 @@ use crate::regexes::{NamedPattern, EXCLUDE_PATTERNS, PATTERNS};
|
|||
|
||||
/// Holds data for the `Ui`.
|
||||
pub struct Model<'a> {
|
||||
pub lines: &'a Vec<&'a str>,
|
||||
// buffer: &'a str,
|
||||
pub lines: Vec<&'a str>,
|
||||
alphabet: &'a Alphabet,
|
||||
named_patterns: &'a Vec<NamedPattern>,
|
||||
custom_regexes: &'a Vec<String>,
|
||||
|
|
@ -18,13 +19,16 @@ pub struct Model<'a> {
|
|||
|
||||
impl<'a> Model<'a> {
|
||||
pub fn new(
|
||||
lines: &'a Vec<&'a str>,
|
||||
buffer: &'a str,
|
||||
alphabet: &'a Alphabet,
|
||||
named_patterns: &'a Vec<NamedPattern>,
|
||||
custom_regexes: &'a Vec<String>,
|
||||
reverse: bool,
|
||||
) -> Model<'a> {
|
||||
let lines = buffer.split('\n').collect();
|
||||
|
||||
Model {
|
||||
// buffer,
|
||||
lines,
|
||||
alphabet,
|
||||
named_patterns,
|
||||
|
|
@ -264,17 +268,13 @@ mod tests {
|
|||
use super::*;
|
||||
use crate::alphabets::Alphabet;
|
||||
|
||||
fn split(output: &str) -> Vec<&str> {
|
||||
output.split("\n").collect::<Vec<&str>>()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn match_reverse() {
|
||||
let lines = split("lorem 127.0.0.1 lorem 255.255.255.255 lorem 127.0.0.1 lorem");
|
||||
let buffer = "lorem 127.0.0.1 lorem 255.255.255.255 lorem 127.0.0.1 lorem";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.first().unwrap().hint, "a");
|
||||
|
|
@ -283,11 +283,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_unique() {
|
||||
let lines = split("lorem 127.0.0.1 lorem 255.255.255.255 lorem 127.0.0.1 lorem");
|
||||
let buffer = "lorem 127.0.0.1 lorem 255.255.255.255 lorem 127.0.0.1 lorem";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(true);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(true);
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.first().unwrap().hint, "a");
|
||||
|
|
@ -296,11 +296,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_docker() {
|
||||
let lines = split("latest sha256:30557a29d5abc51e5f1d5b472e79b7e296f595abcf19fe6b9199dbbc809c6ff4 20 hours ago");
|
||||
let buffer = "latest sha256:30557a29d5abc51e5f1d5b472e79b7e296f595abcf19fe6b9199dbbc809c6ff4 20 hours ago";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(
|
||||
|
|
@ -311,11 +311,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_ansi_colors() {
|
||||
let lines = split("path: [32m/var/log/nginx.log[m\npath: [32mtest/log/nginx-2.log:32[mfolder/.nginx@4df2.log");
|
||||
let buffer = "path: [32m/var/log/nginx.log[m\npath: [32mtest/log/nginx-2.log:32[mfolder/.nginx@4df2.log";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.get(0).unwrap().text, "/var/log/nginx.log");
|
||||
|
|
@ -325,11 +325,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_paths() {
|
||||
let lines = split("Lorem /tmp/foo/bar_lol, lorem\n Lorem /var/log/boot-strap.log lorem ../log/kern.log lorem");
|
||||
let buffer = "Lorem /tmp/foo/bar_lol, lorem\n Lorem /var/log/boot-strap.log lorem ../log/kern.log lorem";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.get(0).unwrap().text, "/tmp/foo/bar_lol");
|
||||
|
|
@ -339,11 +339,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_home() {
|
||||
let lines = split("Lorem ~/.gnu/.config.txt, lorem");
|
||||
let buffer = "Lorem ~/.gnu/.config.txt, lorem";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(results.get(0).unwrap().text, "~/.gnu/.config.txt");
|
||||
|
|
@ -351,23 +351,22 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_uuids() {
|
||||
let lines =
|
||||
split("Lorem ipsum 123e4567-e89b-12d3-a456-426655440000 lorem\n Lorem lorem lorem");
|
||||
let buffer = "Lorem ipsum 123e4567-e89b-12d3-a456-426655440000 lorem\n Lorem lorem lorem";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn match_shas() {
|
||||
let lines = split("Lorem fd70b5695 5246ddf f924213 lorem\n Lorem 973113963b491874ab2e372ee60d4b4cb75f717c lorem");
|
||||
let buffer = "Lorem fd70b5695 5246ddf f924213 lorem\n Lorem 973113963b491874ab2e372ee60d4b4cb75f717c lorem";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 4);
|
||||
assert_eq!(results.get(0).unwrap().text, "fd70b5695");
|
||||
|
|
@ -381,12 +380,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_ips() {
|
||||
let lines =
|
||||
split("Lorem ipsum 127.0.0.1 lorem\n Lorem 255.255.10.255 lorem 127.0.0.1 lorem");
|
||||
let buffer = "Lorem ipsum 127.0.0.1 lorem\n Lorem 255.255.10.255 lorem 127.0.0.1 lorem";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.get(0).unwrap().text, "127.0.0.1");
|
||||
|
|
@ -396,11 +394,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_ipv6s() {
|
||||
let lines = split("Lorem ipsum fe80::2:202:fe4 lorem\n Lorem 2001:67c:670:202:7ba8:5e41:1591:d723 lorem fe80::2:1 lorem ipsum fe80:22:312:fe::1%eth0");
|
||||
let buffer = "Lorem ipsum fe80::2:202:fe4 lorem\n Lorem 2001:67c:670:202:7ba8:5e41:1591:d723 lorem fe80::2:1 lorem ipsum fe80:22:312:fe::1%eth0";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 4);
|
||||
assert_eq!(results.get(0).unwrap().text, "fe80::2:202:fe4");
|
||||
|
|
@ -414,13 +412,12 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_markdown_urls() {
|
||||
let lines = split(
|
||||
"Lorem ipsum [link](https://github.io?foo=bar)  lorem",
|
||||
);
|
||||
let buffer =
|
||||
"Lorem ipsum [link](https://github.io?foo=bar)  lorem";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 2);
|
||||
assert_eq!(results.get(0).unwrap().pattern, "markdown_url");
|
||||
|
|
@ -431,11 +428,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_urls() {
|
||||
let lines = split("Lorem ipsum https://www.rust-lang.org/tools lorem\n Lorem ipsumhttps://crates.io lorem https://github.io?foo=bar lorem ssh://github.io");
|
||||
let buffer = "Lorem ipsum https://www.rust-lang.org/tools lorem\n Lorem ipsumhttps://crates.io lorem https://github.io?foo=bar lorem ssh://github.io";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 4);
|
||||
assert_eq!(
|
||||
|
|
@ -453,11 +450,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_addresses() {
|
||||
let lines = split("Lorem 0xfd70b5695 0x5246ddf lorem\n Lorem 0x973113tlorem");
|
||||
let buffer = "Lorem 0xfd70b5695 0x5246ddf lorem\n Lorem 0x973113tlorem";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.get(0).unwrap().text, "0xfd70b5695");
|
||||
|
|
@ -467,12 +464,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_hex_colors() {
|
||||
let lines =
|
||||
split("Lorem #fd7b56 lorem #FF00FF\n Lorem #00fF05 lorem #abcd00 lorem #afRR00");
|
||||
let buffer = "Lorem #fd7b56 lorem #FF00FF\n Lorem #00fF05 lorem #abcd00 lorem #afRR00";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 4);
|
||||
assert_eq!(results.get(0).unwrap().text, "#fd7b56");
|
||||
|
|
@ -483,11 +479,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_ipfs() {
|
||||
let lines = split("Lorem QmRdbNSxDJBXmssAc9fvTtux4duptMvfSGiGuq6yHAQVKQ lorem Qmfoobar");
|
||||
let buffer = "Lorem QmRdbNSxDJBXmssAc9fvTtux4duptMvfSGiGuq6yHAQVKQ lorem Qmfoobar";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(
|
||||
|
|
@ -498,23 +494,22 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_process_port() {
|
||||
let lines =
|
||||
split("Lorem 5695 52463 lorem\n Lorem 973113 lorem 99999 lorem 8888 lorem\n 23456 lorem 5432 lorem 23444");
|
||||
let buffer = "Lorem 5695 52463 lorem\n Lorem 973113 lorem 99999 lorem 8888 lorem\n 23456 lorem 5432 lorem 23444";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn match_diff_a() {
|
||||
let lines = split("Lorem lorem\n--- a/src/main.rs");
|
||||
let buffer = "Lorem lorem\n--- a/src/main.rs";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(results.get(0).unwrap().text, "src/main.rs");
|
||||
|
|
@ -522,11 +517,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn match_diff_b() {
|
||||
let lines = split("Lorem lorem\n+++ b/src/main.rs");
|
||||
let buffer = "Lorem lorem\n+++ b/src/main.rs";
|
||||
let named_pat = vec![];
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(results.get(0).unwrap().text, "src/main.rs");
|
||||
|
|
@ -534,7 +529,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn priority() {
|
||||
let lines = split("Lorem [link](http://foo.bar) ipsum CUSTOM-52463 lorem ISSUE-123 lorem\nLorem /var/fd70b569/9999.log 52463 lorem\n Lorem 973113 lorem 123e4567-e89b-12d3-a456-426655440000 lorem 8888 lorem\n https://crates.io/23456/fd70b569 lorem");
|
||||
let buffer = "Lorem [link](http://foo.bar) ipsum CUSTOM-52463 lorem ISSUE-123 lorem\nLorem /var/fd70b569/9999.log 52463 lorem\n Lorem 973113 lorem 123e4567-e89b-12d3-a456-426655440000 lorem 8888 lorem\n https://crates.io/23456/fd70b569 lorem";
|
||||
|
||||
let named_pat = vec![];
|
||||
let custom: Vec<String> = ["CUSTOM-[0-9]{4,}", "ISSUE-[0-9]{3}"]
|
||||
|
|
@ -542,7 +537,7 @@ mod tests {
|
|||
.map(|&s| s.to_string())
|
||||
.collect();
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 9);
|
||||
assert_eq!(results.get(0).unwrap().text, "http://foo.bar");
|
||||
|
|
@ -564,14 +559,14 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn named_patterns() {
|
||||
let lines = split("Lorem [link](http://foo.bar) ipsum CUSTOM-52463 lorem ISSUE-123 lorem\nLorem /var/fd70b569/9999.log 52463 lorem\n Lorem 973113 lorem 123e4567-e89b-12d3-a456-426655440000 lorem 8888 lorem\n https://crates.io/23456/fd70b569 lorem");
|
||||
let buffer = "Lorem [link](http://foo.bar) ipsum CUSTOM-52463 lorem ISSUE-123 lorem\nLorem /var/fd70b569/9999.log 52463 lorem\n Lorem 973113 lorem 123e4567-e89b-12d3-a456-426655440000 lorem 8888 lorem\n https://crates.io/23456/fd70b569 lorem";
|
||||
|
||||
use crate::regexes::parse_pattern_name;
|
||||
let named_pat = vec![parse_pattern_name("url").unwrap()];
|
||||
|
||||
let custom = vec![];
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let results = Model::new(&lines, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
let results = Model::new(buffer, &alphabet, &named_pat, &custom, false).matches(false);
|
||||
|
||||
assert_eq!(results.len(), 2);
|
||||
assert_eq!(results.get(0).unwrap().text, "http://foo.bar");
|
||||
|
|
|
|||
10
src/ui.rs
10
src/ui.rs
|
|
@ -314,7 +314,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(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() {
|
||||
let focused = index == self.focus_index;
|
||||
|
|
@ -798,12 +798,10 @@ path: /usr/local/bin/cargo";
|
|||
|
||||
Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
|
||||
|
||||
let lines = content.split('\n').collect();
|
||||
|
||||
let named_pat = vec![];
|
||||
let custom_regexes = vec![];
|
||||
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(content, &alphabet, &named_pat, &custom_regexes, false);
|
||||
let rendering_colors = UiColors {
|
||||
text_fg: Box::new(color::Black),
|
||||
text_bg: Box::new(color::White),
|
||||
|
|
@ -861,13 +859,11 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
|
|||
|
||||
Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
|
||||
|
||||
let lines = content.split('\n').collect();
|
||||
|
||||
let named_pat = vec![];
|
||||
let custom_regexes = vec![];
|
||||
let alphabet = alphabets::Alphabet("abcd".to_string());
|
||||
let reverse = true;
|
||||
let mut model = model::Model::new(&lines, &alphabet, &named_pat, &custom_regexes, reverse);
|
||||
let mut model = model::Model::new(content, &alphabet, &named_pat, &custom_regexes, reverse);
|
||||
let unique_hint = false;
|
||||
let wrap_around = false;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue