Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow multiline matching and double quotes in regexp expression #140

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ termion = "1.5.6"
regex = "1.7.1"
clap = "2.34.0"
base64 = "0.13.1"
unicode-width = "0.1.10"
lazy_static = "1.4.0"
itertools = "0.10.5"

[[bin]]
name = "thumbs"
Expand Down
23 changes: 11 additions & 12 deletions src/alphabets.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

const ALPHABETS: [(&'static str, &'static str); 22] = [
const ALPHABETS: [(&str, &str); 22] = [
("numeric", "1234567890"),
("abcd", "abcd"),
("qwerty", "asdfqwerzxcvjklmiuopghtybn"),
Expand Down Expand Up @@ -44,18 +44,17 @@ impl<'a> Alphabet<'a> {
if expansion.len() + expanded.len() >= matches {
break;
}
if expansion.is_empty() {
if let Some(prefix) = expansion.pop() {
let sub_expansion: Vec<String> = letters
.iter()
.take(matches - expansion.len() - expanded.len())
.map(|s| prefix.clone() + s)
.collect();

expanded.splice(0..0, sub_expansion);
} else {
break;
}

let prefix = expansion.pop().expect("Ouch!");
let sub_expansion: Vec<String> = letters
.iter()
.take(matches - expansion.len() - expanded.len())
.map(|s| prefix.clone() + s)
.collect();

expanded.splice(0..0, sub_expansion);
}

expansion = expansion.iter().take(matches - expanded.len()).cloned().collect();
Expand All @@ -69,7 +68,7 @@ pub fn get_alphabet(alphabet_name: &str) -> Alphabet {

alphabets
.get(alphabet_name)
.expect(format!("Unknown alphabet: {}", alphabet_name).as_str()); // FIXME
.unwrap_or_else(|| panic!("Unknown alphabet: {}", alphabet_name)); // FIXME

Alphabet::new(alphabets[alphabet_name])
}
Expand Down
15 changes: 9 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::io::{self, Read, Write};
fn dbg(msg: &str) {
let mut file = std::fs::OpenOptions::new()
.append(true)
.create(true)
.open("/tmp/thumbs.log")
.expect("Unable to open log file");

Expand Down Expand Up @@ -170,9 +171,7 @@ fn main() {

handle.read_to_string(&mut output).unwrap();

let lines = output.split('\n').collect::<Vec<&str>>();

let mut state = state::State::new(&lines, alphabet, &regexp);
let mut state = state::State::new(output.as_str(), alphabet, &regexp);

let selected = {
let mut viewbox = view::View::new(
Expand Down Expand Up @@ -204,11 +203,15 @@ fn main() {
let mut output = format.to_string();

output = str::replace(&output, "%U", upcase_value);
output = str::replace(&output, "%H", text.as_str());
output = str::replace(&output, "%H", text
.split('\n')
.map(|line| line.trim_end())
.collect::<Vec<&str>>()
.join("\n").as_str());
output
})
.collect::<Vec<_>>()
.join("\n");
.join("\0");

if let Some(target) = target {
let mut file = OpenOptions::new()
Expand All @@ -218,7 +221,7 @@ fn main() {
.open(target)
.expect("Unable to open the target file");

file.write(output.as_bytes()).unwrap();
file.write_all(output.as_bytes()).unwrap();
} else {
print!("{}", output);
}
Expand Down
Loading