From a0bb023d782dd2d8703670d560b5ccd8d25d640e Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Thu, 4 Nov 2021 12:25:04 +0300 Subject: bins: shfmt, weasel suite, spark --- .local/bin/bukuadd_c | 4 +- .local/bin/done | 4 +- .local/bin/dups | 54 ++++++++++++++++++++ .local/bin/epub_lander | 1 - .local/bin/i3-get-window-criteria | 8 +-- .local/bin/letsread | 2 +- .local/bin/mailsync | 32 +++++++----- .local/bin/mailto_handler | 2 +- .local/bin/openfile | 4 +- .local/bin/passive | 65 ++++++++++++++++++++++++ .local/bin/power.sh | 8 +-- .local/bin/rfv | 12 ++--- .local/bin/spark | 103 ++++++++++++++++++++++++++++++++++++++ .local/bin/texclear | 6 +-- .local/bin/type.sh | 12 ++--- .local/bin/weasel | 38 ++++++++++++++ 16 files changed, 310 insertions(+), 45 deletions(-) create mode 100755 .local/bin/dups create mode 100755 .local/bin/passive create mode 100755 .local/bin/spark create mode 100755 .local/bin/weasel (limited to '.local') diff --git a/.local/bin/bukuadd_c b/.local/bin/bukuadd_c index 925022a..86f705d 100755 --- a/.local/bin/bukuadd_c +++ b/.local/bin/bukuadd_c @@ -10,7 +10,7 @@ fi FOO=$(buku --nostdin --np --nc -p -1) OUT="$(buku --nostdin --np --nc -a "$url" "$tags" 2>&1)" -if [[ $OUT =~ "ERROR" ]] ; then +if [[ $OUT =~ "ERROR" ]]; then notify-send "Bookmarking Failed" "$OUT" --app-name="buku" --icon="dialog-error" exit 1 fi @@ -20,7 +20,7 @@ BAR=$FOO while [[ $BAR == "$FOO" ]]; do sleep 1 BAR=$(buku --nostdin --np --nc -p -1) -done; +done notify-send "Success" "$BAR" --app-name="buku" --icon="checkmark" diff --git a/.local/bin/done b/.local/bin/done index 8bb0a3d..ae76287 100755 --- a/.local/bin/done +++ b/.local/bin/done @@ -27,9 +27,9 @@ if ACTIVE_TASK=$(task active); then timew "@1" annotate "${annot:0:1}" else if maybe_delete=$(printf "no no\nyea" | rofi -dmenu -p "should we delete the tracking?" -lines 2 -no-custom); then - if [[ "${maybe_delete}" == "no no" ]]; then + if [[ ${maybe_delete} == "no no" ]]; then notify-send "tracking unannotated, not deleted" "nobody will know how that went" --app-name="done" --icon="itmages-question" - elif [[ "${maybe_delete}" == "yea" ]]; then + elif [[ ${maybe_delete} == "yea" ]]; then timew delete "@1" notify-send "tracking deleted" "like you never worked on that" --app-name="done" --icon="pack-more" fi diff --git a/.local/bin/dups b/.local/bin/dups new file mode 100755 index 0000000..eb450f5 --- /dev/null +++ b/.local/bin/dups @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +# Finds duplicate adjacent words. + +use strict ; + +my $DupCount = 0 ; + +if (!@ARGV) { + print "usage: dups ...\n" ; + exit ; +} + +while (1) { + my $FileName = shift @ARGV ; + + # Exit code = number of duplicates found. + exit $DupCount if (!$FileName) ; + + open FILE, $FileName or die $!; + + my $LastWord = "" ; + my $LineNum = 0 ; + + while () { + chomp ; + + $LineNum ++ ; + + my @words = split (/(\W+)/) ; + + foreach my $word (@words) { + # Skip spaces: + next if $word =~ /^\s*$/ ; + + # Skip punctuation: + if ($word =~ /^\W+$/) { + $LastWord = "" ; + next ; + } + + # Found a dup? + if (lc($word) eq lc($LastWord)) { + print "$FileName:$LineNum $word\n" ; + $DupCount ++ ; + } # Thanks to Sean Cronin for tip on case. + + # Mark this as the last word: + $LastWord = $word ; + } + } + + close FILE ; +} diff --git a/.local/bin/epub_lander b/.local/bin/epub_lander index 801381a..19e0a1c 100755 --- a/.local/bin/epub_lander +++ b/.local/bin/epub_lander @@ -15,4 +15,3 @@ mv $HOME/down/*.pdf $HOME/docs/pdf_landing/ if [[ $? -ne 1 ]]; then notify-send "Moved some pdfs" "" --app-name="epub_lander" --icon="emblem_success" fi - diff --git a/.local/bin/i3-get-window-criteria b/.local/bin/i3-get-window-criteria index f46f65e..6d6ea1b 100755 --- a/.local/bin/i3-get-window-criteria +++ b/.local/bin/i3-get-window-criteria @@ -9,7 +9,7 @@ # quotes in "" are not escaped properly. This is a problem with the output of `xprop`, # reported upstream: https://bugs.freedesktop.org/show_bug.cgi?id=66807 -PROGNAME=`basename "$0"` +PROGNAME=$(basename "$0") # Check for xwininfo and xprop for cmd in xwininfo xprop; do @@ -25,13 +25,13 @@ match_qstring='"[^"\\]*(\\.[^"\\]*)*"' # NOTE: Adds 1 backreference { # Run xwininfo, get window id - window_id=`xwininfo -int | sed -nre "s/^xwininfo: Window id: ($match_int) .*$/\1/p"` + window_id=$(xwininfo -int | sed -nre "s/^xwininfo: Window id: ($match_int) .*$/\1/p") echo "id=$window_id" # Run xprop, transform its output into i3 criteria. Handle fallback to # WM_NAME when _NET_WM_NAME isn't set - xprop -id $window_id | - sed -nr \ + xprop -id $window_id \ + | sed -nr \ -e "s/^WM_CLASS\(STRING\) = ($match_qstring), ($match_qstring)$/instance=\1\nclass=\3/p" \ -e "s/^WM_WINDOW_ROLE\(STRING\) = ($match_qstring)$/window_role=\1/p" \ -e "/^WM_NAME\(STRING\) = ($match_string)$/{s//title=\1/; h}" \ diff --git a/.local/bin/letsread b/.local/bin/letsread index 5e3cf5a..d5f1e0c 100755 --- a/.local/bin/letsread +++ b/.local/bin/letsread @@ -1,4 +1,4 @@ #!/usr/bin/env bash -firefox $(buku --stag toread -f 10 | shuf -n 1) +firefox "$(buku --stag toread -f 10 | shuf -n 1)" dunstify "Time to read" "Don't forget to remove 'toread' from that when you're done" diff --git a/.local/bin/mailsync b/.local/bin/mailsync index ae0801c..cc7010b 100755 --- a/.local/bin/mailsync +++ b/.local/bin/mailsync @@ -3,9 +3,15 @@ # Sync mail and give notification if there is new mail. # Run only if user logged in (prevent cron errors) -pgrep -u "${USER:=$LOGNAME}" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;} +pgrep -u "${USER:=$LOGNAME}" > /dev/null || { + echo "$USER not logged in; sync will not run." + exit +} # Run only if not already running in other instance -pgrep -x mbsync >/dev/null && { notify-send --app-name="mailsync" "mbsync is already running" "you should fix this" ; exit ;} +pgrep -x mbsync > /dev/null && { + notify-send --app-name="mailsync" "mbsync is already running" "you should fix this" + exit +} if [[ -f "$HOME/.local/share/.dnd" ]]; then echo "do not disturb is on" @@ -13,8 +19,11 @@ if [[ -f "$HOME/.local/share/.dnd" ]]; then fi # Checks for internet connection and set notification script. -ping -q -c 1 1.1.1.1 > /dev/null || ping -q -c 1 1.0.0.1 > /dev/null || ping -q -c 1 example.org || { echo "No internet connection detected."; exit ;} -command -v notify-send >/dev/null || echo "Note that \`libnotify\` or \`libnotify-send\` should be installed for pop-up mail notifications with this script." +ping -q -c 1 1.1.1.1 > /dev/null || ping -q -c 1 1.0.0.1 > /dev/null || ping -q -c 1 example.org || { + echo "No internet connection detected." + exit +} +command -v notify-send > /dev/null || echo 'Note that `libnotify` or `libnotify-send` should be installed for pop-up mail notifications with this script.' # Required to display notifications if run as a cronjob: export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus @@ -25,11 +34,11 @@ export DISPLAY=:0.0 # Settings are different for MacOS (Darwin) systems. if [ "$(uname)" = "Darwin" ]; then - notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;} - messageinfo() { osascript -e "display notification with title \"📧 $from\" subtitle \"$subject\"" ;} + notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2; } + messageinfo() { osascript -e "display notification with title \"📧 $from\" subtitle \"$subject\""; } else - notify() { notify-send --icon="mail_new" --app-name="excerpt" "\`$1\`" "📬 $2 new mail(s)" ;} - messageinfo() { notify-send --icon="mail-mark-unread" --app-name="mutt" "📧 $from" "$subject" ;} + notify() { notify-send --icon="mail_new" --app-name="excerpt" "\`$1\`" "📬 $2 new mail(s)"; } + messageinfo() { notify-send --icon="mail-mark-unread" --app-name="mutt" "📧 $from" "$subject"; } fi # Check account for new mail. Notify if there is new content. @@ -44,7 +53,7 @@ syncandnotify() { # Extract subject and sender from mail. from=$(awk '/^From: / && ++n ==1,/^\<.*\>:/' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; if (NF>=3)$NF=""; print $0 }' | sed 's/^[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//') subject=$(awk '/^Subject: / && ++n == 1,/^\<.*\>: / && ++i == 2' "$file" | head -n 1 | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | sed 's/^{[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//' | tr -d '\n') - messageinfo & + messageinfo & done fi } @@ -62,15 +71,14 @@ fi # ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null # Parallelize multiple accounts -for account in $accounts -do +for account in $accounts; do syncandnotify & done wait # ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null -notmuch new 2>/dev/null +notmuch new 2> /dev/null #Create a touch file that indicates the time of the last run of mailsync touch "$HOME/.config/mutt/.mailsynclastrun" diff --git a/.local/bin/mailto_handler b/.local/bin/mailto_handler index 9493558..36c38bd 100755 --- a/.local/bin/mailto_handler +++ b/.local/bin/mailto_handler @@ -21,6 +21,6 @@ for account in "${ACCOUNTSDIR}"/*.muttrc; do done -picked=$(printf "${choices}" | rofi -dmenu) +picked=$(printf '%s' "${choices}" | rofi -dmenu) exec kitty -e neomutt -F "$HOME/.config/mutt/muttrc" -e "source /home/yigit/.config/mutt/accounts/${picked}.muttrc" -- "$@" diff --git a/.local/bin/openfile b/.local/bin/openfile index cf3c6c3..742b9c0 100755 --- a/.local/bin/openfile +++ b/.local/bin/openfile @@ -1,10 +1,10 @@ #!/bin/sh # Helps open a file with xdg-open from mutt in a external program without weird side effects. -tempdir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard/files" +tempdir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt/files" file="$tempdir/$(basename "$1")" [ "$(uname)" = "Darwin" ] && opener="open" || opener="setsid -f xdg-open" mkdir -p "$tempdir" cp -f "$1" "$file" -$opener "$file" >/dev/null 2>&1 +$opener "$file" > /dev/null 2>&1 find "${tempdir:?}" -mtime +1 -type f -delete diff --git a/.local/bin/passive b/.local/bin/passive new file mode 100755 index 0000000..276d61b --- /dev/null +++ b/.local/bin/passive @@ -0,0 +1,65 @@ +#!/usr/bin/env bash + +irregulars="awoken|\ + been|born|beat|\ + become|begun|bent|\ + beset|bet|bid|\ + bidden|bound|bitten|\ + bled|blown|broken|\ + bred|brought|broadcast|\ + built|burnt|burst|\ + bought|cast|caught|\ + chosen|clung|come|\ + cost|crept|cut|\ + dealt|dug|dived|\ +done|drawn|dreamt|\ +driven|drunk|eaten|fallen|\ +fed|felt|fought|found|\ +fit|fled|flung|flown|\ +forbidden|forgotten|\ +foregone|forgiven|\ +forsaken|frozen|\ +gotten|given|gone|\ +ground|grown|hung|\ +heard|hidden|hit|\ +held|hurt|kept|knelt|\ +knit|known|laid|led|\ +leapt|learnt|left|\ +lent|let|lain|lighted|\ +lost|made|meant|met|\ +misspelt|mistaken|mown|\ +overcome|overdone|overtaken|\ +overthrown|paid|pled|proven|\ +put|quit|read|rid|ridden|\ +rung|risen|run|sawn|said|\ +seen|sought|sold|sent|\ +set|sewn|shaken|shaven|\ +shorn|shed|shone|shod|\ +shot|shown|shrunk|shut|\ +sung|sunk|sat|slept|\ +slain|slid|slung|slit|\ +smitten|sown|spoken|sped|\ +spent|spilt|spun|spit|\ +split|spread|sprung|stood|\ +stolen|stuck|stung|stunk|\ +stridden|struck|strung|\ +striven|sworn|swept|\ +swollen|swum|swung|taken|\ +taught|torn|told|thought|\ +thrived|thrown|thrust|\ +trodden|understood|upheld|\ +upset|woken|worn|woven|\ +wed|wept|wound|won|\ +withheld|withstood|wrung|\ +written" + +if [ "$1" = "" ]; then + echo "usage: $(basename "$0") ..." + exit +fi + +grep -E -n -i --color \ + "\\b(am|are|were|being|is|been|was|be)\ + \\b[ ]*(\w+ed|($irregulars))\\b" "$*" + +exit $? diff --git a/.local/bin/power.sh b/.local/bin/power.sh index f794211..2dd7e5d 100755 --- a/.local/bin/power.sh +++ b/.local/bin/power.sh @@ -15,10 +15,10 @@ chosen=$(echo -e "lock\nshutdown\nrestart" | rofi -dmenu -i) # Info about some states are available here: # https://www.freedesktop.org/software/systemd/man/systemd-sleep.conf.html#Description -if [[ $chosen = "lock" ]]; then - $HOME/.local/bin/lock -elif [[ $chosen = "shutdown" ]]; then +if [[ $chosen == "lock" ]]; then + "$HOME"/.local/bin/lock +elif [[ $chosen == "shutdown" ]]; then systemctl poweroff -elif [[ $chosen = "restart" ]]; then +elif [[ $chosen == "restart" ]]; then systemctl reboot fi diff --git a/.local/bin/rfv b/.local/bin/rfv index 4c9a632..a178e67 100755 --- a/.local/bin/rfv +++ b/.local/bin/rfv @@ -4,11 +4,11 @@ # 2. Interactively narrow down the list using fzf # 3. Open the file in Vim IFS=: read -ra selected < <( - rg --color=always --line-number --no-heading --smart-case "${*:-}" | - fzf --ansi \ - --color "hl:-1:underline,hl+:-1:underline:reverse" \ - --delimiter : \ - --preview 'bat --color=always {1} --highlight-line {2}' \ - --preview-window='up:60%:+{2}+3/3:~3' + rg --color=always --line-number --no-heading --smart-case "${*:-}" \ + | fzf --ansi \ + --color "hl:-1:underline,hl+:-1:underline:reverse" \ + --delimiter : \ + --preview 'bat --color=always {1} --highlight-line {2}' \ + --preview-window='up:60%:+{2}+3/3:~3' ) [ -n "${selected[0]}" ] && nvim "${selected[0]}" "+${selected[1]}" diff --git a/.local/bin/spark b/.local/bin/spark new file mode 100755 index 0000000..53a38be --- /dev/null +++ b/.local/bin/spark @@ -0,0 +1,103 @@ +#!/usr/bin/env bash +# +# spark +# https://github.com/holman/spark +# +# Generates sparklines for a set of data. +# +# Here's a good web-based sparkline generator that was a bit of inspiration +# for spark: +# +# https://datacollective.org/sparkblocks +# +# spark takes a comma-separated or space-separated list of data and then prints +# a sparkline out of it. +# +# Examples: +# +# spark 1 5 22 13 53 +# # => ▁▁▃▂▇ +# +# spark 0 30 55 80 33 150 +# # => ▁▂▃▅▂▇ +# +# spark -h +# # => Prints the spark help text. + +# Generates sparklines. +# +# $1 - The data we'd like to graph. +_echo() +{ + if [ "X$1" = "X-n" ]; then + shift + printf "%s" "$*" + else + printf "%s\n" "$*" + fi +} + +spark() +{ + local n numbers= + + # find min/max values + local min=0xffffffff max=0 + + for n in ${@//,/ } + do + # on Linux (or with bash4) we could use `printf %.0f $n` here to + # round the number but that doesn't work on OS X (bash3) nor does + # `awk '{printf "%.0f",$1}' <<< $n` work, so just cut it off + n=${n%.*} + (( n < min )) && min=$n + (( n > max )) && max=$n + numbers=$numbers${numbers:+ }$n + done + + # print ticks + local ticks=(▁ ▂ ▃ ▄ ▅ ▆ ▇ █) + + # use a high tick if data is constant + (( min == max )) && ticks=(▅ ▆) + + local f=$(( (($max-$min)<<8)/(${#ticks[@]}-1) )) + (( f < 1 )) && f=1 + + for n in $numbers + do + _echo -n ${ticks[$(( ((($n-$min)<<8)/$f) ))]} + done + _echo +} + +# If we're being sourced, don't worry about such things +if [ "$BASH_SOURCE" == "$0" ]; then + # Prints the help text for spark. + help() + { + local spark=$(basename $0) + cat <&1 echo -n "${a:- }" | tee /dev/tty echo -n "${a:- }" | tee /dev/tty done - sleep 0.$((RANDOM%7)) - 1>&2 echo -ne $"\n" + sleep 0.$((RANDOM % 7)) + echo 1>&2 -ne $"\n" echo sleep 1.0 done diff --git a/.local/bin/weasel b/.local/bin/weasel new file mode 100755 index 0000000..ce82ce3 --- /dev/null +++ b/.local/bin/weasel @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +weasels="many|various|very|fairly|several|extremely\ + |exceedingly|quite|remarkably|few|surprisingly\ + |mostly|largely|huge|tiny|((are|is) a number)\ + |excellent|interestingly|significantly\ + |substantially|clearly|vast|relatively|completely" + +wordfile="" + +# Check for an alternate weasel file +if [ -f "$HOME"/etc/words/weasels ]; then + wordfile="$HOME/etc/words/weasels" +fi + +if [ -f "$WORDSDIR"/weasels ]; then + wordfile="$WORDSDIR/weasels" +fi + +if [ -f words/weasels ]; then + wordfile="words/weasels" +fi + +if [ ! "$wordfile" = "" ]; then + weasels="xyzabc123" + for w in $(cat $wordfile); do + weasels="$weasels|$w" + done +fi + +if [ "$1" = "" ]; then + echo "usage: $(basename "$0") ..." + exit +fi + +grep -E -i -n --color "\\b($weasels)\\b" "$*" + +exit $? -- cgit v1.2.3-70-g09d2