~metalune/Dotfiles

ef6119daaee743c687d3ca80aa7c6b62e61e19e4 — metalune 20 days ago a34b7c4
Add .local/bin directory
A .local/bin/dmenu-kill-process => .local/bin/dmenu-kill-process +19 -0
@@ 0,0 1,19 @@
#!/usr/bin/env bash

lines="-l 20"

selected="$(ps -a -u $USER | \
            dmenu -sb "#478061" -i -p "Select process to kill" $lines | \
            awk '{print $1" "$4}')";

if [[ ! -z $selected ]]; then
    answer="$(echo -e "Yes\nNo" | \
                dmenu -sb "#478061" -i -p "Are you sure you wanna kill $selected?", $lines)"

    if [[ $answer == "Yes" ]]; then
        selpid="$(awk '{print $1}' <<< $selected)";
        kill -9 $selpid
    fi
fi

exit 0

A .local/bin/statusbar/battery => .local/bin/statusbar/battery +34 -0
@@ 0,0 1,34 @@
#!/bin/sh

# Prints all batteries, their percentage remaining and an emoji corresponding
# to charge status (🔌 for plugged up, 🔋 for discharging on battery, etc.).

case $BLOCK_BUTTON in
	3) notify-send "🔋 Battery module" "-: discharging
N: not charging
S: stagnant charge
C: charging
D: charged
!: battery very low!
- Scroll to change adjust xbacklight." ;;
	4) xbacklight -inc 10 ;;
	5) xbacklight -dec 10 ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac 
# acpi alternative
# acpi | sed "s/Battery [0-9]: //;s/[Dd]ischarging, /🔋/;s/[Nn]ot charging, /🛑/;s/[Cc]harging, /🔌/;s/[Uu]nknown, /♻️/;s/[Ff]ull, /⚡/;s/ \(remaining\|until charged\)//"; exit

# Loop through all attached batteries.
for battery in /sys/class/power_supply/BAT?
do
	# Get its remaining capacity and charge status.
	capacity=$(cat "$battery"/capacity 2>/dev/null) || break
	status=$(sed "s/[Dd]ischarging/.o./;s/[Nn]ot charging/x_x/;s/[Cc]harging/^_^/;s/[Uu]nknown/O.o/;s/[Ff]ull/O_O/" "$battery"/status)

	# If it is discharging and 25% or less, we will add a ❗ as a warning.
	 [ "$capacity" -le 25 ] && [ "$status" = "!" ] && warn="!"

     printf "%s%s%% %s \n" "$warn" "$capacity" "$status"
	unset warn
done
# | sed 's/ *$//'

A .local/bin/statusbar/clock => .local/bin/statusbar/clock +14 -0
@@ 0,0 1,14 @@
#!/bin/sh

clock=$(date '+%I')
icon="TIME:"

case $BLOCK_BUTTON in
	1) notify-send "This Month" "$(cal --color=always | sed "s/..7m/<b><span color=\"red\">/;s/..27m/<\/span><\/b>/")" && notify-send "Appointments" "$(calcurse -d3)" ;;
	2) setsid -f "$TERMINAL" -e calcurse ;;
	3) notify-send "📅 Time/date module" "\- Left click to show upcoming appointments for the next three days via \`calcurse -d3\` and show the month via \`cal\`
- Middle click opens calcurse if installed" ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

date "+ DATE: %d-%b-%Y (%a) $icon %I:%M%p "

A .local/bin/statusbar/cpu => .local/bin/statusbar/cpu +12 -0
@@ 0,0 1,12 @@
#!/bin/sh

case $BLOCK_BUTTON in
	1) notify-send "CPU: CPU hogs" "$(ps axch -o cmd:15,%cpu --sort=-%cpu | head)\\n(100% per core)" ;;
	2) setsid -f "$TERMINAL" -e htop ;;
	3) notify-send "CPU: CPU module " "\- Shows CPU temperature.
- Click to show intensive processes.
- Middle click to open htop." ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

printf " %s \n" "$(sensors | awk '/Core 0/ {print "CPU: "$3} ')"

A .local/bin/statusbar/cpubar => .local/bin/statusbar/cpubar +44 -0
@@ 0,0 1,44 @@
#!/bin/sh

# Module showing CPU load as a changing bars.
# Just like in polybar.
# Each bar represents amount of load on one core since
# last run.

# Cache in tmpfs to improve speed and reduce SSD load
cache=/tmp/cpubarscache

case $BLOCK_BUTTON in
    2) setsid -f "$TERMINAL" -e htop ;;
    3) notify-send "🪨 CPU load module" "Each bar represents
one CPU core";;
    6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

