Compare commits

...

2 commits

Author SHA1 Message Date
xeruf
7fad5c8fbe bin: various fixes 2022-12-13 13:25:04 +01:00
xeruf
3b01d60544 config/shell: improve completion & some functions 2022-12-13 13:07:02 +01:00
12 changed files with 79 additions and 48 deletions

View file

@ -36,7 +36,7 @@ cl() {
cd() { cd() {
if test -d "$DATA" && test "${1:--}" != "-" -a ! -d "$1" -a $# -eq 1 if test -d "$DATA" && test "${1:--}" != "-" -a ! -d "$1" -a $# -eq 1
then then
dir=$(fd --no-ignore --glob "$1*" "$DATA" $MNT --maxdepth 2 --type d --max-results 1 2>/dev/null) dir=$(fd --no-ignore --glob "$1*" "$DATA" "$MNT" --maxdepth 2 --type d --max-results 1 2>/dev/null)
test -n "$dir" && cd "$dir" test -n "$dir" && cd "$dir"
else else
builtin cd "$@" && builtin cd "$@" &&
@ -45,9 +45,10 @@ cd() {
# - column malaligns output with ANSI escape sequences (view with LESS="" less) # - column malaligns output with ANSI escape sequences (view with LESS="" less)
# column --fillrows --output-width $(expr $COLUMNS + 20) # column --fillrows --output-width $(expr $COLUMNS + 20)
# pr -4 -l1 -W $COLUMNS -S" " # pr -4 -l1 -W $COLUMNS -S" "
# TODO test whether still needed with exa
command ls --sort=none --quote-name | command ls --sort=none --quote-name |
head -12 | head -12 |
xargs ls --sort=time --format=vertical --directory --literal --classify --color=always --width $COLUMNS | COLUMNS=$(tput cols) xargs $(which exa >/dev/null 2>&1 && echo "exa --list-dirs --icons --reverse" || echo 'ls --format=vertical --literal --classify --directory') --sort=time --color=always |
head -3 head -3
code=$? && test $code -eq 141 || return $code code=$? && test $code -eq 141 || return $code
fi fi

View file

@ -50,6 +50,7 @@ unalias rd 2>/dev/null
# System help {{{1 # System help {{{1
compdef help=man compdef help=man
compdef h=help
alias info='info --vi-keys' alias info='info --vi-keys'
sudos() { sudos() {
@ -76,9 +77,9 @@ xtrace() {
set +x set +x
} }
# Shows source code for command, resolving nested aliases # Shows source for given command, resolving nested aliases
wh() { wh() {
local res=$(which "$@") || return $? local res=$(which "$@" 2>/dev/null || type "$@") || return $?
# only works in zsh, not bash # only works in zsh, not bash
if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null
then echo "$res" | bat --style=plain --language=sh && then echo "$res" | bat --style=plain --language=sh &&
@ -90,6 +91,12 @@ wh() {
} }
compdef wh=which compdef wh=which
# Find where a function was defined
def() {
autoload +X "$@"
type "$@"
}
pathadd() { pathadd() {
local IFS=":" local IFS=":"
local result="$@" local result="$@"
@ -134,6 +141,7 @@ edit-shell() {
# Edit an executable in the PATH # Edit an executable in the PATH
edit-bin() { edit-bin() {
local toedit="$(resolvealias "$1")" local toedit="$(resolvealias "$1")"
case "$toedit" in (*\ text*);; (*) toedit="$1";; esac
if f="$(which "$toedit" 2>/dev/null)" && test -f "$f" if f="$(which "$toedit" 2>/dev/null)" && test -f "$f"
then edit "$f" then edit "$f"
else edit-shell -f "$toedit" else edit-shell -f "$toedit"
@ -358,7 +366,14 @@ alias grp='grpc --ignore-case'
# TODO remove some prefixes \([^ ]*/\)\? # TODO remove some prefixes \([^ ]*/\)\?
grpr() { grp --color=always --recursive $(echo $DIRS_IGNORE | sed 's|-x |--exclude-dir |g') "$@" | less -FX; } grpr() { grp --color=always --recursive $(echo $DIRS_IGNORE | sed 's|-x |--exclude-dir |g') "$@" | less -FX; }
# Grep in shell config files # Grep in shell config files
grsh() { grpr --no-ignore-case "$@" $HOME/.{ba,z}sh* $HOME/.local/bin $CONFIG_SHELLS $CONFIG_ZSH; } grsh() {
local search=$1
shift
case $1 in (-*|"");; (*) local filter=$1; shift;; esac
grpr --no-ignore-case "$search" "$@" $(test -z "$filter" &&
echo $HOME/.local/bin $CONFIG_SHELLS $CONFIG_ZSH $HOME/.{ba,z}sh* /{etc,usr/share}/{ba,z}sh* /etc/profile* ||
compgen -G "$XDG_CONFIG_HOME/$filter" -G "/usr/share/$filter" -G "/etc/$filter*" -G "$HOME/.$filter*")
}
# Recover stray swap files from neovim # Recover stray swap files from neovim
vrec() { vrec() {
@ -455,7 +470,9 @@ killJava() {
# Files {{{1 # Files {{{1
alias l="ls -l --almost-all --human-readable --group-directories-first --file-type" which exa >/dev/null &&
alias l='exa --icons' &&
alias ll='l --long --extended --git --all'
if which fd >/dev/null if which fd >/dev/null
then fn() { $(command -v fd || echo fdfind) --hidden --no-ignore-vcs --one-file-system "$@" | less -F; } # [F]ind [n]o ignore then fn() { $(command -v fd || echo fdfind) --hidden --no-ignore-vcs --one-file-system "$@" | less -F; } # [F]ind [n]o ignore
@ -463,7 +480,7 @@ then fn() { $(command -v fd || echo fdfind) --hidden --no-ignore-vcs --one-file-
compdef ff=fd compdef ff=fd
compdef fn=fd compdef fn=fd
else else
alias ff='find -not -path "*.sync*" -and -not \( -name daten -prune \)' alias ff='find -not -path "*.sync*" -and -not \( -name daten -prune \) -iname'
alias f1='find -mindepth 1 -maxdepth 1' alias f1='find -mindepth 1 -maxdepth 1'
fi fi
@ -539,29 +556,31 @@ umoul() {
# get the last arg # get the last arg
for arg; do true; done for arg; do true; done
if test "$arg" if test "$arg"
then mountpoint="$(test -d "$arg" && realpath "$arg" || echo "${MNT:-${XDG_RUNTIME_DIR}/mnt}/$arg")" then mountpoints="$(test -d "$arg" && realpath "$arg" || echo "${MNT:-${XDG_RUNTIME_DIR}/mnt}/$arg")"
mountpoint "$mountpoint" 2>/dev/null || test -b "$mountpoint" || mountpoint "$mountpoints" 2>/dev/null || test -b "$mountpoints" ||
mountpoint="$(mount --show-labels | grep "$arg" | cut -d' ' -f3)" mountpoints="$(mount --show-labels | grep "$arg" | cut -d' ' -f3)"
test "$mountpoint" || return 1 test "$mountpoints" || return 1
else mountpoint="$PWD" else mountpoints="$PWD"
while test "$mountpoint" != "/"; do while test "$mountpoints" != "/"; do
mountpoint "$mountpoint" >/dev/null && break mountpoint "$mountpoints" >/dev/null && break
mountpoint="$(dirname "$mountpoint")" mountpoints="$(dirname "$mountpoints")"
done done
test "$(dirname "$mountpoint")" != "/" || test "$(dirname "$mountpoints")" != "/" ||
mountpoint="$(mount | mountpoints="$(mount |
grep --invert-match -e " /[^m][^/]*\(/[^/]*\)\? " -e "/sys" -e "/run/user" -e "/run/docker" | grep --invert-match -e " /[^m][^/]*\(/[^/]*\)\? " -e "/sys" -e "/run/user" -e "/run/docker" |
fzf --select-1 --exit-0 | awk '{print $3}')" || fzf --select-1 --exit-0 | awk '{print $3}')" ||
return $? return $?
fi fi
while true; do while true; do
case "$PWD" in case "$PWD" in
("$mountpoint"*) popd || builtin cd "$(dirname $mountpoint)";; ("$mountpoints"*) popd || builtin cd "$(dirname $mountpoints)";;
(*) break;; (*) break;;
esac esac
done done
# pass on all args except last # pass on all args except last
moul -u "${@:1:$(((# > 1) ? #-1 : 0))}" "$mountpoint" for mountpoint in $mountpoints
do moul -u "${@:1:$(((# > 1) ? #-1 : 0))}" "$mountpoint"
done
} }
resetdocker() { resetdocker() {

View file

@ -5,7 +5,6 @@ command -v notify-send >/dev/null &&
if which jrnl >/dev/null; then if which jrnl >/dev/null; then
alias j='jrnl' alias j='jrnl'
jn() { jrnl -to today "$@" | less +G; } jn() { jrnl -to today "$@" | less +G; }
compdef jn=jrnl
alias jnc='jn -contains' alias jnc='jn -contains'
alias jne='jrnl --edit' alias jne='jrnl --edit'
# TODO jump to end # TODO jump to end

View file

@ -96,7 +96,7 @@ bindkey '^q' push-line-or-edit
export KEYTIMEOUT=1 export KEYTIMEOUT=1
# Obsolete: zsh-vim-mode plugin - Custom VIM bindings {{{ # Obsolete because of zsh-vim-mode plugin - Custom VIM bindings {{{
#bindkey -v #bindkey -v
#autoload -Uz history-search-end #autoload -Uz history-search-end
# #

View file

@ -91,7 +91,7 @@ for arg; do
(*\ video/*) (*\ video/*)
suffix=_thumbs.jpg suffix=_thumbs.jpg
mtn -q -i -t -W -r2 -D6 -b 0,6 -c $grid -w $(expr $(tput cols) '*' 20) \ mtn -q -i -t -W -r2 -D6 -b 0,6 -c $grid -w $(expr $(tput cols) '*' 20) \
-O "$prefix" -o "$suffix" "$arg" -O "$prefix" -o "$suffix" "$arg" &&
timg -W "$prefix/$(basename "${arg%.*}")$suffix" timg -W "$prefix/$(basename "${arg%.*}")$suffix"
;; ;;
(*\ image/*) (*\ image/*)
@ -134,7 +134,7 @@ for arg; do
fi fi
esac esac
;; ;;
(*:\ *database*) highlight "Tables" && sqlite3 "$arg" ".tables";; (*:\ SQLite\ *\ database*) highlight "Tables" && sqlite3 "$arg" ".tables";;
(*:\ data) ;; (*:\ data) ;;
(*) bat+=("$arg") (*) bat+=("$arg")
timga+=("$arg") timga+=("$arg")
@ -175,6 +175,7 @@ pager="${PAGER:-'less -RF'}"
# bat: unknown files # bat: unknown files
# batplain: files to print without header # batplain: files to print without header
if test "$bat" -o "$batplain"; then if test "$bat" -o "$batplain"; then
# TODO allow plain less
test "$(bat --version | cut -d. -f2)" -gt 16 && rule=,rule test "$(bat --version | cut -d. -f2)" -gt 16 && rule=,rule
if test $# -gt ${#bat[@]} -a $# -gt ${#batplain[@]} && test -z "$flags" if test $# -gt ${#bat[@]} -a $# -gt ${#batplain[@]} && test -z "$flags"
then cut="--line-range :7" then cut="--line-range :7"
@ -214,7 +215,7 @@ if test "$bat" -o "$batplain"; then
else $batcommand "${batpager:-$pager}" $batstyle,header$(test $# -gt 1 && echo ",numbers") "${bat[@]}" else $batcommand "${batpager:-$pager}" $batstyle,header$(test $# -gt 1 && echo ",numbers") "${bat[@]}"
fi fi
fi fi
if $inspect || test $# -lt $(expr $(tput lines) / 3); if $inspect || test $# -lt $(expr $(tput lines) / 10);
then fileinfo "${bat[@]}" then fileinfo "${bat[@]}"
fi fi
fi fi
@ -230,7 +231,7 @@ if test "$ls" -o $# -le $(echo "$flags" | wc -w); then
if test '$tree' if test '$tree'
then $elevate tree -a --dirsfirst --du -h -C -L 3 $flags $(printf "'%s' " "${ls[@]:-.}") then $elevate tree -a --dirsfirst --du -h -C -L 3 $flags $(printf "'%s' " "${ls[@]:-.}")
elif which exa 2>/dev/null >&2 elif which exa 2>/dev/null >&2
then $elevate exa --icons --color=always --long --group --classify --all --all --sort=changed --reverse $flags $(printf "'%s' " "${ls[@]:-.}") then $elevate exa --icons --color=always --long --group --classify --all --all --sort=modified --reverse $flags $(printf "'%s' " "${ls[@]:-.}")
else $elevate ls -l $(test $# -gt ${#ls[@]} && echo '-d') --color=always --human-readable --si --group-directories-first --file-type --dereference-command-line-symlink-to-dir --all $flags $(printf "'%s' " "${ls[@]:-.}") else $elevate ls -l $(test $# -gt ${#ls[@]} && echo '-d') --color=always --human-readable --si --group-directories-first --file-type --dereference-command-line-symlink-to-dir --all $flags $(printf "'%s' " "${ls[@]:-.}")
fi fi
" || $elevate ls $(test $# -gt ${#ls[@]} && echo "-d") --color=always --human-readable --si --dereference-command-line --all --sort=none $flags "${ls[@]:-.}" " || $elevate ls $(test $# -gt ${#ls[@]} && echo "-d") --color=always --human-readable --si --dereference-command-line --all --sort=none $flags "${ls[@]:-.}"

View file

@ -78,7 +78,7 @@ highlight "c :: clean electron caches"
highlight "o :: optimize space extensively" highlight "o :: optimize space extensively"
if [[ $1 =~ "o" ]]; then if [[ $1 =~ "o" ]]; then
sudo find $XDG_CACHE_HOME /var/cache /var/log /var/tmp -maxdepth 2 -atime +2 -exec rm -r {} + sudo find $XDG_CACHE_HOME /var/cache /var/log /var/tmp -mindepth 1 -maxdepth 2 -atime +2 -exec rm -r {} + -prune
if test -f "/var/log/apt/history.log"; then if test -f "/var/log/apt/history.log"; then
aptclean_cur=$(cat "/var/log/apt/history.log" | wc -l) aptclean_cur=$(cat "/var/log/apt/history.log" | wc -l)
@ -90,9 +90,10 @@ if [[ $1 =~ "o" ]]; then
sudo rm -rf "$XDG_DATA_HOME/baloo" sudo rm -rf "$XDG_DATA_HOME/baloo"
docker image prune docker image prune
which yay &>/dev/null && yay -Sc --noconfirm which yay &>/dev/null &&
nix-collect-garbage -d yay -Sc --noconfirm
nix-store --optimize nix-collect-garbage -d 2>/dev/null &&
nix-store --optimize
fi fi
df --output="source,avail" -h $(test -n "$local" && echo ".") "$dir" df --output="source,avail" -h $(test -n "$local" && echo ".") "$dir"

View file

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
# Emacs in the terminal # Emacs in the terminal
pgrep --full "emacs --daemon" >/dev/null || emacs --daemon pgrep --full "emacs .*-daemon" >/dev/null || emacs --daemon
emacsclient --create-frame --tty "$@" emacsclient --create-frame --tty "$@"
exit=$? exit=$?
stty sane stty sane

View file

@ -1,3 +1,5 @@
#!/bin/sh #!/bin/sh
# Grep through all manuals # Grep through all manuals
find /usr/share/man -name "*$1*" | xargs gunzip -c | grep $2 --color=auto manual=$1
shift
find /usr/share/man -name "*$manual*" | xargs gunzip -c | grep "$@" --color=auto

View file

@ -4,7 +4,10 @@
# optdepends: highlight(dotfiles) # optdepends: highlight(dotfiles)
# TODO show tldr page # TODO show tldr page
paginate="${PAGER:-less} +Gg" paginate="${PAGER:-less} +Gg"
test "$1" = "-d" && browse=1 && shift case "$1" in
(-d) browse=1; shift;;
(-v) set -o xtrace; shift;;
esac
showinfo() { showinfo() {
which info >/dev/null 2>&1 && which info >/dev/null 2>&1 &&
@ -38,7 +41,8 @@ case "$cmd" in
for last; do true; done for last; do true; done
# need to install zsh-doc package for info pages # need to install zsh-doc package for info pages
showinfo zsh $(test "$last" != zsh && echo "$last") || showinfo zsh $(test "$last" != zsh && echo "$last") ||
LESS="$LESS +/^ *$last *\\[" man zshall;; LESS="$LESS$(test "$last" != zsh && echo "+/^ *$last *\\[")" man zshall;;
(emacs) man emacs;;
(*) showinfo "$@" || man "$@" || return=$? (*) showinfo "$@" || man "$@" || return=$?
if test "${return:-0}" -gt 0 && which "$cmd" >/dev/null if test "${return:-0}" -gt 0 && which "$cmd" >/dev/null
then { unbuffer "$@" --help || { test $? == 1 && "$@" -help; } || { test $? == 1 && "$@" -h; }; } 2>&1 | $paginate then { unbuffer "$@" --help || { test $? == 1 && "$@" -help; } || { test $? == 1 && "$@" -h; }; } 2>&1 | $paginate
@ -47,18 +51,19 @@ case "$cmd" in
esac esac
docs="/usr/share/doc/$cmd" docs="/usr/share/doc/$cmd"
if test -d "$docs"; then if test -d "$docs"; then
len() { echo "$1" | wc -c; }
shortest() {
read shortest
while read in
do test $(len $shortest) -gt $(len $in) && shortest=$in
done
echo $shortest
}
file="$(find $docs -name index.html | shortest | grep . || find $docs -name $1*.html | shortest)"
if test -z "$browse" if test -z "$browse"
then echo "Find more in $docs" then echo "Find more in file://${file:-docs}"
else else firefox "file://$file"
len() { echo "$1" | wc -c; }
shortest() {
read shortest
while read in
do test $(len $shortest) -gt $(len $in) && shortest=$in
done
echo $shortest
}
firefox "file://$(find $docs -name index.html | shortest | grep . || find $docs -name $1*.html | shortest)"
fi fi
fi fi
exit $return exit $return

View file

@ -4,8 +4,9 @@
# depends: xargs realpath mpc # depends: xargs realpath mpc
# env: MUSIC # env: MUSIC
# TODO auto-convert unknown types with ffmpeg to flac rather than linking (wav, opus, ...) # TODO auto-convert unknown types with ffmpeg to flac rather than linking (wav, opus, ...)
MUSIC="${MUSIC:-$(cat ${XDG_CONFIG_HOME:-$HOME/.config}/mpd/mpd.conf | grep music_directory | cut -d'"' -f2 | sed "s|~|$HOME|")}" MPD_CONF=${XDG_CONFIG_HOME:-$HOME/.config}/mpd/mpd.conf
PLAYLISTS="$MUSIC/Playlists" MUSIC="${MUSIC:-$(cat $MPD_CONF | grep music_directory | cut -d'"' -f2 | sed "s|~|$HOME|")}"
PLAYLISTS="${$(cat $MPD_CONF | grep playlist_directory | cut -d'"' -f2 | sed "s|~|$HOME|"):-$MUSIC/Playlists}"
LINKS="$MUSIC/links" LINKS="$MUSIC/links"
if test "$1" = "-r" if test "$1" = "-r"
then shift then shift

View file

@ -14,8 +14,8 @@ do
continue continue
fi fi
$elevate find -H "$f" -maxdepth $(expr 1 \& "$f" = "/" \| 5 \& $# \> 0 \| 4) -type d -empty -name .stfolder -exec rm -div {} \; # $elevate find -H "$f" -maxdepth $(expr 1 \& "$f" = "/" \| 5 \& $# \> 0 \| 4) -type d -empty -name .stfolder -exec rm -div {} \;
$elevate find -H "$f" -maxdepth $(expr 1 \& "$f" = "/" \| 5 \& $# \> 0 \| 3) -not -name .stfolder \( -type d -o -type f \) -a -empty -printf 'Removing empty %p\n' -delete $elevate find -H "$f" -maxdepth $(expr 1 \& "$f" = "/" \| 5 \& $# \> 0 \| 3) -not \( -name '.stfolder' -o -name '.*keep' \) \( -type d -o -type f \) -a -empty -printf 'Removing empty %p\n' -delete
test $# -eq 0 && exit $? test $# -eq 0 && exit $?
if test -e "$f"; then if test -e "$f"; then
echo -n "$f ($(ls -A "$f" | head -3 | paste -s -d' ')) " >&2 && echo -n "$f ($(ls -A "$f" | head -3 | paste -s -d' ')) " >&2 &&

2
.local/bin/scripts/swaylist Executable file
View file

@ -0,0 +1,2 @@
#!/bin/sh -e
unbuffer swaymsg -t get_$1 | less -RF