Add note list command

This commit is contained in:
Joscha 2025-05-03 11:15:21 +02:00
parent 11d03aac57
commit d08922e753
3 changed files with 68 additions and 0 deletions

View file

@ -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),
}
}
}

View file

@ -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),
}
}
}

View file

@ -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::<Vec<_>>();
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::<Vec<_>>()
.join(", ");
println!("{id}: {} [{children}]", note.text);
}
}
Ok(())
}
}