aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs')
-rw-r--r--pkgs/by-name/ts/tskm/src/cli.rs38
-rw-r--r--pkgs/by-name/ts/tskm/src/interface/input/handle.rs22
2 files changed, 54 insertions, 6 deletions
diff --git a/pkgs/by-name/ts/tskm/src/cli.rs b/pkgs/by-name/ts/tskm/src/cli.rs
index a1dd2d8e..ac8f8ee9 100644
--- a/pkgs/by-name/ts/tskm/src/cli.rs
+++ b/pkgs/by-name/ts/tskm/src/cli.rs
@@ -16,7 +16,10 @@ use clap_complete::{ArgValueCompleter, CompletionCandidate};
use url::Url;
use crate::{
- interface::{input::Input, project::ProjectName},
+ interface::{
+ input::{Input, Tag},
+ project::ProjectName,
+ },
state, task,
};
@@ -153,7 +156,14 @@ pub enum InputCommand {
/// Add all URLs in the file as inputs to be categorized.
///
/// This expects each line to contain one URL.
- File { file: PathBuf },
+ File {
+ /// The file to read from.
+ file: PathBuf,
+
+ /// Additional tags to apply to every read URL in the file.
+ #[arg(add = ArgValueCompleter::new(complete_tag))]
+ tags: Vec<Tag>,
+ },
/// Like 'review', but for the inputs that have previously been added.
/// It takes a project in which to open the URLs.
@@ -272,3 +282,27 @@ fn complete_input_url(current: &OsStr) -> Vec<CompletionCandidate> {
output
}
+fn complete_tag(current: &OsStr) -> Vec<CompletionCandidate> {
+ let mut output = vec![];
+
+ let Some(current) = current.to_str() else {
+ return output;
+ };
+
+ if !current.starts_with('+') {
+ output.push(CompletionCandidate::new(format!("+{current}")));
+ }
+
+ output
+}
+
+#[cfg(test)]
+mod test {
+ use clap::CommandFactory;
+
+ use super::CliArgs;
+ #[test]
+ fn verify_cli() {
+ CliArgs::command().debug_assert();
+ }
+}
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 9c39cfef..09827fca 100644
--- a/pkgs/by-name/ts/tskm/src/interface/input/handle.rs
+++ b/pkgs/by-name/ts/tskm/src/interface/input/handle.rs
@@ -9,6 +9,7 @@
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
use std::{
+ collections::HashSet,
fs, process,
str::FromStr,
thread::{self, sleep},
@@ -43,10 +44,23 @@ pub fn handle(command: InputCommand) -> Result<()> {
})?;
}
}
- InputCommand::File { file } => {
- let file = fs::read_to_string(file)?;
- for line in file.lines() {
- let input = Input::from_str(line)?;
+ 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.")
})?;