-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
151 additions
and
1 deletion.
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
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" |
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,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; | ||
|
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,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)"; | ||
|
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,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; | ||
|