Skip to content

Commit

Permalink
feat(zsh): new zsh config structure and better utilised zcompile
Browse files Browse the repository at this point in the history
  • Loading branch information
umgbhalla committed Jun 4, 2022
1 parent 793fd88 commit 9122889
Show file tree
Hide file tree
Showing 10 changed files with 500 additions and 210 deletions.
399 changes: 232 additions & 167 deletions base/zsh/.config/zsh/.zcompdump

Large diffs are not rendered by default.

24 changes: 8 additions & 16 deletions base/zsh/.config/zsh/.zshrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#lib/shared/services ██
#
# ░██
# ██████ ██████░██ ██████ █████
# ░░░░██ ██░░░░ ░██████ ░░██░░█ ██░░░██
Expand Down Expand Up @@ -28,24 +28,16 @@ sie() {
alias na='nvim ~/.config/zsh/aliases.zsh'

foreach file (
completion.zsh
opts.zsh
prompt.zsh
plugins.zsh
keys.zsh
completion.zsh
git.zsh
aliases.zsh
funcs.zsh
colors.zsh
zoxide.zsh
aws.zsh
opts.zsh
# android.zsh
# tmux.zsh
# conda.zsh
kubctl.zsh
# terraform.zsh
# fly.zsh
docker.zsh
keys.zsh
) {
sie $ZDOTDIR/$file
}
Expand All @@ -56,10 +48,10 @@ sie $HOME/.cargo/env
# Performance Warning
END=$(date +%s.%N)
ZSHRC_PERF=$(printf %.2f $(echo "$END - $START" | bc))
# if (( $ZSHRC_PERF > 0.09)); then
# echo "\033[0;31mperformance warning!"
# echo ".zshrc startup time" $ZSHRC_PERF "seconds\e[0m"
# fi
if (( $ZSHRC_PERF > 0.09)); then
echo "\033[0;31mperformance warning!"
echo ".zshrc startup time" $ZSHRC_PERF "seconds\e[0m"
fi

# yearprog
# upwr
Expand Down
2 changes: 0 additions & 2 deletions base/zsh/.config/zsh/aliases.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ alias cd='z '
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# mini funcs
alias dicc="cat /usr/share/dict/cracklib-small | fzf-tmux -l 20% --multi --reverse | tr -d '\n' | xcopy"
alias glg="git log --graph --abbrev-commit --decorate --format=format:'%C(bold green)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold yellow)(%ar)%C(reset)%C(auto)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all"
alias lol=" figlet -c -f ~/.local/share/fonts/figlet-fonts/3d.flf "
alias logi="sudo systemctl restart logid.service"
alias neo='neofetch --backend kitty --source Downloads/figma/Slice\ 1\ \(4\).png'
alias gdiff='git diff --name-only --diff-filter=d | xargs bat --diff'
alias srt='du -sh ./* | sort -h | bat'
alias siz='du -sh '
alias ttystat='echo $(tty) $TERM ${COLUMNS}x$LINES'
Expand Down
63 changes: 45 additions & 18 deletions base/zsh/.config/zsh/completion.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ foreach function (
compinit
promptinit
surround
add-zsh-hook
vcs_info
url-quote-magic # originally referenced in ./opts.zsh
tetriscurses
) {
autoload -Uz $function
}

compinit
# Define module to load them on execution.
foreach module (
complete
Expand All @@ -26,23 +30,23 @@ foreach module (
}

# Initialize the completion system with a cache time of 24 hours.
mkdir -p $HOME/.local/share/zsh
typeset -g zcompdump="$HOME/.local/share/zsh/zcompdump"
typeset -g comp_files=($zcompdump(Nm-24))

if (( $#comp_files )) {
compinit -i -C -d $zcompdump

} else {
compinit -i -d $zcompdump
{
# Compile the completion dump to increase startup speed.
if [[ "$zcompdump" -nt "${zcompdump}.zwc" || ! -s "${zcompdump}.zwc" ]]; then
zcompile "$zcompdump"
fi
} &!
}

# mkdir -p $HOME/.local/share/zsh
# typeset -g zcompdump="$HOME/.local/share/zsh/zcompdump"
# typeset -g comp_files=($zcompdump(Nm-24))
#
# if (( $#comp_files )) {
# compinit -i -C -d $zcompdump
#
# } else {
# compinit -i -d $zcompdump
# {
# # Compile the completion dump to increase startup speed.
# if [[ "$zcompdump" -nt "${zcompdump}.zwc" || ! -s "${zcompdump}.zwc" ]]; then
# zcompile "$zcompdump"
# fi
# } &!
# }
#

# Enable completion caching, use rehash to clear

Expand Down Expand Up @@ -72,3 +76,26 @@ _fzf_compgen_dir() {

unset zcompdump
unset comp_files

foreach file (
# aws.zsh
# android.zsh
# tmux.zsh
# conda.zsh
kubctl.zsh
# terraform.zsh
# fly.zsh
docker.zsh
) {
sie $ZDOTDIR/$file
}

{ setopt extendedglob
for zcompdump in $ZDOTDIR/.zcompdump(#qN.mh+24); do
if [[ -s "$zcompdump" && (! -s "${zcompdump}.zwc" || "$zcompdump" -nt "${zcompdump}.zwc") ]]; then
zcompile "$zcompdump"
fi
done
unsetopt extendedglob
} &!
compinit -C
2 changes: 2 additions & 0 deletions base/zsh/.config/zsh/git.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,6 @@ alias grc='git rebase --continue'
alias grm='git rebase master'
alias gs='git status -s'
alias gst='git status'
alias glg="git log --graph --abbrev-commit --decorate --format=format:'%C(bold green)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold yellow)(%ar)%C(reset)%C(auto)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all"
alias gdiff='git diff --name-only --diff-filter=d | xargs bat --diff'

15 changes: 15 additions & 0 deletions base/zsh/.config/zsh/keys.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@ for m in visual viopp; do
bindkey -M $m $c select-bracketed
done
done
fancy-ctrl-z () {
if [[ $#BUFFER -eq 0 ]]; then
BUFFER="fg"
zle accept-line
else
zle push-input
zle clear-screen
fi
}
zle -N fancy-ctrl-z
bindkey '^Z' fancy-ctrl-z
196 changes: 194 additions & 2 deletions base/zsh/.config/zsh/kubctl.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,198 @@ alias kgd='kubectl get deployment'
alias kge='kubectl get events'
alias kgpvc='kubectl get pvc'

if (( $+commands[kubectl] )); then
source <(kubectl completion zsh)
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#compdef kubectl
compdef _kubectl kubectl

# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#compdef _kubectl kubectl

# zsh completion for kubectl -*- shell-script -*-

__kubectl_debug()
{
local file="$BASH_COMP_DEBUG_FILE"
if [[ -n ${file} ]]; then
echo "$*" >> "${file}"
fi
}

_kubectl()
{
local shellCompDirectiveError=1
local shellCompDirectiveNoSpace=2
local shellCompDirectiveNoFileComp=4
local shellCompDirectiveFilterFileExt=8
local shellCompDirectiveFilterDirs=16

local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace
local -a completions

__kubectl_debug "\n========= starting completion logic =========="
__kubectl_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}"

# The user could have moved the cursor backwards on the command-line.
# We need to trigger completion from the $CURRENT location, so we need
# to truncate the command-line ($words) up to the $CURRENT location.
# (We cannot use $CURSOR as its value does not work when a command is an alias.)
words=("${=words[1,CURRENT]}")
__kubectl_debug "Truncated words[*]: ${words[*]},"

lastParam=${words[-1]}
lastChar=${lastParam[-1]}
__kubectl_debug "lastParam: ${lastParam}, lastChar: ${lastChar}"

# For zsh, when completing a flag with an = (e.g., kubectl -n=<TAB>)
# completions must be prefixed with the flag
setopt local_options BASH_REMATCH
if [[ "${lastParam}" =~ '-.*=' ]]; then
# We are dealing with a flag with an =
flagPrefix="-P ${BASH_REMATCH}"
fi

# Prepare the command to obtain completions
requestComp="${words[1]} __complete ${words[2,-1]}"
if [ "${lastChar}" = "" ]; then
# If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go completion code.
__kubectl_debug "Adding extra empty parameter"
requestComp="${requestComp} \"\""
fi

__kubectl_debug "About to call: eval ${requestComp}"

# Use eval to handle any environment variables and such
out=$(eval ${requestComp} 2>/dev/null)
__kubectl_debug "completion output: ${out}"

# Extract the directive integer following a : from the last line
local lastLine
while IFS='\n' read -r line; do
lastLine=${line}
done < <(printf "%s\n" "${out[@]}")
__kubectl_debug "last line: ${lastLine}"

if [ "${lastLine[1]}" = : ]; then
directive=${lastLine[2,-1]}
# Remove the directive including the : and the newline
local suffix
(( suffix=${#lastLine}+2))
out=${out[1,-$suffix]}
else
# There is no directive specified. Leave $out as is.
__kubectl_debug "No directive found. Setting do default"
directive=0
fi

__kubectl_debug "directive: ${directive}"
__kubectl_debug "completions: ${out}"
__kubectl_debug "flagPrefix: ${flagPrefix}"

if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
__kubectl_debug "Completion received error. Ignoring completions."
return
fi

while IFS='\n' read -r comp; do
if [ -n "$comp" ]; then
# If requested, completions are returned with a description.
# The description is preceded by a TAB character.
# For zsh's _describe, we need to use a : instead of a TAB.
# We first need to escape any : as part of the completion itself.
comp=${comp//:/\\:}

local tab=$(printf '\t')
comp=${comp//$tab/:}

__kubectl_debug "Adding completion: ${comp}"
completions+=${comp}
lastComp=$comp
fi
done < <(printf "%s\n" "${out[@]}")

if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
__kubectl_debug "Activating nospace."
noSpace="-S ''"
fi

if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
# File extension filtering
local filteringCmd
filteringCmd='_files'
for filter in ${completions[@]}; do
if [ ${filter[1]} != '*' ]; then
# zsh requires a glob pattern to do file filtering
filter="\*.$filter"
fi
filteringCmd+=" -g $filter"
done
filteringCmd+=" ${flagPrefix}"

__kubectl_debug "File filtering command: $filteringCmd"
_arguments '*:filename:'"$filteringCmd"
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
# File completion for directories only
local subdir
subdir="${completions[1]}"
if [ -n "$subdir" ]; then
__kubectl_debug "Listing directories in $subdir"
pushd "${subdir}" >/dev/null 2>&1
else
__kubectl_debug "Listing directories in ."
fi

local result
_arguments '*:dirname:_files -/'" ${flagPrefix}"
result=$?
if [ -n "$subdir" ]; then
popd >/dev/null 2>&1
fi
return $result
else
__kubectl_debug "Calling _describe"
if eval _describe "completions" completions $flagPrefix $noSpace; then
__kubectl_debug "_describe found some completions"

# Return the success of having called _describe
return 0
else
__kubectl_debug "_describe did not find completions."
__kubectl_debug "Checking if we should do file completion."
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
__kubectl_debug "deactivating file completion"

# We must return an error code here to let zsh know that there were no
# completions found by _describe; this is what will trigger other
# matching algorithms to attempt to find completions.
# For example zsh can match letters in the middle of words.
return 1
else
# Perform file completion
__kubectl_debug "Activating file completion"

# We must return the result of this command, so it must be the
# last command, or else we must store its result to return it.
_arguments '*:filename:_files'" ${flagPrefix}"
fi
fi
fi
}

# don't run the completion function when being source-ed or eval-ed
if [ "$funcstack[1]" = "_kubectl" ]; then
_kubectl
fi
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
4 changes: 1 addition & 3 deletions base/zsh/.config/zsh/opts.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ unopts=(
flowcontrol
bg_nice
nomatch
prompt_sp
# prompt_sp
global_rcs
)
unsetopt $unopts

autoload -Uz url-quote-magic
zle -N self-insert url-quote-magic
unset zle_bracketed_paste
# autoload -Uz tetriscurses
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

2 changes: 1 addition & 1 deletion base/zsh/.config/zsh/profile.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export XTERM_SHELL=/usr/bin/zsh
# 10ms for key sequences
export KEYTIMEOUT=1
# suppress no newline % symbol marker
# export PROMPT_EOL_MARK=''
export PROMPT_EOL_MARK=''
export ZSH_AUTOSUGGEST_MANUAL_REBIND=1 # make prompt faster
export DISABLE_MAGIC_FUNCTIONS=true # make pasting into terminal faster
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Loading

0 comments on commit 9122889

Please sign in to comment.