From 9725317a4a301aefc126f1f9eeebb9eb364819f5 Mon Sep 17 00:00:00 2001 From: Konstantin Demin Date: Sun, 18 Feb 2024 02:32:08 +0300 Subject: [PATCH] zsh: update --- .config/dotfiles/gitignore | 4 +-- .config/zsh/alias/buildah.zsh | 3 -- .config/zsh/alias/containers.zsh | 45 +++++++++++++++++++++++++++++ .config/zsh/alias/podman.zsh | 4 --- .config/zsh/alias/sudo.zsh | 10 +++++-- .config/zsh/completion/external.zsh | 29 +++++++++++++++---- .config/zsh/lib/completion.zsh | 24 ++++++++++----- .config/zsh/local.zsh.example | 4 +++ 8 files changed, 100 insertions(+), 23 deletions(-) delete mode 100644 .config/zsh/alias/buildah.zsh create mode 100644 .config/zsh/alias/containers.zsh delete mode 100644 .config/zsh/alias/podman.zsh create mode 100644 .config/zsh/local.zsh.example diff --git a/.config/dotfiles/gitignore b/.config/dotfiles/gitignore index 5fd0e8f..2e3dd51 100644 --- a/.config/dotfiles/gitignore +++ b/.config/dotfiles/gitignore @@ -9,7 +9,7 @@ !/.config/htop/htoprc.example !/.config/zsh/_.zsh !/.config/zsh/alias.zsh -!/.config/zsh/alias/buildah.zsh +!/.config/zsh/alias/containers.zsh !/.config/zsh/alias/diff.zsh !/.config/zsh/alias/directories.zsh !/.config/zsh/alias/git.zsh @@ -20,7 +20,6 @@ !/.config/zsh/alias/k8s.zsh !/.config/zsh/alias/kconfig.zsh !/.config/zsh/alias/ls.zsh -!/.config/zsh/alias/podman.zsh !/.config/zsh/alias/quilt.zsh !/.config/zsh/alias/sbuild.zsh !/.config/zsh/alias/sudo.zsh @@ -53,6 +52,7 @@ !/.config/zsh/lib/term.zsh !/.config/zsh/lib/time.zsh !/.config/zsh/lib/title.zsh +!/.config/zsh/local.zsh.example !/.config/zsh/local/.keep !/.config/zsh/opt.zsh !/.config/zsh/opt/chase.zsh diff --git a/.config/zsh/alias/buildah.zsh b/.config/zsh/alias/buildah.zsh deleted file mode 100644 index 9d97436..0000000 --- a/.config/zsh/alias/buildah.zsh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/zsh - -alias bud='buildah bud --isolation chroot --network host --format docker -f ' diff --git a/.config/zsh/alias/containers.zsh b/.config/zsh/alias/containers.zsh new file mode 100644 index 0000000..12a31d6 --- /dev/null +++ b/.config/zsh/alias/containers.zsh @@ -0,0 +1,45 @@ +#!/bin/zsh + +alias bud='buildah bud --isolation chroot --network host --format docker -f ' + +alias pod-run='podman run -e "TERM=${TERM:-linux}" --network host --rm -it ' +alias pod-run-sh="pod-run --entrypoint='[\"/bin/sh\"]' --user=0:0 " +alias pod-ps='podman ps ' +alias pod-images='podman images --format "table {{.ID}} {{.Repository}}:{{.Tag}} {{.Size}} {{.Created}} |{{.CreatedAt}}" ' +alias pod-inspect='podman inspect ' +alias pod-logs='podman logs ' + +sko-inspect() { + command skopeo inspect "docker://${1:?}" +} + +sko-list-tags() { + command skopeo list-tags "docker://${1:?}" +} + +pod-dive() { + command dive "podman://${1:?}" +} + +jq-visual() { + jq -C | less +} + +jq-config() { + jq '.[].Config' +} + +jq-tags() { + jq -r '.Tags[]' +} + +if [ ${UID} -ne 0 ] ; then + alias docker='sudo docker ' +fi +alias dkr='docker ' +alias dkr-run='dkr run -e "TERM=${TERM:-linux}" --network host --rm -it ' +alias dkr-run-sh="dkr-run --entrypoint='' --user=0:0 " +alias dkr-ps='dkr ps ' +alias dkr-images='dkr images --format "table {{.ID}}\\t{{.Repository}}:{{.Tag}}\\t{{.Size}}\\t{{.CreatedAt}}" ' +alias dkr-inspect='dkr inspect ' +alias dkr-logs='dkr logs ' diff --git a/.config/zsh/alias/podman.zsh b/.config/zsh/alias/podman.zsh deleted file mode 100644 index 330ec30..0000000 --- a/.config/zsh/alias/podman.zsh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/zsh - -alias run='podman run -e "TERM=$TERM" --network host --rm -it ' -alias run-sh="run --entrypoint='[\"/bin/sh\"]' --user=0:0 " diff --git a/.config/zsh/alias/sudo.zsh b/.config/zsh/alias/sudo.zsh index e97c825..69a7517 100644 --- a/.config/zsh/alias/sudo.zsh +++ b/.config/zsh/alias/sudo.zsh @@ -1,4 +1,10 @@ #!/bin/zsh -alias sudo-i='sudo -i ' -alias sudoi='sudo -i ' +function { + local c + if [ ${UID} -ne 0 ] ; then + c='sudo -i ' + fi + alias sudo-i="$c" + alias sudoi="$c" +} \ No newline at end of file diff --git a/.config/zsh/completion/external.zsh b/.config/zsh/completion/external.zsh index 8a39774..48e9821 100644 --- a/.config/zsh/completion/external.zsh +++ b/.config/zsh/completion/external.zsh @@ -1,9 +1,28 @@ #!/bin/zsh -__z_comp__kubectl() { command kubectl completion zsh ; } -__z_comp__podman() { command podman completion zsh ; } +typeset -A ZSHU_COMP_EXTERNAL -for i ( kubectl podman ) ; do - __z_comp_external $i "__z_comp__$i" +for i ( kubectl podman skopeo docker ) ; do + ZSHU_COMP_EXTERNAL[$i]="command $i completion zsh" done ; unset i -unset -fm '__z_comp__*' +# ZSHU_COMP_EXTERNAL[yq]='command yq shell-completion zsh' +## example of "automatic" shell completion generation +__z_comp_ext__yq() { command yq shell-completion zsh ; } + +## example of more complex shell completion generation +# __z_comp__shifty_nifty() { command shifty-nifty completion zsh | sed -E 's/shifty_nifty/shifty-nifty/g' ; } +# ZSHU_COMP_EXTERNAL[shifty-nifty]='__z_comp__shifty_nifty' + +z-comp-auto() { + local c f + + for c ( ${(k)ZSHU_COMP_EXTERNAL} ) ; do + __z_comp_external "$c" "${(@s: :)ZSHU_COMP_EXTERNAL[$c]}" && unset "ZSHU_COMP_EXTERNAL[$c]" + done + + for f ( ${functions[(I)__z_comp_ext__*]} ) ; do + c=${f#__z_comp_ext__} + __z_comp_external $c $f && unset -f "$f" + done +} +z-comp-auto diff --git a/.config/zsh/lib/completion.zsh b/.config/zsh/lib/completion.zsh index a504ef5..f1e3d3e 100644 --- a/.config/zsh/lib/completion.zsh +++ b/.config/zsh/lib/completion.zsh @@ -42,6 +42,7 @@ __z_compdump_finalize() { unset 'ZSHU[compdump_meta]' } +## TODO: refactor (e.g. buildah completion is a "bit" broken) __z_comp_bash() { local f p x @@ -62,16 +63,25 @@ __z_comp_bash() { } __z_comp_external() { - local f + local c f + c="$1" ; shift - (( ${+commands[$1]} )) || return 1 - (( ${+_comps[$1]} )) && return 2 + [ $# -gt 0 ] || return 1 - f="${ZSHU[d_cache]}/completion/_$1" - if ! [ -s "$f" ] ; then - "$2" > "$f" || return 3 + (( ${+commands[$c]} )) || return 2 + + if ! (( ${+ZSHU_COMP_FORCE[$c]} )) ; then + (( ${+_comps[$c]} )) && return fi - autoload -Uz "_$1" + + f="${ZSHU[d_cache]}/completion/_$c" + if ! [ -s "$f" ] ; then + if ! "$@" > "$f" ; then + rm -f "$f" + return 3 + fi + fi + autoload -Uz "_$c" return 0 } diff --git a/.config/zsh/local.zsh.example b/.config/zsh/local.zsh.example new file mode 100644 index 0000000..66f2e34 --- /dev/null +++ b/.config/zsh/local.zsh.example @@ -0,0 +1,4 @@ +#!/bin/zsh + +typeset -gA ZSHU_COMP_FORCE +ZSHU_COMP_FORCE[podman]=1