From 5070c97ffeeb359874132ff6b34b0c39e43217f2 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Thu, 4 Nov 2021 12:24:23 +0100 Subject: [PATCH] bin: improve helpers, add own and dsync --- .config/shell/functions | 5 +++-- .local/bin/scripts/b | 14 +++++++++----- .local/bin/scripts/clean | 11 +++++++---- .local/bin/scripts/dsync | 4 ++++ .local/bin/scripts/mp | 2 +- .local/bin/scripts/own | 3 +++ .local/bin/scripts/roam | 2 +- 7 files changed, 28 insertions(+), 13 deletions(-) create mode 100755 .local/bin/scripts/dsync create mode 100755 .local/bin/scripts/own diff --git a/.config/shell/functions b/.config/shell/functions index d96df0e..8ed3ac1 100644 --- a/.config/shell/functions +++ b/.config/shell/functions @@ -41,7 +41,8 @@ wh() { res=$(which "$@") if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null then echo "$res" | bat --style=plain --language=sh && - wh $(expr "$res" : "${@:$#}: aliased to ${@:$#} " >/dev/null && echo "-p") $(echo "$res" | cut -d' ' -f$(expr 5 '&' "$res" : ".*to \(sudo\|noglob\) " '|' 4) | cut -d'(' -f2) + tool="$(echo "$res" | cut -d' ' -f$(expr 5 '&' "$res" : ".*to \(sudo\|noglob\) " '|' 4) | cut -d'(' -f2)" + wh $(test $tool = $1 && echo "-p") $tool # use command which for other shells else test -r "$res" && b --style=header "$res" || echo "$res" | bat --style=plain --language=sh fi @@ -77,7 +78,7 @@ edshell() { esac test -f "$file" || return 2 checksum="$(md5sum "$file")" - $EDITOR "$(test "$line" && echo "+normal! ${line}ggzx")" "${file%:*}" + $EDITOR "$(test "$line" && echo "+normal! ${line}ggzx" || echo "--")" "${file%:*}" test "$checksum" != "$(md5sum $file)" && source "$HOME/.zprofile" && exec $SHELL } diff --git a/.local/bin/scripts/b b/.local/bin/scripts/b index 5f17d81..f054efb 100755 --- a/.local/bin/scripts/b +++ b/.local/bin/scripts/b @@ -13,14 +13,18 @@ last=${last:-.} test -x "$(dirname "$last")" && { test -r "$last" || ! test -e "$last"; } || elevate=sudo -tput setaf 4 && $elevate file -E "$last" | ( grep -v --color=never 'directory$' || true ) +tput setaf 4 && $elevate file -E "$last" | { grep -v --color=never 'directory$' || true; } case "$($elevate file --dereference --mime "$last")" in (*inode/directory*) tput sgr0 && $(test -x "$last" || echo "sudo") ls -l --color=always --human-readable --group-directories-first --file-type --dereference-command-line --all "$@" | less -XF;; - (*binary) if expr "$(file "$last")" : ".*: .*compress" >/dev/null + (*binary) + if expr "$(file "$last")" : ".*: .*compress" >/dev/null then /usr/share/nvim/runtime/macros/less.sh "$@" - fi;; + fi + tput setaf 3 && $elevate stat --format '%A size %sB, birth: %.10w mod %.10y' "$last" + ;; (*) $elevate bat --style header "$@" --pager 'less -RXF' - tput setaf 3 && $elevate stat --format '%A size %sB, birth: %.10w mod %.10y' "$last" - ;; + tput setaf 3 && $elevate stat --format '%A size %sB, birth: %.10w mod %.10y' "$last" + # TODO don't duplicate stat + ;; esac diff --git a/.local/bin/scripts/clean b/.local/bin/scripts/clean index 6987504..8a4f824 100755 --- a/.local/bin/scripts/clean +++ b/.local/bin/scripts/clean @@ -2,13 +2,16 @@ # Cleans up files according to given parameters typeset -A _clean_map -_clean_map=([h]=$XDG_CACHE_HOME [t]=/var/tmp) +_clean_map=([h]=$XDG_CACHE_HOME [t]=/var/tmp [l]=/var/log [c]=/var/cache) # Directories to always clean -_clean_folders=() +_clean_folders=(~/.ant ~/.autopsy ~/.cargo ~/.electron-gyp ~/.nix-defexpr ~/.npm ~/.parallel ~/.stack ~/.yarn + ~/.lesshst) -for f in $_clean_folders -do test -d $f && rm -rv $f + +for f in ${_clean_folders[@]} +do test -f $f || test -d $f && highlight $f && rm -rf $f done +find $HOME -maxdepth 2 -not -name ".stfolder" -type d -empty -exec rm -vd {} + highlight "g to clean gradle" if [[ $1 =~ "g" ]]; then diff --git a/.local/bin/scripts/dsync b/.local/bin/scripts/dsync new file mode 100755 index 0000000..095564c --- /dev/null +++ b/.local/bin/scripts/dsync @@ -0,0 +1,4 @@ +#!/bin/sh +# rsync directory properly +# suffix both dirs with / to act on contents +rsync -recursive --specials --times --links --delete --info=progress2,remove,symsafe,flist,del --human-readable "$@" diff --git a/.local/bin/scripts/mp b/.local/bin/scripts/mp index 14e819e..a872687 100755 --- a/.local/bin/scripts/mp +++ b/.local/bin/scripts/mp @@ -19,7 +19,7 @@ then shift then pushd "$(dirname "$path")" >/dev/null && cat "$path" | sed '/#.*/D' | xargs --delim='\n' "$0" -r $verbose && popd >/dev/null else test -n "$verbose" && echo "Finding path '$path' $(test "$arg" != "$path" && echo "from '$arg' ")(MUSIC: '$MUSIC', PWD: '$PWD')" >&2 - find "$path" -name "*.flac" -o -name "*.mp3" | sort | while read file + find "$path" -iname "*.flac" -o -iname "*.mp3" -o -iname "*.ogg" -o -iname "*.opus" | sort | while read file do case "$path" in ($MUSIC/*) echo "${file#$MUSIC/}";; (*) mkdir -p "$LINKS" && ln -fs "$file" "$LINKS/$(basename "$file")" && echo "${LINKS#$MUSIC/}/$(basename "$file")";; diff --git a/.local/bin/scripts/own b/.local/bin/scripts/own new file mode 100755 index 0000000..c9a61ac --- /dev/null +++ b/.local/bin/scripts/own @@ -0,0 +1,3 @@ +#!/bin/sh +# Take ownership of the given files recursively +sudo chown -R --preserve-root $USER "$@" diff --git a/.local/bin/scripts/roam b/.local/bin/scripts/roam index e1c8c1c..cdd7677 100755 --- a/.local/bin/scripts/roam +++ b/.local/bin/scripts/roam @@ -1,4 +1,4 @@ #!/bin/sh -e # Move roam files into subfolders -cd $DATA/2-standards/notes/roam +cd $DATA/2-standards/notes find . -mindepth 1 -type d -printf '%f\n' | while read dir; do grep -l --directories=skip "#+filetags: :$dir" * | xargs --verbose --no-run-if-empty mv -t "$dir"; done