Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: input query with stdin #39

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading