1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
use anyhow::anyhow;
use rand::seq::IndexedRandom;
use regex::{Regex, RegexBuilder, RegexSet};
use std::collections::HashMap;
use std::io;
use std::ops::Range;
use std::path::PathBuf;
mod config;
struct Config {
patterns: Vec<Regex>,
set: RegexSet,
}
impl Config {
fn new(patterns: Vec<Regex>) -> Self {
let set = RegexSet::new(patterns.iter().map(|x| x.to_string())).unwrap();
Self { patterns, set }
}
}
struct Match {
line: String,
group: Range<usize>,
}
impl Match {
fn new(line: String, group: Range<usize>) -> Self {
Self { line, group }
}
fn prefix(&self) -> &str {
&self.line[0..self.group.start]
}
fn infix(&self) -> &str {
&self.line[self.group.clone()]
}
fn suffix(&self) -> &str {
&self.line[self.group.end..self.line.len()]
}
}
struct PatternGroup {
matches: Vec<Match>,
}
impl PatternGroup {
fn new(matches: Vec<Match>) -> Self {
Self { matches }
}
fn calculate_groups(&self) -> Vec<usize> {
let mut groups = Vec::with_capacity(self.matches.len());
groups.push(0);
for i in 1..self.matches.len() {
if self.matches[i - 1].prefix() != self.matches[i].prefix()
|| self.matches[i - 1].suffix() != self.matches[i].suffix()
{
groups.push(i);
}
}
groups
}
}
fn load_config(path: &PathBuf) -> anyhow::Result<Config> {
if !path.try_exists()? {
return Ok(default_config());
}
let groups = config::load_groups(path)?;
if let Some(patterns_str) = groups.get("groups") {
let mut patterns_regex = Vec::with_capacity(patterns_str.len());
for pattern in patterns_str {
let regex = RegexBuilder::new(pattern).size_limit(42_000).build()?;
if regex.captures_len() != 1 {
return Err(anyhow!(
"Invalid pattern {}, capture groups are not one",
pattern
));
}
patterns_regex.push(regex);
}
return Ok(Config::new(patterns_regex));
}
Ok(Config::new(vec![]))
}
fn default_config() -> Config {
Config::new(vec![Regex::new(r"part([0-9]+)").unwrap()])
}
fn get_group(config: &Config, input: &str) -> (i64, Range<usize>) {
if let Some(index) = config.set.matches(input).iter().next() {
return (
index as i64,
config.patterns[index]
.captures(input)
.unwrap()
.get(1)
.unwrap()
.range(),
);
}
(-1, 0..input.len())
}
fn print_group(matches: &[Match]) {
for m in matches {
println!("{}", m.line);
}
}
fn main() -> anyhow::Result<()> {
let xdg_dirs = xdg::BaseDirectories::new();
let config;
if let Some(config_path) = xdg_dirs.get_config_file("shuf-grp.conf") {
config = load_config(&config_path)
.unwrap_or_else(|err| panic!("Error parsing {}: {}", config_path.display(), err));
} else {
eprintln!("Using default config, create ~/.config/shuf-grp.conf");
config = default_config();
}
let stdin = io::stdin();
let mut groups = HashMap::new();
loop {
let mut buffer = String::new();
let bytes = stdin.read_line(&mut buffer)?;
if bytes == 0 {
break;
}
buffer.truncate(buffer.trim_end().len());
let (group, part) = get_group(&config, &buffer);
let m = Match::new(buffer, part);
if let std::collections::hash_map::Entry::Vacant(e) = groups.entry(group) {
e.insert(PatternGroup::new(vec![m]));
} else {
let pattern_group: &mut PatternGroup = groups.get_mut(&group).unwrap();
match pattern_group.matches.binary_search_by(|x| {
x.prefix().cmp(m.prefix()).then(
x.suffix()
.cmp(m.suffix())
.then(natord::compare(x.infix(), m.infix())),
)
}) {
Ok(pos) => pattern_group.matches.insert(pos, m),
Err(pos) => pattern_group.matches.insert(pos, m),
}
}
}
let mut indexes = Vec::<(i64, Range<usize>)>::with_capacity(groups.len());
for pattern_group in groups.iter_mut() {
let groups = pattern_group.1.calculate_groups();
for i in 1..groups.len() {
indexes.push((*pattern_group.0, groups[i - 1]..groups[i]));
}
indexes.push((
*pattern_group.0,
groups[groups.len() - 1]..pattern_group.1.matches.len(),
));
}
let mut rng = rand::rng();
for (group, range) in indexes.choose_multiple(&mut rng, indexes.len()) {
print_group(&groups.get(group).unwrap().matches[range.clone()]);
}
Ok(())
}
|