From ce5cf76fc450c279b5ffb4cc5f3f9db8017495b1 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 22 Jan 2024 22:42:57 +0000 Subject: [PATCH] feat: input query with stdin (#39) --- README.md | 8 ++++---- src/main.rs | 34 ++++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9f16fd0..bd918f8 100644 --- a/README.md +++ b/README.md @@ -31,13 +31,11 @@ Once you've added taglines to your local files you can run `tag`. `tag` will sea The `tag` help message: ``` -Search for local text files with a simple tagging system. - -Usage: tag [OPTIONS] +Usage: tag [OPTIONS] [QUERY] Arguments: - Search query for the tags The path that will be searched + [QUERY] Search query for the tags Options: -s, --silent @@ -48,6 +46,8 @@ Options: A command that must run successfully for a file to be accepted -n, --no-color Disable coloring + -q, --query-stdin + Receive a query from the standard input -h, --help Print help -V, --version diff --git a/src/main.rs b/src/main.rs index 3903306..0a2128b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::io::IsTerminal; +use std::io::{BufRead, IsTerminal}; use std::{path::Path, process::Command}; use colored::Colorize; @@ -14,14 +14,14 @@ mod cli { #[derive(Parser)] #[command(author, version, about, long_about = None)] pub struct Cli { - #[clap(value_name = "QUERY")] - /// Search query for the tags. - pub query: String, - #[clap(value_name = "PATH")] /// The path that will be searched. pub path: String, + #[clap(value_name = "QUERY", group = "q-input")] + /// Search query for the tags. + pub query: Option, + #[arg(short, long)] /// Only print the paths of matched files. pub silent: bool, @@ -37,6 +37,10 @@ mod cli { #[arg(short, long)] /// Disable coloring. pub no_color: bool, + + #[arg(short, long, group = "q-input")] + /// Receive a query from the standard input. + pub query_stdin: bool, } impl Cli { @@ -113,8 +117,26 @@ fn main() -> Result<(), Box> { colored::control::set_override(false); } + if !args.query_stdin && args.query.is_none() { + eprintln!( + "{} {}", + "[ERROR]".red().bold(), + "Please provide a query, either through stdin or by manually adding it.".red() + ); + std::process::exit(1); + } + + // fetch the query + let query = if args.query.is_some() { + args.query.unwrap() + } else { + let mut query = String::new(); + std::io::stdin().lock().read_line(&mut query)?; + query + }; + let file_index = get_tags_from_files(args.path.as_str())?; - let query = QueryParser::parse(Rule::tagsearch, args.query.as_str()); + let query = QueryParser::parse(Rule::tagsearch, query.as_str()); if let Err(e) = &query { eprintln!(