# id total idle
stats=$(awk '/cpu[0-9]+/ {printf "%d %d %d\n", substr($1,4), ($2 + $3 + $4 + $5), $5 }' /proc/stat)
[ ! -f $cache ] && echo "$stats" > "$cache"
old=$(cat "$cache")
printf "🪨"
echo "$stats" | while read -r row; do
    id=${row%% *}
    rest=${row#* }
    total=${rest%% *}
    idle=${rest##* }

    case "$(echo "$old" | awk '{if ($1 == id)
        printf "%d\n", (1 - (idle - $3)  / (total - $2))*100 /12.5}' \
        id="$id" total="$total" idle="$idle")" in

        "0") printf "▁";;
        "1") printf "▂";;
        "2") printf "▃";;
        "3") printf "▄";;
        "4") printf "▅";;
        "5") printf "▆";;
        "6") printf "▇";;
        "7") printf "█";;
        "8") printf "█";;
    esac
done; printf "\\n"
echo "$stats" > "$cache"

A .local/bin/statusbar/disk => .local/bin/statusbar/disk +23 -0
@@ 0,0 1,23 @@
#!/bin/sh

# Status bar module for disk space
# $1 should be drive mountpoint, otherwise assumed /.

location=${1:-/}

[ -d "$location" ] || exit

case $BLOCK_BUTTON in
	1) notify-send "💽 Disk space" "$(df -h --output=target,used,size)" ;;
	3) notify-send "💽 Disk module" "\- Shows used hard drive space.
- Click to show all disk info." ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

case "$location" in
	"/home"* ) icon="🏠" ;;
	"/mnt"* ) icon="💾" ;;
	*) icon="🖥";;
esac

printf "%s: %s\n" "$icon" "$(df -h "$location" | awk ' /[0-9]/ {print $3 "/" $2}')"

A .local/bin/statusbar/internet => .local/bin/statusbar/internet +22 -0
@@ 0,0 1,22 @@
#!/bin/sh

# Show wifi 📶 and percent strength or 📡 if none.
# Show 🌐 if connected to ethernet or ❎ if none.

case $BLOCK_BUTTON in
	1) "$TERMINAL" -e nmtui; pkill -RTMIN+4 dwmblocks ;;
	3) notify-send "🌐 Internet module" "\- Click to connect
NW: no wifi connection
W: wifi connection with quality
NE: no ethernet
E: ethernet working
" ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

case "$(cat /sys/class/net/w*/operstate 2>/dev/null)" in
	down) wifiicon="📡 " ;;
	up) wifiicon="$(awk '/^\s*w/ { print "Q", int($3 * 100 / 70) "% " }' /proc/net/wireless)" ;;
esac

printf " %s%s \n" "$wifiicon" "$(sed "s/down/D/;s/up/U/" /sys/class/net/e*/operstate 2>/dev/null)"

A .local/bin/statusbar/iplocate => .local/bin/statusbar/iplocate +10 -0
@@ 0,0 1,10 @@
#!/bin/sh

# Gets your public ip address checks which country you are in and
# displays that information in the statusbar
#
# https://www.maketecheasier.com/ip-address-geolocation-lookups-linux/

ifinstalled "geoip" || exit
addr="$(curl ifconfig.me 2>/dev/null)" || exit
grep "flag: " "${XDG_DATA_HOME:-$HOME/.local/share}/larbs/emoji" | grep "$(geoiplookup "$addr" | sed 's/.*, //')" | sed "s/flag: //;s/;.*//"

A .local/bin/statusbar/mailbox => .local/bin/statusbar/mailbox +20 -0
@@ 0,0 1,20 @@
#!/bin/sh

# Displays number of unread mail and an loading icon if updating.
# When clicked, brings up `neomutt`.

case $BLOCK_BUTTON in
	1) setsid -f "$TERMINAL" -e neomutt ;;
	2) setsid -f mw sync >/dev/null ;;
	3) notify-send "📬 Mail module" "\- Shows unread mail
- Shows 🔃 if syncing mail
- Left click opens neomutt
- Middle click syncs mail" ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

unread="$(find "${XDG_DATA_HOME:-$HOME/.local/share}"/mail/*/[Ii][Nn][Bb][Oo][Xx]/new/* -type f | wc -l 2>/dev/null)"

pidof mbsync >/dev/null 2>&1 && icon="🔃"

[ "$unread" = "0" ] && [ "$icon" = "" ] || echo " 📬 $unread$icon "

A .local/bin/statusbar/memory => .local/bin/statusbar/memory +3 -0
@@ 0,0 1,3 @@
#!/bin/sh

free --mebi | sed -n '2{p;q}' | awk '{printf (" MEM: %2.0fMiB \n", ($3))}'

