Skip to content

Commit

Permalink
Merge cf11f1e into e4c77cc
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad-Wahid committed Jun 6, 2023
2 parents e4c77cc + cf11f1e commit a03f147
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Note: use tabs
# actions which are virtual, i.e. not a script
.PHONY: install install-for-dev install-deps install-flexmeasures run-local test freeze-deps upgrade-deps update-docs update-docs-pdf show-file-space show-data-model clean-db
.PHONY: install install-for-dev install-deps install-flexmeasures run-local test freeze-deps upgrade-deps update-docs update-docs-pdf show-file-space show-data-model clean-db cli-autocomplete


# ---- Development ---
Expand Down Expand Up @@ -90,3 +90,7 @@ show-data-model:

clean-db:
./flexmeasures/data/scripts/clean_database.sh ${db_name} ${db_user}


cli-autocomplete:
./flexmeasures/data/scripts/add_scripts_path.sh ${extension}
60 changes: 60 additions & 0 deletions flexmeasures/data/scripts/add_scripts_path.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

current_dir=$(pwd)

# Declares a function named `save_path` that takes two arguments: `path` and `extension`.
# The function is responsible for checking if the given path is already present in the `bashrc` file.
function save_path() {
path="$1"
extension="$2"

# Check if the path is already present in bashrc
if grep -qF ". $1" ~/.$2rc; then
echo "Path already exists in .$2rc"
else
# Add the path to bashrc
echo ". $1" >> ~/.$2rc
echo "Path added to .$2rc"
fi
}

# The `main` function is responsible for executing the main logic of the script.
# It sets the `script_path` variable to the path of the "ci" directory under the current directory.
# It sets the `extension` variable to the value passed as an argument to the script.

function find_file() {
directory="$1"
extension="$2"
file=$(find "$directory" -type f -name "click-complete.$extension")
# Searches for a file with the name "click-complete.<extension>" in the given directory.
# Assigns the file path to the `file` variable.

# Check if file was found
if [[ -n "$file" ]]; then
echo "$file"
return 0
else
return 1
fi
}

function main() {
script_path="$current_dir/flexmeasures/data/scripts"
extension="$1"

if [[ "$extension" != "bash" && "$extension" != "fish" && "$extension" != "zsh" ]]; then
echo "Invalid extension. Only 'bash', 'fish', or 'zsh' extensions are allowed."
exit 1
fi

# In case file is found, then add the complete path in the required file
if found_file=$(find_file "$script_path" "$extension"); then
save_path "$found_file" "$extension"
else
echo "No file found with $extension in its name."
exit 1
fi
}

# Run the main function with the given extension argument
main "$1"
29 changes: 29 additions & 0 deletions flexmeasures/data/scripts/click-complete.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
_click_completion() {
local IFS=$'\n'
local response

response=$(env COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD=$COMP_CWORD _FLEXMEASURES_COMPLETE=bash_complete $1)

for completion in $response; do
IFS=',' read type value <<< "$completion"

if [[ $type == 'dir' ]]; then
COMREPLY=()
compopt -o dirnames
elif [[ $type == 'file' ]]; then
COMREPLY=()
compopt -o default
elif [[ $type == 'plain' ]]; then
COMPREPLY+=($value)
fi
done

return 0
}

_click_completion_setup() {
complete -o nosort -F _click_completion flexmeasures
}

_click_completion_setup;

22 changes: 22 additions & 0 deletions flexmeasures/data/scripts/click-complete.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function _click_completion;
set -l response;

for value in (env _FLEXMEASURES_COMPLETE=fish_complete COMP_WORDS=(commandline -cp) COMP_CWORD=(commandline -t) flexmeasures);
set response $response $value;
end;

for completion in $response;
set -l metadata (string split "," $completion);

if test $metadata[1] = "dir";
__fish_complete_directories $metadata[2];
else if test $metadata[1] = "file";
__fish_complete_path $metadata[2];
else if test $metadata[1] = "plain";
echo $metadata[2];
end;
end;
end;

complete --no-files --command flexmeasures --arguments "(_click_completion)";

35 changes: 35 additions & 0 deletions flexmeasures/data/scripts/click-complete.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#compdef click

_click_completion() {
local -a completions
local -a completions_with_descriptions
local -a response
(( ! $+commands[flexmeasures] )) && return 1

response=("${(@f)$(env COMP_WORDS="${words[*]}" COMP_CWORD=$((CURRENT-1)) _FLEXMEASURES_COMPLETE=zsh_complete flexmeasures)}")

for type key descr in ${response}; do
if [[ "$type" == "plain" ]]; then
if [[ "$descr" == "_" ]]; then
completions+=("$key")
else
completions_with_descriptions+=("$key":"$descr")
fi
elif [[ "$type" == "dir" ]]; then
_path_files -/
elif [[ "$type" == "file" ]]; then
_path_files -f
fi
done

if [ -n "$completions_with_descriptions" ]; then
_describe -V unsorted completions_with_descriptions -U
fi

if [ -n "$completions" ]; then
compadd -U -V unsorted -a completions
fi
}

compdef _click_completion flexmeasures;

0 comments on commit a03f147

Please sign in to comment.