mirror of
https://github.com/notwa/rc
synced 2025-03-14 14:12:49 -07:00
change argc
interface (now $#
comes first instead of $@
)
This commit is contained in:
parent
e6e0283c20
commit
45d888d866
5 changed files with 39 additions and 34 deletions
39
sh/argc
39
sh/argc
|
@ -6,53 +6,58 @@
|
|||
|
||||
argc() { ### @- validate the number of arguments in a function.
|
||||
### ```sh
|
||||
### # usage: myfunc() { argc -(eq|le|ge) [0-9] "$0" "$@" || return; }
|
||||
### # usage: myfunc() { argc $# -(eq|le|ge) [0-9] "$0" || return; }
|
||||
###
|
||||
### myfunc() {
|
||||
### # use one of the following:
|
||||
### argc -eq N "$0" "$@" || return
|
||||
### argc -le N "$0" "$@" || return
|
||||
### argc -ge N "$0" "$@" || return
|
||||
### argc $# -eq N "$0" || return
|
||||
### argc $# -le N "$0" || return
|
||||
### argc $# -ge N "$0" || return
|
||||
### # where N is an integer between 0 and 9.
|
||||
### }
|
||||
### ```
|
||||
|
||||
# local usage='usage: myfunc() { %s -(eq|lt|gt|le|ge) [0-9] "$0" "$@" || return; }\n'
|
||||
local usage='usage: myfunc() { %s -(eq|le|ge) [0-9] "$0" "$@" || return; }\n'
|
||||
local usage='usage: myfunc() { %s $# -(eq|le|ge) [0-9] "$0" || return; }\n'
|
||||
|
||||
# note that $zero can be empty, but must be set.
|
||||
if [ $# -lt 3 ] || [ -z "$1" ] || [ -z "$2" ] || [ "${3+x}" != x ]; then
|
||||
if [ $# -ne 4 ] || [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
|
||||
printf "$usage" argc >&2
|
||||
return 2
|
||||
fi
|
||||
|
||||
local argc="$1"; shift
|
||||
local cond="$1"; shift
|
||||
local want="$1"; shift
|
||||
local zero="$1"; shift
|
||||
local many='' plural=''
|
||||
[ "$want" = 1 ] || plural='s'
|
||||
|
||||
if [ "$argc" != "${argc%%[!0-9]*}" ]; then
|
||||
printf "$usage" argc >&2
|
||||
return 2
|
||||
fi
|
||||
|
||||
case "$cond" in
|
||||
'-eq') many='exactly';;
|
||||
# '-lt') many='fewer than';;
|
||||
# '-gt') many='more than';;
|
||||
'-le') many='at most';;
|
||||
'-ge') many='at least';;
|
||||
*)
|
||||
('-eq') many='exactly';;
|
||||
('-le') many='at most';;
|
||||
('-ge') many='at least';;
|
||||
(*)
|
||||
printf "$usage" argc >&2
|
||||
return 2;;
|
||||
esac
|
||||
|
||||
case "$want" in
|
||||
[0-9]) :;; # no error, continue.
|
||||
*)
|
||||
([0-9]) :;; # no error, continue.
|
||||
(*)
|
||||
printf "$usage" argc >&2
|
||||
return 2;;
|
||||
esac
|
||||
|
||||
if [ ! $# "$cond" "$want" ]; then
|
||||
if [ "$argc" "$cond" "$want" ] 2>/dev/null; then
|
||||
:
|
||||
else
|
||||
local err="expected $many $want argument$plural, got"
|
||||
printf '%s: %s %s\n' "$zero" "$err" "$#" >&2
|
||||
printf '%s: %s %s\n' "$zero" "$err" "$argc" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ baknow() { ### @- backup files by appending their timestamps given by [`now`.](#
|
|||
### -rw-r--r-- 1 notwa None 0 Dec 25 2001 butts
|
||||
### -rw-r--r-- 1 notwa None 0 Dec 25 2001 butts.2001-12-26_01800000.bak
|
||||
### ```
|
||||
argc -ge 1 "$0" "$@" || return
|
||||
argc $# -ge 1 "$0" || return
|
||||
local ret=0
|
||||
for f; do
|
||||
cp -ip "$f" "$f.$(now "$f").bak" || ret=1
|
||||
|
|
4
sh/cutv
4
sh/cutv
|
@ -7,8 +7,8 @@
|
|||
cutv() { ### @-
|
||||
### (WIP) create a short clip of a long video file.
|
||||
|
||||
argc -ge 2 "$0" "$@" || return
|
||||
argc -le 4 "$0" "$@" || return
|
||||
argc $# -ge 2 "$0" || return
|
||||
argc $# -le 4 "$0" || return
|
||||
local nel="${1?missing length parameter}"
|
||||
local f="${2?missing file parameter}"
|
||||
local g="cut."${${f##*/}%.*}".mp4"
|
||||
|
|
2
sh/stfu
2
sh/stfu
|
@ -35,7 +35,7 @@ stfu() { ### @-
|
|||
###
|
||||
###
|
||||
### ```
|
||||
argc -ge 1 "$0" "$@" || return
|
||||
argc $# -ge 1 "$0" || return
|
||||
|
||||
local dirty=0 temp="$STFU_DIR"
|
||||
if [ -z "$temp" ]; then
|
||||
|
|
|
@ -14,7 +14,7 @@ mpvs() { ### @- invoke mpv with some extra flags suited for streamed sources.
|
|||
}
|
||||
|
||||
getladspa() {
|
||||
argc -eq 1 "$0" "$@" || return
|
||||
argc $# -eq 1 "$0" || return
|
||||
printf '%s' volume
|
||||
if [ -e "/usr/lib/ladspa/${1}.so" ]; then
|
||||
printf '%s' "ladspa=f=${1}:p=${1}"
|
||||
|
@ -36,7 +36,7 @@ _M_DANNY="$_M_PRE_EMPH,alimiter,dynaudnorm=f=400:g=5:p=1:m=32:r=0.1:c=1:s=9:t=0.
|
|||
_M_LEVEL="$(getladspa crap_level)"
|
||||
|
||||
earphones() {
|
||||
argc -eq 0 "$0" "$@" || return
|
||||
argc $# -eq 0 "$0" || return
|
||||
_M_PROCESS="lowpass=8000:p=1,$_M_KILL,alimiter=level_out=0.707"
|
||||
}
|
||||
|
||||
|
@ -47,7 +47,7 @@ case "$(uname -n | tr A-Z a-z)" in
|
|||
|
||||
neobanshee)
|
||||
speakers() {
|
||||
argc -eq 0 "$0" "$@" || return
|
||||
argc $# -eq 0 "$0" || return
|
||||
_M_PROCESS="highpass=311,$_M_KILL,$_M_NEO,alimiter=level_in=2"
|
||||
}
|
||||
speakers
|
||||
|
@ -56,7 +56,7 @@ case "$(uname -n | tr A-Z a-z)" in
|
|||
banshee)
|
||||
_M_SPEAKERS="$(getladspa crap_eq_const_T420)"
|
||||
speakers() {
|
||||
argc -eq 0 "$0" "$@" || return
|
||||
argc $# -eq 0 "$0" || return
|
||||
_M_PROCESS="$_M_LEVEL,$_M_SPEAKERS,alimiter=level_in=0.5"
|
||||
}
|
||||
speakers
|
||||
|
@ -69,7 +69,7 @@ esac
|
|||
|
||||
mpv_watch() { ### @-
|
||||
### play some media in mpv with a bunch of unnecessary filters.
|
||||
argc -ge 1 "$0" "$@" || return
|
||||
argc $# -ge 1 "$0" || return
|
||||
pushd ~/play >/dev/null
|
||||
local url="$1"; shift
|
||||
|
||||
|
@ -88,7 +88,7 @@ mpv_watch() { ### @-
|
|||
|
||||
mpv_stream() { ### @-
|
||||
### watch a stream in mpv with a bunch of unnecessary filters.
|
||||
argc -ge 1 "$0" "$@" || return
|
||||
argc $# -ge 1 "$0" || return
|
||||
pushd ~/play >/dev/null
|
||||
local url="$1"; shift
|
||||
mpvs --af=lavfi="[$_M_PROCESS]" "$@" -- "$url"
|
||||
|
@ -97,7 +97,7 @@ mpv_stream() { ### @-
|
|||
|
||||
twitch() { ### @-
|
||||
### watch a twitch stream in mpv with a bunch of unnecessary filters.
|
||||
argc -ge 1 "$0" "$@" || return
|
||||
argc $# -ge 1 "$0" || return
|
||||
local user="$1"; shift
|
||||
mpv_stream "http://twitch.tv/$user" "$@"
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ yt() { ### @-
|
|||
###
|
||||
### **NOTE:** there also exists a yt(1) program provided by
|
||||
### the *python3-yt* package that i don't use.
|
||||
argc -ge 1 "$0" "$@" || return
|
||||
argc $# -ge 1 "$0" || return
|
||||
local vid="$1"; shift
|
||||
[ -e "$vid" ] || [ "$vid" != "${vid#http}" ] || vid="ytdl://$vid"
|
||||
mpv_watch "$vid" "$@"
|
||||
|
@ -118,7 +118,7 @@ yt() { ### @-
|
|||
|
||||
ytg() { # @-
|
||||
# watch a youtube video. like `yt`, but with a preference for different formats.
|
||||
argc -ge 1 "$0" "$@" || return
|
||||
argc $# -ge 1 "$0" || return
|
||||
local vid="$1"; shift
|
||||
# --ytdl-format=22/95/300/best
|
||||
yt "$vid" "$@"
|
||||
|
@ -127,26 +127,26 @@ ytg() { # @-
|
|||
ytll() { ### @-
|
||||
### watch a stream from youtube in mpv, etcetera etcetera.
|
||||
### this is the low latency version that does not support seeking.
|
||||
argc -ge 1 "$0" "$@" || return
|
||||
argc $# -ge 1 "$0" || return
|
||||
youtube-dl -q -f best "$1" -o - | mpv_stream - --no-ytdl
|
||||
}
|
||||
|
||||
ytgll() { # @-
|
||||
# watch a stream from youtube in mpv. like `ytll`, but with a preference for different formats.
|
||||
argc -ge 1 "$0" "$@" || return
|
||||
argc $# -ge 1 "$0" || return
|
||||
youtube-dl -q -f 22/95/300/best "$1" -o - | mpv_stream - --no-ytdl
|
||||
}
|
||||
|
||||
ai() { # @-
|
||||
# hai domo!
|
||||
argc -ge 1 "$0" "$@" || return
|
||||
argc $# -ge 1 "$0" || return
|
||||
yt "$@" --slang=en --sub-font='Tekton Pro' --sub-bold=yes \
|
||||
--sub-font-size=60 --sub-border-color='#DD6180' --sub-margin-y=52
|
||||
}
|
||||
|
||||
asmr() { # @-
|
||||
# for your aural pleasure.
|
||||
argc -ge 1 "$0" "$@" || return
|
||||
argc $# -ge 1 "$0" || return
|
||||
# --ytdl-format=251/300/best
|
||||
_M_PROCESS="acompressor=threshold=0.001:ratio=1.33:attack=900:release=6000:makeup=6:knee=8:mix=0.9,alimiter" \
|
||||
yt "$@" --volume=90
|
||||
|
|
Loading…
Add table
Reference in a new issue