diff --git a/Cargo.lock b/Cargo.lock index 5498d0e..ec7460a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -493,7 +493,7 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "leth" -version = "0.4.1" +version = "0.6.0" dependencies = [ "regex", "skim", diff --git a/Cargo.toml b/Cargo.toml index f513b82..0a02b69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "leth" -version = "0.5.0" +version = "0.6.0" authors = ["fcd "] edition = "2018" diff --git a/src/main.rs b/src/main.rs index d97a997..e003110 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ use regex::Regex; use skim::prelude::*; use structopt::StructOpt; +const LINE_SPLITTER: char = '='; const URL_REGEX: &str = r"(http(s)?://[a-zA-Z0-9_/?+&.=@%#;~:-]+)"; #[derive(Debug, StructOpt)] @@ -31,16 +32,41 @@ pub fn main() { io::stdin().read_to_string(&mut buffer).unwrap(); let lines = buffer.split("\n"); - let mut matches: HashMap<&str, u8> = HashMap::new(); - let mut match_index = 1; - + let mut split_lines = false; + let mut split_line_buffer: Vec<&str> = Vec::new(); + let mut merged_lines: Vec = Vec::new(); for line in lines { - for capture in re.captures_iter(line) { + if line.len() == 0 { + continue + } + + if line.ends_with(LINE_SPLITTER) { + let mergable = line.get(0..line.len() - 1).unwrap_or(""); + split_line_buffer.push(mergable); + split_lines = true; + continue; + } + + if split_lines { + split_lines = false; + split_line_buffer.push(line); + let merged_line = &split_line_buffer.join(""); + merged_lines.push(merged_line.to_string()); + split_line_buffer = Vec::new(); + } else { + merged_lines.push(line.to_string()); + } + } + + let mut matches: HashMap = HashMap::new(); + let mut match_index = 1; + for line in merged_lines { + for capture in re.captures_iter(&line) { let url_match = capture.get(1).unwrap().as_str(); if matches.contains_key(url_match) { continue; } - matches.insert(url_match, match_index); + matches.insert(url_match.to_string(), match_index); match_index += 1; } } @@ -48,7 +74,7 @@ pub fn main() { let mut ordered_items: Vec<_> = matches.into_iter().collect(); ordered_items.sort_by(|a, b| a.1.cmp(&b.1)); - let item_list: Vec<_> = ordered_items.iter().map(|item| item.0).collect(); + let item_list: Vec<_> = ordered_items.iter().map(|item| item.0.as_str()).collect(); let items = item_list.join("\n"); let item_reader = SkimItemReader::default();