Skip to content

Commit

Permalink
Add support for shell completion (#580)
Browse files Browse the repository at this point in the history
* feat: added supported for shell completions

* docs: completions flag instructions
  • Loading branch information
rootCircle authored Oct 14, 2023
1 parent 5d4ba17 commit 5bf295d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions auto_cpufreq/bin/auto_cpufreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
@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():
Expand All @@ -50,7 +51,6 @@ def config_info_dialog():
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")

Expand Down Expand Up @@ -265,5 +265,22 @@ def config_info_dialog():
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()
17 changes: 9 additions & 8 deletions scripts/auto-cpufreq-venv-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5bf295d

Please sign in to comment.