Skip to content

Commit

Permalink
Add completion command
Browse files Browse the repository at this point in the history
  • Loading branch information
lukahartwig committed Apr 11, 2023
1 parent 898747a commit 91bb342
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ anyhow = "1.0.70"
chrono = "0.4.24"
chrono-humanize = "0.2.2"
clap = { version = "4.2.1", features = ["derive"] }
clap_complete = "4.2.0"
home = "0.5.4"
refinery = { version = "0.8.7", features = ["rusqlite-bundled"] }
rusqlite = { version = "0.28.0", features = ["bundled", "chrono"] }
14 changes: 11 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::Result;
use chrono_humanize::HumanTime;
use clap::{Parser, Subcommand};
use std::fs;
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::{generate, Shell};
use std::{fs, io};

mod store;
mod todo;
Expand All @@ -12,6 +13,8 @@ use crate::todo::{Todo, TodoStatus};
#[derive(Debug, Parser)]
#[clap(name = "todo", version)]
struct App {
#[arg(short)]
fail: String,
#[clap(subcommand)]
command: Commands,
}
Expand All @@ -27,6 +30,8 @@ enum Commands {
Set { id: u32, status: TodoStatus },
/// Remove done todos
Prune,
/// Generates completions
Completions { shell: Shell },
}

fn main() -> Result<()> {
Expand All @@ -38,7 +43,7 @@ fn main() -> Result<()> {
if !path.exists() {
fs::create_dir_all(path.as_path())?;
}

let store = Store::open(path.join("todo.db"))?;

match app.command {
Expand All @@ -65,6 +70,9 @@ fn main() -> Result<()> {
Commands::Prune => {
store.prune_todos().expect("failed pruning todos");
}
Commands::Completions { shell } => {
generate(shell, &mut App::command(), "todo", &mut io::stdout());
}
}

Ok(())
Expand Down

0 comments on commit 91bb342

Please sign in to comment.