A .local/bin/statusbar/moonphase => .local/bin/statusbar/moonphase +37 -0
@@ 0,0 1,37 @@
#!/bin/sh

# Shows the current moon phase.

moonfile="${XDG_DATA_HOME:-$HOME/.local/share}/moonphase"

[ "$(stat -c %y "$moonfile" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] ||
    { curl -sf "wttr.in/?format=%m" > "$moonfile" || exit 1 ;}

icon="$(cat "$moonfile")"

case "$icon" in
    🌑) name="New" ;;
    🌒) name="Waxing Crescent" ;;
    🌓) name="First Quarter" ;;
    🌔) name="Waxing Gibbous" ;;
    🌕) name="Full" ;;
    🌖) name="Waning Gibbous" ;;
    🌗) name="Last Quarter" ;;
    🌘) name="Waning Crescent" ;;
    *) exit 1 ;;
esac

echo "${icon-?}"

case $BLOCK_BUTTON in
    3) notify-send "🌜 Moon phase module" "Displays current moon phase.
- 🌑: New
- 🌒: Waxing Crescent
- 🌓: First Quarter
- 🌔: Waxing Gibbous
- 🌕: Full
- 🌖: Waning Gibbous
- 🌗: Last Quarter
- 🌘: Waning Crescent" ;;
    6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

A .local/bin/statusbar/nettraf => .local/bin/statusbar/nettraf +29 -0
@@ 0,0 1,29 @@
#!/bin/sh

# Module showing network traffic. Shows how much data has been received (RX) or
# transmitted (TX) since the previous time this script ran. So if run every
# second, gives network traffic per second.

case $BLOCK_BUTTON in
	1) setsid -f "$TERMINAL" -e bmon ;;
	3) notify-send "🌐 Network traffic module" "🔻: Traffic received
🔺: Traffic transmitted" ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

update() {
    sum=0
    for arg; do
        read -r i < "$arg"
        sum=$(( sum + i ))
    done
    cache=${XDG_CACHE_HOME:-$HOME/.cache}/${1##*/}
    [ -f "$cache" ] && read -r old < "$cache" || old=0
    printf %d\\n "$sum" > "$cache"
    printf %d\\n $(( sum - old ))
}

rx=$(update /sys/class/net/[ew]*/statistics/rx_bytes)
tx=$(update /sys/class/net/[ew]*/statistics/tx_bytes)

printf "RX: %4sB TX: %4sB\\n" $(numfmt --to=iec $rx) $(numfmt --to=iec $tx)

A .local/bin/statusbar/pacpackages => .local/bin/statusbar/pacpackages +29 -0
@@ 0,0 1,29 @@
#!/bin/sh

# Displays number of upgradeable packages.
# For this to work, have a `pacman -Sy` command run in the background as a
# cronjob every so often as root. This script will then read those packages.
# When clicked, it will run an upgrade via pacman.
#
# Add the following text as a file in /usr/share/libalpm/hooks/statusbar.hook:
#
# [Trigger]
# Operation = Upgrade
# Type = Package
# Target = *
#
# [Action]
# Description = Updating statusbar...
# When = PostTransaction
# Exec = /usr/bin/pkill -RTMIN+8 dwmblocks # Or i3blocks if using i3.

case $BLOCK_BUTTON in
	1) setsid -f "$TERMINAL" -e popupgrade ;;
	2) notify-send "$(/usr/bin/pacman -Qu)" ;;
	3) notify-send "🎁 Upgrade module" "📦: number of upgradable packages
- Left click to upgrade packages
- Middle click to show upgradable packages" ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

pacman -Qu | grep -Fcv "[ignored]" | sed "s/^/📦/;s/^📦0$//g"

A .local/bin/statusbar/popupgrade => .local/bin/statusbar/popupgrade +9 -0
@@ 0,0 1,9 @@
#!/bin/sh

printf "Beginning upgrade.\\n"

yay -Syu
pkill -RTMIN+8 "${STATUSBAR:-dwmblocks}"

printf "\\nUpgrade complete.\\nPress <Enter> to exit window.\\n\\n"
read -r

A .local/bin/statusbar/price => .local/bin/statusbar/price +38 -0
@@ 0,0 1,38 @@
#!/bin/dash

# Usage:
#	price <url> <Name of currency> <icon>
#	price bat "Basic Attention Token" 🦁
# When the name of the currency is multi-word, put it in quotes.

[ -z "$3" ] && exit 1
interval="@14d"	# History contained in chart preceded by '@' (7d = 7 days)
dir="${XDG_DATA_HOME:-$HOME/.local/share}/crypto-prices"
pricefile="$dir/$1"
chartfile="$dir/$1-chart"

