diff --git a/README-old.md b/README-old.md new file mode 100644 index 0000000..6266e05 --- /dev/null +++ b/README-old.md @@ -0,0 +1,489 @@ +## obligatory personal dotfiles repository + +_(plus some little shell scripts)_ + +quick install for random boxes: + +``` +cd && curl -L https://github.com/notwa/rc/archive/master.tar.gz | tar zx && mv rc-master rc && rc/install +``` + +## stuff defined in RC files + +such as [.bashrc](/home/bashrc) and [.zshrc](/home/zshrc) +but mostly [-shrc](/home/-shrc) and [.streamcrap](/home/streamcrap) + +### reload + +exec's the current shell. +if it's `zsh`, it'll also recompile `.zshrc` so everything actually reloads. + +### ADDPATH + +adds a directory to `$PATH` if it isn't already there. + +### ify + +pipes a command through another command, so you can pass arguments at the end as usual. + +this is mainly useful for aliases. 99% of the time you'll use this with `less`. + +``` +$ alias ll="ify less ls -ACX --group-directories-first --color=force" +$ ll /etc +``` + +### has + +prints the result of `which` if the program is found, or shuts up and returns 1. + +``` +export CC="$(has clang || has clang-3.8 || has gcc)" +``` + +### revend + +reverses the 4-byte endianness of a file. this is an in-place operation! + +### nocom + +strips lines that begin with a `#` character. + +### jrep + +extracts ascii and japanese unicode characters. + +### bomb + +adds a Byte Order Mark to a file. + +### cleanse + +extracts readable ascii characters. + +### rot13 + +rot13 with rotated numbers as well. + +### unwrap + +unwraps text that was wrapped using double-newlines as spacing, +e.g. this readme file. + +### double + +prints every line twice. +prints every line twice. + +### join2 + +joins every other line. + +``` +$ echo "hey\nyou\nout there\nwe bringing it live" | join2 +hey you +out there we bringing it live +``` + +### katagana + +converts katakana to hiragana. +useful for translating [debug text from ancient games.](https://tcrf.net/) + +### picky + unused + +attempts to find any non-standard packages that were installed +with pacman so that you can make note to install them, later, +on a fresh installation. + +`unused` will print unused packages instead. + +### makepkgf + rakef + +make the freakin' package! + +### trash + +given a number of bytes, outputs binary garbage from `/dev/random`. + +### wipe + +(zsh) clears the screen and scrollback and prints an ugly horizontal line, +so you know with absolute certainty that you're looking +at the top of your history, and that your terminal's scrollback +didn't cap out and eat text. + +``` +$ wipe; ./configure && make +[insert a bajillion lines you'd never be able to find the top of unless + you piped it to a file or less or wiped scrollback beforehand] +``` + +### yt + +watches a youtube video through mpv with a bunch of audio filtering crap. + +can be given a full URL or just a video ID. +remaining arguments are passed to mpv. + +there exist several other variants for specific use cases. + +### twitch + +watches twitch streams through mpv with a bunch of audio filtering crap. + +give it a username. +remaining arguments are passed to mpv. + +## shell scripts + +most (but not all) stuff that's written for bash will work in zsh too. +if it just says `(sh)` then it'll probably work on any shell, +but might depend on GNU awk. + +[prezto]: https://github.com/sorin-ionescu/prezto + +### [arith](/sh/arith) + [hex](/sh/hex) + +(zsh) does arithmetic using the shell. + +``` +$ noglob arith 10**(6/20.) +1.9952623149688795 +$ hex 0x221EA8-0x212020 +0000FE88 +``` + +### [aur](/sh/aur) + +(bash) downloads, edits, makes, and installs packages from the [AUR.](//aur.archlinux.org) + +it's a little broken. + +``` +$ aur -eyoI cmdpack-uips applyppf +``` + +### [autosync](/sh/autosync) + +(zsh) combines inotifywait and rsync. + +sometimes nicer than ssh-ing into a server and running vim remotely. + +### [cdbusiest](/sh/cdbusiest) + [dbusiest](/sh/dbusiest) + +(zsh) cd to the directory with the most files in it (recursive). +dbusiest just outputs file counts without changing the directory. + +useful for finding the biggest stinkers when archiving. + +``` +$ cd +$ cdbusiest +197195 src +$ pwd +/home/notwa/src +``` + +### [colors](/sh/colors) + +(bash) print out all the foreground and background terminal color combinations. +a 76-character script! + +### [compandy](/sh/compandy) + +(zsh) a dumb thing to generate compand arguments for ffmpeg audio filters. + +kinda pointless now that acompressor is wildly supported. + +### [compile](/sh/compile) + +(zsh) a huge mess for compiling single-file C and C++ programs. + +supports gcc and clang on \*nix, +and mingw64 gcc, msvc clang, and regular msvc on Windows. +tested on x86\_64 and on ARMv7 as well. +does not support MacOS, maybe someday… + +defaults to gnu11 and gnu++1z as C and C++ standards respectively. +defaults to clang, gcc, and msvc in that order. + +`compile` attempts to guess the most sane switches for any program, +so that compilation may reduce to: + +```sh +# debug build +compile rd.c +compile debug rd.c +# debug build with warning/error flags defined in .-shrc +# (requires .zshrc for global alias expansion) +compile WHOA rd.c +# likewise for C++ +compile WHOA WELP rd.cc +compile WHOA WELP rd.cpp +# "derelease" build (release build with debug information) +compile derelease WHOA rd.c +# release build (with symbols stripped) +compile release WHOA rd.c +# hardened build (only useful on *nix) +compile hardened WHOA rd.c +# specifying compiler +compile gcc WHOA rd.c +compile msvc WHOA rd.c +compile release clang WHOA rd.c +# compile and execute (FIXME: writing to /tmp is a security concern) +compile derelease rd.c && /tmp/rd +``` + +also contains a helper function that dumps the commands required to +install a recent version of clang on an ubuntu machine. + +### [confirm](/sh/confirm) + +(bash/zsh) displays a simple yes/no prompt and returns 0 or 1 respectively. + +``` +$ confirm && echo yay || echo nay +Continue? [y/N] y +yay +$ confirm && echo yay || echo nay +Continue? [y/N] n +nay +``` + +### [days](/sh/days) + +(bash) compute days since a date. + +``` +$ days 'January 1 1970' +17229 +``` + +### [dfu](/sh/dfu) + +(sh) pretty-print `df` in GiB. + +``` +$ dfu +Filesystem Used Max Left Misc +/dev 0.00 0.45 0.45 0.00 +/ 12.13 13.75 1.62 0.75 +/boot 0.02 0.10 0.07 0.00 +/media/chibi 1275.73 1304.45 28.72 69.88 +``` + +### [disf](/sh/disf) + +disassembles a single function from an unstripped executable. +not robust. + +### [e](/sh/e) + +(zsh) wraps around $EDITOR to run as root if necessary. +this still needs some work to detect root-owned directories. + +``` +$ e /etc/sudoers +[sudo] password for notwa: +``` + +### [is\_empty](/sh/is_empty) + +(bash) returns 0 if the directory given by $1 is empty. + +### [isup](/sh/isup) + +(zsh) returns 0 if a website returns a 2xx HTTP code. + +``` +$ isup google.com && echo yay || echo nay +yay +$ isup fdhafdslkjgfjs.com && echo yay || echo nay +nay +``` + +### [lsarchive](/sh/lsarchive) + [unarchive](/sh/unarchive) + +(zsh) guess what these do. [via prezto.][prezto] +includes autocomplete files. + +### [minutemaid](/sh/minutemaid) + +(zsh) returns 0 if the current minute is divisible by a number. + +note that a minute is relative to the seconds since the epoch, +not the minute of the hour. + +``` +# crontab usage: +* * * * * minutemaid 9 cd repo && git pull # runs every nine minutes +``` + +### [monitor](/sh/monitor) + +(zsh) basically just `watch` reimplemented as a shell script. kinda nice though. + +### [now](/sh/now) + +(bash) prints a date-time (UTC) in a sortable format. +it takes a date or file as an argument, +else defaults to the current time. + +``` +$ now +2019-05-27_35083906 +$ now ~/sh/monitor +2017-03-14_82387259 +$ now '@1234567890' +2009-02-13_84690000 +``` + +### [pacbm](/sh/pacbm) + +(zsh) lists installed pacman packages by their filesize, and the sum, ascending. +requires `expac`. + +``` +$ pacbm | head -n -1 | tail -2 + 204.78M clang + 235.44M linux-firmware +``` + +### [pacman-list-disowned](/sh/pacman-list-disowned) + +(zsh?) lists disowned pacman files. this might take a while. +[via prezto.][prezto] + +### [pause](/sh/pause) + +(bash/zsh) pause; the companion script of `confirm`. + +``` +$ pause +Press any key to continue +$ +``` + +### [pre](/sh/pre) + +(bash/zsh) dumps all the #defines that `$CC $CPPFLAGS $CFLAGS $LDFLAGS` would result in. + +``` +$ pre | shuf | head -10 +#define __NO_MATH_INLINES 1 +#define __FLT_MIN_10_EXP__ (-37) +#define __INT_LEAST32_TYPE__ int +#define __FLT_MIN_EXP__ (-125) +#define __LDBL_MIN_EXP__ (-16381) +#define __UINT8_C_SUFFIX__ +#define __WINT_UNSIGNED__ 1 +#define __INT_LEAST16_FMTd__ "hd" +#define __UINT_FAST32_MAX__ 4294967295U +#define __SSE__ 1 +``` + +### [psbm](/sh/psbm) + +(sh) lists processes by their memory usage, and the sum, ascending. + +``` +$ psbm | head -n -1 | tail -2 + 185.08M 1163 chromium + 199.95M 1060 chromium +``` + +### [randir](/sh/randir) + +(sh) outputs a random directory in the working directory. + +``` +$ randir +./sh +``` + +### [rs](/sh/rs) + +(zsh) record screen. does not record audio. + +currently only works on Windows (gdigrab). +i'm sure there's something equivalent for Linux. + +### [sc](/sh/sc) + +(bash) uploads given files to a webserver and returns a direct link for sharing. +you'll want to tweak this if you use it yourself. + +has some extra logic for screenshots created by `scropt`. + +### [scramble](/sh/sc) + +(bash) scrambles text in a predictable way using regex. + +(bhas) sacbremls ttex in a pdrceailtbe way unsig reegx. + +### [screeny](/sh/screeny) + [unscreen](/sh/unscreen) + +(zsh) sets up and detaches a screen for running daemons as other users, etc. + +this will close any existing screens of the same name using its companion script, unscreen. + +e.g. run znc as user znc in a screen called znc: `screeny znc znc znc -f`. znc! + + + +### [scropt](/sh/scropt) + +(bash) runs `scrot` through `optipng` and saves to `~/play/$(now).png`. + +``` +$ ~/sh/sc $(~/sh/scropt -s -d0.5) +``` + +### [similar](/sh/similar) + +(sh) highlights similarities between adjacent lines. + +### [slit](/sh/slit) + [slitt](/sh/slitt) + +(zsh) views specific columns of text. +[via pretzo.][prezto] + +``` +$ df | slit 1 5 +Filesystem Use% +dev 0% +run 1% +/dev/sda6 30% +tmpfs 3% +tmpfs 0% +tmpfs 1% +tmpfs 1% +``` + +`slitt` is exactly the same, but it works with tabs instead of spaces. + +### [sram](/sh/sram) + +(zsh) converts between a couple saveram formats for N64 emulators. + +### [sv](/sh/sv) + +(bash) collects the lastmost value of every key. +takes the field separator as an argument. + +``` +echo "this=that\nthem=those\nthis=cat" | sv = +this=cat +them=those +``` + +### [tpad](/sh/tpad) + +(bash) adds a 1px transparent border around an image +so that twitter doesn't mangle it into a jpg. +sadly, this trick doesn't work anymore. + +### [trunc](/sh/trunc) + +(bash) truncates text to fit within your terminal using the unicode character `…`. diff --git a/README.md b/README.md index 6266e05..2be02cf 100644 --- a/README.md +++ b/README.md @@ -10,480 +10,8 @@ cd && curl -L https://github.com/notwa/rc/archive/master.tar.gz | tar zx && mv r ## stuff defined in RC files -such as [.bashrc](/home/bashrc) and [.zshrc](/home/zshrc) -but mostly [-shrc](/home/-shrc) and [.streamcrap](/home/streamcrap) - -### reload - -exec's the current shell. -if it's `zsh`, it'll also recompile `.zshrc` so everything actually reloads. - -### ADDPATH - -adds a directory to `$PATH` if it isn't already there. - -### ify - -pipes a command through another command, so you can pass arguments at the end as usual. - -this is mainly useful for aliases. 99% of the time you'll use this with `less`. - -``` -$ alias ll="ify less ls -ACX --group-directories-first --color=force" -$ ll /etc -``` - -### has - -prints the result of `which` if the program is found, or shuts up and returns 1. - -``` -export CC="$(has clang || has clang-3.8 || has gcc)" -``` - -### revend - -reverses the 4-byte endianness of a file. this is an in-place operation! - -### nocom - -strips lines that begin with a `#` character. - -### jrep - -extracts ascii and japanese unicode characters. - -### bomb - -adds a Byte Order Mark to a file. - -### cleanse - -extracts readable ascii characters. - -### rot13 - -rot13 with rotated numbers as well. - -### unwrap - -unwraps text that was wrapped using double-newlines as spacing, -e.g. this readme file. - -### double - -prints every line twice. -prints every line twice. - -### join2 - -joins every other line. - -``` -$ echo "hey\nyou\nout there\nwe bringing it live" | join2 -hey you -out there we bringing it live -``` - -### katagana - -converts katakana to hiragana. -useful for translating [debug text from ancient games.](https://tcrf.net/) - -### picky + unused - -attempts to find any non-standard packages that were installed -with pacman so that you can make note to install them, later, -on a fresh installation. - -`unused` will print unused packages instead. - -### makepkgf + rakef - -make the freakin' package! - -### trash - -given a number of bytes, outputs binary garbage from `/dev/random`. - -### wipe - -(zsh) clears the screen and scrollback and prints an ugly horizontal line, -so you know with absolute certainty that you're looking -at the top of your history, and that your terminal's scrollback -didn't cap out and eat text. - -``` -$ wipe; ./configure && make -[insert a bajillion lines you'd never be able to find the top of unless - you piped it to a file or less or wiped scrollback beforehand] -``` - -### yt - -watches a youtube video through mpv with a bunch of audio filtering crap. - -can be given a full URL or just a video ID. -remaining arguments are passed to mpv. - -there exist several other variants for specific use cases. - -### twitch - -watches twitch streams through mpv with a bunch of audio filtering crap. - -give it a username. -remaining arguments are passed to mpv. +hold on, this needs to be rewritten. ## shell scripts -most (but not all) stuff that's written for bash will work in zsh too. -if it just says `(sh)` then it'll probably work on any shell, -but might depend on GNU awk. - -[prezto]: https://github.com/sorin-ionescu/prezto - -### [arith](/sh/arith) + [hex](/sh/hex) - -(zsh) does arithmetic using the shell. - -``` -$ noglob arith 10**(6/20.) -1.9952623149688795 -$ hex 0x221EA8-0x212020 -0000FE88 -``` - -### [aur](/sh/aur) - -(bash) downloads, edits, makes, and installs packages from the [AUR.](//aur.archlinux.org) - -it's a little broken. - -``` -$ aur -eyoI cmdpack-uips applyppf -``` - -### [autosync](/sh/autosync) - -(zsh) combines inotifywait and rsync. - -sometimes nicer than ssh-ing into a server and running vim remotely. - -### [cdbusiest](/sh/cdbusiest) + [dbusiest](/sh/dbusiest) - -(zsh) cd to the directory with the most files in it (recursive). -dbusiest just outputs file counts without changing the directory. - -useful for finding the biggest stinkers when archiving. - -``` -$ cd -$ cdbusiest -197195 src -$ pwd -/home/notwa/src -``` - -### [colors](/sh/colors) - -(bash) print out all the foreground and background terminal color combinations. -a 76-character script! - -### [compandy](/sh/compandy) - -(zsh) a dumb thing to generate compand arguments for ffmpeg audio filters. - -kinda pointless now that acompressor is wildly supported. - -### [compile](/sh/compile) - -(zsh) a huge mess for compiling single-file C and C++ programs. - -supports gcc and clang on \*nix, -and mingw64 gcc, msvc clang, and regular msvc on Windows. -tested on x86\_64 and on ARMv7 as well. -does not support MacOS, maybe someday… - -defaults to gnu11 and gnu++1z as C and C++ standards respectively. -defaults to clang, gcc, and msvc in that order. - -`compile` attempts to guess the most sane switches for any program, -so that compilation may reduce to: - -```sh -# debug build -compile rd.c -compile debug rd.c -# debug build with warning/error flags defined in .-shrc -# (requires .zshrc for global alias expansion) -compile WHOA rd.c -# likewise for C++ -compile WHOA WELP rd.cc -compile WHOA WELP rd.cpp -# "derelease" build (release build with debug information) -compile derelease WHOA rd.c -# release build (with symbols stripped) -compile release WHOA rd.c -# hardened build (only useful on *nix) -compile hardened WHOA rd.c -# specifying compiler -compile gcc WHOA rd.c -compile msvc WHOA rd.c -compile release clang WHOA rd.c -# compile and execute (FIXME: writing to /tmp is a security concern) -compile derelease rd.c && /tmp/rd -``` - -also contains a helper function that dumps the commands required to -install a recent version of clang on an ubuntu machine. - -### [confirm](/sh/confirm) - -(bash/zsh) displays a simple yes/no prompt and returns 0 or 1 respectively. - -``` -$ confirm && echo yay || echo nay -Continue? [y/N] y -yay -$ confirm && echo yay || echo nay -Continue? [y/N] n -nay -``` - -### [days](/sh/days) - -(bash) compute days since a date. - -``` -$ days 'January 1 1970' -17229 -``` - -### [dfu](/sh/dfu) - -(sh) pretty-print `df` in GiB. - -``` -$ dfu -Filesystem Used Max Left Misc -/dev 0.00 0.45 0.45 0.00 -/ 12.13 13.75 1.62 0.75 -/boot 0.02 0.10 0.07 0.00 -/media/chibi 1275.73 1304.45 28.72 69.88 -``` - -### [disf](/sh/disf) - -disassembles a single function from an unstripped executable. -not robust. - -### [e](/sh/e) - -(zsh) wraps around $EDITOR to run as root if necessary. -this still needs some work to detect root-owned directories. - -``` -$ e /etc/sudoers -[sudo] password for notwa: -``` - -### [is\_empty](/sh/is_empty) - -(bash) returns 0 if the directory given by $1 is empty. - -### [isup](/sh/isup) - -(zsh) returns 0 if a website returns a 2xx HTTP code. - -``` -$ isup google.com && echo yay || echo nay -yay -$ isup fdhafdslkjgfjs.com && echo yay || echo nay -nay -``` - -### [lsarchive](/sh/lsarchive) + [unarchive](/sh/unarchive) - -(zsh) guess what these do. [via prezto.][prezto] -includes autocomplete files. - -### [minutemaid](/sh/minutemaid) - -(zsh) returns 0 if the current minute is divisible by a number. - -note that a minute is relative to the seconds since the epoch, -not the minute of the hour. - -``` -# crontab usage: -* * * * * minutemaid 9 cd repo && git pull # runs every nine minutes -``` - -### [monitor](/sh/monitor) - -(zsh) basically just `watch` reimplemented as a shell script. kinda nice though. - -### [now](/sh/now) - -(bash) prints a date-time (UTC) in a sortable format. -it takes a date or file as an argument, -else defaults to the current time. - -``` -$ now -2019-05-27_35083906 -$ now ~/sh/monitor -2017-03-14_82387259 -$ now '@1234567890' -2009-02-13_84690000 -``` - -### [pacbm](/sh/pacbm) - -(zsh) lists installed pacman packages by their filesize, and the sum, ascending. -requires `expac`. - -``` -$ pacbm | head -n -1 | tail -2 - 204.78M clang - 235.44M linux-firmware -``` - -### [pacman-list-disowned](/sh/pacman-list-disowned) - -(zsh?) lists disowned pacman files. this might take a while. -[via prezto.][prezto] - -### [pause](/sh/pause) - -(bash/zsh) pause; the companion script of `confirm`. - -``` -$ pause -Press any key to continue -$ -``` - -### [pre](/sh/pre) - -(bash/zsh) dumps all the #defines that `$CC $CPPFLAGS $CFLAGS $LDFLAGS` would result in. - -``` -$ pre | shuf | head -10 -#define __NO_MATH_INLINES 1 -#define __FLT_MIN_10_EXP__ (-37) -#define __INT_LEAST32_TYPE__ int -#define __FLT_MIN_EXP__ (-125) -#define __LDBL_MIN_EXP__ (-16381) -#define __UINT8_C_SUFFIX__ -#define __WINT_UNSIGNED__ 1 -#define __INT_LEAST16_FMTd__ "hd" -#define __UINT_FAST32_MAX__ 4294967295U -#define __SSE__ 1 -``` - -### [psbm](/sh/psbm) - -(sh) lists processes by their memory usage, and the sum, ascending. - -``` -$ psbm | head -n -1 | tail -2 - 185.08M 1163 chromium - 199.95M 1060 chromium -``` - -### [randir](/sh/randir) - -(sh) outputs a random directory in the working directory. - -``` -$ randir -./sh -``` - -### [rs](/sh/rs) - -(zsh) record screen. does not record audio. - -currently only works on Windows (gdigrab). -i'm sure there's something equivalent for Linux. - -### [sc](/sh/sc) - -(bash) uploads given files to a webserver and returns a direct link for sharing. -you'll want to tweak this if you use it yourself. - -has some extra logic for screenshots created by `scropt`. - -### [scramble](/sh/sc) - -(bash) scrambles text in a predictable way using regex. - -(bhas) sacbremls ttex in a pdrceailtbe way unsig reegx. - -### [screeny](/sh/screeny) + [unscreen](/sh/unscreen) - -(zsh) sets up and detaches a screen for running daemons as other users, etc. - -this will close any existing screens of the same name using its companion script, unscreen. - -e.g. run znc as user znc in a screen called znc: `screeny znc znc znc -f`. znc! - - - -### [scropt](/sh/scropt) - -(bash) runs `scrot` through `optipng` and saves to `~/play/$(now).png`. - -``` -$ ~/sh/sc $(~/sh/scropt -s -d0.5) -``` - -### [similar](/sh/similar) - -(sh) highlights similarities between adjacent lines. - -### [slit](/sh/slit) + [slitt](/sh/slitt) - -(zsh) views specific columns of text. -[via pretzo.][prezto] - -``` -$ df | slit 1 5 -Filesystem Use% -dev 0% -run 1% -/dev/sda6 30% -tmpfs 3% -tmpfs 0% -tmpfs 1% -tmpfs 1% -``` - -`slitt` is exactly the same, but it works with tabs instead of spaces. - -### [sram](/sh/sram) - -(zsh) converts between a couple saveram formats for N64 emulators. - -### [sv](/sh/sv) - -(bash) collects the lastmost value of every key. -takes the field separator as an argument. - -``` -echo "this=that\nthem=those\nthis=cat" | sv = -this=cat -them=those -``` - -### [tpad](/sh/tpad) - -(bash) adds a 1px transparent border around an image -so that twitter doesn't mangle it into a jpg. -sadly, this trick doesn't work anymore. - -### [trunc](/sh/trunc) - -(bash) truncates text to fit within your terminal using the unicode character `…`. +hold on, this needs to be rewritten. diff --git a/home/-shrc b/home/-shrc index 742b9c2..4e0a566 100644 --- a/home/-shrc +++ b/home/-shrc @@ -1,27 +1,26 @@ #!/usr/bin/env zsh -# for zsh, bash, and ash. +# for zsh, bash, and dash. [ -n "$ZSH_VERSION" -o -n "$BASH" ] && FANCY=1 || FANCY=0 # {{{1 utilities -ify() { - [ $# -ge 2 ] || return - local ex=$1 - shift - $@ | $ex -} +#SOURCING=$((SOURCING+1)) +#. ~/sh/ify +#. ~/sh/has +#. ~/sh/maybesudo +#SOURCING=$((SOURCING-1)) -if which sudo >/dev/null 2>/dev/null; then +# not available in busybox: +#exports() { +# printenv | cut -d= -f1 +#} + +if has sudo >/dev/null; then alias maybesudo=sudo else - maybesudo() { - while [[ "$1" == -* ]]; do shift; done - "$@" - } + alias maybesudo="maybesudo_ " # allows aliases within the alias fi -has() { which "$1" >&/dev/null && which "$1"; } - ADDPATH() { new="$(readlink -f "$1")" if [ ! -d "$new" ]; then @@ -54,10 +53,12 @@ umask 022 ADDPATH "$HOME/opt/local/bin" [ -n "$MSYSTEM" ] && ADDPATH "/c/path" -[ -n "$MSYSTEM" ] && ADDPATH "/c/path/mpv-shinchiro" 2>/dev/null [ -n "$MSYSTEM" ] && ADDPATH "/c/Program Files/7-Zip-Zstandard" 2>/dev/null [ -n "$MSYSTEM" ] && ADDPATH ~win"/Desktop/_programs/rakudo-2021.06/bin" 2>/dev/null +# for dash: +export ENV="$HOME/.-shrc" + # https://bugs.launchpad.net/ubuntu/+source/openssh/+bug/1380084 export SSH_AUTH_SOCK=0 @@ -130,13 +131,11 @@ alias gb='git --no-pager branch' # being specific {{{2 [ -e "$HOME/python3" ] && alias py="~/python3" || alias py="python3" -alias maybesudo="maybesudo " # allows aliases within the alias alias pip="maybesudo -H pip3" alias revend='objcopy -I binary -O binary --reverse-bytes=4' alias aur="BUILDDIR=$HOME/src $HOME/sh/aur -jj" alias clone='maybesudo rsync -aHA --info=progress2 --no-i-r' alias aligntabs="column -t -s$'\t'" -alias pippy="maybesudo py -m pip install --upgrade --upgrade-strategy only-if-needed" alias crawla='ssh joshua@crawl.akrasiac.org -i ~/.ssh/crawl' alias crawlz='ssh crawl@crawl.develz.org -i ~/.ssh/crawl' @@ -165,78 +164,13 @@ alias join2='paste -d" " - -' alias katagana='perlu -MUnicode::Normalize -pe'"'"'$_=NFKD($_)=~y/ァ-ヶ /ぁ-ゖ /r'"'" alias picky='{ pacman -Qgq base base-devel xorg xorg-drivers xfce4 | double; pacman -Qeq; } | sort | uniq -u' alias unused='{ pacman -Qtq; pacman -Qeq | double; } | sort | uniq -u' -alias makepkgf='maybesudo -u $USER makepkg -Af --skipchecksums --skippgpcheck' +alias makepkgf='makepkg -Af --skipchecksums --skippgpcheck' alias rakef='rake && gem build *.gemspec && gem install *.gem' -trash() { - dd status=none if=/dev/random bs=1 count="$1" -} - -explore() { - explorer "$(cygpath -w "${1:-.}")" -} - -pegg() { - pippy "https://github.com/$1/$2/tarball/${3:-master}#egg=$2" -} - -bak() { - if [ -s "$1.bak" ]; then - bak "$1.bak" || return $? - fi - cp -p "$1" "$1.bak" -} - -baks() { # backup, timestamped - local fp="$1" - local now="$(date -u '+%y-%m-%d_%H-%M-%S')" - local bak="$fp.$now.bak" - if [ -s "$bak" ]; then - printf "how in the hell? %s already exists\n" "$bak" >&2 - return 1 - fi - cp -p "$fp" "$bak" -} - eashare() { # NOTE: this only works on MSYS2 for now. # NOTE: i lied, this doesn't work anymore. idk local dest="${1##*/}" eaput "$1" || return 1 - printf "%s" "$_REMOTE_DOMAIN/$_REMOTE_DIR/$dest" > /dev/clipboard + printf "%s" "$_REMOTE_DOMAIN/$_REMOTE_DIR/$dest" >> /dev/clipboard } - -echo2() { - local IFS=" " - printf "%s\n" "$*" >&2 -} - -note() { - local IFS=" " - printf "\033[1m%s\033[0m\n" "$*" >&2 -} - -stfu() { - local ret=0 - "$@" > .stdout 2> .stderr || ret=$? - if test "$ret" -ne 0; then - printf "command failed with exit status %s:\n" "$ret" >&2 - echo2 "$@" - echo2 - note '$ tail -n 20 .stdout' - tail -n 20 .stdout >&2 - echo2 - note '$ tail -n 20 .stderr' - tail -n 20 .stderr >&2 - return "$ret" - fi -} - -# sources {{{1 - -if [ $FANCY -eq 1 ]; then - source ~/.arrays - source ~/.streamcrap -fi - -. ~/.ea diff --git a/home/bashrc b/home/bashrc index 4c4d7ba..4a49c8b 100644 --- a/home/bashrc +++ b/home/bashrc @@ -1,6 +1,16 @@ [ -z "$PS1" ] && return +echo START.bashrc -if [[ "$TERM" == "rxvt-unicode-256color" ]]; then +# trace: (on ubuntu) +# /etc/bash.bashrc +#=> /etc/debian_chroot +#=> /.sudo_as_admin_successful +#=> /.hushlogin +#=> /usr/lib/command-not-found +#=> /usr/share/command-not-found/command-not-found +# ~/.bashrc + +if [ "$TERM" = rxvt-unicode-256color ]; then export TERM=xterm-256color fi @@ -28,9 +38,25 @@ ret_fail="$_title${Cfail}$_line1\n${Cfail}$_line2" PROMPT_COMMAND='[ $? = 0 ] && PS1=${ret_succ} || PS1=${ret_fail}' +SOURCING=$((SOURCING+1)) +. ~/sh/echo2 +. ~/sh/note +. ~/sh/stfu +. ~/sh/has +SOURCING=$((SOURCING-1)) + +echo START.-shrc . ~/.-shrc +echo FINISH.-shrc + +SOURCING=$((SOURCING+1)) +. ~/sh/arrays +. ~/sh/streamcrap +SOURCING=$((SOURCING-1)) # this doesn't work for all scripts at the moment, but ADDPATH "$HOME/sh" +# TODO: respect initctl like in .zshrc. alias reload='cd; exec bash' +echo FINISH.bashrc diff --git a/home/prezto-compinit b/home/prezto-compinit index 96b1257..f7ea243 100644 --- a/home/prezto-compinit +++ b/home/prezto-compinit @@ -1,16 +1,6 @@ # this code comes from prezto: # https://github.com/sorin-ionescu/prezto/blob/master/modules/completion/init.zsh -setopt COMPLETE_IN_WORD # Complete from both ends of a word. -setopt ALWAYS_TO_END # Move cursor to the end of a completed word. -setopt PATH_DIRS # Perform path search even on command names with slashes. -setopt AUTO_MENU # Show completion menu on a successive tab press. -setopt AUTO_LIST # Automatically list choices on ambiguous completion. -setopt AUTO_PARAM_SLASH # If completed parameter is a directory, add a trailing slash. -setopt EXTENDED_GLOB # Needed for file modification glob modifiers with compinit -unsetopt MENU_COMPLETE # Do not autoselect the first completion entry. -unsetopt FLOW_CONTROL # Disable start/stop characters in shell editor. - # Load and initialize the completion system ignoring insecure directories with a # cache time of 20 hours, so it should almost always regenerate the first time a # shell is opened each day. diff --git a/home/zshrc b/home/zshrc index 5d8cbca..61b795c 100644 --- a/home/zshrc +++ b/home/zshrc @@ -1,61 +1,84 @@ +echo START.PROFILE [ -e /etc/profile ] && emulate sh -c "source /etc/profile" +echo FINISH.PROFILE -if [ "$SHLVL" -le 1 ] && [[ "$TERM" != screen* ]] && [[ "$TERM" != tmux* ]] -then - if which tmux >/dev/null 2>/dev/null; then - # create a new session called "what" or attach if it already exists - LANG="en_US.UTF-8" TZ=":/etc/localtime" tmux new -A -s what && exit - echo "tmux died ($?), continuing..." - fi -fi - -setopt extended_glob # required for various scripts in this file and otherwise - -local host="${(L)HOST}" -fpath=(~/sh $fpath) - -function { - local f - for f in ~/sh/^([_.]*)(N^/:t); do - if [[ "$f" == fasd ]]; then - emulate sh -c "autoload -U $f" - else - autoload -Uz $f - fi - done -} +# trace: +# /etc/zsh/zshenv{.zwc,} +# ~/.zshenv{.zwc,} +# /etc/zsh/zshrc{.zwc,} HISTFILE=~/.histfile HISTSIZE=99999 SAVEHIST=99999 +TIMEFMT=$'\e[93m%*U/%*E cpu/real (%P), %MM mem:\e[36m %J\e[0m' +DIRSTACKSIZE=24 -function { - local -a opts - opts=( no_beep - append_history share_history # across sessions - hist_expire_dups_first # sharing/appending will result in dups - hist_ignore_dups # don't push lines identical to the previous - hist_ignore_space # don't push lines beginning with spaces - auto_cd # exec a dir to cd - auto_pushd # cd acts as pushd - no_match # error on bad tab-complete - check_jobs notify # automatic job reporting - chase_links # cd into link resolves link - complete_aliases # allow original command completion within alias - complete_in_word # enable tab completion when cursor between words - rc_quotes # 'you''re dumb' like "you're dumb" - brace_ccl # for character ranges like {a-z} - ksh_typeset # treat `local x=$(cmd)` and `x=$(cmd)` the same - ) - setopt "${opts[@]}" -} +setopt always_to_end # Move cursor to the end of a completed word. +setopt append_history +setopt share_history # across sessions +setopt auto_cd # exec a dir to cd +setopt auto_list # Automatically list choices on ambiguous completion. +setopt auto_menu # Show completion menu on a successive tab press. +setopt auto_param_slash # If completed parameter is a directory, add a trailing slash. +setopt auto_pushd # cd acts as pushd +setopt brace_ccl # for character ranges like {a-z} +setopt chase_links # cd into link resolves link +setopt check_jobs notify # automatic job reporting +setopt complete_aliases # allow original command completion within alias +setopt complete_in_word # enable tab completion from any characters in a word +setopt extended_glob # required for various scripts in this file and otherwise +setopt hist_expire_dups_first # sharing/appending will result in dups +setopt hist_ignore_dups # don't push lines identical to the previous +setopt hist_ignore_space # don't push lines beginning with spaces +setopt ksh_typeset # treat `local x=$(cmd)` and `local x="$(cmd)"` the same +setopt no_beep +setopt no_match # error on bad tab-complete +setopt path_dirs # Perform path search even on command names with slashes. +setopt rc_quotes # 'it''s okay' becomes "it's okay" +unsetopt flow_control # Disable start/stop characters in shell editor. +unsetopt menu_complete # Do not autoselect the first completion entry. + +if [ "$SHLVL" -le 1 ] && [ "$TERM" = "${TERM#screen}" ] && [ "$TERM" = "${TERM#tmux}" ]; then + if (( $+commands[tmux] )); then + # create a new session called "what" or attach if it already exists + LANG="en_US.UTF-8" TZ=":/etc/localtime" tmux new -A -s what && exit + printf '\e[91m\ntmux died (%i), continuing...\n\e[0m\n' $? + fi +fi autoload -U zmv - -zmodload zsh/mathfunc +autoload -U zrecompile autoload -Uz zcalc +autoload edit-command-line +autoload history-search-end +zmodload zsh/mathfunc + +echo START.SH + +function { + local a b + + [ -s ~/.sh.sha1 ] && read -r b < ~/.sh.sha1 + ( cd ~/sh \ + && print -l '#!/usr/bin/env false' '[ "${SOURCING:-0}" -gt 0 ] || exit 1' '' \ + | cat - $(grep -lF 'YES_ZSH' *(.)) \ + | tee ~/.sh \ + ) | sha1sum - \ + | cut -d' ' -f1 \ + | tee ~/.sh.sha1 \ + | read -r a + + [ "$a" = "$b" ] && touch ~/.sh.zwc || zrecompile -p ~/.sh +} + +SOURCING=$((SOURCING+1)) +. ~/.sh +SOURCING=$((SOURCING-1)) + +echo FINISH.SH + +local fuzzy=`has fzy || print` -DIRSTACKSIZE=24 dirprev() { pushd -q +1 zle reset-prompt @@ -72,11 +95,17 @@ dirup() { precmd } dirview() { - # TODO: print under prompt if possible, - # truncate and columnize print - dirs -v + if [ -n "$fuzzy" ]; then + local d="$(dirs -pl | awk '!seen[$0]++' | "$fuzzy")" + cd "$d" + else + # TODO: print under prompt if possible, + # truncate and columnize + dirs -v + fi zle reset-prompt + precmd } for x (dirprev dirnext dirup dirview) zle -N $x @@ -84,7 +113,6 @@ for x (dirprev dirnext dirup dirview) zle -N $x bindkey -e # emacs-style keybinds # oh thank god: http://blog.samsonis.me/2013/12/bash-like-history-search-functionality-for-zsh/ -autoload history-search-end zle -N history-beginning-search-backward-end history-search-end zle -N history-beginning-search-forward-end history-search-end bindkey '^[[A' history-beginning-search-backward-end # up @@ -133,39 +161,26 @@ bindkey -s '^[[6;2~' '\a' # shift+PgDn, do nothing, already at bottom (tmux) #bindkey -s '^[[1;8C' '\a' # ctrl+alt+shift+arrow #bindkey -s '^[[1;8D' '\a' # ctrl+alt+shift+arrow -autoload edit-command-line zle -N edit-command-line # new widget of the same function name bindkey '^Xe' edit-command-line # ctrl+x -> e +echo START.- . ~/.-shrc +echo FINISH.- alias -g OMFG="1>/dev/null" alias -g STFU="2>/dev/null" alias -g WHOA='${whoa[@]}' alias -g WELP='${welp[@]}' -wipe() { - clear - clear # twice because mintty is weird - echo "\e[30m\e[107m" - printf "*%.0s" {1..$COLUMNS} - echo "\e[0m\n" -} - alias sc="~/sh/sc" # only runs in bash (for now), so be explicit with path alias pl="print -l" # not in -shrc because this only makes sense with zsh -function tw() { # needs the "function" keyword or else zsh complains +function tw() { # needs the "function" keyword or else zsh complains twitch "$@" OMFG STFU & } -sum() { - local sum=0 - for i; do - sum=$((sum+i)) - done - echo "$sum" -} +local host="${(L)HOST}" function { local t="${TERM%%-*}" @@ -193,67 +208,37 @@ function { local good=42 # green local bad=41 # red - [[ "$host" == neobanshee ]] && good=46 # cyan - [[ "$host" == spectre ]] && good=47 # white - [[ "$host" == wraith ]] && good=43 # yellow - [[ "$host" == sabotage ]] && good=45 # magenta + [ "$host" = neobanshee ] && good=46 # cyan + [ "$host" = spectre ] && good=47 # white + [ "$host" = wraith ] && good=43 # yellow + [ "$host" = sabotage ] && good=45 # magenta # NOTE: i had ${s}10${e} here before, is it still necessary? PROMPT="%{$reset${s}%(?.${good}.${bad})${e}${s}97${e}%}%#%{$reset%} " } -TIMEFMT=$'\e[93m%*U/%*E cpu/real (%P), %MM mem:\e[36m %J\e[0m' - reload() { + # initctl has a "reload" symlink, but i'm already too used to typing this. + # to remedy this, when args are passed, invoke initctl instead. + if [ $# -gt 0 ]; then + "${commands[reload]:?initctl is missing}" "$@" + return $? + fi + # this doesn't seem to help with _vim_files errors, eh. # you wanna rm .zcompdump first, then exit. that's why! cd ~ - autoload -U zrecompile - [ -f .zshrc ] && zrecompile -p .zshrc - rm -f .zcompdump - [ -f .zshrc.zwc.old ] && rm -f .zshrc.zwc.old - [ -f .zcompdump.zwc.old ] && rm -f .zcompdump.zwc.old + if [ $? -eq 0 ]; then + [ -f .zshrc ] && zrecompile -p .zshrc + [ -f .prezto-compinit ] && zrecompile -p .prezto-compinit + rm -f .zcompdump .zshrc.zwc.old .zcompdump.zwc.old + fi exec zsh # reload shell, inheriting environment } -if [[ "$host" == "spectre" ]] || [[ "$host" == *"banshee" ]]; then - # via https://github.com/whjvenyl/fasd -# if [ ! -s "$HOME/.fasd_init" ]; then -# # note that posix-alias defines aliases for the following: -# # a s d f sd sf z zz -# fasd --init \ -# posix-alias \ -# zsh-hook zsh-ccomp zsh-ccomp-install \ -# zsh-wcomp zsh-wcomp-install \ -# >| "$HOME/.fasd_init" -# fi -# source "$HOME/.fasd_init" -# alias v="f -e $EDITOR" -fi - -baknow() { cp -ip "$1" "$1.$(now "$1").bak"; } - -ff() { find "${1:-.}" -type f | fzy } - -cutv() { - local nel="${1?missing length parameter}" - local f="${2?missing file parameter}" - local g="cut."${${f##*/}%.*}".mp4" - ffmpeg -hide_banner -i "$f" 2>&1 \ - | grep Duration \ - | grep -Eo '[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9]' | awk -F: '{print (($1*60+$2)*60+$3)+$4/100}' | read len - [[ "$len" -gt 0 ]] || return 1 - echo "$g" - ffmpeg \ - -hide_banner -loglevel warning -stats -stats_period 5 \ - -i "$f" -ss "$(((nel&2; return 1; } + local ret=0 + for f; do + if [ -s "$f.bak" ]; then + bak "$f.bak" || ret=1 + fi + cp -p "$f" "$f.bak" + done + return $ret +} + +[ "${SOURCING:-0}" -gt 0 ] || bak "$@" diff --git a/sh/baknow b/sh/baknow new file mode 100644 index 0000000..4c10550 --- /dev/null +++ b/sh/baknow @@ -0,0 +1,8 @@ +#!/usr/bin/env sh +# YES_ZSH + +baknow() { + cp -ip "${1:?missing file argument}" "$1.$(now "$1").bak" +} + +[ "${SOURCING:-0}" -gt 0 ] || baknow "$@" diff --git a/sh/baks b/sh/baks new file mode 100644 index 0000000..c28400f --- /dev/null +++ b/sh/baks @@ -0,0 +1,22 @@ +#!/usr/bin/env sh +# YES_ZSH + +#SOURCING=$((SOURCING+1)) +#. ~/sh/note # FIXME: don't do this? +#SOURCING=$((SOURCING-1)) + +baks() { # backup, timestamped + local ret=0 + for fp; do + local now="$(date -u '+%y-%m-%d_%H-%M-%S')" + local bak="$fp.$now.bak" + if [ -s "$bak" ]; then + note "how in the hell?" "$bak" "already exists" + ret=1 + fi + cp -p "$fp" "$bak" || ret=1 + done + return $ret +} + +[ "${SOURCING:-0}" -gt 0 ] || baks "$@" diff --git a/sh/bin b/sh/bin new file mode 100644 index 0000000..bb8cfe4 --- /dev/null +++ b/sh/bin @@ -0,0 +1,10 @@ +#!/usr/bin/env zsh +# YES_ZSH + +bin() { + local a="$(($@))" + let 'a=(((((((((((((a + 0x0FFFFF80) & 0x1000007F) + 0x00FFFFC0) & 0x1100003F) + 0x000FFFE0) & 0x1110001F) + 0x0000FFF0) & 0x1111000F) + 0x00000FF8) & 0x11111007) + 0x000000FC) & 0x11111103) + 0x0000000E) & 0x11111111' + printf "%08X\n" "$a" +} + +[ "${SOURCING:-0}" -gt 0 ] || bin "$@" diff --git a/sh/cdbusiest b/sh/cdbusiest index 6875226..2ccf324 100755 --- a/sh/cdbusiest +++ b/sh/cdbusiest @@ -1,5 +1,11 @@ #!/usr/bin/env zsh -dbusiest | read -r c d -[ -z $c ] && return 1 -<<<"$c $d" -cd $d +# YES_ZSH + +cdbusiest() { + dbusiest | read -r c d + [ -z $c ] && return 1 + <<<"$c $d" + cd $d +} + +[ "${SOURCING:-0}" -gt 0 ] || cdbusiest "$@" diff --git a/sh/colors b/sh/colors index 4fe1e2b..c57219f 100755 --- a/sh/colors +++ b/sh/colors @@ -1,2 +1,8 @@ #!/usr/bin/env bash -printf "\e[%dm$(printf " \e[%dm mV \e[40m" {4,10}{0..7})\e[0m\n" {3,9}{0..7} +# YES_ZSH + +colors() { + printf "\e[%dm$(printf " \e[%dm mV \e[40m" {4,10}{0..7})\e[0m\n" {3,9}{0..7} +} + +[ "${SOURCING:-0}" -gt 0 ] || colors "$@" diff --git a/sh/compandy b/sh/compandy index b20a229..b1d5e78 100755 --- a/sh/compandy +++ b/sh/compandy @@ -1,5 +1,7 @@ #!/usr/bin/zsh +# YES_ZSH # script to make sense of compand's parameters + compandy() { if [ $# -eq 0 ]; then echo -E "usage: $0 {threshold} {ratio} [attack] [release] [knee] [gain] [delay]" @@ -17,4 +19,5 @@ compandy() { printf "compand=%.5f|%.5f:%.5f|%.5f:%.1f/%.1f|0/%.2f:%.2f:%.2f:%.1f:%.5f" \ $atk $atk $rel $rel $thresh $thresh $end $knee $gain $start $delay } -compandy "$@" + +[ "${SOURCING:-0}" -gt 0 ] || compandy "$@" diff --git a/sh/compile b/sh/compile index b7fd207..9af05e1 100755 --- a/sh/compile +++ b/sh/compile @@ -1,4 +1,5 @@ #!/usr/bin/env zsh +# YES_ZSH # I'll just leave this here... __setup_clang_ubuntu() { @@ -298,4 +299,4 @@ compile() { $compiler $std ${final_flags[@]} $file ${libraries[@]} ${warnings[@]} $outflag $out } -compile "$@" +[ "${SOURCING:-0}" -gt 0 ] || compile "$@" diff --git a/sh/confirm b/sh/confirm index a74bb14..4b3267d 100755 --- a/sh/confirm +++ b/sh/confirm @@ -1,6 +1,9 @@ #!/usr/bin/env bash +# YES_ZSH + if [ -n "${ZSH_VERSION:-}" ]; then confirm() { + [ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } read -q '?Continue? [y/N] ' ret=$? echo @@ -9,10 +12,12 @@ if [ -n "${ZSH_VERSION:-}" ]; then else confirm() { # specify stdin (1) to avoid taking input from pipes + [ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } read -n1 -u 1 -p "Continue? [y/N] " c echo [ "$c" != 'y' ] && [ "$c" != 'Y' ] && return 1 return 0 } fi -confirm + +[ "${SOURCING:-0}" -gt 0 ] || confirm "$@" diff --git a/sh/countdiff b/sh/countdiff index bfe8956..d7402dd 100644 --- a/sh/countdiff +++ b/sh/countdiff @@ -1,6 +1,10 @@ #!/usr/bin/env sh +# YES_ZSH + countdiff() { + [ $# -le 2 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } git --no-pager diff --stat --no-color --no-index "$1" "$2" \ | awk '/changed/{print $4+$6;a=1}END{if(!a)print 0}' } -countdiff "$@" + +[ "${SOURCING:-0}" -gt 0 ] || countdiff "$@" diff --git a/sh/cutv b/sh/cutv new file mode 100644 index 0000000..4e62f17 --- /dev/null +++ b/sh/cutv @@ -0,0 +1,25 @@ +#!/usr/bin/env zsh +# YES_ZSH + +cutv() { + [ $# -le 4 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } + local nel="${1?missing length parameter}" + local f="${2?missing file parameter}" + local g="cut."${${f##*/}%.*}".mp4" + ffmpeg -hide_banner -i "$f" 2>&1 \ + | grep Duration \ + | grep -Eo '[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9]' | awk -F: '{print (($1*60+$2)*60+$3)+$4/100}' | read len + [[ "$len" -gt 0 ]] || return 1 + echo "$g" + ffmpeg \ + -hide_banner -loglevel warning -stats -stats_period 5 \ + -i "$f" -ss "$(((nel&2; return 1; } + echo $(( ($(date +%s) - $(date -d "$1" +%s)) / 60 / 60 / 24 )) +} + +[ "${SOURCING:-0}" -gt 0 ] || days "$@" diff --git a/sh/dbusiest b/sh/dbusiest index 48fae33..87ea4c0 100755 --- a/sh/dbusiest +++ b/sh/dbusiest @@ -1,8 +1,15 @@ #!/usr/bin/env zsh -local c d -for d in *(FDN); do - print $(find $d 2>/dev/null | wc -l) $d -done | sort -nr | while read c d; do - echo -E $c $d -done -[ -z $d ] && return 1 +# YES_ZSH + +dbusiest() { + [ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } + local c d + for d in *(FDN); do + print $(find $d 2>/dev/null | wc -l) $d + done | sort -nr | while read c d; do + echo -E $c $d + done + [ -z $d ] && return 1 +} + +[ "${SOURCING:-0}" -gt 0 ] || dbusiest diff --git a/sh/dfu b/sh/dfu index defd56e..7f75d36 100755 --- a/sh/dfu +++ b/sh/dfu @@ -1,4 +1,11 @@ #!/usr/bin/env sh -df -xtmpfs | awk ' -NR==1{printf"%-20s %7s %7s %7s %7s\n","Filesystem","Used","Max","Left","Misc"} -NR>1{printf"%-20s %7.2f %7.2f %7.2f %7.2f\n",$6,$3/2^20,($3+$4)/2^20,($4)/2^20,($2-$4-$3)/2^20}' +# YES_ZSH + +dfu() { + [ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } + df -xtmpfs | awk ' + NR==1{printf"%-20s %7s %7s %7s %7s\n","Filesystem","Used","Max","Left","Misc"} + NR>1{printf"%-20s %7.2f %7.2f %7.2f %7.2f\n",$6,$3/2^20,($3+$4)/2^20,($4)/2^20,($2-$4-$3)/2^20}' +} + +[ "${SOURCING:-0}" -gt 0 ] || dfu diff --git a/sh/disf b/sh/disf index 1678d48..ed469fa 100755 --- a/sh/disf +++ b/sh/disf @@ -1,9 +1,13 @@ #!/usr/bin/env bash # disassembles a function given its symbolic name. +# YES_ZSH + function disf() { + [ $# -le 2 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } local exe="${1:?first argument should be an unstripped executable}" local symbol="${2:?second argument should be a function name}" # note: relies on GNU sed: https://unix.stackexchange.com/a/78485 objdump -d -C --no-show-raw-insn "$exe" | sed '/<'"$symbol"'>:/,/^$/!d;//d' } -disf "$@" + +[ "${SOURCING:-0}" -gt 0 ] || disf "$@" diff --git a/sh/e b/sh/e index 324d5a8..5a6f2cc 100755 --- a/sh/e +++ b/sh/e @@ -1,50 +1,55 @@ #!/usr/bin/env zsh +# YES_ZSH -local editor=(${=EDITOR}) -local running=0 +e() { + local editor=(${=EDITOR}) + local running=0 -if [ -n "$MSYSTEM" ]; then - if ps -f | awk '{print $6}' \ - | grep -o '[^/]*$' | grep -qFx "${editor[1]}"; then + if [ -n "$MSYSTEM" ]; then + if ps -f | awk '{print $6}' \ + | grep -o '[^/]*$' | grep -qFx "${editor[1]}"; then + running=1 + fi + elif ps -o comm | grep -qFx "${editor[1]}"; then running=1 fi -elif ps -o comm | grep -qFx "${editor[1]}"; then - running=1 -fi -if [ $running -eq 1 ]; then - printf "%s\n" "${editor[1]} is already running" >&2 - read -q '?Continue? [y/N] ' _ || { echo; return; } - echo -fi + if [ $running -eq 1 ]; then + printf "%s\n" "${editor[1]} is already running" >&2 + read -q '?Continue? [y/N] ' _ || { echo; return; } + echo + fi -if [ $# -eq 0 ] || [ -n "$MSYSTEM" ]; then - $=EDITOR "$@" - return -fi + if [ $# -eq 0 ] || [ -n "$MSYSTEM" ]; then + $=EDITOR "$@" + return + fi -local f needroot=0 -for f in "$@"; do - # easy: file exists, we have write permissions - [ -w "$f" ] && continue + local f needroot=0 + for f in "$@"; do + # easy: file exists, we have write permissions + [ -w "$f" ] && continue - # easy: file exists, but no write permissions - [ -e "$f" ] && { needroot=1; break } + # easy: file exists, but no write permissions + [ -e "$f" ] && { needroot=1; break } - # hard: file may be in a directory that we can't inspect - local d="$f" - while expr index "$d" / >/dev/null; do - d="${d%/*}" - # NOTE: this gets weird with the root directory, not sure how to handle - [ -e "$d" ] && [ ! -w "$d" ] && { needroot=1 && break; } + # hard: file may be in a directory that we can't inspect + local d="$f" + while expr index "$d" / >/dev/null; do + d="${d%/*}" + # NOTE: this gets weird with the root directory, not sure how to handle + [ -e "$d" ] && [ ! -w "$d" ] && { needroot=1 && break; } + done + [ $needroot -eq 1 ] && break + + # easy: file just doesn't exist done - [ $needroot -eq 1 ] && break - # easy: file just doesn't exist -done + if [ $needroot -eq 0 ]; then + $=EDITOR "$@" + else + sudo -e "$@" + fi +} -if [ $needroot -eq 0 ]; then - $=EDITOR "$@" -else - sudo -e "$@" -fi +[ "${SOURCING:-0}" -gt 0 ] || e "$@" diff --git a/home/ea b/sh/ea similarity index 90% rename from home/ea rename to sh/ea index 9c6745c..6033d91 100644 --- a/home/ea +++ b/sh/ea @@ -1,5 +1,6 @@ #!/usr/bin/env bash # remote file access (http file-sharing shenanigans) +# YES_ZSH _REMOTE_DOMAIN="https://eaguru.guru" _REMOTE_DIR="t" @@ -36,3 +37,5 @@ eadelete() { alias eamv=eamove alias eacp=eacopy alias earm=eadelete + +# TODO: write "ea" interface with subcommands for each action, invoke it. diff --git a/sh/echo2 b/sh/echo2 new file mode 100644 index 0000000..20c33b7 --- /dev/null +++ b/sh/echo2 @@ -0,0 +1,9 @@ +#!/usr/bin/env sh +# YES_ZSH + +echo2() { + local IFS=" " + printf "%s\n" "$*" >&2 +} + +[ "${SOURCING:-0}" -gt 0 ] || echo2 "$@" diff --git a/sh/explore b/sh/explore new file mode 100644 index 0000000..47e70df --- /dev/null +++ b/sh/explore @@ -0,0 +1,9 @@ +#!/usr/bin/env sh +# YES_ZSH + +explore() { + [ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } + explorer "$(cygpath -w "${1:-.}")" +} + +[ "${SOURCING:-0}" -gt 0 ] || explore "$@" diff --git a/sh/ff b/sh/ff new file mode 100644 index 0000000..c9de645 --- /dev/null +++ b/sh/ff @@ -0,0 +1,9 @@ +#!/usr/bin/env sh +# YES_ZSH + +ff() { + [ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } + find "${1:-.}" -type f | fzy +} + +[ "${SOURCING:-0}" -gt 0 ] || ff "$@" diff --git a/sh/has b/sh/has new file mode 100644 index 0000000..7ab5694 --- /dev/null +++ b/sh/has @@ -0,0 +1,12 @@ +#!/usr/bin/env sh +# YES_ZSH + +has() { + if [ -n "$ZSH_VERSION" ]; then + whence -p "$1" + else + which "$1" >/dev/null 2>&1 && which "$1" + fi +} + +[ "${SOURCING:-0}" -gt 0 ] || has "$@" diff --git a/sh/hex b/sh/hex index b7cafd0..a9b3a16 100755 --- a/sh/hex +++ b/sh/hex @@ -1,2 +1,8 @@ #!/usr/bin/env zsh -printf "%08X\n" "$(($@))" +# YES_ZSH + +hex() { + printf "%08X\n" "$(($@))" +} + +[ "${SOURCING:-0}" -gt 0 ] || hex "$@" diff --git a/sh/ify b/sh/ify new file mode 100644 index 0000000..16cf9e9 --- /dev/null +++ b/sh/ify @@ -0,0 +1,11 @@ +#!/usr/bin/sh +# YES_ZSH + +ify() { + [ $# -ge 2 ] || { printf "%s\n" "$0: too few arguments" >&2; return 1; } + local ex="$1" + shift + "$@" | "$ex" +} + +[ "${SOURCING:-0}" -gt 0 ] || ify "$@" diff --git a/sh/is_empty b/sh/is_empty index 749b024..53bde61 100755 --- a/sh/is_empty +++ b/sh/is_empty @@ -1,4 +1,6 @@ #!/usr/bin/env bash +# YES_ZSH + is_empty() { while read -r; do [ ! -d "$REPLY" ] && [ -s "$REPLY" ] && return 1 @@ -7,4 +9,5 @@ is_empty() { # this needs to skip empty files as well. return 0 } -is_empty "$@" + +[ "${SOURCING:-0}" -gt 0 ] || is_empty "$@" diff --git a/sh/isup b/sh/isup index 739b122..c6bab2e 100755 --- a/sh/isup +++ b/sh/isup @@ -1,7 +1,10 @@ #!/usr/bin/zsh +# YES_ZSH + isup() { local c curl -sLI -w '%{http_code}' -o /dev/null "$1" | read -r c [ "${c:0:1}" -eq 2 ] } -isup "$@" + +[ "${SOURCING:-0}" -gt 0 ] || isup "$@" diff --git a/sh/maybesudo b/sh/maybesudo new file mode 100644 index 0000000..4d2a4c4 --- /dev/null +++ b/sh/maybesudo @@ -0,0 +1,131 @@ +#!/usr/bin/env sh +# YES_ZSH + +maybesudo_() { + local name + local env_cleanup=0 + while getopts ':AEHKPSVbhiklnsvC:U:g:p:r:t:u:' name; do + case $name in + K|V|k) + # K: sure kill + # V: version + # k: kill + note 'maybesudo: your system does not have sudo installed!' + return 0 + ;; + + h) + printf "%s\n" 'maybesudo - a dumb utility for systems without sudo' + printf "\n" + printf "%s\n" 'usage: maybesudo -h | -V' + printf "%s\n" 'usage: maybesudo [command]' + return 0 + ;; + + A|E|H|P|S|n|p) + # A: askpass + # E: preserve environment + # H: HOME + # P: preserve group vector + # S: stdin (password) + # n: non-interactive + # p: prompt + note 'maybesudo: option has no effect --' "'$name'" + ;; + + C|U|b|g|i|l|r|s|t) + # C: close from (fd) + # U: other user (in conjunction wiht -l) + # b: background + # g: group + # i: simulate initial login (TODO) + # l: list + # r: role (SELinux) + # s: shell (TODO) + # t: type (SELinux) + note "maybesudo: unsupported option --" "'$name'" + return 1 + ;; + + u) # user + if [ -z "$USER" -o "$OPTARG" != "$USER" ]; then + note 'maybesudo: users other than yourself are unsupported!' + return 1 + fi + env_cleanup=1 + ;; + + :) + note 'maybesudo: option requires an argument --' "'$OPTARG'" + return 1 + ;; + default) + note 'maybesudo: invalid option --' "'$OPTARG'" + return 1 + ;; + esac + done + + shift $((OPTIND-1)) + + if [ "$env_cleanup" = 1 ]; then + ( + # portably listing exported variable names is virtually impossible + # without the blessing of null-terminated strings, so don't even try. + # just export the bare minimum that a fairly stock sudo would. + + # $path is special in zsh, so call it pathy instead. + local colors display dpkg_colors home hostname krb5ccname \ + ls_colors pathy ps1 ps2 user username xauthority xauthorization + + [ -z "$COLORS" ] || colors=COLORS="$COLORS" + [ -z "$DISPLAY" ] || display=DISPLAY="$DISPLAY" + [ -z "$DPKG_COLORS" ] || dpkg_colors=DPKG_COLORS="$DPKG_COLORS" + [ -z "$HOME" ] || home=HOME="$HOME" + [ -z "$HOSTNAME" ] || hostname=HOSTNAME="$HOSTNAME" + [ -z "$KRB5CCNAME" ] || krb5ccname=KRB5CCNAME="$KRB5CCNAME" + [ -z "$LS_COLORS" ] || ls_colors=LS_COLORS="$LS_COLORS" + [ -z "$PATH" ] || pathy=PATH="$PATH" + [ -z "$PS1" ] || ps1=PS1="$PS1" + [ -z "$PS2" ] || ps2=PS2="$PS2" + [ -z "$USER" ] || user=USER="$USER" + [ -z "$USERNAME" ] || username=USERNAME="$USERNAME" + [ -z "$XAUTHORITY" ] || xauthority=XAUTHORITY="$XAUTHORITY" + [ -z "$XAUTHORIZATION" ] || xauthorization=XAUTHORIZATION="$XAUTHORIZATION" + + # don't eat up precious command space with dumb colors. + # xargs --show-limits says: + # Maximum length of command we could actually use: 5080 + [ "${#ls_colors}" -le 1024 ] || ls_colors== + + # sudo also sets SUDO_COMMAND, SUDO_GID, SUDO_UID, SUDO_USER, and MAIL, + # but who needs those? + + # env seems to treat arguments of "=" as a no-op across GNU and busybox. + env -i \ + "${colors:-=}" \ + "${display:-=}" \ + "${dpkg_colors:-=}" \ + "${home:-=}" \ + "${hostname:-=}" \ + "${krb5ccname:-=}" \ + "${ls_colors:-=}" \ + "${pathy:-=}" \ + "${ps1:-=}" \ + "${ps2:-=}" \ + "${user:-=}" \ + "${username:-=}" \ + "${xauthority:-=}" \ + "${xauthorization:-=}" \ + "$@" + ) + + else + # run it in a subshell so it won't affect ours. + ( "$@"; ) + fi + + # don't put any code here or you'll clobber $?. +} + +[ "${SOURCING:-0}" -gt 0 ] || maybesudo_ "$@" diff --git a/sh/minutemaid b/sh/minutemaid index 0da3ec5..76588c6 100755 --- a/sh/minutemaid +++ b/sh/minutemaid @@ -1,30 +1,35 @@ #!/usr/bin/env zsh # crontab usage: #* * * * * minutemaid 9 cd repo && git pull # runs every nine minutes +# YES_ZSH -local offset=0 opt= -while getopts 'o:h' opt; do - case $opt in - o) offset="$OPTARG";; - ?) local fd=0 - [ $opt = h ] && fd=0 || fd=2 - echo -E "usage: $0 [-o offset] {interval} [{command} [{args...}]]" >&$fd - [ $opt = h ] && return 0 || return 1;; - esac -done -shift $((OPTIND-1)) +minutemaid() { + local offset=0 opt= + while getopts 'o:h' opt; do + case $opt in + o) offset="$OPTARG";; + ?) local fd=0 + [ $opt = h ] && fd=0 || fd=2 + echo -E "usage: $0 [-o offset] {interval} [{command} [{args...}]]" >&$fd + [ $opt = h ] && return 0 || return 1;; + esac + done + shift $((OPTIND-1)) -local interval="${1:?no interval specified}" -shift - -local sec="$(date +%s)" -let min=sec/60+offset -let mod=min%interval -if [ $# -gt 0 ]; then - local cmd="${1}" + local interval="${1:?no interval specified}" shift - [ $mod -eq 0 ] && "$cmd" "$@" -else - [ $mod -eq 0 ] && return 0 || return 1 -fi + local sec="$(date +%s)" + let min=sec/60+offset + let mod=min%interval + if [ $# -gt 0 ]; then + local cmd="${1}" + shift + + [ $mod -eq 0 ] && "$cmd" "$@" + else + [ $mod -eq 0 ] && return 0 || return 1 + fi +} + +[ "${SOURCING:-0}" -gt 0 ] || minutemaid "$@" diff --git a/sh/monitor b/sh/monitor index 323f5d5..c7014e9 100755 --- a/sh/monitor +++ b/sh/monitor @@ -1,35 +1,41 @@ #!/usr/bin/env zsh -local bottom=0 strip=0 interval=2 opt= -while getopts 'fsn:h' opt; do - case $opt in - f) bottom=1;; # align with bottom of terminal - s) strip=1;; # strip last character assuming it's a newline - n) interval="$OPTARG";; - ?) echo -E "usage: $0 [-fs] [-n {period}] {command} [{args...}]" - [ $opt = h ] && return 0 || return 1;; - esac -done -shift $((OPTIND-1)) +# YES_ZSH -local cmd=${1:?no command specified} -shift +monitor() { + local bottom=0 strip=0 interval=2 opt= + while getopts 'fsn:h' opt; do + case $opt in + f) bottom=1;; # align with bottom of terminal + s) strip=1;; # strip last character assuming it's a newline + n) interval="$OPTARG";; + ?) echo -E "usage: $0 [-fs] [-n {period}] {command} [{args...}]" + [ $opt = h ] && return 0 || return 1;; + esac + done + shift $((OPTIND-1)) -function _monclear() { - [ $bottom -eq 1 ] && printf ${(l:$((LINES*2))::\n:)} -} -function _monfit() { - if [ $strip -eq 1 ]; then - tail -$((LINES)) | head -c-1 - else - tail -$((LINES-1)) - fi + local cmd=${1:?no command specified} + shift + + function _monclear() { + [ $bottom -eq 1 ] && printf ${(l:$((LINES*2))::\n:)} + } + function _monfit() { + if [ $strip -eq 1 ]; then + tail -$((LINES)) | head -c-1 + else + tail -$((LINES-1)) + fi + } + + local stop=0 + trap stop=1 INT + echo -en "\e[H\e[2J" + while [ $stop -eq 0 ]; do + echo -en "\e[1J\e[H" + { _monclear; "$cmd" "$@" 2>&1 } | fold -w $COLUMNS | _monfit + sleep "$interval" || break + done } -local stop=0 -trap stop=1 INT -echo -en "\e[H\e[2J" -while [ $stop -eq 0 ]; do - echo -en "\e[1J\e[H" - { _monclear; "$cmd" "$@" 2>&1 } | fold -w $COLUMNS | _monfit - sleep "$interval" || break -done +[ "${SOURCING:-0}" -gt 0 ] || monitor "$@" diff --git a/sh/noccom b/sh/noccom index d8d2b53..5f1db86 100644 --- a/sh/noccom +++ b/sh/noccom @@ -1,6 +1,8 @@ #!/usr/bin/env sh # the first expression is taken from this FAQ: # https://perldoc.perl.org/perlfaq6.html#How-do-I-use-a-regular-expression-to-strip-C-style-comments-from-a-file%3f +# YES_ZSH + noccom() { [ -s ~/opt/local/bin/noccom ] || cat > ~/opt/local/bin/noccom <&2 +} + +[ "${SOURCING:-0}" -gt 0 ] || note "$@" diff --git a/sh/now b/sh/now index ef3afe8..de83b19 100755 --- a/sh/now +++ b/sh/now @@ -1,7 +1,9 @@ #!/usr/bin/env bash +# YES_ZSH now() { - local dt= + [ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } + local dt local fmt='+%F_%T_%N' if [ -z "$1" ]; then dt="$(date -u "$fmt")" @@ -11,12 +13,12 @@ now() { dt="$(date -u -d "$1" "$fmt")" fi - local F= T= N= + local F T N F=${dt%%_*} T=${dt#*_} T=${T%_*} N=${dt##*_} - local H= M= S= + local H M S H=${T%%:*} M=${T#*:} M=${M%:*} @@ -36,4 +38,4 @@ now() { echo "${F}_${ms}" } -now "$@" +[ "${SOURCING:-0}" -gt 0 ] || now "$@" diff --git a/sh/pacbm b/sh/pacbm index 0c94aff..a65fedf 100755 --- a/sh/pacbm +++ b/sh/pacbm @@ -1,5 +1,11 @@ #!/usr/bin/env sh -expac -s '%m %n' | awk ' -{t+=$1;printf("%8.2fM %s\n",$1/(1024*1024),$2)} -END{printf("%8.2fM\n",t/(1024*1024))} -' | sort -n +# YES_ZSH + +pacbm() { + expac -s '%m %n' | awk ' + {t+=$1;printf("%8.2fM %s\n",$1/(1024*1024),$2)} + END{printf("%8.2fM\n",t/(1024*1024))} + ' | sort -n +} + +[ "${SOURCING:-0}" -gt 0 ] || pacbm "$@" diff --git a/sh/pause b/sh/pause index a753371..78c5e4c 100755 --- a/sh/pause +++ b/sh/pause @@ -1,5 +1,11 @@ #!/usr/bin/env bash -[ -n "${ZSH_VERSION:-}" ] \ -&& read -sk '?Press any key to continue -' || read -n1 -u 1 -sp 'Press any key to continue -' +# YES_ZSH + +pause() { + [ -n "${ZSH_VERSION:-}" ] \ + && read -sk '?Press any key to continue + ' || read -n1 -u 1 -sp 'Press any key to continue + ' +} + +[ "${SOURCING:-0}" -gt 0 ] || pause "$@" diff --git a/sh/pegg b/sh/pegg new file mode 100644 index 0000000..d720dc5 --- /dev/null +++ b/sh/pegg @@ -0,0 +1,9 @@ +#!/usr/bin/env sh +# YES_ZSH + +pegg() { + [ $# -ge 2 ] || { printf "%s\n" "$0: too few arguments" >&2; return 1; } + pippy "https://github.com/$1/$2/tarball/${3:-master}#egg=$2" +} + +[ "${SOURCING:-0}" -gt 0 ] || pegg "$@" diff --git a/sh/pippy b/sh/pippy new file mode 100644 index 0000000..e3e9a75 --- /dev/null +++ b/sh/pippy @@ -0,0 +1,12 @@ +#!/usr/bin/env +# YES_ZSH + +#. ~/sh/maybesudo +#. ~/sh/has + +pippy() { + local sudo="$(has sudo || echo maybesudo_)" + "$sudo" py -m pip install --upgrade --upgrade-strategy only-if-needed "$@" +} + +[ "${SOURCING:-0}" -gt 0 ] || pippy "$@" diff --git a/sh/pre b/sh/pre index bca3d62..9c09a59 100755 --- a/sh/pre +++ b/sh/pre @@ -1,6 +1,12 @@ #!/usr/bin/env bash -if [ -n "${ZSH_VERSION:-}" ]; then - ${CC:-gcc} -dM -E - $=CPPFLAGS $=CFLAGS $=LDFLAGS < /dev/null -else - ${CC:-gcc} -dM -E - $CPPFLAGS $CFLAGS $LDFLAGS < /dev/null -fi +# YES_ZSH + +pre() { + if [ -n "${ZSH_VERSION:-}" ]; then + ${CC:-gcc} -dM -E - $=CPPFLAGS $=CFLAGS $=LDFLAGS < /dev/null + else + ${CC:-gcc} -dM -E - $CPPFLAGS $CFLAGS $LDFLAGS < /dev/null + fi +} + +[ "${SOURCING:-0}" -gt 0 ] || pre "$@" diff --git a/sh/psbm b/sh/psbm index 51735d9..80300cd 100755 --- a/sh/psbm +++ b/sh/psbm @@ -1,5 +1,11 @@ #!/usr/bin/env sh -ps axco rss,pid,command | awk ' -NR>1&&$1>0{t+=$1;printf("%8.2fM %7d %s\n",$1/1024,$2,$3)} -END{printf("%8.2fM\n",t/1024)} -' | sort -n +# YES_ZSH + +psbm() { + ps axco rss,pid,command | awk ' + NR>1&&$1>0{t+=$1;printf("%8.2fM %7d %s\n",$1/1024,$2,$3)} + END{printf("%8.2fM\n",t/1024)} + ' | sort -n +} + +[ "${SOURCING:-0}" -gt 0 ] || psbm "$@" diff --git a/sh/randir b/sh/randir index 17f5660..3e42104 100755 --- a/sh/randir +++ b/sh/randir @@ -1,2 +1,8 @@ #!/usr/bin/env sh -find -maxdepth 1 -type d \( -path '/root' -prune -o -print \) | tail -n+2 | shuf | head -1 +# YES_ZSH + +randir() { + find -maxdepth 1 -type d \( -path '/root' -prune -o -print \) | tail -n+2 | shuf | head -1 +} + +[ "${SOURCING:-0}" -gt 0 ] || randir "$@" diff --git a/sh/refresh b/sh/refresh new file mode 100644 index 0000000..ef66f9b --- /dev/null +++ b/sh/refresh @@ -0,0 +1,8 @@ +#!/usr/bin/env sh +# YES_ZSH + +refresh() { + hash -r +} + +[ "${SOURCING:-0}" -gt 0 ] || refresh "$@" diff --git a/sh/rs b/sh/rs index b833230..30de4b7 100644 --- a/sh/rs +++ b/sh/rs @@ -1,5 +1,6 @@ #!/usr/bin/env zsh # rs: record screen +# YES_ZSH rs() { set -e @@ -67,4 +68,4 @@ rs() { $o_overwrite "$extless.enc.$ext" } -rs "$@" +[ "${SOURCING:-0}" -gt 0 ] || rs "$@" diff --git a/sh/sc b/sh/sc index 5cc9e4c..5acd3ce 100755 --- a/sh/sc +++ b/sh/sc @@ -1,5 +1,6 @@ #!/usr/bin/env bash -. ~/.ea + +. ~/.ea # FIXME: don't do this? somehow? sc_shorten() { REPLY="${1:2:2}${1:5:2}${1:8:2}${1:11:12}" diff --git a/sh/scramble b/sh/scramble index 285aac6..674f1cf 100755 --- a/sh/scramble +++ b/sh/scramble @@ -1,4 +1,6 @@ #!/usr/bin/env bash +# YES_ZSH + scramble() { local eggs='s/@\(\w\)\(\w\)\(\w\)/@\3\1@\2/g' sed \ @@ -7,3 +9,5 @@ scramble() { -e "$eggs" -e "$eggs" -e "$eggs" -e "$eggs" \ -e 's/@//g' } + +[ "${SOURCING:-0}" -gt 0 ] || scramble "$@" diff --git a/sh/screeny b/sh/screeny index 9357a08..ed3ffa8 100755 --- a/sh/screeny +++ b/sh/screeny @@ -1,4 +1,5 @@ #!/usr/bin/env zsh +# YES_ZSH screeny() { local user="${1:?needs arg for user name}" @@ -14,4 +15,4 @@ screeny() { return 0 } -screeny "$@" +[ "${SOURCING:-0}" -gt 0 ] || screeny "$@" diff --git a/sh/scropt b/sh/scropt index 52c73ed..9b5bbc4 100755 --- a/sh/scropt +++ b/sh/scropt @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# YES_ZSH scropt() { local now= @@ -7,4 +8,5 @@ scropt() { scrot "$fn" -e "optipng -quiet \$f" "$@" echo "$fn" } -scropt "$@" + +[ "${SOURCING:-0}" -gt 0 ] || scropt "$@" diff --git a/sh/similar b/sh/similar index 2cf588d..7a78368 100755 --- a/sh/similar +++ b/sh/similar @@ -1,13 +1,19 @@ #!/usr/bin/env sh -awk ' -{ - i=0 - split($0,a,"") - split(L,b,"") - for (c in a) - if (a[c]==b[c]) i++ - else break - #print substr($0,1,i) "\033[7m" substr($0,i+1) "\033[0m" - print "\033[31m" substr($0,1,i) "\033[0m" substr($0,i+1) - L=$0 -}' +# YES_ZSH + +similar() { + awk ' + { + i=0 + split($0,a,"") + split(L,b,"") + for (c in a) + if (a[c]==b[c]) i++ + else break + #print substr($0,1,i) "\033[7m" substr($0,i+1) "\033[0m" + print "\033[31m" substr($0,1,i) "\033[0m" substr($0,i+1) + L=$0 + }' +} + +[ "${SOURCING:-0}" -gt 0 ] || similar "$@" diff --git a/sh/slit b/sh/slit index bdd6d2e..d4a6e80 100755 --- a/sh/slit +++ b/sh/slit @@ -1,3 +1,9 @@ #!/usr/bin/env zsh # from https://github.com/sorin-ionescu/prezto/ -awk "{ print ${(j:,:):-\$${^@}} }" +# YES_ZSH + +slit() { + awk "{ print ${(j:,:):-\$${^@}} }" +} + +[ "${SOURCING:-0}" -gt 0 ] || slit "$@" diff --git a/sh/slitt b/sh/slitt index cd4673a..ec63387 100644 --- a/sh/slitt +++ b/sh/slitt @@ -1,4 +1,10 @@ #!/usr/bin/env zsh # via: https://github.com/sorin-ionescu/prezto/ # this version uses tabs for input/output field separators. -awk "BEGIN { FS=\"\\t\"; OFS=\"\\t\" } { print ${(j:,:):-\$${^@}} }" +# YES_ZSH + +slitt() { + awk "BEGIN { FS=\"\\t\"; OFS=\"\\t\" } { print ${(j:,:):-\$${^@}} }" +} + +[ "${SOURCING:-0}" -gt 0 ] || slitt "$@" diff --git a/sh/sram b/sh/sram index 534be6c..dfb6ec4 100755 --- a/sh/sram +++ b/sh/sram @@ -1,4 +1,5 @@ #!/usr/bin/env zsh +# YES_ZSH sram() { die() { @@ -47,4 +48,4 @@ sram() { revend "$out" } -sram "$@" +[ "${SOURCING:-0}" -gt 0 ] || sram "$@" diff --git a/sh/stfu b/sh/stfu new file mode 100644 index 0000000..9f59659 --- /dev/null +++ b/sh/stfu @@ -0,0 +1,42 @@ +#!/usr/bin/env sh +# YES_ZSH + +#. ~/sh/echo2 # FIXME +#. ~/sh/note # FIXME + +stfu() { + # NOTE: don't use stfu for handling sensitive data or commands! + # use it for 7zip. + [ $# -gt 0 ] || { printf "%s\n" "$0: too few arguments" >&2; return 1; } + local temp="${TMP:-/tmp}/stfu" + + local time="$(date -u '+%s')" + [ $? -eq 0 ] || { printf "%s\n" "$0: failed to get current time" >&2; return 1; } + + mkdir -p "$temp" || { printf "%s\n" "$0: failed to create temp directory" >&2; return 1; } + + while [ -e "$temp/out_$time" -o -e "$temp/err_$time" ]; do time=$((time+1)); done + local out="$temp/out_$time" + local err="$temp/err_$time" + + #local out="$(mktemp -t -p "$temp" out_XXXXXX)" + #[ $? -eq 0 ] || { printf "%s\n" "$0: failed to create temp file" >&2; return 1; } + #local err="$(mktemp -t -p "$temp" err_XXXXXX)" + #[ $? -eq 0 ] || { printf "%s\n" "$0: failed to create temp file" >&2; return 1; } + + local ret=0 + "$@" > "$out" 2> "$err" || ret=$? + if [ $ret -ne 0 ]; then + printf "command failed with exit status %s:\n" $ret >&2 + echo2 "$@" + echo2 + note '$ tail -n 20' "$out" + tail -n 20 "$out" >&2 + echo2 + note '$ tail -n 20' "$err" + tail -n 20 "$err" >&2 + fi + return $ret +} + +[ "${SOURCING:-0}" -gt 0 ] || stfu "$@" diff --git a/home/streamcrap b/sh/streamcrap similarity index 98% rename from home/streamcrap rename to sh/streamcrap index 395d10b..c9f6aaf 100644 --- a/home/streamcrap +++ b/sh/streamcrap @@ -1,5 +1,6 @@ -#!/usr/bin/env zsh +#!/usr/bin/env false # awful things +# YES_ZSH MPV_STREAM_FLAGS="--quiet --autofit=1280x720 --loop-playlist=no --no-resume-playback --no-sub \ --no-initial-audio-sync --mc=0.02 --autosync=30" @@ -111,5 +112,3 @@ asmr() { _M_PROCESS="acompressor=threshold=0.001:ratio=1.33:attack=900:release=6000:makeup=6:knee=8:mix=0.9,alimiter" \ yt "$@" --volume=90 --ytdl-format=251/300/best } - -[ -e "$HOME/.nara" ] && . "$HOME/.nara" diff --git a/sh/sum b/sh/sum new file mode 100644 index 0000000..d47b4bb --- /dev/null +++ b/sh/sum @@ -0,0 +1,12 @@ +#!/usr/bin/env sh +# YES_ZSH + +sum() { + local sum=0 + for i; do + sum=$((sum+i)) + done + echo "$sum" +} + +[ "${SOURCING:-0}" -gt 0 ] || sum "$@" diff --git a/sh/sv b/sh/sv index 909c938..a469180 100755 --- a/sh/sv +++ b/sh/sv @@ -1,4 +1,10 @@ #!/usr/bin/env bash -awk "-F${1:- }" ' -NF>1{f[$1]=substr($0,length($1)+1+length(FS))}END{for(k in f)print k FS f[k]} -' +# YES_ZSH + +sv() { + awk "-F${1:- }" ' + NF>1{f[$1]=substr($0,length($1)+1+length(FS))}END{for(k in f)print k FS f[k]} + ' +} + +[ "${SOURCING:-0}" -gt 0 ] || sv "$@" diff --git a/sh/tpad b/sh/tpad index fab27b6..c5d4ea7 100755 --- a/sh/tpad +++ b/sh/tpad @@ -1,8 +1,11 @@ #!/usr/bin/env bash +# YES_ZSH + tpad() { while [ $# -gt 0 ]; do gm convert -bordercolor "#000000FF" -border 1x1 "$1" "${1/.png/}.tt.png"; shift done } -tpad "$@" + +[ "${SOURCING:-0}" -gt 0 ] || tpad "$@" diff --git a/sh/trash b/sh/trash new file mode 100644 index 0000000..380d760 --- /dev/null +++ b/sh/trash @@ -0,0 +1,9 @@ +#!/usr/bin/env sh +# YES_ZSH + +trash() { + [ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } + dd status=none if=/dev/random bs=1 count="${1:?missing count argument}" +} + +[ "${SOURCING:-0}" -gt 0 ] || trash "$@" diff --git a/sh/trunc b/sh/trunc index 3c6c312..4223079 100755 --- a/sh/trunc +++ b/sh/trunc @@ -1,3 +1,9 @@ #!/usr/bin/env bash -COLUMNS=${COLUMNS:-$(tput cols)} -awk -vL=${1:-$COLUMNS} '{e=length>L?"…":"";print substr($0,0,L-(e?1:0)) e}' +# YES_ZSH + +trunc() { + COLUMNS=${COLUMNS:-$(tput cols)} + awk -vL=${1:-$COLUMNS} '{e=length>L?"…":"";print substr($0,0,L-(e?1:0)) e}' +} + +[ "${SOURCING:-0}" -gt 0 ] || trunc "$@" diff --git a/sh/unscreen b/sh/unscreen index 3c84858..0153594 100755 --- a/sh/unscreen +++ b/sh/unscreen @@ -1,4 +1,5 @@ #!/usr/bin/env zsh +# YES_ZSH unscreen() { local pids="$(screen -ls | fgrep "$1" | cut -d. -f1)" @@ -9,4 +10,4 @@ unscreen() { return 0 } -unscreen "$@" +[ "${SOURCING:-0}" -gt 0 ] || unscreen "$@" diff --git a/sh/wat b/sh/wat new file mode 100644 index 0000000..5ad2bf9 --- /dev/null +++ b/sh/wat @@ -0,0 +1,35 @@ +#!/usr/bin/env zsh +# YES_ZSH + +# wat - a better and recursive which/whence +# via: https://leahneukirchen.org/dotfiles/tools.html +wat() { + ( # constrain unalias + for cmd; do + if (( $+aliases[$cmd] )); then + printf '%s: aliased to %s\n' $cmd $aliases[$cmd] + local -a words=(${${(z)aliases[$cmd]}:#(*=*|rlwrap|noglob|command)}) + unalias $cmd + if [[ $words[1] == '\'* ]]; then + words[1]=${words[1]#'\'} + unalias $words[1] 2>/dev/null + fi + wat $words[1] + elif (( $+functions[$cmd] )); then + whence -v $cmd + whence -f $cmd + elif (( $+commands[$cmd] )); then + wat $commands[$cmd] + elif [[ -h $cmd ]]; then + file $cmd + wat $cmd:A + elif [[ -x $cmd ]]; then + file $cmd + else + which $cmd + fi + done + ) +} + +[ "${SOURCING:-0}" -gt 0 ] || wat "$@" diff --git a/sh/wipe b/sh/wipe new file mode 100644 index 0000000..63a2d5b --- /dev/null +++ b/sh/wipe @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +# YES_ZSH + +wipe() { + # TODO: rename because wipe(1) already exists. + [ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } + clear + clear # twice because mintty is weird + echo "\033[30m\033[107m" + printf "*%.0s" {1..$COLUMNS} + echo "\033[0m\n" +} + +[ "${SOURCING:-0}" -gt 0 ] || wipe "$@"