Skip to content

Commit

Permalink
chore: merge pull request #15 from graelo/feat/cli-args
Browse files Browse the repository at this point in the history
Add command-line arguments matching
  • Loading branch information
graelo authored Sep 1, 2024
2 parents e691596 + f596ce0 commit 53bdf54
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 39 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 59 additions & 27 deletions src/textbuf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod tests {

assert_eq!(spans.len(), 1);
assert_eq!(
spans.get(0).unwrap().text,
spans.first().unwrap().text,
"30557a29d5abc51e5f1d5b472e79b7e296f595abcf19fe6b9199dbbc809c6ff4"
);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ mod tests {
.spans;

assert_eq!(spans.len(), 3);
assert_eq!(spans.get(0).unwrap().text, "/var/log/nginx.log");
assert_eq!(spans.first().unwrap().text, "/var/log/nginx.log");
assert_eq!(spans.get(1).unwrap().text, "test/log/nginx-2.log");
assert_eq!(spans.get(2).unwrap().text, "folder/[email protected]");
}
Expand Down Expand Up @@ -143,7 +143,7 @@ mod tests {
.spans;

assert_eq!(spans.len(), 3);
assert_eq!(spans.get(0).unwrap().text, "/tmp/foo/bar_lol");
assert_eq!(spans.first().unwrap().text, "/tmp/foo/bar_lol");
assert_eq!(spans.get(1).unwrap().text, "/var/log/boot-strap.log");
assert_eq!(spans.get(2).unwrap().text, "../log/kern.log");
}
Expand All @@ -170,7 +170,7 @@ mod tests {
.spans;

assert_eq!(spans.len(), 1);
assert_eq!(spans.get(0).unwrap().text, "~/.gnu/.config.txt");
assert_eq!(spans.first().unwrap().text, "~/.gnu/.config.txt");
}

#[test]
Expand Down Expand Up @@ -219,7 +219,7 @@ mod tests {
.spans;

assert_eq!(spans.len(), 4);
assert_eq!(spans.get(0).unwrap().text, "fd70b5695");
assert_eq!(spans.first().unwrap().text, "fd70b5695");
assert_eq!(spans.get(1).unwrap().text, "5246ddf");
assert_eq!(spans.get(2).unwrap().text, "f924213");
assert_eq!(
Expand Down Expand Up @@ -250,8 +250,8 @@ mod tests {
.spans;

assert_eq!(spans.len(), 3);
assert_eq!(spans.get(0).unwrap().pattern, "ipv4");
assert_eq!(spans.get(0).unwrap().text, "127.0.0.1");
assert_eq!(spans.first().unwrap().pattern, "ipv4");
assert_eq!(spans.first().unwrap().text, "127.0.0.1");
assert_eq!(spans.get(1).unwrap().pattern, "ipv4");
assert_eq!(spans.get(1).unwrap().text, "255.255.10.255");
assert_eq!(spans.get(2).unwrap().pattern, "ipv4");
Expand Down Expand Up @@ -280,7 +280,7 @@ mod tests {
.spans;

assert_eq!(spans.len(), 4);
assert_eq!(spans.get(0).unwrap().text, "fe80::2:202:fe4");
assert_eq!(spans.first().unwrap().text, "fe80::2:202:fe4");
assert_eq!(
spans.get(1).unwrap().text,
"2001:67c:670:202:7ba8:5e41:1591:d723"
Expand Down Expand Up @@ -312,8 +312,8 @@ mod tests {
.spans;

assert_eq!(spans.len(), 2);
assert_eq!(spans.get(0).unwrap().pattern, "markdown-url");
assert_eq!(spans.get(0).unwrap().text, "https://github.io?foo=bar");
assert_eq!(spans.first().unwrap().pattern, "markdown-url");
assert_eq!(spans.first().unwrap().text, "https://github.io?foo=bar");
assert_eq!(spans.get(1).unwrap().pattern, "markdown-url");
assert_eq!(spans.get(1).unwrap().text, "http://cdn.com/img.jpg");
}
Expand Down Expand Up @@ -344,10 +344,10 @@ mod tests {

assert_eq!(spans.len(), 6);
assert_eq!(
spans.get(0).unwrap().text,
spans.first().unwrap().text,
"https://www.rust-lang.org/tools"
);
assert_eq!(spans.get(0).unwrap().pattern, "url");
assert_eq!(spans.first().unwrap().pattern, "url");
assert_eq!(spans.get(1).unwrap().text, "https://crates.io");
assert_eq!(spans.get(1).unwrap().pattern, "url");
assert_eq!(spans.get(2).unwrap().text, "https://github.io?foo=bar");
Expand Down Expand Up @@ -383,8 +383,8 @@ mod tests {
.spans;

assert_eq!(spans.len(), 2);
assert_eq!(spans.get(0).unwrap().pattern, "email");
assert_eq!(spans.get(0).unwrap().text, "[email protected]");
assert_eq!(spans.first().unwrap().pattern, "email");
assert_eq!(spans.first().unwrap().text, "[email protected]");
assert_eq!(spans.get(1).unwrap().pattern, "email");
assert_eq!(
spans.get(1).unwrap().text,
Expand Down Expand Up @@ -414,8 +414,8 @@ mod tests {
.spans;

assert_eq!(spans.len(), 3);
assert_eq!(spans.get(0).unwrap().pattern, "pointer-address");
assert_eq!(spans.get(0).unwrap().text, "0xfd70b5695");
assert_eq!(spans.first().unwrap().pattern, "pointer-address");
assert_eq!(spans.first().unwrap().text, "0xfd70b5695");
assert_eq!(spans.get(1).unwrap().pattern, "pointer-address");
assert_eq!(spans.get(1).unwrap().text, "0x5246ddf");
assert_eq!(spans.get(2).unwrap().pattern, "pointer-address");
Expand Down Expand Up @@ -444,7 +444,7 @@ mod tests {
.spans;

assert_eq!(spans.len(), 4);
assert_eq!(spans.get(0).unwrap().text, "#fd7b56");
assert_eq!(spans.first().unwrap().text, "#fd7b56");
assert_eq!(spans.get(1).unwrap().text, "#FF00FF");
assert_eq!(spans.get(2).unwrap().text, "#00fF05");
assert_eq!(spans.get(3).unwrap().text, "#abcd00");
Expand Down Expand Up @@ -473,7 +473,7 @@ mod tests {

assert_eq!(spans.len(), 1);
assert_eq!(
spans.get(0).unwrap().text,
spans.first().unwrap().text,
"QmRdbNSxDJBXmssAc9fvTtux4duptMvfSGiGuq6yHAQVKQ"
);
}
Expand Down Expand Up @@ -524,8 +524,8 @@ mod tests {
.spans;

assert_eq!(spans.len(), 1);
assert_eq!(spans.get(0).unwrap().pattern, "diff-a");
assert_eq!(spans.get(0).unwrap().text, "src/main.rs");
assert_eq!(spans.first().unwrap().pattern, "diff-a");
assert_eq!(spans.first().unwrap().text, "src/main.rs");
}

#[test]
Expand All @@ -550,8 +550,8 @@ mod tests {
.spans;

assert_eq!(spans.len(), 1);
assert_eq!(spans.get(0).unwrap().pattern, "diff-b");
assert_eq!(spans.get(0).unwrap().text, "src/main.rs");
assert_eq!(spans.first().unwrap().pattern, "diff-b");
assert_eq!(spans.first().unwrap().text, "src/main.rs");
}

#[test]
Expand All @@ -576,8 +576,8 @@ mod tests {
.spans;

assert_eq!(spans.len(), 1);
assert_eq!(spans.get(0).unwrap().pattern, "datetime");
assert_eq!(spans.get(0).unwrap().text, "2021-03-04T12:23:34");
assert_eq!(spans.first().unwrap().pattern, "datetime");
assert_eq!(spans.first().unwrap().text, "2021-03-04T12:23:34");
}

#[test]
Expand Down Expand Up @@ -610,11 +610,43 @@ mod tests {
.spans;

assert_eq!(spans.len(), 3);
assert_eq!(spans.get(0).unwrap().text, "first string");
assert_eq!(spans.first().unwrap().text, "first string");
assert_eq!(spans.get(1).unwrap().text, "second string");
assert_eq!(spans.get(2).unwrap().text, "rustc --explain E0223");
}

#[test]
fn match_commandline_args() {
let buffer =
"command --arg arg1 --arg=arg2 --arg arg3-long -x hashes -a -u -l -x others\n'";
let lines = buffer.split('\n').collect::<Vec<_>>();

let use_all_patterns = false;
use crate::textbuf::regexes::parse_pattern_name;
let named_pat = vec![parse_pattern_name("command-line-args").unwrap()];
let custom = vec![];
let alphabet = Alphabet("abcd".to_string());
let reverse = false;
let unique_hint = false;
let spans = Model::new(
&lines,
&alphabet,
use_all_patterns,
&named_pat,
&custom,
reverse,
unique_hint,
)
.spans;

assert_eq!(spans.len(), 5);
assert_eq!(spans.first().unwrap().text, "arg1");
assert_eq!(spans.get(1).unwrap().text, "arg2");
assert_eq!(spans.get(2).unwrap().text, "arg3-long");
assert_eq!(spans.get(3).unwrap().text, "hashes");
assert_eq!(spans.get(4).unwrap().text, "others");
}

#[test]
fn priority_between_regexes() {
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";
Expand All @@ -640,7 +672,7 @@ mod tests {
.spans;

assert_eq!(spans.len(), 9);
assert_eq!(spans.get(0).unwrap().text, "http://foo.bar");
assert_eq!(spans.first().unwrap().text, "http://foo.bar");
assert_eq!(spans.get(1).unwrap().text, "CUSTOM-52463");
assert_eq!(spans.get(2).unwrap().text, "ISSUE-123");
assert_eq!(spans.get(3).unwrap().text, "/var/fd70b569/9999.log");
Expand Down Expand Up @@ -682,7 +714,7 @@ mod tests {
.spans;

assert_eq!(spans.len(), 2);
assert_eq!(spans.get(0).unwrap().text, "http://foo.bar");
assert_eq!(spans.first().unwrap().text, "http://foo.bar");
assert_eq!(
spans.get(1).unwrap().text,
"https://crates.io/23456/fd70b569"
Expand Down
6 changes: 5 additions & 1 deletion src/textbuf/regexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(super) const EXCLUDE_PATTERNS: [(&str, &str); 1] =
///
/// The email address was obtained at https://www.regular-expressions.info/email.html.
/// Some others were obtained from Ferran Basora, the rest is by me.
pub(super) const PATTERNS: [(&str, &str); 20] = [
pub(super) const PATTERNS: [(&str, &str); 21] = [
("markdown-url", r"\[[^]]*\]\(([^)]+)\)"),
(
"url",
Expand Down Expand Up @@ -44,6 +44,10 @@ pub(super) const PATTERNS: [(&str, &str); 20] = [
("quoted-double", r#""([^"]+)""#),
("quoted-backtick", r#"`([^`]+)`"#),
("digits", r"([0-9]{4,})"),
(
"command-line-args",
r#"(?:--[a-z][0-9a-z\-_]+|-[a-z])(?: |=)([^-\s\n]\S+)"#,
),
];

/// Type-safe string Pattern Name (newtype).
Expand Down
8 changes: 5 additions & 3 deletions src/tmux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,11 @@ pub fn available_panes() -> Result<Vec<Pane>> {
result
}

/// Returns tmux global options as a `HashMap`. The prefix argument is for
/// convenience, in order to target only some of our options. For instance,
/// `get_options("@copyrat-")` will return a `HashMap` which keys are tmux options names like `@copyrat-command`, and associated values.
/// Returns tmux global options as a `HashMap`.
///
/// The prefix argument is for convenience, in order to target only some of our options. For
/// instance, `get_options("@copyrat-")` will return a `HashMap` which keys are tmux options names
/// like `@copyrat-command`, and associated values.
///
/// # Example
/// ```get_options("@copyrat-")```
Expand Down
2 changes: 2 additions & 0 deletions tmux-copyrat.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ setup_pattern_binding () {
tmux bind-key -T ${keytable} ${key} new-window -d -n ${window_name} "${BINARY} run --window-name '"${window_name}"' --clipboard-exe ${clipboard_exe} --reverse --unique-hint ${pattern_arg}"
}

# prefix + t + a searches for command-line arguments
setup_pattern_binding "a" "--pattern-name command-line-args"
# prefix + t + c searches for hex colors #aa00f5
setup_pattern_binding "c" "--pattern-name hexcolor"
# prefix + t + d searches for dates or datetimes
Expand Down

0 comments on commit 53bdf54

Please sign in to comment.