From d08922e753e556935bc4c97fbb581a0df86c5650 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 3 May 2025 11:15:21 +0200 Subject: [PATCH] Add note list command --- gdn-cli/src/commands.rs | 6 +++++ gdn-cli/src/commands/note.rs | 20 +++++++++++++++ gdn-cli/src/commands/note/list.rs | 42 +++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 gdn-cli/src/commands/note.rs create mode 100644 gdn-cli/src/commands/note/list.rs diff --git a/gdn-cli/src/commands.rs b/gdn-cli/src/commands.rs index 097b823..9bdcf58 100644 --- a/gdn-cli/src/commands.rs +++ b/gdn-cli/src/commands.rs @@ -2,6 +2,7 @@ use clap::Parser; use crate::Environment; +mod note; mod repo; mod status; mod tidy; @@ -17,6 +18,10 @@ pub enum Command { #[command(subcommand)] #[command(visible_alias = "r")] Repo(repo::Command), + + #[command(subcommand)] + #[command(visible_alias = "n")] + Note(note::Command), } impl Command { @@ -25,6 +30,7 @@ impl Command { Self::Status(command) => command.run(env), Self::Tidy(command) => command.run(env), Self::Repo(command) => command.run(env), + Self::Note(command) => command.run(env), } } } diff --git a/gdn-cli/src/commands/note.rs b/gdn-cli/src/commands/note.rs new file mode 100644 index 0000000..0bb7887 --- /dev/null +++ b/gdn-cli/src/commands/note.rs @@ -0,0 +1,20 @@ +mod list; + +use clap::Parser; + +use crate::Environment; + +/// Perform note operations. +#[derive(Debug, Parser)] +pub enum Command { + #[command(visible_alias = "l")] + List(list::Command), +} + +impl Command { + pub fn run(self, env: &Environment) -> anyhow::Result<()> { + match self { + Self::List(command) => command.run(env), + } + } +} diff --git a/gdn-cli/src/commands/note/list.rs b/gdn-cli/src/commands/note/list.rs new file mode 100644 index 0000000..cf84fd5 --- /dev/null +++ b/gdn-cli/src/commands/note/list.rs @@ -0,0 +1,42 @@ +use clap::Parser; + +use crate::Environment; + +/// List all notes in the current repository. +#[derive(Debug, Parser)] +pub struct Command {} + +impl Command { + pub fn run(self, env: &Environment) -> anyhow::Result<()> { + let data = gdn::data::open(env.data_dir.clone())?; + let state = gdn::data::load_state(&data)?; + let Some(selected) = state.selected_repo else { + println!("No repo selected"); + return Ok(()); + }; + let repo = gdn::data::load_repo(&data, selected)?; + let mut notes = repo.notes.into_iter().collect::>(); + notes.sort_unstable_by_key(|(id, _)| *id); + + if notes.is_empty() { + println!("No notes"); + return Ok(()); + } + + for (id, note) in notes { + if note.children.is_empty() { + println!("{id}: {}", note.text); + } else { + let children = note + .children + .iter() + .map(|it| it.to_string()) + .collect::>() + .join(", "); + println!("{id}: {} [{children}]", note.text); + } + } + + Ok(()) + } +}