Skip to content

Commit

Permalink
feat: input query with stdin (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf authored Jan 22, 2024
1 parent a62e3c2 commit ce5cf76
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] <QUERY> <PATH>
Usage: tag [OPTIONS] <PATH> [QUERY]
Arguments:
<QUERY> Search query for the tags
<PATH> The path that will be searched
[QUERY] Search query for the tags
Options:
-s, --silent
Expand All @@ -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
Expand Down
34 changes: 28 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::IsTerminal;
use std::io::{BufRead, IsTerminal};
use std::{path::Path, process::Command};

use colored::Colorize;
Expand All @@ -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<String>,

#[arg(short, long)]
/// Only print the paths of matched files.
pub silent: bool,
Expand All @@ -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 {
Expand Down Expand Up @@ -113,8 +117,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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!(
Expand Down

0 comments on commit ce5cf76

Please sign in to comment.