Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shell completion command #332

Merged
merged 30 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c82f3bb
implemented a rough concept for generating shelll completion scripts …
MitchellBerend Oct 25, 2022
9570d4a
ran cargo fmt
MitchellBerend Oct 25, 2022
a940057
added docstring for completion command and put it in alphabetic order
MitchellBerend Oct 26, 2022
feb6251
implemented bash, fish powershell and zsh shell completion suggestions
MitchellBerend Oct 26, 2022
fdfbba6
updated help output in the README.md
MitchellBerend Oct 26, 2022
cd65873
updated the catch all case for the shell match and added a docstring …
MitchellBerend Oct 26, 2022
e1cde1b
removed fixed comment
MitchellBerend Oct 26, 2022
d3b225e
added elvish suggestion
MitchellBerend Oct 26, 2022
7cb5a46
swapped the order of printing the script and printing the suggestion,…
MitchellBerend Oct 26, 2022
bfc379f
fixed clippy lints
MitchellBerend Oct 26, 2022
e8a250e
removed rename reference in suggestion text
MitchellBerend Oct 26, 2022
6e0cfee
removed suggestion output
MitchellBerend Oct 26, 2022
f850f43
added documentation for shell completion scripts
MitchellBerend Oct 26, 2022
e29b630
fixed copied typo
MitchellBerend Oct 26, 2022
d0e692b
Added extra newline in docs
MitchellBerend Oct 27, 2022
363864d
added a config command and a completion sub command to that
MitchellBerend Oct 28, 2022
9da5a62
Deleting forgotten section from the README.md
MitchellBerend Oct 28, 2022
9563d18
moved comment from config struct to the actual command enum
MitchellBerend Oct 28, 2022
45d62fe
updated file structure
MitchellBerend Oct 28, 2022
2a9dc62
added install and uninstall commands to automatically add shell compl…
MitchellBerend Oct 29, 2022
35a59a4
Update src/bin/huak/commands/config/completion.rs
MitchellBerend Oct 30, 2022
10d5016
added new line to end of file
MitchellBerend Oct 30, 2022
b7c81ea
removed config from ops
MitchellBerend Oct 30, 2022
8c3f749
removed config
MitchellBerend Oct 30, 2022
887c476
moved config code to bin
MitchellBerend Oct 30, 2022
31d045c
removed (un)install commands and added flags for them on completion i…
MitchellBerend Oct 30, 2022
41191dc
added a os dependency on tests
MitchellBerend Oct 31, 2022
995138f
fixed target attribute for tests
MitchellBerend Oct 31, 2022
e70f6f3
made the shell flag only madatory when --install or --uninstall is pa…
MitchellBerend Oct 31, 2022
c77fc9c
Finalize completion command
cnpryer Oct 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ path = "./src/huak/lib.rs"

[dependencies]
clap = {version = "4.0.15", features = ["cargo", "derive"]}
clap_complete = "4.0"

thiserror = "1.0.36"
fs_extra = "1.2.0"
glob = "0.3.0"
Expand Down
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,26 @@ A Python package manager written in Rust inspired by Cargo.
Usage: huak <COMMAND>

