From 94d21e9c4ffbe50ed5732af968f590e430e9f6a4 Mon Sep 17 00:00:00 2001 From: Harvey Bates Date: Thu, 18 Jan 2024 07:47:02 +1100 Subject: [PATCH] added tab autocompletions for zsh shells --- CMakeLists.txt | 13 +++++++++++ kandle_autocomplete.zsh | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 kandle_autocomplete.zsh diff --git a/CMakeLists.txt b/CMakeLists.txt index a8678d0..3be822e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/kandle_autocomplete.zsh b/kandle_autocomplete.zsh new file mode 100644 index 0000000..2ea1cc6 --- /dev/null +++ b/kandle_autocomplete.zsh @@ -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