aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/ts/tskm/src/interface/input/handle.rs
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-04-04 11:48:44 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-04-04 11:48:44 +0200
commit135d09bfb305d54cac1ba1fb9861d5b9309a7b3a (patch)
tree459109a40320530993ae560f55a730a72df31416 /pkgs/by-name/ts/tskm/src/interface/input/handle.rs
parentrefactor(modules/legacy/firefox): Move to by-name (diff)
downloadnixos-config-135d09bfb305d54cac1ba1fb9861d5b9309a7b3a.zip
feat(pkgs/neorg): Rewrite in rust
This improves upon neorg by integrating it better into the system context.
Diffstat (limited to 'pkgs/by-name/ts/tskm/src/interface/input/handle.rs')
-rw-r--r--pkgs/by-name/ts/tskm/src/interface/input/handle.rs112
1 files changed, 112 insertions, 0 deletions
diff --git a/pkgs/by-name/ts/tskm/src/interface/input/handle.rs b/pkgs/by-name/ts/tskm/src/interface/input/handle.rs
new file mode 100644
index 00000000..0ff0e56e
--- /dev/null
+++ b/pkgs/by-name/ts/tskm/src/interface/input/handle.rs
@@ -0,0 +1,112 @@
+use std::{
+ 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 } => {
+ let file = fs::read_to_string(file)?;
+ for line in file.lines() {
+ let input = Input::from_str(line)?;
+ 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 => {
+ for url in Input::all()? {
+ println!("{url}");
+ }
+ }
+ }
+ Ok(())
+}