From d9031203e22e4ef381c482b4594521323647487e Mon Sep 17 00:00:00 2001 From: nea Date: Thu, 13 Jan 2022 00:01:56 +0100 Subject: Initial commit --- README | 21 ++++++++++++++ choose | 63 ++++++++++++++++++++++++++++++++++++++++++ generate-multimc-desktop-files | 32 +++++++++++++++++++++ osc | 25 +++++++++++++++++ readkey | 36 ++++++++++++++++++++++++ reddit | 28 +++++++++++++++++++ 6 files changed, 205 insertions(+) create mode 100644 README create mode 100755 choose create mode 100755 generate-multimc-desktop-files create mode 100755 osc create mode 100755 readkey create mode 100755 reddit 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 <"$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 < "$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 < [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 <&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 -- cgit