Commands:
activate Activate the project's virtual environment
add Add a dependency to the existing project
audit Check for vulnerable dependencies and license compatibility*
build Build tarball and wheel for the project
clean Remove tarball and wheel from the built project
doc Generates documenation for the project*
fix Auto-fix fixable lint conflicts
fmt Format the project's Python code
init Initialize the existing project
install Install the dependencies of an existing project
lint Lint the project's Python code
new Create a new project at <path>
publish Builds and uploads current project to a registry*
remove Remove a dependency from the project
run Run a command within the project's environment context
test Test the project's Python code
update Update dependencies added to the project*
version Display the version of the project
help Print this message or the help of the given subcommand(s)
activate Activate the project's virtual environment
add Add a dependency to the existing project
audit Check for vulnerable dependencies and license compatibility*
build Build tarball and wheel for the project
completion Generates a shell completion script for supported shells. See the help menu for more information on supported shells
clean Remove tarball and wheel from the built project
doc Generates documentation for the project*
fix Auto-fix fixable lint conflicts
fmt Format the project's Python code
init Initialize the existing project
install Install the dependencies of an existing project
lint Lint the project's Python code
new Create a new project at <path>
publish Builds and uploads current project to a registry*
remove Remove a dependency from the project
run Run a command within the project's environment context
test Test the project's Python code
update Update dependencies added to the project*
version Display the version of the project
help Print this message or the help of the given subcommand(s)
cnpryer marked this conversation as resolved.
Show resolved Hide resolved
```
_"*" indicates first-pass of implementation is incomplete._

Expand Down
63 changes: 63 additions & 0 deletions src/bin/huak/commands/config/completion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use crate::commands::Cli;
use crate::errors::CliResult;

use huak::ops::config;

use clap::{Args, Command, CommandFactory, Subcommand};
use clap_complete::{generate, Shell};

/// Prints the script to stdout and a way to add the script to the shell init file to stderr. This
/// way if the user runs completion <shell> > completion.sh only the stdout will be redirected into
/// completion.sh.
pub fn run(config_command: Config) -> CliResult<()> {
match config_command.command {
ConfigCommand::Completion { shell } => {
generate_shell_completion_script(shell)
}
ConfigCommand::Install { shell } => {
let mut cmd: Command = Cli::command();
let _result = match shell {
Shell::Bash => config::_add_completion_bash(),
Shell::Elvish => config::_add_completion_elvish(),
Shell::Fish => config::_add_completion_fish(&mut cmd),
Shell::PowerShell => config::_add_completion_powershell(),
Shell::Zsh => config::_add_completion_zsh(&mut cmd),
_ => Ok(()),
};
}
ConfigCommand::Uninstall { shell } => {
let _result = match shell {
Shell::Bash => config::_remove_completion_bash(),
Shell::Elvish => config::_remove_completion_elvish(),
Shell::Fish => config::_remove_completion_fish(),
Shell::PowerShell => config::_remove_completion_powershell(),
Shell::Zsh => config::_remove_completion_zsh(),
MitchellBerend marked this conversation as resolved.
Show resolved Hide resolved
_ => Ok(()),
};
}
}
Ok(())
}

fn generate_shell_completion_script(shell: Shell) {
let mut cmd = Cli::command();

generate(shell, &mut cmd, "huak", &mut std::io::stdout())
}

#[derive(Args)]
pub struct Config {
#[command(subcommand)]
command: ConfigCommand,
}

#[derive(Debug, Subcommand)]
pub enum ConfigCommand {
MitchellBerend marked this conversation as resolved.
Show resolved Hide resolved
/// Generates a shell completion script for supported shells.
/// See the help menu for more information on supported shells.
Completion { shell: Shell },
/// Installs the completion script in your shell init file.
Install { shell: Shell },
/// Uninstalls the completion script from your shell init file.
Uninstall { shell: Shell },
MitchellBerend marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 2 additions & 0 deletions src/bin/huak/commands/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod completion;
pub use completion::*;
5 changes: 5 additions & 0 deletions src/bin/huak/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::errors::CliResult;

use clap::{Parser, Subcommand};

pub(crate) mod activate;
pub(crate) mod add;
pub(crate) mod audit;
pub(crate) mod build;
pub(crate) mod clean;
pub(crate) mod config;
pub(crate) mod doc;
pub(crate) mod fix;
pub(crate) mod fmt;
Expand Down Expand Up @@ -46,6 +48,8 @@ pub enum Commands {
Audit,
/// Build tarball and wheel for the project.
Build,
/// Interact with the configuration of huak.
Config(config::Config),
/// Remove tarball and wheel from the built project.
Clean {
#[arg(long, required = false)]
Expand Down Expand Up @@ -119,6 +123,7 @@ pub enum Commands {
impl Cli {
pub fn run(self) -> CliResult<()> {
match self.command {
Commands::Config(config_command) => config::run(config_command),
Commands::Activate => activate::run(),
Commands::Add { dependency, dev } => add::run(dependency, dev),
Commands::Audit => audit::run(),
Expand Down
55 changes: 55 additions & 0 deletions src/huak/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,61 @@
//!
//!Around 0.1.0 you'll be able to install `huak` using `brew` or `pip`. Distribution plans will be finalized closer to 0.1.0.
//!
//!## Shell completion
//!
//!##### Bash
//!First, ensure that you install `bash-completion` using your package manager.
//!After, add this to your `~/.bash_profile`:
//!
//!```bash
//! eval "$(huak config completion bash)"
//!```
//!
//!##### Elvish
//!This shell is supported, but the suggestion as to how this should be added to your shell init file is missing.
//!
//!If you are able to test this please head over to [github](https://github.com/cnpryer/huak/issues) and file an issue

//!##### Fish
MitchellBerend marked this conversation as resolved.
Show resolved Hide resolved
//!Generate a `huak.fish` completion script:
//!
//!```bash
//! huak config completion fish > ~/.config/fish/completions/huak.fish
//!```
//!
//!##### Powershell
//!Open your profile script with:
//!
//!```bash
//! mkdir -Path (Split-Path -Parent $profile) -ErrorAction SilentlyContinue
//!```
//!
//!```bash
//! notepad $profile
//!```
//!
//!Add the line and save the file:
//!```bash
//! Invoke-Expression -Command $(huak config completion powershell| Out-String)
//!```
//!
//!##### Zsh
//!Generate a `_huak` completion script and put it somewhere in your `$fpath`:
//!```bash
//! huak config completion zsh > /usr/local/share/zsh/site-functions/_huak
//!```
//!
//!Ensure that the following is present in your `~/.zshrc`:
//!
//!```bash
//! autoload -U compinit
//!```
//!
//!```bash
//! compinit -i
//!```
//!
//!
//!## Goals
//!
//!Besides some of my own experience with the Python ecosystem, there are a few additional guiding principles steering the development of Huak:
Expand Down
Loading