Skip to content

Commit

Permalink
add completion
Browse files Browse the repository at this point in the history
  • Loading branch information
8LWXpg committed Dec 3, 2024
1 parent 66d6d04 commit 6117a82
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Added new field `admin` in `version.toml` set to `false` to disable killing as admin. **(Breaking Change)**
- Added new subcommand `pin`, pinned plugins will not be updated with `update --all`.
- Added new subcommand `completion` that generates PowerShell completion.

## [0.8.0]

Expand Down
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 @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.93"
clap = { version = "4.5.21", features = ["derive"] }
clap_complete = "4.5.38"
colored = "2.1.0"
reqwest = { version = "0.12.9", features = ["blocking", "json"] }
serde = { version = "1.0.215", features = ["derive"] }
Expand Down
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ This tool will create a file at `%LOCALAPPDATA%\Microsoft\PowerToys\PowerToys Ru
Usage: ptr.exe <COMMAND>
Commands:
add Add a plugin [aliases: a]
update Update plugins [aliases: u]
remove Remove plugins [aliases: r]
list List all installed plugins [aliases: l]
pin Pin plugins so it's not updated with `update --all` [aliases: p]
import Import plugins from configuration file [aliases: i]
restart Restart PowerToys
help Print this message or the help of the given subcommand(s)
add Add a plugin [aliases: a]
update Update plugins [aliases: u]
remove Remove plugins [aliases: r]
list List all installed plugins [aliases: l]
pin Pin plugins so it's not updated with `update --all` [aliases: p]
import Import plugins from configuration file [aliases: i]
restart Restart PowerToys
completion Generate shell completion (PowerShell)
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
Expand Down Expand Up @@ -161,6 +162,16 @@ Options:
Usage: ptr.exe restart
```

### Completion

```
Usage: ptr.exe completion
```

```pwsh
(ptr completion) -join "`n" | iex
```

## Why Rust?

The `clap` crate in Rust is very powerful and easy to use for building command line applications, so I chose Rust to build this tool.
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ mod config;
mod polling;
mod util;

use clap::{builder::styling, Parser, Subcommand};
use std::{env, path::PathBuf, sync::LazyLock};
use clap::{builder::styling, CommandFactory, Parser, Subcommand};
use clap_complete::aot::PowerShell;
use std::{env, io, path::PathBuf, sync::LazyLock};

static PLUGIN_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
PathBuf::from(&env::var("LOCALAPPDATA").unwrap())
Expand Down Expand Up @@ -85,6 +86,10 @@ enum TopCommand {
#[clap()]
/// Restart PowerToys
Restart,

#[clap()]
/// Generate shell completion (PowerShell)
Completion,
}

#[derive(Subcommand)]
Expand Down Expand Up @@ -157,6 +162,12 @@ fn main() {
},
TopCommand::List => print!("{}", config),
TopCommand::Restart => config.restart(),
TopCommand::Completion => clap_complete::generate(
PowerShell,
&mut App::command(),
"ptr",
&mut io::stdout(),
),
_ => unreachable!(),
},
Err(e) => exit!(e),
Expand Down

0 comments on commit 6117a82

Please sign in to comment.