updateprice() { ping -q -c 1 example.org >/dev/null 2>&1 &&
	curl -s "rate.sx/1$1" > "$pricefile" &&
	curl -s "rate.sx/$1$interval" > "$chartfile" ;}

[ -d "$dir" ] || mkdir -p "$dir"

[ "$(stat -c %x "$pricefile" 2>/dev/null | cut -d' ' -f1)" != "$(date '+%Y-%m-%d')" ] &&
	updateprice "$1"

case $BLOCK_BUTTON in
	1) setsid "$TERMINAL" -e less -Sf "$chartfile" ;;
	2) notify-send -u low "$3 Updating..." "Updating $2 price..."
		updateprice "$1" && notify-send "$3 Update complete." "$2 price is now
\$$(cat "$pricefile")" ;;
	3) uptime="$(date -d "$(stat -c %x "$pricefile")" '+%D at %T' | sed "s|$(date '+%D')|Today|")"
		notify-send "$3 $2 module" "\- <b>Exact price: \$$(cat "$pricefile")</b>
- Left click for chart of changes.
- Middle click to update.
- Shows 🔃 if updating prices.
- <b>Last updated:
	$uptime</b>" ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

printf "$3$%0.2f" "$(cat "$pricefile")"

A .local/bin/statusbar/statusbar => .local/bin/statusbar/statusbar +13 -0
@@ 0,0 1,13 @@
#!/bin/sh

while true
do

    date=$(date +'%d.%m.%Y')
    time=$(date +'%l:%M %p')
    memory=$(~/.local/bin/statusbar/memory)
    battery=$(~/.local/bin/statusbar/battery)
    printf "%s|%s| %s |%s\n" "$battery" "$memory" "$date" "$time"
    
    sleep 10
done

A .local/bin/statusbar/volume => .local/bin/statusbar/volume +28 -0
@@ 0,0 1,28 @@
#!/bin/sh

# Prints the current volume or 🔇 if muted.

case $BLOCK_BUTTON in
	1) setsid -f "$TERMINAL" -e pulsemixer ;;
	2) pamixer -t ;;
	4) pamixer --allow-boost -i 1 ;;
	5) pamixer --allow-boost -d 1 ;;
	3) notify-send "📢 Volume module" "\- Shows volume 🔊, 🔇 if muted.
- Middle click to mute.
- Scroll to change." ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

[ $(pamixer --get-mute) = true ] && echo 🔇 && exit

vol="$(pamixer --get-volume)"

if [ "$vol" -gt "70" ]; then
	icon="🔊"
elif [ "$vol" -lt "30" ]; then
	icon="🔈"
else
	icon="🔉"
fi

echo " $icon $vol% "

A .local/bin/statusbar/weather => .local/bin/statusbar/weather +35 -0
@@ 0,0 1,35 @@
#!/bin/sh

# Displays todays precipication chance (☔) and daily low (🥶) and high (🌞).
# Usually intended for the statusbar.

# If we have internet, get a weather report from wttr.in and store it locally.
# You could set up a shell alias to view the full file in a pager in the
# terminal if desired. This function will only be run once a day when needed.
weatherreport="${XDG_DATA_HOME:-$HOME/.local/share}/weatherreport"
getforecast() { curl -sf "wttr.in/$LOCATION" > "$weatherreport" || exit 1 ;}

# Some very particular and terse stream manipulation. We get the maximum
# precipitation chance and the daily high and low from the downloaded file and
# display them with coresponding emojis.
showweather() { printf "%s" "$(sed '16q;d' "$weatherreport" |
	grep -wo "[0-9]*%" | sort -rn | sed "s/^/☔ /g;1q" | tr -d '\n')"
sed '13q;d' "$weatherreport" | grep -o "m\\([-+]\\)*[0-9]\\+" | sort -n -t 'm' -k 2n | sed -e 1b -e '$!d' | tr '\n|m' ' ' | awk '{print " 🥶 " $1 "°","🌞 " $2 "°"}' ;}

case $BLOCK_BUTTON in
	1) setsid -f "$TERMINAL" -e less -Srf "$weatherreport" ;;
	2) getforecast && showweather ;;
	3) notify-send "🌈 Weather module" "\- Left click for full forecast.
- Middle click to update forecast.
☔: Chance of rain/snow
🥶: Daily low
🌞: Daily high" ;;
	6) "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

# The test if our forcecast is updated to the day. If it isn't download a new
# weather report from wttr.in with the above function.
[ "$(stat -c %y "$weatherreport" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] ||
	getforecast

printf "%s " $(showweather)