-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Resolves #133 This pr aims to add a tab completion script so users don't have to remember every command if they want to experiment with config options. ### Additional tasks - [x] Documentation for changes provided/changed - [x] Tests added - [x] Updated CHANGELOG.md Co-authored-by: Dmitrii Kovanikov <[email protected]>
- Loading branch information
1 parent
38663d0
commit 395b21c
Showing
10 changed files
with
240 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//! This file contains all logic revolving the generation of the shell completion script | ||
|
||
use clap_complete::Shell; | ||
|
||
// This function can break when clap_complete adds support for a new shell type | ||
pub fn rename_completion_suggestion(shell: &Shell, bin_name: &str) -> Result<(), RenameError> { | ||
let completion_str: String = match shell { | ||
Shell::Zsh => zsh_completion_help(bin_name), | ||
Shell::Bash => bash_completion_help(bin_name), | ||
Shell::Fish => fish_completion_help(bin_name), | ||
Shell::Elvish => elvish_completion_help(bin_name), | ||
Shell::PowerShell => powershell_completion_help(bin_name), | ||
_ => return Err(RenameError::NewShellFound(shell.to_owned())), | ||
}; | ||
|
||
eprintln!( | ||
"\n\n############################\n{}\n############################", | ||
completion_str | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub enum RenameError { | ||
NewShellFound(Shell), | ||
} | ||
|
||
impl std::fmt::Display for RenameError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { | ||
match self { | ||
RenameError::NewShellFound(shell) => write!(f, "[Rename error]: {}", shell), | ||
} | ||
} | ||
} | ||
|
||
//##################// | ||
// Helper functions // | ||
//##################// | ||
|
||
fn zsh_completion_help(bin_name: &str) -> String { | ||
format!( | ||
r##"Generate a `_{bin_name}` completion script and put it somewhere in your `$fpath`: | ||
`{bin_name} completion zsh --rename {bin_name} > /usr/local/share/zsh/site-functions/_{bin_name}` | ||
Ensure that the following is present in your `~/.zshrc`: | ||
`autoload -U compinit` | ||
`compinit -i`"## | ||
) | ||
} | ||
|
||
fn bash_completion_help(bin_name: &str) -> String { | ||
format!( | ||
r##"First, ensure that you install `bash-completion` using your package manager. | ||
After, add this to your `~/.bash_profile`: | ||
`eval "$({bin_name} completion bash --rename {bin_name})"`"## | ||
) | ||
} | ||
|
||
fn fish_completion_help(bin_name: &str) -> String { | ||
format!( | ||
r##"Generate a `tool.fish` completion script: | ||
`{bin_name} completion fish --rename {bin_name} > ~/.config/fish/completions/{bin_name}.fish`"## | ||
) | ||
} | ||
|
||
fn elvish_completion_help(_bin_name: &str) -> String { | ||
r##"This suggestion is missing, if you use this and know how to implement this please file an issue over at https://github.com/chshersh/tool-sync/issues"##.into() | ||
} | ||
|
||
fn powershell_completion_help(bin_name: &str) -> String { | ||
format!( | ||
r##"Open your profile script with: | ||
`mkdir -Path (Split-Path -Parent $profile) -ErrorAction SilentlyContinue` | ||
`notepad $profile` | ||
Add the line and save the file: | ||
`Invoke-Expression -Command $({bin_name} completion powershell --rename {bin_name} | Out-String)`"## | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
############################ | ||
First, ensure that you install `bash-completion` using your package manager. | ||
|
||
After, add this to your `~/.bash_profile`: | ||
|
||
`eval "$(test-name completion bash --rename test-name)"` | ||
############################ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
############################ | ||
Generate a `_test-name` completion script and put it somewhere in your `$fpath`: | ||
`test-name completion zsh --rename test-name > /usr/local/share/zsh/site-functions/_test-name` | ||
|
||
Ensure that the following is present in your `~/.zshrc`: | ||
|
||
`autoload -U compinit` | ||
|
||
`compinit -i` | ||
############################ |