From fa524625d9bd1d73f0334b445f4e467efcb436c0 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Wed, 7 Jul 2021 00:48:35 +0200 Subject: [PATCH] bin: revamp IGNOREDIRS and rd, add d alias & zf script for zoxide --- .config/shell/functions | 46 +++++++++++++++++++++++------------ .local/bin/scripts/b | 4 +-- .local/bin/scripts/dif | 3 +-- .local/bin/scripts/diff-color | 4 +++ .local/bin/scripts/help | 6 ++--- .local/bin/scripts/pdiff | 3 ++- .local/bin/scripts/zf | 14 +++++++++++ .zprofile | 2 +- 8 files changed, 57 insertions(+), 25 deletions(-) create mode 100755 .local/bin/scripts/diff-color create mode 100755 .local/bin/scripts/zf diff --git a/.config/shell/functions b/.config/shell/functions index b333c9b..fa1f3e4 100644 --- a/.config/shell/functions +++ b/.config/shell/functions @@ -12,8 +12,8 @@ fi alias h='help' alias info='info --vi-keys' +d() { cd $(zf "$@") } alias c=z -alias d=z alias v=edit xdh="$XDG_DATA_HOME" @@ -33,18 +33,27 @@ which lsb_release >/dev/null && export DIST=$(lsb_release --id | cut -d' ' -f2) compdef help=info unalias rd 2>/dev/null rd() { - while test $# -gt 0 + for f in "$@" do - test -d "$1" && find "$1" -maxdepth 1 -type d -empty -printf "Removing %p\n" -delete || rm -iv "$@" + if test -d "$f"; then + if test -d "$f/.git" + then echo "Force delete git project?" && read answer && test "$answer" = "y" && rm -rf "$f" + else find "$f" -maxdepth 1 -type d -empty -printf "Removing empty %p\n" -delete + fi + else rm -i "$f" + fi shift done } -rr() { mv "$@" /tmp } # like "which", but shows contents if it resolves to a file wh() { res=$(which "$@") - test -r "$res" && b --style=numbers,header "$res" || echo "$res" + if expr "$res" : "${@:$#}: aliased to" >/dev/null + then echo "$res" && wh $(expr "$res" : "${@:$#}: aliased to ${@:$#} " >/dev/null && echo "-p") $(echo "$res" | cut -d' ' -f4) + # use command which for other shells + else test -r "$res" && b --style=header "$res" || echo "$res" | bat --style=plain --language=sh + fi } compdef wh=which @@ -84,10 +93,10 @@ edconf() { test "$sel" && ((echo "$sel" | cat - "$conf_cache" | head -30 >"$conf_tmp" && mv "$conf_tmp" "$conf_cache") & edit "$sel") } -alias hist='print -z $(history | fzf --nth 4.. | tr -s " " | cut -d" " -f5-)' +alias hist='print -z $(history | tac | fzf --tiebreak=index | tr -s " " | cut -d" " -f5-)' CONFIG_SHELL_FUNCTIONS="${BASH_SOURCE[0]:-${(%):-%x}}" -alias r="reset && exec zsh" +alias rs="reset && exec zsh" edcomp() { file=$(echo "$1" | sed 's/^\([^_]\)/_\1/') $EDITOR "${fpath[-1]}/$file" @@ -155,9 +164,8 @@ xo() { done } alias sqli='rlwrap sqlite3 -column -header -cmd .tables' -__loci='locate --ignore-case --basename' -alias loc="noglob $__loci" -alias uloc="noglob sudo updatedb && $__loci" +loc() { noglob locate --ignore-case --basename --existing "$@" | grep --color=always "$@" | less -F } +alias uloc='noglob sudo updatedb && loc' alias syslog='less +F /var/log/syslog' alias println='printf "\n"' alias graphics='lspci -vnn | grep VGA --color=never && xrandr --listproviders' @@ -189,8 +197,8 @@ alias jcl='sudo SYSTEMD_LESS=FRKi journalctl --boot --no-hostname -e' alias jc='jcl -u' alias jcj='jcl -o json-pretty -u' -alias grp='grep --line-number --ignore-case --binary-files=without-match --directories=skip' -alias grpr="grp --recursive --exclude-dir={$IGNOREDIRS}" +alias grp='grep --color=auto --line-number --ignore-case --binary-files=without-match --directories=skip' +grpr() { grp --color=always --recursive $(echo $IGNOREDIRS | sed 's/-x/--exclude-dir/g') "$@" | less -F } # Recover stray swap files from neovim alias vrec="ls $XDG_DATA_HOME/nvim/swap | sed 's/\%/\//g' | sed 's|\(.*\)\..*|\1|' | head -1 | xargs -r nvim" @@ -352,8 +360,8 @@ resetdocker() { typeset -A _clean_map if test "$BASH_VERSION" -then _clean_map=([c]=$XDG_CACHE_HOME [t]=/var/tmp) -else _clean_map=(c $XDG_CACHE_HOME t /var/tmp) +then _clean_map=([h]=$XDG_CACHE_HOME [t]=/var/tmp) +else _clean_map=(h $XDG_CACHE_HOME t /var/tmp) fi _clean_folders=() clean() { @@ -363,8 +371,8 @@ clean() { highlight "g to clean gradle" if [[ $1 =~ "g" ]]; then - find ${GRADLE_USER_HOME:-$HOME/.gradle} -mindepth 1 -maxdepth 1 -type d -exec rm -r {} \; -print - find $projects_dir -name .gradle -print -exec rm -r {} \; + find ${GRADLE_USER_HOME:-$HOME/.gradle} -mindepth 1 -maxdepth 1 -type d -print -exec rm -r {} + + find $projects_dir -name .gradle -print -exec rm -r {} + fi highlight "m to clean mac-files" @@ -373,6 +381,12 @@ clean() { find -name '*.DS_Store' -delete fi + highlight "c to clean local caches" + if [[ $1 =~ "c" ]]; then + find -name ".gradle" -o -name "generated" -o -name "dist_newstyle" -o -name "cache" -o -name "node_modules" -print -exec rm -r {} + + find -name "*build" -ok rm -r {} \; -prune + fi + if test "$BASH_VERSION"; then for k in "${!_clean_map[@]}"; do highlight "$k to delete $_clean_map[$k]" diff --git a/.local/bin/scripts/b b/.local/bin/scripts/b index 30caac9..292d302 100755 --- a/.local/bin/scripts/b +++ b/.local/bin/scripts/b @@ -5,13 +5,13 @@ for last; do true; done last=${last:-.} -file "$last" | grep -v --color=never 'directory$' +tput setaf 4 && file "$last" | grep -v --color=never 'directory$' test -e "$last" || exit 1 case "$(file --dereference --mime "$last")" in *inode/directory*) ls -l --color=always --human-readable --group-directories-first --file-type --dereference-command-line --all "$@";; *binary) ;; - *) $(test -f "$last" -a ! -r "$last" && echo "sudo") bat --style header --pager "less --LONG-PROMPT --quit-if-one-screen" "$@" + *) $(test -f "$last" -a ! -r "$last" && echo "sudo") bat --style header "$@" tput setaf 3 && stat --format '%A size %sB, birth: %.10w mod %.10y' "$last" ;; esac diff --git a/.local/bin/scripts/dif b/.local/bin/scripts/dif index 72f414d..5f0910f 100755 --- a/.local/bin/scripts/dif +++ b/.local/bin/scripts/dif @@ -1,6 +1,5 @@ #!/bin/sh set -o pipefail # interactive diff with pagination -diff --color=always --report-identical-files -U 2 "$@" | - diffr --colors refine-added:none:background:0x33,0x66,0x33:bold --colors added:none:background:0x33,0x44,0x33 --colors refine-removed:none:background:0x66,0x33,0x33:bold --colors removed:none:background:0x44,0x33,0x33 | +diff-color --report-identical-files "$@" | less --RAW-CONTROL-CHARS --quit-on-intr --quit-if-one-screen diff --git a/.local/bin/scripts/diff-color b/.local/bin/scripts/diff-color new file mode 100755 index 0000000..3a84074 --- /dev/null +++ b/.local/bin/scripts/diff-color @@ -0,0 +1,4 @@ +#!/bin/sh +# Colorize with diffr +diff --color=always -U 2 "$@" | + diffr --colors refine-added:none:background:0x33,0x66,0x33:bold --colors added:none:background:0x33,0x44,0x33 --colors refine-removed:none:background:0x66,0x33,0x33:bold --colors removed:none:background:0x44,0x33,0x33 diff --git a/.local/bin/scripts/help b/.local/bin/scripts/help index 0e46054..ef85f50 100755 --- a/.local/bin/scripts/help +++ b/.local/bin/scripts/help @@ -1,8 +1,8 @@ #!/bin/sh case $1 in (vlc) unbuffer vlc --full-help "${@:2}" | ${PAGER:-less} --quit-if-one-screen;; - (zmv) # https://unix.stackexchange.com/questions/19290/zmv-for-zsh-dry-runs-and-man-pages + (zsh|zmv) # https://unix.stackexchange.com/questions/19290/zmv-for-zsh-dry-runs-and-man-pages # need to install zsh-doc for info - info --index-search=zmv zsh || LESS="$LESS +/^ *zmv *\\[" man zshcontrib;; - (*) man "$@" || "$@" --help | ${PAGER:-less} --quit-if-one-screen;; + info --vi-keys --index-search="${@:$#}" zsh || LESS="$LESS +/^ *zmv *\\[" man zshcontrib;; + (*) info "$1" -w | grep -v "manpages" && pinfo "$@" || man "$@" || "$@" --help | ${PAGER:-less} --quit-if-one-screen;; esac diff --git a/.local/bin/scripts/pdiff b/.local/bin/scripts/pdiff index c51906c..f030662 100755 --- a/.local/bin/scripts/pdiff +++ b/.local/bin/scripts/pdiff @@ -1,2 +1,3 @@ #!/bin/sh -diff -r $1 $2 -x .git -x .idea -x .gradle -x build -x cmake_build -x dist_newstyle +# Diff project directories, excluding any generated content +diff-color -r $IGNOREDIRS "$@" | less -F diff --git a/.local/bin/scripts/zf b/.local/bin/scripts/zf new file mode 100755 index 0000000..e5faed9 --- /dev/null +++ b/.local/bin/scripts/zf @@ -0,0 +1,14 @@ +#!/bin/sh +# Quick directory switcher based on zoxide and fzf +if test -d "$1" -o "$1" = "-" +then echo "$@" +else +set -o noglob +query=$(zoxide query -sl "$@") +# First find out whether there is an obvious match (score at least ten times that of runner-up) +# If not select with fzf, using locate to find extra options +( expr $(echo "$query" | sed 'N;s|/.*\n|> 10 *|;q' | sed 's| */.*||') >/dev/null && + echo "$query" | head -1 || + ( echo "$query"; locate --basename --existing "$@" | while read -r file; do test -d "$file" && echo "$file"; done | sed 's/^/ 0 /' ) | + fzf -0 -1 -n2.. --tiebreak=index --preview="ls -a --color --human-readable --group-directories-first --file-type {2..}" --preview-window=20% ) | tr -s ' ' | cut -d' ' -f3- +fi diff --git a/.zprofile b/.zprofile index 371c4a6..9daf810 100644 --- a/.zprofile +++ b/.zprofile @@ -45,7 +45,7 @@ export CCACHE_DIR="$XDG_CACHE_HOME"/ccache # environment which nvim >/dev/null && export EDITOR='nvim' || export EDITOR='vim' export LESS='--RAW-CONTROL-CHARS --ignore-case --incsearch --LONG-PROMPT --jump-target=5' -export IGNOREDIRS=".sync,.stfolder,.git,out,build,dev" +export IGNOREDIRS="-x dev -x .sync -x .stfolder -x .git -x .gradle -x .idea -x out -x *build -x dist_newstyle -x generated -x cache -x node_modules" # red stderr test -f "/usr/lib/libstderred.so" && export LD_PRELOAD="/usr/lib/libstderred.so${LD_PRELOAD:+:$LD_PRELOAD}" # software config