From 1048bb7f40a32b9aaf641c62e959c2984ed2aa6f Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Wed, 22 Feb 2023 22:20:13 +0200 Subject: [PATCH] Added some useful scripts from Luke Smith - https://github.com/LukeSmithxyz/voidrice/tree/master/.local/bin --- .local/bin/compiler | 59 +++++++++++++++++++++++++++ .local/bin/getcomproot | 12 ++++++ .local/bin/ifinstalled | 12 ++++++ .local/bin/linkhandler | 31 ++++++++++++++ .local/bin/maimpick | 18 ++++++++ .local/bin/mounter | 93 ++++++++++++++++++++++++++++++++++++++++++ .local/bin/opout | 16 ++++++++ .local/bin/remaps | 15 +++++++ .local/bin/rotdir | 12 ++++++ .local/bin/setbg | 37 +++++++++++++++++ .local/bin/td-toggle | 11 +++++ .local/bin/texclear | 15 +++++++ .local/bin/tordone | 8 ++++ .local/bin/torwrap | 8 ++++ .local/bin/transadd | 9 ++++ .local/bin/unmounter | 28 +++++++++++++ 16 files changed, 384 insertions(+) create mode 100755 .local/bin/compiler create mode 100755 .local/bin/getcomproot create mode 100755 .local/bin/ifinstalled create mode 100755 .local/bin/linkhandler create mode 100755 .local/bin/maimpick create mode 100755 .local/bin/mounter create mode 100755 .local/bin/opout create mode 100755 .local/bin/remaps create mode 100755 .local/bin/rotdir create mode 100755 .local/bin/setbg create mode 100755 .local/bin/td-toggle create mode 100755 .local/bin/texclear create mode 100755 .local/bin/tordone create mode 100755 .local/bin/torwrap create mode 100755 .local/bin/transadd create mode 100755 .local/bin/unmounter diff --git a/.local/bin/compiler b/.local/bin/compiler new file mode 100755 index 00000000..d15befc5 --- /dev/null +++ b/.local/bin/compiler @@ -0,0 +1,59 @@ +#!/bin/sh + +# This script will compile or run another finishing operation on a document. I +# have this script run via vim. +# +# Compiles .tex. groff (.mom, .ms), .rmd, .md, .org. Opens .sent files as sent +# presentations. Runs scripts based on extension or shebang. +# +# Note that .tex files which you wish to compile with XeLaTeX should have the +# string "xelatex" somewhere in a comment/command in the first 5 lines. + +file=$(readlink -f "$1") +dir=${file%/*} +base="${file%.*}" +ext="${file##*.}" + +cd "$dir" || exit 1 + +textype() { + textarget="$(getcomproot "$file" || echo "$file")" + echo "$textarget" + command="pdflatex" + (head -n5 "$textarget" | grep -qi 'xelatex') && command="xelatex" + $command --output-directory="${textarget%/*}" "${textarget%.*}" + grep -qi addbibresource "$textarget" && + biber --input-directory "${textarget%/*}" "${textarget%.*}" && + $command --output-directory="${textarget%/*}" "${textarget%.*}" && + $command --output-directory="${textarget%/*}" "${textarget%.*}" +} + +case "$ext" in +# Try to keep these cases in alphabetical order. +[0-9]) preconv "$file" | refer -PS -e | groff -mandoc -T pdf >"$base".pdf ;; +c) cc "$file" -o "$base" && "$base" ;; +cpp) g++ "$file" -o "$base" && "$base" ;; +cs) mcs "$file" && mono "$base".exe ;; +go) go run "$file" ;; +h) sudo make install ;; +java) javac -d classes "$file" && java -cp classes "${1%.*}" ;; +m) octave "$file" ;; +md) if [ -x "$(command -v lowdown)" ]; then + lowdown --parse-no-intraemph "$file" -Tms | groff -mpdfmark -ms -kept -T pdf >"$base".pdf +elif [ -x "$(command -v groffdown)" ]; then + groffdown -i "$file" | groff -T pdf >"$base".pdf +else + pandoc -t ms --highlight-style=kate -s -o "$base".pdf "$file" +fi ;; +mom) preconv "$file" | refer -PS -e | groff -mom -kept -T pdf >"$base".pdf ;; +ms) preconv "$file" | refer -PS -e | groff -me -ms -kept -T pdf >"$base".pdf ;; +org) emacs "$file" --batch -u "$USER" -f org-latex-export-to-pdf ;; +py) python "$file" ;; +[rR]md) Rscript -e "rmarkdown::render('$file', quiet=TRUE)" ;; +rs) cargo build ;; +sass) sassc -a "$file" "$base".css ;; +scad) openscad -o "$base".stl "$file" ;; +sent) setsid -f sent "$file" 2>/dev/null ;; +tex) textype "$file" ;; +*) sed -n '/^#!/s/^#!//p; q' "$file" | xargs -r -I % "$file" ;; +esac diff --git a/.local/bin/getcomproot b/.local/bin/getcomproot new file mode 100755 index 00000000..d34a2e42 --- /dev/null +++ b/.local/bin/getcomproot @@ -0,0 +1,12 @@ +#!/bin/bash + +# A helper script for LaTeX/groff files used by `compiler` and `opout`. +# The user can add the root file of a larger project as a comment as below: +# % root = mainfile.tex +# And the compiler script will run on that instead of the opened file. + +texroot="$(grep -i "^.\+\s*root\s*=\s*\S\+" "$1")" +texroot="${texroot##*=}" +texroot="${texroot//[\"\' ]}" + +[ -f "$texroot" ] && readlink -f "$texroot" || exit 1 diff --git a/.local/bin/ifinstalled b/.local/bin/ifinstalled new file mode 100755 index 00000000..c192eba0 --- /dev/null +++ b/.local/bin/ifinstalled @@ -0,0 +1,12 @@ +#!/bin/sh + +# Some optional functions in LARBS require programs not installed by default. I +# use this little script to check to see if a command exists and if it doesn't +# it informs the user that they need that command to continue. This is used in +# various other scripts for clarity's sake. + +for x in "$@"; do + if ! which "$x" >/dev/null 2>&1 && ! pacman -Qq "$x" >/dev/null 2>&1; then + notify-send "๐Ÿ“ฆ $x" "must be installed for this function." && exit 1 ; + fi +done diff --git a/.local/bin/linkhandler b/.local/bin/linkhandler new file mode 100755 index 00000000..fa495e11 --- /dev/null +++ b/.local/bin/linkhandler @@ -0,0 +1,31 @@ +#!/bin/sh + +# Feed script a url or file location. +# If an image, it will view in nsxiv, +# if a video or gif, it will view in mpv +# if a music file or pdf, it will download, +# otherwise it opens link in browser. + +if [ -z "$1" ]; then + url="$(xclip -o)" +else + url="$1" +fi + +case "$url" in +*mkv | *webm | *mp4 | *youtube.com/watch* | *youtube.com/playlist* | *youtube.com/shorts* | *youtu.be* | *hooktube.com* | *bitchute.com* | *odysee.com*) + setsid -f mpv -quiet "$url" >/dev/null 2>&1 + ;; +*png | *jpg | *jpe | *jpeg | *gif) + curl -sL "$url" >"/tmp/$(echo "$url" | sed "s/.*\///;s/%20/ /g")" && nsxiv -a "/tmp/$(echo "$url" | sed "s/.*\///;s/%20/ /g")" >/dev/null 2>&1 & + ;; +*pdf | *cbz | *cbr) + curl -sL "$url" >"/tmp/$(echo "$url" | sed "s/.*\///;s/%20/ /g")" && zathura "/tmp/$(echo "$url" | sed "s/.*\///;s/%20/ /g")" >/dev/null 2>&1 & + ;; +*mp3 | *flac | *opus | *mp3?source*) + qndl "$url" 'curl -LO' >/dev/null 2>&1 + ;; +*) + [ -f "$url" ] && setsid -f "$TERMINAL" -e "$EDITOR" "$url" >/dev/null 2>&1 || setsid -f "$BROWSER" "$url" >/dev/null 2>&1 + ;; +esac diff --git a/.local/bin/maimpick b/.local/bin/maimpick new file mode 100755 index 00000000..bf1972ea --- /dev/null +++ b/.local/bin/maimpick @@ -0,0 +1,18 @@ +#!/bin/sh + +# This is bound to Shift+PrintScreen by default, requires maim. It lets you +# choose the kind of screenshot to take, including copying the image or even +# highlighting an area to copy. scrotcucks on suicidewatch right now. + +# variables +output="$(date '+%y%m%d-%H%M-%S').png" +xclip_cmd="xclip -sel clip -t image/png" + +case "$(printf "a selected area\\ncurrent window\\nfull screen\\na selected area (copy)\\ncurrent window (copy)\\nfull screen (copy)" | dmenu -l 6 -i -p "Screenshot which area?")" in +"a selected area") maim -s pic-selected-"${output}" ;; +"current window") maim -q -d 0.2 -i "$(xdotool getactivewindow)" pic-window-"${output}" ;; +"full screen") maim -q -d 0.2 pic-full-"${output}" ;; +"a selected area (copy)") maim -s | ${xclip_cmd} ;; +"current window (copy)") maim -q -d 0.2 -i "$(xdotool getactivewindow)" | ${xclip_cmd} ;; +"full screen (copy)") maim -q -d 0.2 | ${xclip_cmd} ;; +esac diff --git a/.local/bin/mounter b/.local/bin/mounter new file mode 100755 index 00000000..f82bccf8 --- /dev/null +++ b/.local/bin/mounter @@ -0,0 +1,93 @@ +#!/bin/bash + +# Mounts Android Phones and USB drives (encrypted or not). This script will +# replace the older `dmenumount` which had extra steps and couldn't handle +# encrypted drives. +# TODO: Remove already mounted Android phones from prompt. +# TODO: Try mount first for drives if in fstab to avoid directory prompt? +# TODO: Add some support for connecting iPhones (although they are annoying). + +set -e + +# Check for phones. +phones="$(simple-mtpfs -l 2>/dev/null | sed "s/^/๐Ÿ“ฑ/")" +# Check for drives. +alldrives="$(lsblk -rpo "uuid,name,type,size,label,mountpoint,fstype")" +# Get all LUKS drives +allluks="$(echo "$alldrives" | grep crypto_LUKS)" || true +# Get a list of the LUKS drive UUIDs already decrypted. +decrypted="$(find /dev/disk/by-id/dm-uuid-CRYPT-LUKS2-* | sed "s|.*LUKS2-||;s|-.*||")" +# Functioning for formatting drives correctly for dmenu: +filter() { sed "s/ /:/g" | awk -F':' '$7==""{printf "%s%s (%s) %s\n",$1,$3,$5,$6}'; } + +# Get only LUKS drives that are not decrypted. +IFS=' +' +unopenedluks="$(for drive in $allluks; do + uuid="${drive%% *}" + uuid="${uuid//-/}" # This is a bashism. + for open in $decrypted; do + [ "$uuid" = "$open" ] && break 1 + done && continue 1 + echo "๐Ÿ”’ $drive" +done | filter)" + +# Get all normal, non-encrypted or decrypted partitions that are not mounted. +normalparts="$(echo "$alldrives" | grep -v crypto_LUKS | grep 'part\|rom\|crypt' | sed "s/^/๐Ÿ’พ /" | filter)" + +# Add all to one variable. If no mountable drives found, exit. +alldrives="$(echo "$phones +$unopenedluks +$normalparts" | sed "/^$/d;s/ *$//")" +test -n "$alldrives" + +# Feed all found drives to dmenu and get user choice. +chosen="$(echo "$alldrives" | dmenu -p "Mount which drive?" -i)" + +# Function for prompting user for a mountpoint. +getmount() { + mp="$(find /mnt /media /mount /home -maxdepth 1 -type d 2>/dev/null | dmenu -i -p "Mount this drive where?")" + test -n "$mp" + if [ ! -d "$mp" ]; then + mkdiryn=$(printf "No\\nYes" | dmenu -i -p "$mp does not exist. Create it?") + [ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo -A mkdir -p "$mp") + fi +} + +case "$chosen" in +๐Ÿ’พ*) + chosen="${chosen%% *}" + chosen="${chosen:1}" # This is a bashism. + getmount + sudo mount "$chosen" "$mp" + notify-send "๐Ÿ’พDrive Mounted." "$chosen mounted to $mp." + ;; + +๐Ÿ”’*) + chosen="${chosen%% *}" + chosen="${chosen:1}" # This is a bashism. + # Number the drive. + while true; do + [ -f "/dev/mapper/usb$num" ] || break + num="$(printf "%02d" "$((num + 1))")" + done + + # Decrypt in a terminal window + ${TERMINAL:-st} -n floatterm -g 60x1 -e sudo cryptsetup open "$chosen" "usb$num" + # Check if now decrypted. + test -b "/dev/mapper/usb$num" + + getmount + sudo mount "/dev/mapper/usb$num" "$mp" + notify-send "๐Ÿ”“Decrypted drive Mounted." "$chosen decrypted and mounted to $mp." + ;; + +๐Ÿ“ฑ*) + getmount + echo "OK" | dmenu -i -p "Tap Allow on your phone if it asks for permission and then press enter" + chosen="${chosen%%:*}" + chosen="${chosen:1}" # This is a bashism. + simple-mtpfs --device "$chosen" "$mp" + notify-send "๐Ÿค– Android Mounted." "Android device mounted to $mp." + ;; +esac diff --git a/.local/bin/opout b/.local/bin/opout new file mode 100755 index 00000000..719275d3 --- /dev/null +++ b/.local/bin/opout @@ -0,0 +1,16 @@ +#!/bin/sh + +# opout: "open output": A general handler for opening a file's intended output, +# usually the pdf of a compiled document. I find this useful especially +# running from vim. + +basename="${1%.*}" + +case "${*}" in +*.tex | *.sil | *.m[dse] | *.[rR]md | *.mom | *.[0-9]) + target="$(getcomproot "$1" || echo "$1")" + setsid -f xdg-open "${target%.*}".pdf >/dev/null 2>&1 + ;; +*.html) setsid -f "$BROWSER" "$basename".html >/dev/null 2>&1 ;; +*.sent) setsid -f sent "$1" >/dev/null 2>&1 ;; +esac diff --git a/.local/bin/remaps b/.local/bin/remaps new file mode 100755 index 00000000..f8409b67 --- /dev/null +++ b/.local/bin/remaps @@ -0,0 +1,15 @@ +#!/bin/sh + +# This script is called on startup to remap keys. +# Decrease key repeat delay to 300ms and increase key repeat rate to 50 per second. +xset r rate 300 50 + +# Map the caps lock key to super, and map the menu key to right super. +# setxkbmap -option caps:super,altwin:menu_win + +# When caps lock is pressed only once, treat it as escape. +killall xcape 2>/dev/null +# xcape -e 'Super_L=Escape' + +# Turn off caps lock if on since there is no longer a key for it. +xset -q | grep "Caps Lock:\s*on" && xdotool key Caps_Lock diff --git a/.local/bin/rotdir b/.local/bin/rotdir new file mode 100755 index 00000000..86da6dbe --- /dev/null +++ b/.local/bin/rotdir @@ -0,0 +1,12 @@ +#!/bin/sh + +# When I open an image from the file manager in sxiv (the image viewer), I want +# to be able to press the next/previous keys to key through the rest of the +# images in the same directory. This script "rotates" the content of a +# directory based on the first chosen file, so that if I open the 15th image, +# if I press next, it will go to the 16th etc. Autistic, I know, but this is +# one of the reasons that sxiv is great for being able to read standard input. + +[ -z "$1" ] && echo "usage: rotdir regex 2>&1" && exit 1 +base="$(basename "$1")" +ls "$PWD" | awk -v BASE="$base" 'BEGIN { lines = ""; m = 0; } { if ($0 == BASE) { m = 1; } } { if (!m) { if (lines) { lines = lines"\n"; } lines = lines""$0; } else { print $0; } } END { print lines; }' diff --git a/.local/bin/setbg b/.local/bin/setbg new file mode 100755 index 00000000..c17841cc --- /dev/null +++ b/.local/bin/setbg @@ -0,0 +1,37 @@ +#!/bin/sh + +# This script does the following: +# Run by itself, set the wallpaper (at X start). +# If given a file, set that as the new wallpaper. +# If given a directory, choose random file in it. +# If wal is installed, also generates a colorscheme. + +# Location of link to wallpaper link. +bgloc="${XDG_DATA_HOME:-$HOME/.local/share}/bg" + +# Configuration files of applications that have their themes changed by pywal. +dunstconf="${XDG_CONFIG_HOME:-$HOME/.config}/dunst/dunstrc" +zathuraconf="${XDG_CONFIG_HOME:-$HOME/.config}/zathura/zathurarc" + +trueloc="$(readlink -f "$1")" && + case "$(file --mime-type -b "$trueloc")" in + image/*) ln -sf "$(readlink -f "$1")" "$bgloc" && notify-send -i "$bgloc" "Changing wallpaper..." ;; + inode/directory) ln -sf "$(find "$trueloc" -iregex '.*.\(jpg\|jpeg\|png\|gif\)' -type f | shuf -n 1)" "$bgloc" && notify-send -i "$bgloc" "Random Wallpaper chosen." ;; + *) + notify-send "๐Ÿ–ผ๏ธ Error" "Not a valid image or directory." + exit 1 + ;; + esac + +# If pywal is installed, use it. +if command -v wal >/dev/null 2>&1; then + wal -n -i "$(readlink -f $bgloc)" -o "${XDG_CONFIG_HOME:-$HOME/.config}/wal/postrun" >/dev/null 2>&1 + # If pywal is removed, return config files to normal. +else + [ -f "$dunstconf.bak" ] && unlink "$dunstconf" && mv "$dunstconf.bak" "$dunstconf" + [ -f "$zathuraconf.bak" ] && unlink "$zathuraconf" && mv "$zathuraconf.bak" "$zathuraconf" +fi + +xwallpaper --zoom "$bgloc" +# If running, dwm hit the key to refresh the color scheme. +pidof dwm >/dev/null && xdotool key super+F5 diff --git a/.local/bin/td-toggle b/.local/bin/td-toggle new file mode 100755 index 00000000..429c0c62 --- /dev/null +++ b/.local/bin/td-toggle @@ -0,0 +1,11 @@ +#!/bin/sh + +# If transmission-daemon is running, will ask to kill, else will ask to start. + +if pidof transmission-daemon >/dev/null; then + [ "$(printf "No\\nYes" | dmenu -i -p "Turn off transmission-daemon?")" = "Yes" ] && killall transmission-daemon && notify-send "transmission-daemon disabled." +else + ifinstalled transmission-cli || exit + [ "$(printf "No\\nYes" | dmenu -i -p "Turn on transmission daemon?")" = "Yes" ] && transmission-daemon && notify-send "transmission-daemon enabled." +fi +sleep 3 && pkill -RTMIN+7 "${STATUSBAR:-dwmblocks}" diff --git a/.local/bin/texclear b/.local/bin/texclear new file mode 100755 index 00000000..8eed1940 --- /dev/null +++ b/.local/bin/texclear @@ -0,0 +1,15 @@ +#!/bin/sh + +# Clears the build files of a LaTeX/XeLaTeX build. +# I have vim run this file whenever I exit a .tex file. + +case "$1" in +*.tex) + file=$(readlink -f "$1") + dir=$(dirname "$file") + base="${file%.*}" + find "$dir" -maxdepth 1 -type f -regextype gnu-awk -regex "^$base\\.(4tc|xref|tmp|pyc|pyg|pyo|fls|vrb|fdb_latexmk|bak|swp|aux|log|synctex\\(busy\\)|lof|lot|maf|idx|mtc|mtc0|nav|out|snm|toc|bcf|run\\.xml|synctex\\.gz|blg|bbl)" -delete + rm -rdf "$dir/_minted-$(basename -- "$base")" + ;; +*) printf "Give .tex file as argument.\\n" ;; +esac diff --git a/.local/bin/tordone b/.local/bin/tordone new file mode 100755 index 00000000..e88a0dd6 --- /dev/null +++ b/.local/bin/tordone @@ -0,0 +1,8 @@ +#/bin/sh +# * TR_APP_VERSION +# * TR_TIME_LOCALTIME +# * TR_TORRENT_DIR +# * TR_TORRENT_HASH +# * TR_TORRENT_ID +# * TR_TORRENT_NAME +notify-send "Transmission-daemon" "$TR_TORRENT_NAME has completely downloaded." diff --git a/.local/bin/torwrap b/.local/bin/torwrap new file mode 100755 index 00000000..2049b08d --- /dev/null +++ b/.local/bin/torwrap @@ -0,0 +1,8 @@ +#!/bin/sh + +ifinstalled stig transmission-cli || exit + +! pidof transmission-daemon >/dev/null && transmission-daemon && notify-send "Starting torrent daemon..." + +$TERMINAL -e stig +pkill -RTMIN+7 "${STATUSBAR:-dwmblocks}" diff --git a/.local/bin/transadd b/.local/bin/transadd new file mode 100755 index 00000000..a598fad3 --- /dev/null +++ b/.local/bin/transadd @@ -0,0 +1,9 @@ +#!/bin/sh + +# Mimeapp script for adding torrent to transmission-daemon, but will also start the daemon first if not running. + +# transmission-daemon sometimes fails to take remote requests in its first moments, hence the sleep. + +pidof transmission-daemon >/dev/null || (transmission-daemon && notify-send "Starting transmission daemon..." && sleep 3 && pkill -RTMIN+7 "${STATUSBAR:-dwmblocks}") + +transmission-remote -a "$@" && notify-send "๐Ÿ”ฝ Torrent added." diff --git a/.local/bin/unmounter b/.local/bin/unmounter new file mode 100755 index 00000000..126f1b81 --- /dev/null +++ b/.local/bin/unmounter @@ -0,0 +1,28 @@ +#!/bin/sh + +# Unmount USB drives or Android phones. Replaces the older `dmenuumount`. Fewer +# prompt and also de-decrypts LUKS drives that are unmounted. + +set -e + +mounteddroids="$(grep simple-mtpfs /etc/mtab | awk '{print "๐Ÿ“ฑ" $2}')" +lsblkoutput="$(lsblk -nrpo "name,type,size,mountpoint")" +mounteddrives="$(echo "$lsblkoutput" | awk '($2=="part"||$2="crypt")&&$4!~/\/boot|\/home$|SWAP/&&length($4)>1{printf "๐Ÿ’พ%s (%s)\n",$4,$3}')" + +allunmountable="$(echo "$mounteddroids +$mounteddrives" | sed "/^$/d;s/ *$//")" +test -n "$allunmountable" + +chosen="$(echo "$allunmountable" | dmenu -i -p "Unmount which drive?")" +chosen="${chosen%% *}" +test -n "$chosen" + +doas umount -l "/${chosen#*/}" +notify-send "Device unmounted." "$chosen has been unmounted." + +# Close the chosen drive if decrypted. +cryptid="$(echo "$lsblkoutput" | grep "/${chosen#*/}$")" +cryptid="${cryptid%% *}" +test -b /dev/mapper/"${cryptid##*/}" +doas cryptsetup close "$cryptid" +notify-send "๐Ÿ”’Device dencryption closed." "Drive is now securely locked again."