about summary refs log tree commit diff stats
path: root/pkgs/by-name/ts/tskm/src/interface/input/handle.rs
blob: 839e122f161d02226da3f9c410c4579746d610c2 (plain) (blame)
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
// 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::{HashMap, HashSet},
    fs,
    str::FromStr,
};

use anyhow::{Context, Result};
use log::info;

use crate::{browser::open_in_browser, cli::InputCommand, state::State};

use super::{Input, Tag};

/// # Errors
/// When command handling fails.
///
/// # Panics
/// When internal assertions fail.
#[allow(clippy::too_many_lines)]
pub fn handle(command: InputCommand, state: &mut State) -> 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 } => {
            'outer: for all in Input::all()?.chunks(100) {
                info!("Starting review for the first hundred URLs.");

                for input in all {
                    info!("-> '{input}'");
                    open_in_browser(&project, state, Some(input.url.clone()))?;
                }

                {
                    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;
                    }
                }
            }
        }
        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(())
}