Skip to content

Commit

Permalink
added tab autocompletions for zsh shells
Browse files Browse the repository at this point in the history
  • Loading branch information
HarveyBates committed Jan 17, 2024
1 parent e8bf78d commit 94d21e9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ set(CMAKE_CXX_STANDARD 17)

project("kandle")

# ZSH system (for tab auto-completions)
find_program(ZSH_PROGRAM zsh)
if (ZSH_PROGRAM)
# Install zsh tab completions
set(ZSH_COMPLETION_DIR "/usr/local/share/zsh/site-functions/")

message("Installing zsh auto-completions at: " ${ZSH_COMPLETION_DIR})

# Install auto-completions
install(FILES "kandle_autocomplete.zsh" DESTINATION ${ZSH_COMPLETION_DIR}
RENAME "_kandle")
endif()

# All files in source directory
file(GLOB_RECURSE SRC_DIR RELATIVE ${CMAKE_SOURCE_DIR} src/*.cpp)

Expand Down
50 changes: 50 additions & 0 deletions kandle_autocomplete.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#compdef kandle

_arguments \
'1:Commands:->cmds' \
'*::arg:->args'

_contains_arg() {
local arg="$1"
for word in "${words[@]}"; do
if [[ "$word" == "$arg" ]]; then
return 0
fi
done
return 1
}

case "$state" in
cmds)
local -a commands
commands=(
'init:Initialize kandle directory structure'
'-I:Initialize kandle directory structure'
'list:List libraries in project'
'-L:List libraries in project'
'filename:Downloaded .zip filename'
'-f:Downloaded .zip filename'
'library:Specify a component library name (e.g. n-channel-mosfet)'
'-l:Specify a component library name (e.g. n-channel-mosfet)'
'help:Show help'
'-h:Show help'
)
_describe 'Commands' commands
;;
args)
if _contains_arg "library" || _contains_arg "-l"; then
# Search libraries (just looking at symbols)
local symbols_dir="$PWD/components/extern/symbols"
if [[ -d $symbols_dir ]]; then
# Get symbol files, only shows the filename (not extention due to (:r))
_path_files -W $symbols_dir -g '*(.kicad_sym)(:r)'
else
# Internal message (not shown to user)
_message "Symbol directory not found in current working directory."
fi
else
# Handle general files and directories tab completion
_files -/ -g '*'
fi
;;
esac

0 comments on commit 94d21e9

Please sign in to comment.