aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornea <romangraef@gmail.com>2022-01-13 00:01:56 +0100
committernea <romangraef@gmail.com>2022-01-13 00:01:56 +0100
commitd9031203e22e4ef381c482b4594521323647487e (patch)
tree1ea7dcfb53840a42f10d19cf25317138e497911a
downloadscripts-d9031203e22e4ef381c482b4594521323647487e.tar.gz
scripts-d9031203e22e4ef381c482b4594521323647487e.tar.bz2
scripts-d9031203e22e4ef381c482b4594521323647487e.zip
Initial commit
-rw-r--r--README21
-rwxr-xr-xchoose63
-rwxr-xr-xgenerate-multimc-desktop-files32
-rwxr-xr-xosc25
-rwxr-xr-xreadkey36
-rwxr-xr-xreddit28
6 files changed, 205 insertions, 0 deletions
diff --git a/README b/README
new file mode 100644
index 0000000..ea58715
--- /dev/null
+++ b/README
@@ -0,0 +1,21 @@
+Scripts that I tend to use on mostly in desktop environments.
+
+Im planning to expand this, but right now a lot of my scripts are very application specific and/or dependency heavy.
+
+A multimedia capable terminal emulator such as wezterm is probably required for a lot of these things.
+
+jq is a dependency for some of these scripts.
+
+
+Application scripts:
+
+reddit: a script to show a top image post from reddit
+generate-multimc-desktop-files: a perhaps very specific script to generate desktop files for all your multimc instances
+
+
+Library scripts:
+
+choose: a library script i use to prompt for user input
+osc: a library script to execute some OSC escape sequences
+readkey: a library script to read a single key and parse key entities
+
diff --git a/choose b/choose
new file mode 100755
index 0000000..6d750d3
--- /dev/null
+++ b/choose
@@ -0,0 +1,63 @@
+#!/bin/bash
+
+if [ "a$1" == "a--help" ] || [ "a$1" == "a-h" ]; then
+ cat <<EOF
+Usage: $0 [-i] \`tty\` "Option A" "Option B"
+Displays the user in the provided tty all the options and lets the user select an option with their keys.
+
+Prints the selected option (or the (0-based) index of that option with the -i flag) to the default stdout.
+
+Exits with code 1 if the user aborted the selection with q.
+EOF
+fi
+
+printidx=0
+if [ "a$1" == "a-i" ]; then
+ printidx=1
+ shift
+fi
+
+tty="$1"
+shift
+options=("${@}")
+optionslength=${#options[@]}
+sel=0
+printf "\e[s" >"$tty"
+while true; do
+ printf "\e[u\e[s\e[K" >"$tty"
+ for ((i = 0; i < optionslength; i++)); do
+ if [ "$i" -eq $sel ]; then
+ printf "\e[30;47m" >"$tty"
+ fi
+ printf "%s\e[40;37m " "${options[$i]}" >"$tty"
+ done
+ case "$(./readkey <"$tty")" in
+ left)
+ sel=$((sel - 1))
+ ;;
+ right)
+ sel=$((sel + 1))
+ ;;
+ q)
+ printf "\e[u\e[KSelection aborted\n" >"$tty"
+ exit 1
+ ;;
+ enter)
+ break
+ ;;
+ esac
+ if ((sel < 0)); then
+ sel=0
+ fi
+ if ((sel >= optionslength)); then
+ sel=$((optionslength - 1))
+ fi
+
+done
+printf "\e[u\e[KSelected %s\n" "${options[$sel]}" >"$tty"
+
+if [ "$printidx" -eq 1 ]; then
+ echo "$sel"
+else
+ echo "${options[$sel]}"
+fi
diff --git a/generate-multimc-desktop-files b/generate-multimc-desktop-files
new file mode 100755
index 0000000..a16be8c
--- /dev/null
+++ b/generate-multimc-desktop-files
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+echo "Deleting old .desktop files"
+rm ~/.local/share/applications/multimc-*.desktop
+
+echo "Seeking instances"
+for instdir in ~/.local/share/multimc/instances/*; do
+ if [[ (-d "$instdir") && (-f "$instdir/instance.cfg") ]]; then
+ echo "Scanning $instdir"
+ instanceid="$(basename -- "$instdir")"
+ name="$instanceid"
+ while IFS="=" read -r key value; do
+ case "$key" in
+ name)
+ echo "Instance Name: $value"
+ name="$value"
+ ;;
+ esac
+ done <"$instdir/instance.cfg"
+ cat <<EOF > "$HOME/.local/share/applications/multimc-$instanceid.desktop"
+[Desktop Entry]
+Name=MultiMC: $name
+Type=Application
+Categories=Game
+StartupWMClass=multic
+Terminal=false
+Comment=Open MultiMC Instancew
+GenericName=Minecraft
+Exec=$(which multimc) -l "$instanceid"
+EOF
+ fi
+done
diff --git a/osc b/osc
new file mode 100755
index 0000000..d3b0e3a
--- /dev/null
+++ b/osc
@@ -0,0 +1,25 @@
+#!/bin/bash
+esc() {
+ python -c "import sys;print('\x1b]'+';'.join(sys.argv[1:]), end='\x07')" "$@"
+}
+if [ "$1" = "notify" ]; then
+ esc 777 notify "$2" "$3"
+elif [ "$1" = "anchor" ] || [ "$1" = "url" ]; then
+ esc 8 "" "$2"
+ echo -n "$3"
+ esc 8 "" ""
+elif [ "$1" = "showimg" ]; then
+ printf "\e]1337;File=inline=1;preserveAspectRatio=1:"
+ base64 -w 0
+ printf "\a"
+else
+ cat >&2 <<EOF
+Usage: $0 <action> [args]
+
+Actions:
+
+notify [notification title] [notification text] - Show system notification
+anchor [url] [text] - Show hyperlink
+showimg - Show an image from STDIN (NOT base64-encoded)
+EOF
+fi
diff --git a/readkey b/readkey
new file mode 100755
index 0000000..94836bd
--- /dev/null
+++ b/readkey
@@ -0,0 +1,36 @@
+#!/bin/bash
+
+if [ "a$1" == "a-h" ] || [ "a$1" == "a--help" ]; then
+ cat <<EOF
+Usage: $0
+
+Reads a single key from stdin and parses some key codes such as arrow keys and enter.
+EOF
+fi
+read -rsN1 key
+
+read -rsN1 -t 0.0001 k1
+read -rsN1 -t 0.0001 k2
+read -rsN1 -t 0.0001 k3
+key+="${k1}${k2}${k3}"
+
+case "$key" in
+$'\e[A' | $'\e0A')
+ echo up
+ ;;
+$'\e[D' | $'\e0D')
+ echo left
+ ;;
+$'\e[B' | $'\e0B')
+ echo down
+ ;;
+$'\e[C' | $'\e0C')
+ echo right
+ ;;
+$'\n')
+ echo enter
+ ;;
+*)
+ echo "$key"
+ ;;
+esac
diff --git a/reddit b/reddit
new file mode 100755
index 0000000..086d538
--- /dev/null
+++ b/reddit
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+## Please do not judge me, i don't even like reddit, it just had the easiest API
+
+API_URL="https://api.reddit.com/best"
+if [[ "$1" = "--help" ]]; then
+ cat >&2 <<-"EOF"
+ Usage: $0 [subreddit]
+
+ Specifying a subreddit is optional, and should be done without r/ prefix
+ EOF
+ exit 1
+elif [[ -n "$1" ]]; then
+ API_URL="https://api.reddit.com/r/$1/best"
+fi
+
+QUAD=$(
+ curl -H "User-Agent: bash:moe.nea89.meme-tui" "$API_URL" 2>/dev/null | jq -r ".data.children[].data | select(.post_hint == \"image\") | .url,.title,.id,.subreddit_name_prefixed" | head -n 4
+ echo x
+)
+QUAD=${QUAD%?}
+{ read -r image && read -r title && read -r id && read -r subreddit; } < <(echo "$QUAD")
+curl -L "$image" 2>/dev/null | ./osc showimg
+echo
+./osc anchor "https://redd.it/$id" "$title"
+printf " "
+./osc anchor "https://reddit.com/$subreddit" "($subreddit)"
+echo