Skip to content

Commit

Permalink
merge split lines
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Jan 5, 2023
1 parent 648e6e5 commit 654a62f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 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
@@ -1,6 +1,6 @@
[package]
name = "leth"
version = "0.5.0"
version = "0.6.0"
authors = ["fcd <[email protected]>"]
edition = "2018"

Expand Down
38 changes: 32 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -31,24 +32,49 @@ 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<String> = 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<String, u8> = 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;
}
}

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();
Expand Down

0 comments on commit 654a62f

Please sign in to comment.