From 8834da817c4f78d2fb622a1495353614af6fc017 Mon Sep 17 00:00:00 2001 From: Konstantin Demin Date: Mon, 4 Mar 2024 15:35:55 +0300 Subject: [PATCH] zsh: update --- .cache/zsh/ssh/.keep | 0 .config/dotfiles/gitignore | 6 +++++ .config/zsh/alias/containers.zsh | 6 ++--- .config/zsh/alias/gpg.zsh | 2 +- .config/zsh/alias/idle.zsh | 6 ++--- .config/zsh/alias/kconfig.zsh | 14 +++++----- .config/zsh/alias/quilt.zsh | 12 ++++----- .config/zsh/alias/sbuild.zsh | 16 ++++++----- .config/zsh/env.zsh | 4 +++ .config/zsh/env/gopath.zsh | 5 ++-- .config/zsh/lib/completion.zsh | 24 +++++++++-------- .config/zsh/lib/csv.zsh | 33 +++++++++++++++++++++++ .config/zsh/lib/git.zsh | 2 +- .config/zsh/lib/gpg.zsh | 45 +++++++++++++++++++++++++++++++ .config/zsh/lib/pswalk.zsh | 46 ++++++++++++++++++++++++++++++-- .config/zsh/lib/selfservice.zsh | 8 +++--- .config/zsh/lib/systemd.zsh | 18 +++++++++++++ .config/zsh/rc/gpg-agent.zsh | 33 +++++++++++++++++++++++ .config/zsh/rc/ssh-agent.zsh | 29 ++++++++++++++++++++ .config/zsh/rc/terminal.zsh | 5 ++-- 20 files changed, 265 insertions(+), 49 deletions(-) create mode 100644 .cache/zsh/ssh/.keep create mode 100644 .config/zsh/lib/csv.zsh create mode 100644 .config/zsh/lib/gpg.zsh create mode 100644 .config/zsh/lib/systemd.zsh create mode 100644 .config/zsh/rc/gpg-agent.zsh create mode 100644 .config/zsh/rc/ssh-agent.zsh diff --git a/.cache/zsh/ssh/.keep b/.cache/zsh/ssh/.keep new file mode 100644 index 0000000..e69de29 diff --git a/.config/dotfiles/gitignore b/.config/dotfiles/gitignore index ecce00b..18cbdaa 100644 --- a/.config/dotfiles/gitignore +++ b/.config/dotfiles/gitignore @@ -2,6 +2,7 @@ !/.cache/zsh/compcache/.keep !/.cache/zsh/completion/.keep !/.cache/zsh/compzwc/.keep +!/.cache/zsh/ssh/.keep !/.config/dotfiles/bin/.keep !/.config/dotfiles/gen-gitignore.sh !/.config/dotfiles/gitattributes @@ -48,13 +49,16 @@ !/.config/zsh/lib/alternatives.zsh !/.config/zsh/lib/cmdtime.zsh !/.config/zsh/lib/completion.zsh +!/.config/zsh/lib/csv.zsh !/.config/zsh/lib/curl.zsh !/.config/zsh/lib/git.zsh +!/.config/zsh/lib/gpg.zsh !/.config/zsh/lib/history.zsh !/.config/zsh/lib/prompt.zsh !/.config/zsh/lib/pswalk.zsh !/.config/zsh/lib/say-my.zsh !/.config/zsh/lib/selfservice.zsh +!/.config/zsh/lib/systemd.zsh !/.config/zsh/lib/term.zsh !/.config/zsh/lib/time.zsh !/.config/zsh/lib/title.zsh @@ -69,9 +73,11 @@ !/.config/zsh/opt/prompt.zsh !/.config/zsh/rc.zsh !/.config/zsh/rc/completion.zsh +!/.config/zsh/rc/gpg-agent.zsh !/.config/zsh/rc/keyboard.zsh !/.config/zsh/rc/pager.zsh !/.config/zsh/rc/prompt.zsh +!/.config/zsh/rc/ssh-agent.zsh !/.config/zsh/rc/terminal.zsh !/.config/zsh/var/.keep !/.gdbinit diff --git a/.config/zsh/alias/containers.zsh b/.config/zsh/alias/containers.zsh index c45f94d..50a1f6a 100644 --- a/.config/zsh/alias/containers.zsh +++ b/.config/zsh/alias/containers.zsh @@ -12,19 +12,19 @@ alias pod-logs='podman logs ' sko-inspect() { local i i="${1:?}" ; shift - command skopeo inspect "docker://$i" "$@" + command skopeo inspect "$@" "docker://$i" } sko-list-tags() { local i i="${1:?}" ; shift - command skopeo list-tags "docker://$i" "$@" + command skopeo list-tags "$@" "docker://$i" } pod-dive() { local i i="${1:?}" ; shift - command dive "podman://$i" "$@" + command dive "$@" "podman://$i" } jq-visual() { diff --git a/.config/zsh/alias/gpg.zsh b/.config/zsh/alias/gpg.zsh index 473cbb0..e9e8f2e 100644 --- a/.config/zsh/alias/gpg.zsh +++ b/.config/zsh/alias/gpg.zsh @@ -1,7 +1,7 @@ #!/bin/zsh gpg-warmup() { - (( ${+commands[gpg]} )) || return 1 + (( ${+commands[gpg]} )) || return 127 local t r diff --git a/.config/zsh/alias/idle.zsh b/.config/zsh/alias/idle.zsh index 6a92cb8..3cd5d9b 100644 --- a/.config/zsh/alias/idle.zsh +++ b/.config/zsh/alias/idle.zsh @@ -1,7 +1,7 @@ #!/bin/zsh idle() { - [ -n "${1:?}" ] || return 1 + [ -n "${1:?}" ] local f @@ -17,7 +17,7 @@ idle() { } z-idle-ext() { - [ -n "${1:?}" ] || return 1 + [ -n "${1:?}" ] local -a s @@ -28,7 +28,7 @@ z-idle-ext() { } z-idle-int() { - [ -n "${1:?}" ] || return 1 + [ -n "${1:?}" ] ## execute in subshell ( diff --git a/.config/zsh/alias/kconfig.zsh b/.config/zsh/alias/kconfig.zsh index 7a0df71..7a69f31 100644 --- a/.config/zsh/alias/kconfig.zsh +++ b/.config/zsh/alias/kconfig.zsh @@ -1,24 +1,22 @@ #!/bin/zsh kconf-set() { - [ -n "${1:?}" ] || return 1 - - local n=$1 v=$2 + local n v + n="${1:?}" v=$2 shift 2 - [ $# -gt 0 ] || return 2 + [ $# -gt 0 ] || return 1 command grep -ElZ "^((CONFIG_)?$n=|# (CONFIG_)?$n is not set)" "$@" \ | xargs -0 -r sed -i -E -e "s/^(((CONFIG_)?$n)=.+|# ((CONFIG_)?$n) is not set)\$/\\2\\4=$v/" } kconf-unset() { - [ -n "${1:?}" ] || return 1 - - local n=$1 + local n + n="${1:?}" shift - [ $# -gt 0 ] || return 2 + [ $# -gt 0 ] || return 1 command grep -ElZ "^(CONFIG_)?$n=" "$@" \ | xargs -0 -r sed -i -E -e "s/^((CONFIG_)?$n)=.+\$/# \\1 is not set/" diff --git a/.config/zsh/alias/quilt.zsh b/.config/zsh/alias/quilt.zsh index e62efdb..5bb79de 100644 --- a/.config/zsh/alias/quilt.zsh +++ b/.config/zsh/alias/quilt.zsh @@ -5,7 +5,7 @@ quilt-series-strip-comments() { } quilt-series-auto() { - [ -n "${1:?}" ] || return 1 + [ -n "${1:?}" ] find "$1/" -follow -type f -printf '%P\0' \ | sed -zEn '/\.(diff|patch)$/p' \ @@ -13,16 +13,16 @@ quilt-series-auto() { } krd-quilt() { - [ -n "${1:?}" ] || return 1 + (( $+commands[quilt] )) || return 127 - (( $+commands[quilt] )) || return 2 + [ -n "${1:?}" ] local patchdir series tmp_series if [ -d "$1" ] ; then patchdir="$1/debian/patches" if [ -d "${patchdir}" ] ; then - [ -f "${patchdir}/series" ] || return 3 + [ -f "${patchdir}/series" ] || return 1 else patchdir="$1" fi @@ -34,7 +34,7 @@ krd-quilt() { quilt-series-auto "${patchdir}" > "${series}" fi elif [ -f "$1" ] ; then - [ -s "$1" ] || return 3 + [ -s "$1" ] || return 2 series="$1" patchdir=${series:h} @@ -53,7 +53,7 @@ krd-quilt() { command quilt pop -a ; echo r=0 - while read -r i ; do + while read -rs i ; do [ -n "$i" ] || continue k="${patchdir}/$i" diff --git a/.config/zsh/alias/sbuild.zsh b/.config/zsh/alias/sbuild.zsh index 21216b7..a33851a 100644 --- a/.config/zsh/alias/sbuild.zsh +++ b/.config/zsh/alias/sbuild.zsh @@ -1,7 +1,9 @@ #!/bin/zsh krd-debsrc() { - [ -n "${1:?}" ] || return 1 + (( $+commands[deb-src-export] )) || return 127 + + [ -n "${1:?}" ] local dstdir case "$1" in @@ -13,21 +15,21 @@ krd-debsrc() { } krd-sbuild() { - [ -n "${1:?}" ] || return 1 - [ -n "${2:?}" ] || return 1 + (( $+commands[sbuild] )) || return 127 + (( $+commands[xz] )) || return 127 - (( $+commands[sbuild] )) || return 2 - (( $+commands[xz] )) || return 2 + [ -n "${1:?}" ] + [ -n "${2:?}" ] local topdir case "$1" in */* ) topdir="$1" ;; * ) topdir="/tmp/$1" ;; esac - [ -d "${topdir}" ] || return 3 + [ -d "${topdir}" ] || return 1 local srcdir="${topdir}/src" - [ -d "${srcdir}" ] || return 3 + [ -d "${srcdir}" ] || return 2 arch="$2" diff --git a/.config/zsh/env.zsh b/.config/zsh/env.zsh index b83ad29..997586a 100644 --- a/.config/zsh/env.zsh +++ b/.config/zsh/env.zsh @@ -86,3 +86,7 @@ function { ## last resort ZSHU[host]=${ZSHU[host_name]} } + +case "${ZSHU[os_family]}" in +linux ) ZSHU[procfs]=/proc ;; +esac diff --git a/.config/zsh/env/gopath.zsh b/.config/zsh/env/gopath.zsh index 23ea16e..ed01fbb 100644 --- a/.config/zsh/env/gopath.zsh +++ b/.config/zsh/env/gopath.zsh @@ -2,11 +2,12 @@ z-gobin-fixup() { (( ${+commands[go]} )) || return 0 + local gobin - gobin=$(go env GOBIN) + gobin=$(command go env GOBIN) if [ -z "${gobin}" ] ; then local gopath - gopath=$(go env GOPATH) + gopath=$(command go env GOPATH) [ -n "${gopath}" ] || return 1 [ -d "${gopath}" ] || return 0 gobin="${gopath}/bin" diff --git a/.config/zsh/lib/completion.zsh b/.config/zsh/lib/completion.zsh index 91299bf..8be401a 100644 --- a/.config/zsh/lib/completion.zsh +++ b/.config/zsh/lib/completion.zsh @@ -48,11 +48,12 @@ __z_compdump_finalize() { ## TODO: refactor (e.g. buildah completion is a "bit" broken) __z_comp_bash() { + # (( ${+commands[$1]} )) || return 127 + local f p x - (( ${+commands[$1]} )) || return 1 - (( ${+_comps[$1]} )) && return 2 - (( ${+ZSHU[compdump_bash]} )) || return 3 + (( ${+_comps[$1]} )) && return 1 + (( ${+ZSHU[compdump_bash]} )) || return 2 (( ${+2} )) && return 0 f=0 @@ -60,7 +61,7 @@ __z_comp_bash() { x="_$1" ; [ -s "$p/$x" ] && f=1 && break x="$1" ; [ -s "$p/$x" ] && f=1 && break done - [ "$f" = 0 ] && return 4 + [ "$f" = 0 ] && return 3 complete -C "$x" "$1" return 0 @@ -70,9 +71,9 @@ __z_comp_external() { local c f c="$1" ; shift - [ $# -gt 0 ] || return 1 + (( ${+commands[$c]} )) || return 127 - (( ${+commands[$c]} )) || return 2 + [ $# -gt 0 ] || return 1 if ! (( ${+ZSHU_COMP_FORCE[$c]} )) ; then (( ${+_comps[$c]} )) && return 0 @@ -82,7 +83,7 @@ __z_comp_external() { if ! [ -s "$f" ] ; then if ! "$@" > "$f" ; then rm -f "$f" - return 3 + return 2 fi fi # zcompile -zR "$f" @@ -94,10 +95,11 @@ __z_comp_external() { } __z_comp_system() { + # (( ${+commands[$1]} )) || return 127 + local d - (( ${+commands[$1]} )) || return 1 - (( ${+_comps[$1]} )) && return 2 + (( ${+_comps[$1]} )) && return 1 (( ${+ZSHU_COMP_FORCE[$c]} )) && return 0 @@ -113,12 +115,12 @@ __z_comp_system() { return 0 done fpath=( ${_fpath} ) - return 3 + return 2 } ## reload or new session are required to regenerate compcache z-comp-invalidate() { - [ -n "$1" ] || return 1 + [ -n "${1:?}" ] # rm -f "${ZSHU[d_completion]}/_$1" "${ZSHU[d_compzwc]}/_$1.zwc" "${ZSHU[d_compzwc]}/$1.zwc" rm -f "${ZSHU[d_completion]}/_$1" diff --git a/.config/zsh/lib/csv.zsh b/.config/zsh/lib/csv.zsh new file mode 100644 index 0000000..d24bb1d --- /dev/null +++ b/.config/zsh/lib/csv.zsh @@ -0,0 +1,33 @@ +#!/bin/zsh + +## NB: set IFS manually + +z-csv-select() { + local field value + field="${1:?}" value="${2:?}" + + local line + local -a ary + while IFS='' read -rs line ; do + [ -n "${line}" ] || continue + + ary=() + read -rs -A ary <<< "${line}" + [ "${ary[${field}]}" = "${value}" ] || continue + + printf '%s' "${line}" + return 0 + done + + return 1 +} + +z-csv-field() { + local field + field="${1:?}" + + local -a ary + read -rs -A ary + + printf '%s' "${ary[${field}]}" +} diff --git a/.config/zsh/lib/git.zsh b/.config/zsh/lib/git.zsh index eefbda1..ba74351 100644 --- a/.config/zsh/lib/git.zsh +++ b/.config/zsh/lib/git.zsh @@ -21,7 +21,7 @@ __z_git_desc_tag() { __z_git describe --tags "$@" ; } z-git-test() { [ "${ZSHU_PS[git]}" = '1' ] || return 1 - __z_git_avail || return 2 + __z_git_avail || return $? __z_git_is_repo || return 3 diff --git a/.config/zsh/lib/gpg.zsh b/.config/zsh/lib/gpg.zsh new file mode 100644 index 0000000..a599507 --- /dev/null +++ b/.config/zsh/lib/gpg.zsh @@ -0,0 +1,45 @@ +#!/bin/zsh + +z-gpgconf-comp-avail() { + (( ${+commands[gpgconf]} )) || return 127 + + local comp + comp="${1:?}" + + local csv + csv=$(command gpgconf --list-components | IFS=':' z-csv-select 1 "${comp}") + [ -n "${csv}" ] +} + +z-gpgconf-comp-opt-avail() { + (( ${+commands[gpgconf]} )) || return 127 + + local comp opt + comp="${1:?}" opt="${2:?}" + + z-gpgconf-comp-avail "${comp}" || return $? + + local csv + csv=$(command gpgconf --list-options "${comp}" | IFS=':' z-csv-select 1 "${opt}") + [ -n "${csv}" ] +} + +## merely that command: +## gpgconf --list-options "$1" | awk -F: "/^$2:/{ print \$10 }" +z-gpgconf-getopt() { + (( ${+commands[gpgconf]} )) || return 127 + + local comp opt + comp="${1:?}" opt="${2:?}" + + ## not really necessary here + # z-gpgconf-comp-opt-avail "${comp}" "${opt}" || return $? + + local csv + csv=$(command gpgconf --list-options "${comp}" | IFS=':' z-csv-select 1 "${opt}") + [ -n "${csv}" ] || return 1 + + local v + v=$(IFS=':' z-csv-field 10 <<< "${csv}") + printf '%s' "$v" +} diff --git a/.config/zsh/lib/pswalk.zsh b/.config/zsh/lib/pswalk.zsh index bc6d3fa..6824d45 100644 --- a/.config/zsh/lib/pswalk.zsh +++ b/.config/zsh/lib/pswalk.zsh @@ -1,10 +1,28 @@ #!/bin/zsh +z-proc-exists() { + [ -n "${1:?}" ] + + while [ -n "${ZSHU[procfs]}" ] ; do + [ -d "${ZSHU[procfs]}" ] || return 1 + [ -f "${ZSHU[procfs]}/$1/status" ] + return $? + done + + ps -o 'pid=' -p "$1" >/dev/null 2>&1 +} + typeset -Uga ZSHU_PARENTS_PID typeset -ga ZSHU_PARENTS_NAME function { - local i c g + local procfs + while [ -n "${ZSHU[procfs]}" ] ; do + [ -d "${ZSHU[procfs]}" ] || break + procfs=1 ; break + done + + local i c x _unused i=${PPID} while : ; do @@ -13,8 +31,32 @@ function { [ "$i" = 1 ] && break ZSHU_PARENTS_PID+=( $i ) - read -r i c g <<< $(ps -o 'ppid=,comm=' -p "$i" 2>/dev/null) + + c= + while [ "${procfs}" = 1 ] ; do + [ -f "${ZSHU[procfs]}/$i/cmdline" ] || break + read -d $'\0' -rs c <<< $(cat "${ZSHU[procfs]}/$i/cmdline") + break + done + if [ -z "$c" ] ; then + read -rs c _unused <<< "$(ps -o 'comm=' -p "$i" 2>/dev/null)" + fi [ -n "$c" ] && ZSHU_PARENTS_NAME+=( "${c:t}" ) + + x= + while [ "${procfs}" = 1 ] ; do + [ -f "${ZSHU[procfs]}/$i/status" ] || break + # read -rs _unused x <<< "$(cat "${ZSHU[procfs]}/$i/status" | grep -F 'PPid:')" + while read -rs _unused c ; do + [ "${_unused}" = 'PPid:' ] || continue + x=$c ; break + done < "${ZSHU[procfs]}/$i/status" + break + done + if [ -z "$x" ] ; then + read -rs x _unused <<< "$(ps -o 'ppid=' -p "$i" 2>/dev/null)" + fi + i=$x done typeset -r ZSHU_PARENTS_PID ZSHU_PARENTS_NAME diff --git a/.config/zsh/lib/selfservice.zsh b/.config/zsh/lib/selfservice.zsh index fa7bf6b..79c7220 100644 --- a/.config/zsh/lib/selfservice.zsh +++ b/.config/zsh/lib/selfservice.zsh @@ -21,7 +21,7 @@ dotfiles-gen-gitignore() { } if [ -d .config/dotfiles/repo.git ] ; then echo "NOT going to change dotfiles installation" >&2 - return 1 + return 2 fi "$x" "$@" } @@ -55,9 +55,11 @@ z-update() { z-reload() { export ZDOTDIR="${ZSHU[d_zdot]}" + local r exec -a "${ZSH_ARGZERO}" "${ZSH_NAME}" "${argv[@]}" - echo "unable to reload (something went wrong), code $?" >&2 - return 1 + r=$? + echo "unable to reload (something went wrong), code $r" >&2 + return $r } ## reload or new session are required to regenerate compcache diff --git a/.config/zsh/lib/systemd.zsh b/.config/zsh/lib/systemd.zsh new file mode 100644 index 0000000..412460f --- /dev/null +++ b/.config/zsh/lib/systemd.zsh @@ -0,0 +1,18 @@ +#!/bin/zsh + +z-systemctl() { + command systemctl --quiet --no-pager --lines=0 --no-ask-password "$@" +} + +z-systemctl-status-rc() { + z-systemctl status "$@" >/dev/null 2>&1 +} + +z-systemctl-exists() { + z-systemctl-status-rc "$@" + case "$?" in + 0 | 1 | 3 ) return 0 ;; + ## also 4 = "no such unit" + * ) return 1 ;; + esac +} diff --git a/.config/zsh/rc/gpg-agent.zsh b/.config/zsh/rc/gpg-agent.zsh new file mode 100644 index 0000000..e3f4990 --- /dev/null +++ b/.config/zsh/rc/gpg-agent.zsh @@ -0,0 +1,33 @@ +#!/bin/zsh + +z-gpg-agent() { + ## don't bother with gpg agent socket if it already set + [ -z "${GPG_AGENT_INFO}" ] || return 0 + + (( ${+commands[gpg-agent]} )) || return 127 + + local u + for u in gpg-agent.{service,socket} ; do + z-systemctl-exists --user $u || continue + + z-systemctl --user --now enable $u + done + + (( ${+commands[gpgconf]} )) || return 127 + + local agent_sock + agent_sock=$(command gpgconf --list-dirs agent-socket) || return $? + [ -n "${agent_sock}" ] || return 3 + export GPG_AGENT_INFO="${agent_sock}:0:1" + + ## don't bother with ssh agent socket if it already set + [ -z "${SSH_AUTH_SOCK}" ] || return 0 + + local want_ssh_agent ssh_auth_sock + want_ssh_agent=$(z-gpgconf-getopt gpg-agent enable-ssh-support) + if [ "${want_ssh_agent}" = 1 ] ; then + ssh_auth_sock=$(command gpgconf --list-dirs agent-ssh-socket) || return $? + [ -n "${ssh_auth_sock}" ] || return 5 + export SSH_AUTH_SOCK="${ssh_auth_sock}" + fi +} diff --git a/.config/zsh/rc/ssh-agent.zsh b/.config/zsh/rc/ssh-agent.zsh new file mode 100644 index 0000000..7b338d0 --- /dev/null +++ b/.config/zsh/rc/ssh-agent.zsh @@ -0,0 +1,29 @@ +#!/bin/zsh + +z-ssh-agent() { + while [ -n "${SSH_AGENT_PID}" ] ; do + z-proc-exists "${SSH_AGENT_PID}" || break + + ## don't bother with ssh agent socket if it already set + [ -z "${SSH_AUTH_SOCK}" ] || return 0 + + break + done + + (( ${+commands[ssh-agent]} )) || return 127 + + if [ -z "${SSH_AUTH_SOCK}" ] ; then + local sock_dir + if [ "${XDG_RUNTIME_DIR}" = "${TMPDIR}" ] ; then + sock_dir="${ZSHU[d_zdot]}/.cache/ssh" + else + sock_dir="${XDG_RUNTIME_DIR}/ssh" + fi + mkdir -p "${sock_dir}" + SSH_AUTH_SOCK="${sock_dir}/ssh-agent.sock" + fi + + { + eval "$(ssh-agent -s -k -a "${SSH_AUTH_SOCK}")" + } >/dev/null +} diff --git a/.config/zsh/rc/terminal.zsh b/.config/zsh/rc/terminal.zsh index e1d9183..d883c48 100644 --- a/.config/zsh/rc/terminal.zsh +++ b/.config/zsh/rc/terminal.zsh @@ -5,8 +5,9 @@ z-orig-term() { local i x for i ( ${ZSHU_PARENTS_PID} ) ; do - [ -r "/proc/$i/environ" ] || continue - x=$(sed -zEn '/^TERM=(.+)$/{s//\1/;p;}' "/proc/$i/environ" 2>/dev/null | tr -d '\0') + i="${ZSHU[procfs]}/$i/environ" + [ -r "$i" ] || continue + x=$(sed -zEn '/^TERM=(.+)$/{s//\1/;p;}' "$i" 2>/dev/null | tr -d '\0') [ -n "$x" ] || continue a+=( "$x" ) done