From d872b655c9dcb06a8b7322def537b20d7682cfaa Mon Sep 17 00:00:00 2001 From: Konstantin Demin Date: Wed, 28 Feb 2024 23:22:33 +0300 Subject: [PATCH] zsh: update PS: zprof rocks! --- .cache/zsh/dots/.keep | 0 .config/dotfiles/gitattributes | 0 .config/dotfiles/gitignore | 7 +++- .config/dotfiles/install.sh | 42 +++++++++++++++---- .config/zsh/_.zsh | 39 ++++++++--------- .config/zsh/{lib => _wip}/enclave.zsh.wip | 0 .config/zsh/{lib => _wip}/starship.zsh.sample | 0 .config/zsh/env/aux.zsh | 8 ++-- .config/zsh/env/gopath.zsh | 21 ++++++++++ .config/zsh/lib/git.zsh | 8 ++-- .config/zsh/lib/pswalk.zsh | 23 ++++------ .config/zsh/lib/say-my.zsh | 6 ++- .config/zsh/lib/selfservice.zsh | 21 ++++++++-- .config/zsh/rc/completion.zsh | 6 +-- .config/zsh/rc/pager.zsh | 3 +- .config/zsh/rc/prompt.zsh | 8 ++-- .config/zsh/rc/terminal.zsh | 15 +++++-- .zshenv | 1 + 18 files changed, 140 insertions(+), 68 deletions(-) create mode 100644 .cache/zsh/dots/.keep create mode 100644 .config/dotfiles/gitattributes rename .config/zsh/{lib => _wip}/enclave.zsh.wip (100%) rename .config/zsh/{lib => _wip}/starship.zsh.sample (100%) create mode 100644 .config/zsh/env/gopath.zsh diff --git a/.cache/zsh/dots/.keep b/.cache/zsh/dots/.keep new file mode 100644 index 0000000..e69de29 diff --git a/.config/dotfiles/gitattributes b/.config/dotfiles/gitattributes new file mode 100644 index 0000000..e69de29 diff --git a/.config/dotfiles/gitignore b/.config/dotfiles/gitignore index 339034d..262117a 100644 --- a/.config/dotfiles/gitignore +++ b/.config/dotfiles/gitignore @@ -2,8 +2,10 @@ !/.cache/zsh/compcache/.keep !/.cache/zsh/completion/.keep !/.cache/zsh/compzwc/.keep +!/.cache/zsh/dots/.keep !/.config/dotfiles/bin/.keep !/.config/dotfiles/gen-gitignore.sh +!/.config/dotfiles/gitattributes !/.config/dotfiles/gitignore !/.config/dotfiles/install.sh !/.config/dotfiles/scripts/.keep @@ -11,6 +13,8 @@ !/.config/htop/htoprc.example !/.config/mc/ini.example !/.config/zsh/_.zsh +!/.config/zsh/_wip/enclave.zsh.wip +!/.config/zsh/_wip/starship.zsh.sample !/.config/zsh/alias.zsh !/.config/zsh/alias/containers.zsh !/.config/zsh/alias/diff.zsh @@ -34,6 +38,7 @@ !/.config/zsh/env.zsh !/.config/zsh/env/aux.zsh !/.config/zsh/env/containers.zsh +!/.config/zsh/env/gopath.zsh !/.config/zsh/env/history.zsh !/.config/zsh/env/ld.so.zsh !/.config/zsh/env/pager.zsh @@ -44,14 +49,12 @@ !/.config/zsh/lib/cmdtime.zsh !/.config/zsh/lib/completion.zsh !/.config/zsh/lib/curl.zsh -!/.config/zsh/lib/enclave.zsh.wip !/.config/zsh/lib/git.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/starship.zsh.sample !/.config/zsh/lib/term.zsh !/.config/zsh/lib/time.zsh !/.config/zsh/lib/title.zsh diff --git a/.config/dotfiles/install.sh b/.config/dotfiles/install.sh index 42a14e5..7375a4f 100755 --- a/.config/dotfiles/install.sh +++ b/.config/dotfiles/install.sh @@ -56,7 +56,7 @@ main() { umask 0077 if have_cmd git ; then - if [ -s "${HOME}/${d_repo}/info/refs" ] ; then + if [ -s "${HOME}/${d_repo}/HEAD" ] ; then dot_update else dot_install @@ -66,6 +66,8 @@ main() { dot_install_raw fi + propagate_dist_files + echo 'installed.' >&2 } @@ -75,7 +77,7 @@ dot_install() { mkdir -p "${GIT_DIR}" git init git branch -M "${gh_br}" || true - git_config + git_config_init git_update } @@ -118,7 +120,7 @@ dot_install_raw() { cat < "${HOME}/$f" > "${td_backup}/$f" fi done < "${tf_list}" - rm -f "${tf_list}" + rm -f "${tf_list}" ; unset tf_list tar -C "${td_tree}" -cf . - | tar -C "${HOME}" -xf - rm -rf "${td_tree}" @@ -137,7 +139,7 @@ git_env() { export GIT_DIR GIT_WORK_TREE } -git_config() { +git_config_init() { ## remote git remote add origin "${u_repo}" git config remote.origin.fetch "+refs/heads/${gh_br}:refs/remotes/origin/${gh_br}" @@ -146,13 +148,23 @@ git_config() { ## repo-specific git config core.worktree "${GIT_WORK_TREE}" git config core.excludesfile "${f_gitignore}" +} + +git_config() { + ## repo-specific + git config core.attributesfile .config/dotfiles/gitattributes + ## migration (remove later) + git config --unset gc.auto + git config --unset pull.ff + ## size optimization + git config core.compression 9 + git config pack.compression 9 ## generic - git config gc.auto 0 - git config pull.ff only git config receive.denyNonFastForwards true } git_update() { + git_config git remote update -p git pull || git reset --hard "origin/${gh_br}" git gc --aggressive --prune=all --force || git gc || true @@ -195,7 +207,7 @@ backup_unconditionally() { mv -f "${HOME}/$f" "${td_backup}/$f" fi done < "${tf_list}" - rm -f "${tf_list}" + rm -f "${tf_list}" ; unset tf_list if find_fast "${td_backup}/" -mindepth 1 ; then echo "backed-up files are here: ${td_backup}/" @@ -205,4 +217,20 @@ backup_unconditionally() { fi } +propagate_dist_files() { + tf_list=$(mktemp) + sed -En '/^!\/(.+\.dist)$/{s//\1/;p;}' < "${HOME}/${f_gitignore}" > "${tf_list}" + + while read -r f_dist ; do + [ -n "${f_dist}" ] || continue + [ -f "${f_dist}" ] || continue + + f=${f_dist%.dist} + if [ -f "$f" ] ; then continue ; fi + + cp "${f_dist}" "$f" + done < "${tf_list}" + rm -f "${tf_list}" ; unset tf_list +} + main "$@" diff --git a/.config/zsh/_.zsh b/.config/zsh/_.zsh index 83465fa..8f031da 100644 --- a/.config/zsh/_.zsh +++ b/.config/zsh/_.zsh @@ -1,28 +1,24 @@ #!/bin/zsh -: "${ZDOTDIR:=${HOME}}" - typeset -gA ZSHU ZSHU[t_begin]=${(%):-%D{%s.%6.}} ZSHU[d_zdot]="${ZDOTDIR}" -ZSHU[d_cache]="${ZDOTDIR}/.cache/zsh" +ZSHU[d_dotfiles]="${ZDOTDIR}/.config/dotfiles" ZSHU[d_conf]="${ZDOTDIR}/.config/zsh" +ZSHU[d_cache]="${ZDOTDIR}/.cache/zsh" ZSHU[d_var]="${ZSHU[d_conf]}/var" - -ZSHU[d_bin]="${ZDOTDIR}/.config/dotfiles/bin" -ZSHU[d_scripts]="${ZDOTDIR}/.config/dotfiles/scripts" +ZSHU[d_bin]="${ZSHU[d_dotfiles]}/bin" +ZSHU[d_scripts]="${ZSHU[d_dotfiles]}/scripts" ## early escape unsetopt global_rcs ## safety measure: ## redirect all following activity within ZDOTDIR to cache -## (probably) these files are safe to remove -ZDOTDIR="${ZSHU[d_cache]}" -rm -f "${ZDOTDIR}/.zshrc" "${ZDOTDIR}/.zlogin" +export ZDOTDIR="${ZSHU[d_cache]}/dots" ## cleanup: start from scratch for i ( a s f d ) ; do unhash -$i -m '*' ; done ; unset i @@ -33,23 +29,24 @@ umask 0022 zshu_parts=( env opt lib rc alias local ) for n ( ${zshu_parts} ) ; do - f="${ZSHU[d_conf]}/$n.zsh" - [ -s "$f" ] && source "$f" -done ; unset n f + [ -s "${ZSHU[d_conf]}/$n.zsh" ] || continue + source "${ZSHU[d_conf]}/$n.zsh" +done ; unset n for n ( ${zshu_parts} ) ; do - d="${ZSHU[d_conf]}/$n" - [ -d "$d" ] || continue - for i ( "$d"/*.zsh(N.r) ) ; do + [ -d "${ZSHU[d_conf]}/$n" ] || continue + for i ( "${ZSHU[d_conf]}/$n"/*.zsh(N.r) ) ; do source "$i" done -done ; unset i n d +done ; unset i n unset zshu_parts -ZSHU[t_end]=${(%):-%D{%s.%6.}} +hash -f -ZSHU[t_load]=$[ ZSHU[t_end] - ZSHU[t_begin] ] -ZSHU[t_load]=${ZSHU[t_load]:0:6} - -unset 'ZSHU[t_begin]' 'ZSHU[t_end]' +t=${(%):-%D{%s.%6.}} +t=$[ t - ZSHU[t_begin] ] +unset 'ZSHU[t_begin]' +n=${t#*.} +ZSHU[t_load]=${t%.*}.${n:0:4} +unset n t diff --git a/.config/zsh/lib/enclave.zsh.wip b/.config/zsh/_wip/enclave.zsh.wip similarity index 100% rename from .config/zsh/lib/enclave.zsh.wip rename to .config/zsh/_wip/enclave.zsh.wip diff --git a/.config/zsh/lib/starship.zsh.sample b/.config/zsh/_wip/starship.zsh.sample similarity index 100% rename from .config/zsh/lib/starship.zsh.sample rename to .config/zsh/_wip/starship.zsh.sample diff --git a/.config/zsh/env/aux.zsh b/.config/zsh/env/aux.zsh index 940c0cd..dd9fe1e 100644 --- a/.config/zsh/env/aux.zsh +++ b/.config/zsh/env/aux.zsh @@ -1,6 +1,8 @@ #!/bin/zsh -export NO_AT_BRIDGE=1 -export QT_ACCESSIBILITY=0 +set -a +NO_AT_BRIDGE=1 +QT_ACCESSIBILITY=0 -export MENUCONFIG_COLOR=blackbg +MENUCONFIG_COLOR=blackbg +set +a \ No newline at end of file diff --git a/.config/zsh/env/gopath.zsh b/.config/zsh/env/gopath.zsh new file mode 100644 index 0000000..23ea16e --- /dev/null +++ b/.config/zsh/env/gopath.zsh @@ -0,0 +1,21 @@ +#!/bin/zsh + +z-gobin-fixup() { + (( ${+commands[go]} )) || return 0 + local gobin + gobin=$(go env GOBIN) + if [ -z "${gobin}" ] ; then + local gopath + gopath=$(go env GOPATH) + [ -n "${gopath}" ] || return 1 + [ -d "${gopath}" ] || return 0 + gobin="${gopath}/bin" + fi + [ -d "${gobin}" ] || mkdir "${gobin}" || return 1 + ## already in PATH? + [ "${path[(I)${gobin}]}" = 0 ] || return 0 + path=( "${gobin}" ${path} ) + hash -f +} + +z-gobin-fixup diff --git a/.config/zsh/lib/git.zsh b/.config/zsh/lib/git.zsh index 68ad5c6..eefbda1 100644 --- a/.config/zsh/lib/git.zsh +++ b/.config/zsh/lib/git.zsh @@ -29,12 +29,12 @@ z-git-test() { } __z_git_pwd() { - local x - - unset 'ZSHU_PS[git_ref]' 'ZSHU_PS[git_changes]' 'ZSHU_PS[git_tag]' 'ZSHU_GIT[path_root]' 'ZSHU_GIT[path_mid]' 'ZSHU_GIT[path_last]' 'ZSHU_GIT[commit]' 'ZSHU_GIT[detached]' 'ZSHU_GIT[ref]' 'ZSHU_GIT[remote]' 'ZSHU_GIT[tag]' 'ZSHU_GIT[ref_behind]' 'ZSHU_GIT[ref_ahead]' 'ZSHU_GIT[ref_changes]' - + unset 'ZSHU_PS[git_ref]' 'ZSHU_PS[git_changes]' 'ZSHU_PS[git_tag]' + ZSHU_GIT=() z-git-test || return + + local x x=$(__z_git rev-parse --short HEAD 2>/dev/null) [ -n "$x" ] || return ZSHU_GIT[commit]=$x diff --git a/.config/zsh/lib/pswalk.zsh b/.config/zsh/lib/pswalk.zsh index 0eb0a62..bc6d3fa 100644 --- a/.config/zsh/lib/pswalk.zsh +++ b/.config/zsh/lib/pswalk.zsh @@ -4,25 +4,20 @@ typeset -Uga ZSHU_PARENTS_PID typeset -ga ZSHU_PARENTS_NAME function { - local i c + local i c g - i=$$ ; while : ; do - i=$(ps -o ppid= -p $i 2>/dev/null) || : - i=${i//[^0-9]} - [[ "$i" =~ '^[1-9][0-9]*$' ]] || break + i=${PPID} + while : ; do + [ -n "$i" ] || break ## don't deal with PID1 - [ "$i" = 1 ] && continue + [ "$i" = 1 ] && break + ZSHU_PARENTS_PID+=( $i ) + read -r i c g <<< $(ps -o 'ppid=,comm=' -p "$i" 2>/dev/null) + [ -n "$c" ] && ZSHU_PARENTS_NAME+=( "${c:t}" ) done - for i ( ${ZSHU_PARENTS_PID} ) ; do - c=$(ps -o comm= -p $i 2>/dev/null) || : - [ -n "$c" ] || continue - ZSHU_PARENTS_NAME+=( "${c:t}" ) - done - - typeset -r ZSHU_PARENTS_PID - typeset -r ZSHU_PARENTS_NAME + typeset -r ZSHU_PARENTS_PID ZSHU_PARENTS_NAME } typeset -gA ZSHU_RUN diff --git a/.config/zsh/lib/say-my.zsh b/.config/zsh/lib/say-my.zsh index fed0a2d..5d533e8 100644 --- a/.config/zsh/lib/say-my.zsh +++ b/.config/zsh/lib/say-my.zsh @@ -1,15 +1,17 @@ #!/bin/zsh say_my_name() { + set -a GIT_COMMITTER_NAME="$1" GIT_AUTHOR_NAME="$1" DEBFULLNAME="$1" - export GIT_COMMITTER_NAME GIT_AUTHOR_NAME DEBFULLNAME + set +a } say_my_email() { + set -a GIT_COMMITTER_EMAIL="$1" GIT_AUTHOR_EMAIL="$1" DEBEMAIL="$1" - export GIT_COMMITTER_EMAIL GIT_AUTHOR_EMAIL DEBEMAIL + set +a } diff --git a/.config/zsh/lib/selfservice.zsh b/.config/zsh/lib/selfservice.zsh index d367937..4442d89 100644 --- a/.config/zsh/lib/selfservice.zsh +++ b/.config/zsh/lib/selfservice.zsh @@ -1,16 +1,31 @@ #!/bin/zsh dotfiles-update() { - "${ZSHU[d_zdot]}/.config/dotfiles/install.sh" + "${ZSHU[d_dotfiles]}/install.sh" "$@" } dotfiles-git() { ( cd "${ZSHU[d_zdot]}/" - export GIT_DIR="${ZSHU[d_zdot]}/.config/dotfiles/repo.git" - export GIT_WORK_TREE="${ZSHU[d_zdot]}" + set -a + GIT_DIR="${ZSHU[d_dotfiles]}/repo.git" + GIT_WORK_TREE="${ZSHU[d_zdot]}" + set +a zsh -i ) } +dotfiles-gen-gitignore() { + local x='.config/dotfiles/gen-gitignore.sh' + [ -x "$x" ] || { + echo "${x:t} is somewhere else" >&2 + return 1 + } + if [ -d .config/dotfiles/repo.git ] ; then + echo "NOT going to change dotfiles installation" >&2 + return 1 + fi + "$x" "$@" +} + z-zwc-gen() { local i for i ( "${ZSHU[d_conf]}"/**/*.zsh(N.r) ) ; do diff --git a/.config/zsh/rc/completion.zsh b/.config/zsh/rc/completion.zsh index bc2ade0..047172d 100644 --- a/.config/zsh/rc/completion.zsh +++ b/.config/zsh/rc/completion.zsh @@ -23,13 +23,13 @@ zstyle ':completion:*:*:*:*:processes' command "ps -u ${USER} -o pid,user,comm - zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01' zstyle ':completion:*:kill:*' command 'ps -u ${USER} -o pid,%cpu,tty,cputime,cmd' +autoload -Uz +X compinit && \ +compinit -i -C -d "${ZSHU[f_compdump]}" + if autoload -Uz +X bashcompinit ; then bashcompinit && ZSHU[compdump_bash]=1 fi -autoload -Uz +X compinit && \ -compinit -i -C -d "${ZSHU[f_compdump]}" - for i ( "${ZSHU[d_conf]}"/completion/*.zsh(N.r) ) ; do source "$i" done ; unset i diff --git a/.config/zsh/rc/pager.zsh b/.config/zsh/rc/pager.zsh index d74ce26..58b1ce6 100644 --- a/.config/zsh/rc/pager.zsh +++ b/.config/zsh/rc/pager.zsh @@ -5,6 +5,5 @@ if [ -n "${PAGER}" ] ; then export PAGER READNULLCMD=$(which "${PAGER}" | xargs -r readlink -e) else - unset READNULLCMD - unset NULLCMD + unset PAGER READNULLCMD NULLCMD fi diff --git a/.config/zsh/rc/prompt.zsh b/.config/zsh/rc/prompt.zsh index 4baf12f..a474cc9 100644 --- a/.config/zsh/rc/prompt.zsh +++ b/.config/zsh/rc/prompt.zsh @@ -78,7 +78,7 @@ function { ZSHU_PS1[1L]="${(j::)line}" } -z-ps() { +z-ps1() { [ -n "$1" ] || { echo "${ZSHU_PS[ps1]}" return @@ -95,6 +95,6 @@ z-ps() { return 1 } -z-ps 3 -[ "${ZSHU_RUN[nested]}" = 1 ] && z-ps 2 -[ "${ZSHU_RUN[nested1L]}" = 1 ] && z-ps 1 +z-ps1 3 +[ "${ZSHU_RUN[nested]}" = 1 ] && z-ps1 2 +[ "${ZSHU_RUN[nested1L]}" = 1 ] && z-ps1 1 diff --git a/.config/zsh/rc/terminal.zsh b/.config/zsh/rc/terminal.zsh index 4926306..e1d9183 100644 --- a/.config/zsh/rc/terminal.zsh +++ b/.config/zsh/rc/terminal.zsh @@ -1,14 +1,23 @@ #!/bin/zsh -function { +z-orig-term() { local -a a 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) + x=$(sed -zEn '/^TERM=(.+)$/{s//\1/;p;}' "/proc/$i/environ" 2>/dev/null | tr -d '\0') [ -n "$x" ] || continue a+=( "$x" ) done - export ORIG_TERM="${a[-1]}" + case "$1" in + \* | @ ) + local ORIG_TERM=( $a ) + declare -p ORIG_TERM + ;; + * ) + i='-1' ; x="${1:-$i}" + echo "${a[$x]}" + ;; + esac } diff --git a/.zshenv b/.zshenv index 5dbbacc..ae8221c 100644 --- a/.zshenv +++ b/.zshenv @@ -2,5 +2,6 @@ if [[ -o interactive ]] ; then ## early redirect : "${ZDOTDIR:=${HOME}}" + [ "${ZDOTDIR}" = "${HOME}/.cache/zsh/dots" ] && ZDOTDIR="${HOME}" source "${ZDOTDIR}/.config/zsh/_.zsh" fi