dotfiles/.config/zsh/utils.zsh

164 lines
4.2 KiB
Bash

# Utility functions
function copylastcmd() {
echo -n $(fc -ln -1) | wl-copy
}
function getkernelpkg() {
if [[ ${OS_ID} == "arch" ]]; then
pacman -Qqo /usr/lib/modules/$(uname -r)/vmlinuz
else
echo "Unsupported OS" >&2
fi
}
function docker() {
# Wrapper for docker command that executes it inside the systemd-nspawn docker container
# This container has our home directory bound, so we should cd to the current directory before running this command
ssh -t dockerbox "cd $(pwd); /usr/bin/docker $*"
}
function random-xkcd() {
blocklist=(
"404"
"631" # "Anatomy Text", don't want that coming up in public thanks
)
link="https://xkcd.com/404/"
while (( $blocklist[(Ie)$(basename $link)] )); do
link=$(curl https://c.xkcd.com/random/comic/ -v |& grep location | cut -d' ' -f3 | sed 's/\r//')
done
html=$(curl -L ${link/http:/https:} 2>/dev/null)
name=$(echo $html | xmllint --html --xpath 'string(//html/body/div[@id="middleContainer"]/div[@id="ctitle"])' - 2>/dev/null)
title=$(echo $html | xmllint --html --xpath 'string(//html/body/div[@id="middleContainer"]/div[@id="comic"]/img/@title)' - 2>/dev/null)
echo $link
echo -e "\033[1;34m$name\033[0m"
echo -e "\033[1;33m$title\033[0m"
curl $(echo $html | (echo "https:"$(xmllint --html --xpath 'string(//html/body/div[@id="middleContainer"]/div[@id="comic"]/img/@src)' - 2>/dev/null))) 2>/dev/null | feh -
}
if [[ $(hostnamectl chassis) == "container" ]]; then
DEFAULT_WINDOW_NAME="Container Connection: [ $os_icon $hostname Console ]"
elif [[ -z "${SSH_CONNECTION+x}" ]]; then
DEFAULT_WINDOW_NAME="$os_icon $hostname Console"
else
DEFAULT_WINDOW_NAME="Remote Connection: [ $os_icon $hostname Console ]"
fi
function _rename_window() {
echo -ne "\033]0;$@\007"
}
function _reset_window_name() {
_rename_window "$DEFAULT_WINDOW_NAME"
}
function rename_window() {
_rename_window "$@"
DEFAULT_WINDOW_NAME="$@"
}
_rename_window "$DEFAULT_WINDOW_NAME"
function ssh-clean() {
# Clean up ssh connection sockets
for i in $(find ~/.ssh/cm/ -type s); do
fname=$(basename $i)
conn=$(echo $fname | cut -d- -f2-)
user=$(echo $conn | cut -d@ -f1)
host=$(echo $conn | cut -d@ -f2 | cut -d: -f1)
echo "Closing connection to $user@$host"
ssh -O exit -S $i -q _ 2>/dev/null
done
}
function lsssh() {
# List active ssh connections
for i in $(find ~/.ssh/cm/ -type s); do
fname=$(basename $i)
conn=$(echo $fname | cut -d- -f2-)
user=$(echo $conn | cut -d@ -f1)
host=$(echo $conn | cut -d@ -f2 | cut -d: -f1)
echo "$user@$host"
done
}
function bwunlock() {
# Unlock Bitwarden vault and export session key
local bwfd
local bwpasswd
local secretstore
local bwcmd
local skipkeyring
# Use default Bitwarden account
secretstore="bw"
bwcmd="bw"
skipkeyring=0
while [[ $# -gt 0 ]]; do
case $1 in
--personal)
# Use personal Bitwarden account
secretstore="bw-personal"
bwcmd="env BITWARDENCLI_APPDATA_DIR=$HOME/.config/bw-cli-personal bw"
;;
--debug)
# Enable debug output
set -x
;;
--ask)
# Prompt for Bitwarden password
skipkeyring=1
;;
*)
echo "Unknown option: $1" >&2
;;
esac
shift
done
# Attempt to read Bitwarden password from keyring
if ! (( skipkeyring )); then
bwpasswd=$(kwallet-query -r bitwarden kdewallet 2>/dev/null)
fi
if [[ -z "${bwpasswd}" ]]; then
# If password is not found in keyring, prompt for it
read -s -p "Enter Bitwarden password: " bwpasswd
echo
fi
# Unlock Bitwarden vault and export session key
export BW_SESSION=$(eval $bwcmd unlock --raw "'$bwpasswd'" 2>/dev/tty)
if [[ -z "${BW_SESSION}" ]]; then
echo "Failed to unlock Bitwarden vault" >&2
return 1
else
echo "Bitwarden vault unlocked"
fi
}
function urlencode {
# credit to https://stackoverflow.com/a/34407620 for the jq solution
local multiline=0
if [[ "$1" == "-m" ]] || [[ "$1" == "--multiline" ]]; then
# Multiline mode, read directly from stdin
multiline=1
shift
fi
if [[ -z "$1" ]]; then
# Read from stdin
if (( multiline )); then
jq -sRr '@uri'
else
# Treat each line as a separate argument, and encode them separately
for line in $(cat); do
echo -n "$line" | jq -sRr '@uri'
done
fi
else
# Read from arguments (use echo because arguments may contain spaces and --arg wasn't working)
echo -n $@ | jq -sRr '@uri'
fi
}