Skip to content

Commit

Permalink
keep the order of the links
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Apr 5, 2020
1 parent 67f7e44 commit 10817bf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
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.1.6"
version = "0.2.0"
authors = ["fcd <[email protected]>"]
edition = "2018"

Expand Down
25 changes: 18 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
extern crate regex;
extern crate skim;

use std::collections::HashSet;
use std::collections::HashMap;
use std::io::Cursor;
use std::io::{self, Read};
use std::process::Command;

use regex::Regex;
use skim::{Skim, SkimOptionsBuilder};

const URL_REGEX: &str = r"(http(s)?://[a-zA-Z0-9_/?+&.=@%#;-]+)";
const URL_REGEX: &str = r"(http(s)?://[a-zA-Z0-9_/?+&.=@%#;~-]+)";

pub fn main() {
let options = SkimOptionsBuilder::default()
Expand All @@ -23,18 +23,29 @@ pub fn main() {
io::stdin().read_to_string(&mut buffer).unwrap();
let lines = buffer.split("\n");

let mut matches: HashSet<&str> = HashSet::new();
let mut matches: HashMap<&str, u8> = HashMap::new();
let mut match_index = 1;

for line in lines {
for capture in re.captures_iter(line) {
let url_match = capture.get(1).unwrap().as_str();
matches.insert(url_match);
if matches.contains_key(url_match) {
continue;
}
matches.insert(url_match, match_index);
match_index += 1;
}
}

let unique_items: Vec<&str> = matches.into_iter().collect();
let items = unique_items.join("\n");
// `run_with` would read and show items from the stream
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 items = item_list.join("\n");

let selected_items = Skim::run_with(&options, Some(Box::new(Cursor::new(items))))
.map(|out| out.selected_items)
.unwrap_or_else(|| Vec::new());
Expand Down

0 comments on commit 10817bf

Please sign in to comment.