Add support for shell completion (#580)

* feat: added supported for shell completions

* docs: completions flag instructions
This commit is contained in:
Lab Rat 2023-10-14 15:46:00 +00:00 committed by GitHub
parent 5d4ba17d69
commit 5bf295d86a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 10 deletions

View File

@ -356,6 +356,10 @@ auto-cpufreq should be run with with one of the following options:
* help
- Shows all of the above options
* completions=TEXT
- To support shell completions (currently bash, zsh and fish)
- TEXT can be bash, zsh or fish (shell name)
Running `auto-cpufreq --help` will print the same list of options as above. Read [auto-cpufreq modes and options](#auto-cpufreq-modes-and-options) for more details.
## auto-cpufreq modes and options

View File

@ -34,9 +34,10 @@ from auto_cpufreq.power_helper import *
@click.option("--debug", is_flag=True, help="Show debug info (include when submitting bugs)")
@click.option("--version", is_flag=True, help="Show currently installed version")
@click.option("--donate", is_flag=True, help="Support the project")
@click.option("--completions", is_flag=False, help="Enables shell completions for bash, zsh and fish.\n Possible values bash|zsh|fish")
@click.option("--log", is_flag=True, hidden=True)
@click.option("--daemon", is_flag=True, hidden=True)
def main(config, daemon, debug, update, install, remove, live, log, monitor, stats, version, donate, force, get_state):
def main(config, daemon, debug, update, install, remove, live, log, monitor, stats, version, donate, force, get_state, completions):
# display info if config file is used
def config_info_dialog():
@ -50,7 +51,6 @@ def main(config, daemon, debug, update, install, remove, live, log, monitor, sta
set_override(force) # Calling set override, only if force has some values
if len(sys.argv) == 1:
print("\n" + "-" * 32 + " auto-cpufreq " + "-" * 33 + "\n")
print("Automatic CPU speed & power optimizer for Linux")
@ -265,5 +265,22 @@ def main(config, daemon, debug, update, install, remove, live, log, monitor, sta
else:
print("Aborted")
elif completions:
if completions == "bash":
print("Run the below command in your current shell!\n")
print("echo 'eval \"$(_AUTO_CPUFREQ_COMPLETE=bash_source auto-cpufreq)\"' >> ~/.bashrc")
print("source ~/.bashrc")
elif completions == "zsh":
print("Run the below command in your current shell!\n")
print("echo 'eval \"$(_AUTO_CPUFREQ_COMPLETE=zsh_source auto-cpufreq)\"' >> ~/.zshrc")
print("source ~/.zshrc")
elif completions == "fish":
print("Run the below command in your current shell!\n")
print("echo '_AUTO_CPUFREQ_COMPLETE=fish_source auto-cpufreq | source' > ~/.config/fish/completions/auto-cpufreq.fish")
else:
print("Invalid Option, try bash|zsh|fish as argument to --completions")
if __name__ == "__main__":
main()

View File

@ -15,20 +15,21 @@ err_exit()
# invocation handling
#
param=""
if [ "${#}" -ne 1 ];
then
err_exit
else
param="${1}"
fi
# load python virtual environment
venv_dir=/opt/auto-cpufreq/venv
. "${venv_dir}/bin/activate"
# run python code with venv loaded
PYTHONPATH=/opt/auto-cpufreq \
if [[ "${#}" -ne 1 ]]; then
PYTHONPATH=/opt/auto-cpufreq \
/opt/auto-cpufreq/venv/bin/python \
/opt/auto-cpufreq/venv/bin/auto-cpufreq
else
param="${1}"
PYTHONPATH=/opt/auto-cpufreq \
/opt/auto-cpufreq/venv/bin/python \
/opt/auto-cpufreq/venv/bin/auto-cpufreq \
"${param}"
fi