summaryrefslogtreecommitdiff
path: root/bin/extra
diff options
context:
space:
mode:
authorRaymaekers Luca <raymaekers.luca@gmail.com>2023-08-13 02:08:33 +0200
committerRaymaekers Luca <raymaekers.luca@gmail.com>2023-08-13 02:08:33 +0200
commit818dabae65e67cb52f11623ab1e76ad4c85c19e1 (patch)
tree5c508f03e2718fc786edb0ed82f9fab49bb00e97 /bin/extra
parent5eed72797eaf9ec0d0e43b24add8b266b1999b08 (diff)
added confirm, trl and wipe scripts!
Diffstat (limited to 'bin/extra')
-rwxr-xr-xbin/extra/confirm12
-rwxr-xr-xbin/extra/trl115
-rwxr-xr-xbin/extra/wipe13
3 files changed, 140 insertions, 0 deletions
diff --git a/bin/extra/confirm b/bin/extra/confirm
new file mode 100755
index 0000000..116b468
--- /dev/null
+++ b/bin/extra/confirm
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+read_char ()
+{
+ old_stty_cfg=$(stty -g 2> /dev/null)
+ stty raw -echo 2> /dev/null
+ dd ibs=1 count=1 2> /dev/null
+ stty $old_stty_cfg 2> /dev/null
+}
+
+>&2 printf "$1 "
+read_char | grep -q "[yY]"
diff --git a/bin/extra/trl b/bin/extra/trl
new file mode 100755
index 0000000..ee42040
--- /dev/null
+++ b/bin/extra/trl
@@ -0,0 +1,115 @@
+#!/bin/sh
+
+# prints on stderr
+log () { >&2 echo "$@"; }
+
+help ()
+{
+ >&2 cat <<-EOF
+ h help
+ l clear output
+ q quit
+
+ i invert languages
+ p select primary
+ s select secondary
+ EOF
+}
+# returns available languages
+languages () {
+ cat <<-EOF
+ arabic
+ dutch
+ french
+ german
+ polish
+ english
+ portuguese
+ spanish
+ romanian
+ hebrew
+ swedish
+ italian
+ turkish
+ japanese
+ ukrainian
+ korean
+ chinese
+ czech
+ hungarian
+ danish
+ persian
+ greek
+ slovak
+ hindi
+ thai
+ EOF
+}
+
+# translates a word
+# $1: primary language
+# $2: secondary language
+# $3: word to translate
+translate ()
+{
+ curl -s "https://context.reverso.net/translation/$1-$2/$3" \
+ --compressed \
+ -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0' \
+ -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8' \
+ -H 'Accept-Language: en-US,en;q=0.5' \
+ -H 'Accept-Encoding: gzip, deflate, br' |
+ pup 'a.link_highlighted em text{}' |
+ sed 's/.*/\L&/' |
+ sort | uniq | sed 's/.*/ &/'
+}
+
+# prints the current language
+current_language ()
+{
+ log " current: $primary-$secondary"
+}
+
+select_language ()
+{
+ tmp="$(languages | fzf)"
+ [ "$tmp" ] && [ "$tmp" != "$primary" ] && [ "$tmp" != "$secondary" ] &&
+ eval "$1=\"$tmp\""
+ current_language
+}
+
+# set default languages
+primary=french
+secondary=dutch
+
+
+main ()
+{
+ current_language
+ while true
+ do
+ log -n '>'
+ read -r prompt
+ test $? -eq 1 && exit 0 # quit on ctrl-d
+ case "$prompt" in
+ q) break ;;
+ l) clear ;;
+ i) tmp="$secondary"; secondary="$primary"; primary="$tmp"
+ current_language ;;
+ h) help ;; # TODO
+ p) select_language primary ;;
+ s) select_language secondary ;;
+ '') ;;
+ *) translate "$primary" "$secondary" "$prompt" ;;
+ esac
+ done
+}
+
+
+if [ "$1" = "--help" ] || [ "$1" = "-h" ]
+then
+ log "usage: trl"
+ help
+ exit
+fi
+
+main
diff --git a/bin/extra/wipe b/bin/extra/wipe
new file mode 100755
index 0000000..ec2abe9
--- /dev/null
+++ b/bin/extra/wipe
@@ -0,0 +1,13 @@
+#!/bin/sh
+[ 0 -eq "$#" ] && >&2 echo 'usage: wipe <file>' && exit 1
+[ ! -f "$1" ] && [ ! -d "$1" ] && >&2 echo "'$1' not found." && exit 1
+
+confirm "sure?" || exit 1
+>&2 printf "\n"
+
+find "$1" -type f -print0 |
+ xargs -0I{} shred -uz "{}" &&
+ [ -d "$1" ] && # remove leftovver empty directories
+ find "$1" | tac | tr '\n' '\0' |
+ xargs -0I{} rm -d "{}" &&
+ >&2 echo "wiped."