Skip to content

Commit 10817bf

Browse files
committed
keep the order of the links
1 parent 67f7e44 commit 10817bf

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "leth"
3-
version = "0.1.6"
3+
version = "0.2.0"
44
authors = ["fcd <[email protected]>"]
55
edition = "2018"
66

src/main.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
extern crate regex;
22
extern crate skim;
33

4-
use std::collections::HashSet;
4+
use std::collections::HashMap;
55
use std::io::Cursor;
66
use std::io::{self, Read};
77
use std::process::Command;
88

99
use regex::Regex;
1010
use skim::{Skim, SkimOptionsBuilder};
1111

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

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

26-
let mut matches: HashSet<&str> = HashSet::new();
26+
let mut matches: HashMap<&str, u8> = HashMap::new();
27+
let mut match_index = 1;
2728

2829
for line in lines {
2930
for capture in re.captures_iter(line) {
3031
let url_match = capture.get(1).unwrap().as_str();
31-
matches.insert(url_match);
32+
if matches.contains_key(url_match) {
33+
continue;
34+
}
35+
matches.insert(url_match, match_index);
36+
match_index += 1;
3237
}
3338
}
3439

35-
let unique_items: Vec<&str> = matches.into_iter().collect();
36-
let items = unique_items.join("\n");
37-
// `run_with` would read and show items from the stream
40+
let mut ordered_items: Vec<_> = matches.into_iter()
41+
.collect();
42+
ordered_items.sort_by(|a, b| a.1.cmp(&b.1));
43+
44+
let item_list: Vec<_> = ordered_items.iter()
45+
.map(|item|item.0)
46+
.collect();
47+
let items = item_list.join("\n");
48+
3849
let selected_items = Skim::run_with(&options, Some(Box::new(Cursor::new(items))))
3950
.map(|out| out.selected_items)
4051
.unwrap_or_else(|| Vec::new());

0 commit comments

Comments
 (0)