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
|
// nixos-config - My current NixOS configuration
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of my nixos-config.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
use std::{
collections::HashSet,
fs, process,
str::FromStr,
thread::{self, sleep},
time::Duration,
};
use anyhow::{Context, Result};
use log::{error, info};
use crate::cli::InputCommand;
use super::Input;
/// # Errors
/// When command handling fails.
///
/// # Panics
/// When internal assertions fail.
pub fn handle(command: InputCommand) -> Result<()> {
match command {
InputCommand::Add { inputs } => {
for input in inputs {
input.commit().with_context(|| {
format!("Failed to add input ('{input}') to the input storage.")
})?;
}
}
InputCommand::Remove { inputs } => {
for input in inputs {
input.remove().with_context(|| {
format!("Failed to remove input ('{input}') from the input storage.")
})?;
}
}
InputCommand::File { file, tags } => {
let file = fs::read_to_string(&file)
.with_context(|| format!("Failed to read input file '{}'", file.display()))?;
let mut tag_set = HashSet::with_capacity(tags.len());
for tag in tags {
tag_set.insert(tag);
}
for line in file.lines().map(str::trim) {
if line.is_empty() {
continue;
}
let mut input = Input::from_str(line)?;
input.tags = input.tags.union(&tag_set).cloned().collect();
input.commit().with_context(|| {
format!("Failed to add input ('{input}') to the input storage.")
})?;
}
}
InputCommand::Review { project } => {
let project = project.to_project_display();
let local_project = project.clone();
let handle = thread::spawn(move || {
// We assume that the project is not yet open.
let mut firefox = process::Command::new("firefox")
.args(["-P", local_project.as_str(), "about:newtab"])
.spawn()?;
Ok::<_, anyhow::Error>(firefox.wait()?)
});
// Give Firefox some time to start.
info!("Waiting on firefox to start");
sleep(Duration::from_secs(4));
let project_str = project.as_str();
'outer: for all in Input::all()?.chunks(100) {
info!("Starting review for the first hundred URLs.");
for input in all {
info!("-> '{input}'");
let status = process::Command::new("firefox")
.args(["-P", project_str, input.url().to_string().as_str()])
.status()?;
if status.success() {
input.remove()?;
} else {
error!("Adding `{input}` to Firefox failed!");
}
}
{
use std::io::{stdin, stdout, Write};
let mut s = String::new();
eprint!("Continue? (y/N) ");
stdout().flush()?;
stdin()
.read_line(&mut s)
.expect("Did not enter a correct string");
if let Some('\n') = s.chars().next_back() {
s.pop();
}
if let Some('\r') = s.chars().next_back() {
s.pop();
}
if s != "y" {
break 'outer;
}
}
}
info!("Waiting for firefox to stop");
handle.join().expect("Should be joinable")?;
}
InputCommand::List { tags } => {
let mut tag_set = HashSet::with_capacity(tags.len());
for tag in tags {
tag_set.insert(tag);
}
for url in Input::all()?
.iter()
.filter(|input| tag_set.is_subset(&input.tags))
{
println!("{url}");
}
}
}
Ok(())
}
|