Add note list command
This commit is contained in:
parent
11d03aac57
commit
d08922e753
3 changed files with 68 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ use clap::Parser;
|
||||||
|
|
||||||
use crate::Environment;
|
use crate::Environment;
|
||||||
|
|
||||||
|
mod note;
|
||||||
mod repo;
|
mod repo;
|
||||||
mod status;
|
mod status;
|
||||||
mod tidy;
|
mod tidy;
|
||||||
|
|
@ -17,6 +18,10 @@ pub enum Command {
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
#[command(visible_alias = "r")]
|
#[command(visible_alias = "r")]
|
||||||
Repo(repo::Command),
|
Repo(repo::Command),
|
||||||
|
|
||||||
|
#[command(subcommand)]
|
||||||
|
#[command(visible_alias = "n")]
|
||||||
|
Note(note::Command),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Command {
|
impl Command {
|
||||||
|
|
@ -25,6 +30,7 @@ impl Command {
|
||||||
Self::Status(command) => command.run(env),
|
Self::Status(command) => command.run(env),
|
||||||
Self::Tidy(command) => command.run(env),
|
Self::Tidy(command) => command.run(env),
|
||||||
Self::Repo(command) => command.run(env),
|
Self::Repo(command) => command.run(env),
|
||||||
|
Self::Note(command) => command.run(env),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
gdn-cli/src/commands/note.rs
Normal file
20
gdn-cli/src/commands/note.rs
Normal 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
42
gdn-cli/src/commands/note/list.rs
Normal file
42
gdn-cli/src/commands/note/list.rs
Normal 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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue