From 92f9687ecd8ccbbcb627a8edda068761274c74cf Mon Sep 17 00:00:00 2001 From: arabian9ts Date: Sat, 10 Aug 2024 19:35:14 +0900 Subject: [PATCH 1/2] list: add subcommand for listing remind comments --- cli/src/args.rs | 13 +++++++++++++ cli/src/subcommand/list.rs | 28 ++++++++++++++++++++++++++++ cli/src/subcommand/mod.rs | 4 +++- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 cli/src/subcommand/list.rs diff --git a/cli/src/args.rs b/cli/src/args.rs index 1755511..80bba0e 100644 --- a/cli/src/args.rs +++ b/cli/src/args.rs @@ -12,6 +12,7 @@ pub struct Args { pub enum Subcommand { Run(RunCommand), Init(InitCommand), + List(ListCommand), } #[derive(FromArgs, PartialEq, Debug)] @@ -34,6 +35,18 @@ pub struct RunCommand { /// initialize a config of reminder-lint pub struct InitCommand {} +#[derive(FromArgs, PartialEq, Debug)] +#[argh(subcommand, name = "list")] +/// list reminder-lint comments +pub struct ListCommand { + /// path to the config file (default: ./remind.yml) + #[argh(option, short = 'c')] + pub config_file_path: Option, + /// path to the ignore file (default: ./.remindignore) + #[argh(option, short = 'i')] + pub ignore_file_path: Option, +} + impl Args { pub fn new() -> Self { argh::from_env() diff --git a/cli/src/subcommand/list.rs b/cli/src/subcommand/list.rs new file mode 100644 index 0000000..43d2dae --- /dev/null +++ b/cli/src/subcommand/list.rs @@ -0,0 +1,28 @@ +use crate::args::ListCommand; +use anyhow::Error; +use reminder_lint_core::config::builder::ConfigBuilder; + +pub fn execute_list(command: ListCommand) -> Result<(), Error> { + let conf = ConfigBuilder::new() + .config_file_path(command.config_file_path) + .ignore_file_path(command.ignore_file_path) + .build()?; + + let reminders = reminder_lint_core::reminders(&conf)?; + + for remind in &reminders.expired { + println!( + "{}:{} {}", + remind.position.file, remind.position.line, remind.message + ); + } + + for remind in &reminders.upcoming { + println!( + "{}:{} {}", + remind.position.file, remind.position.line, remind.message + ); + } + + Ok(()) +} diff --git a/cli/src/subcommand/mod.rs b/cli/src/subcommand/mod.rs index 60f868a..8c99144 100644 --- a/cli/src/subcommand/mod.rs +++ b/cli/src/subcommand/mod.rs @@ -3,15 +3,17 @@ use crate::{ print::{pretty_print, Status}, }; -use self::{init::execute_init, run::execute_run}; +use self::{init::execute_init, list::execute_list, run::execute_run}; mod init; +mod list; mod run; pub fn execute_subcommand(subcommand: Subcommand) { let result = match subcommand { Subcommand::Run(command) => execute_run(command), Subcommand::Init(command) => execute_init(command), + Subcommand::List(command) => execute_list(command), }; if let Err(e) = result { From 2c60b3034f98ae8d5e9a4110785759f62ac8278c Mon Sep 17 00:00:00 2001 From: dora <31735614+dora1998@users.noreply.github.com> Date: Sun, 18 Aug 2024 21:46:54 +0900 Subject: [PATCH 2/2] feat: pass search path from command args --- cli/src/args.rs | 6 ++++++ cli/src/subcommand/list.rs | 2 +- cli/src/subcommand/run.rs | 3 ++- core/src/lib.rs | 7 +++++-- core/src/remind/mod.rs | 7 +++++-- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cli/src/args.rs b/cli/src/args.rs index 80bba0e..8903a06 100644 --- a/cli/src/args.rs +++ b/cli/src/args.rs @@ -28,6 +28,9 @@ pub struct RunCommand { /// sort by deadline (default: false) #[argh(switch)] pub sort_by_deadline: Option, + // search path (default: search directry written in config file) + #[argh(positional)] + pub search_path: Option, } #[derive(FromArgs, PartialEq, Debug)] @@ -45,6 +48,9 @@ pub struct ListCommand { /// path to the ignore file (default: ./.remindignore) #[argh(option, short = 'i')] pub ignore_file_path: Option, + // search path (default: search directry written in config file) + #[argh(positional)] + pub search_path: Option, } impl Args { diff --git a/cli/src/subcommand/list.rs b/cli/src/subcommand/list.rs index 43d2dae..d6a6841 100644 --- a/cli/src/subcommand/list.rs +++ b/cli/src/subcommand/list.rs @@ -8,7 +8,7 @@ pub fn execute_list(command: ListCommand) -> Result<(), Error> { .ignore_file_path(command.ignore_file_path) .build()?; - let reminders = reminder_lint_core::reminders(&conf)?; + let reminders = reminder_lint_core::reminders(&conf, command.search_path)?; for remind in &reminders.expired { println!( diff --git a/cli/src/subcommand/run.rs b/cli/src/subcommand/run.rs index 45d6d39..61b7627 100644 --- a/cli/src/subcommand/run.rs +++ b/cli/src/subcommand/run.rs @@ -10,7 +10,8 @@ pub fn execute_run(command: RunCommand) -> Result<(), Error> { .sort_by_deadline(command.sort_by_deadline) .build()?; - let reminders = reminder_lint_core::reminders(&conf)?; + let reminders = reminder_lint_core::reminders(&conf, command.search_path)?; + for remind in &reminders.expired { println!( "{}:{} {}", diff --git a/core/src/lib.rs b/core/src/lib.rs index 078ed44..eff5942 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -12,8 +12,11 @@ pub struct Reminders { pub upcoming: Vec, } -pub fn reminders(conf: &Config) -> Result { - let reminders = list_reminders(&conf).map_err(|e| ReminderLintError::from(e))?; +pub fn reminders( + conf: &Config, + search_path: Option, +) -> Result { + let reminders = list_reminders(&conf, search_path).map_err(|e| ReminderLintError::from(e))?; let mut expired = Vec::new(); let mut upcoming = Vec::new(); diff --git a/core/src/remind/mod.rs b/core/src/remind/mod.rs index 1418970..0bd0184 100644 --- a/core/src/remind/mod.rs +++ b/core/src/remind/mod.rs @@ -27,7 +27,7 @@ pub struct Position { pub line: u64, } -pub fn list_reminders(config: &Config) -> Result, Error> { +pub fn list_reminders(config: &Config, target: Option) -> Result, Error> { let meta_regex = convert_meta_regex(&config.comment_regex()); let matcher = RegexMatcherBuilder::new().build(&meta_regex)?; @@ -37,7 +37,10 @@ pub fn list_reminders(config: &Config) -> Result, Error> { .line_number(true) .build(); - let mut builder = WalkBuilder::new(&config.search_directory()); + let mut builder = match target { + Some(t) => WalkBuilder::new(&t), + None => WalkBuilder::new(config.search_directory()), + }; let walker = builder .hidden(false) .add_custom_ignore_filename(&config.ignore_file_path())