diff options
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.rs | 75 |
1 files changed, 39 insertions, 36 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 index 0469870c..11304633 100644 --- a/pkgs/by-name/ts/tskm/src/interface/input/handle.rs +++ b/pkgs/by-name/ts/tskm/src/interface/input/handle.rs @@ -9,26 +9,25 @@ // If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>. use std::{ - collections::HashSet, - fs, process, + collections::{HashMap, HashSet}, + fs, str::FromStr, - thread::{self, sleep}, - time::Duration, }; use anyhow::{Context, Result}; -use log::{error, info}; +use log::info; -use crate::cli::InputCommand; +use crate::{browser::open_in_browser, cli::InputCommand, state::State}; -use super::Input; +use super::{Input, Tag}; /// # Errors /// When command handling fails. /// /// # Panics /// When internal assertions fail. -pub fn handle(command: InputCommand) -> Result<()> { +#[allow(clippy::too_many_lines)] +pub fn handle(command: InputCommand, state: &mut State) -> Result<()> { match command { InputCommand::Add { inputs } => { for input in inputs { @@ -67,36 +66,12 @@ pub fn handle(command: InputCommand) -> Result<()> { } } 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!"); - } + open_in_browser(&project, state, Some(input.url.clone()))?; } { @@ -122,9 +97,6 @@ pub fn handle(command: InputCommand) -> Result<()> { } } } - - info!("Waiting for firefox to stop"); - handle.join().expect("Should be joinable")?; } InputCommand::List { tags } => { let mut tag_set = HashSet::with_capacity(tags.len()); @@ -139,6 +111,37 @@ pub fn handle(command: InputCommand) -> Result<()> { println!("{url}"); } } + InputCommand::Tags {} => { + let mut without_tags = 0; + let mut tag_set: HashMap<Tag, u64> = HashMap::new(); + + for input in Input::all()? { + if input.tags.is_empty() { + without_tags += 1; + } + + for tag in input.tags { + if let Some(number) = tag_set.get_mut(&tag) { + *number += 1; + } else { + tag_set.insert(tag, 1); + } + } + } + + let mut tags: Vec<(Tag, u64)> = tag_set.into_iter().collect(); + tags.sort_by_key(|(_, number)| *number); + tags.reverse(); + + for (tag, number) in tags { + println!("{tag} {number}"); + } + + if without_tags != 0 { + println!(); + println!("Witohut tags: {without_tags}"); + } + } } Ok(()) } |