From 39faa6264cb18f5dd839ab13ecbe72680df11f2c Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Sat, 5 Oct 2019 10:02:23 -0400 Subject: [PATCH] Add polybar scripts / modules for pseudo-caffeine, weather, wpm, idle time --- bin/.local/bin/i3scripts/info-hackspeed | 63 ++++++++++++++++ bin/.local/bin/i3scripts/info-idle | 25 +++++++ bin/.local/bin/i3scripts/weathermap | 96 +++++++++++++++++++++++++ bin/.local/bin/i3scripts/xoffee | 23 ++++++ polybar/.config/polybar/config | 59 +++++++++------ 5 files changed, 244 insertions(+), 22 deletions(-) create mode 100755 bin/.local/bin/i3scripts/info-hackspeed create mode 100755 bin/.local/bin/i3scripts/info-idle create mode 100755 bin/.local/bin/i3scripts/weathermap create mode 100755 bin/.local/bin/i3scripts/xoffee diff --git a/bin/.local/bin/i3scripts/info-hackspeed b/bin/.local/bin/i3scripts/info-hackspeed new file mode 100755 index 0000000..2eef0e6 --- /dev/null +++ b/bin/.local/bin/i3scripts/info-hackspeed @@ -0,0 +1,63 @@ +#!/bin/sh +# shellcheck disable=SC2016,SC2059 + +KEYBOARD_ID="Corsair Corsair STRAFE Gaming Keyboard" + +# cpm: characters per minute +# wpm: words per minute (1 word = 5 characters) +METRIC=wpm +FORMAT=" %d $METRIC" + +INTERVAL=2 + +# If you have a keyboard layout that is not listed here yet, create a condition +# yourself. $3 is the key index. Use `xinput test "AT Translated Set 2 keyboard"` +# to see key codes in real time. Be sure to open a pull request for your +# layout's condition! +LAYOUT=qwerty + +case "$LAYOUT" in + qwerty) CONDITION='($3 >= 10 && $3 <= 19) || ($3 >= 24 && $3 <= 33) || ($3 >= 37 && $3 <= 53) || ($3 >= 52 && $3 <= 58)'; ;; + azerty) CONDITION='($3 >= 10 && $3 <= 19) || ($3 >= 24 && $3 <= 33) || ($3 >= 37 && $3 <= 54) || ($3 >= 52 && $3 <= 57)'; ;; + qwertz) CONDITION='($3 >= 10 && $3 <= 20) || ($3 >= 24 && $3 <= 34) || ($3 == 36) || ($3 >= 38 && $3 <= 48) || ($3 >= 52 && $3 <= 58)'; ;; + dontcare) CONDITION='1'; ;; # Just register all key presses, not only letters and numbers + *) echo "Unsupported layout \"$LAYOUT\""; exit 1; ;; +esac + +# We have to account for the fact we're not listening a whole minute +multiply_by=60 +divide_by=$INTERVAL + +case "$METRIC" in + wpm) divide_by=$((divide_by * 5)); ;; + cpm) ;; + *) echo "Unsupported metric \"$METRIC\""; exit 1; ;; +esac + +hackspeed_cache="$(mktemp -p '' hackspeed_cache.XXXXX)" +trap 'rm "$hackspeed_cache"' EXIT + +# Write a dot to our cache for each key press +printf '' > "$hackspeed_cache" +xinput test "$KEYBOARD_ID" | \ + stdbuf -o0 awk '$1 == "key" && $2 == "press" && ('"$CONDITION"') {printf "."}' >> "$hackspeed_cache" & + +while true; do + # Ask the kernel how big the file is with the command `stat`. The number we + # get is the file size in bytes, which equals the amount of dots the file + # contains, and hence how much keys were pressed since the file was last + # cleared. + lines=$(stat --format %s "$hackspeed_cache") + + # Truncate the cache file so that in the next iteration, we count only new + # keypresses + printf '' > "$hackspeed_cache" + + # The shell only does integer operations, so make sure to first multiply and + # then divide + value=$((lines * multiply_by / divide_by)) + + printf "$FORMAT\\n" "$value" + + sleep $INTERVAL +done diff --git a/bin/.local/bin/i3scripts/info-idle b/bin/.local/bin/i3scripts/info-idle new file mode 100755 index 0000000..ddfdf31 --- /dev/null +++ b/bin/.local/bin/i3scripts/info-idle @@ -0,0 +1,25 @@ +#!/bin/bash +## Author: Shaun Reed | Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ## +## ## +## A script to track system idle time within a polybar menu ## +############################################################################### +# info-idle.sh + + +METRIC=sec +FORMAT=" %d $METRIC" +INTERVAL=2 + +case "$METRIC" in + min) DIVIDE_BY=$((1000 * 60)); ;; + sec) DIVIDE_BY=1000; ;; + msec) DIVIDE_BY=1 ;; + *) echo "Unsupported metric \"$METRIC\""; exit 1; ;; +esac + +while true; do + VALUE=$(($(xprintidle)/ DIVIDE_BY)) + printf "$FORMAT\\n" "$VALUE" + sleep $INTERVAL +done + diff --git a/bin/.local/bin/i3scripts/weathermap b/bin/.local/bin/i3scripts/weathermap new file mode 100755 index 0000000..ea52c6c --- /dev/null +++ b/bin/.local/bin/i3scripts/weathermap @@ -0,0 +1,96 @@ +#!/bin/sh + +get_icon() { + case $1 in + 01d) icon="";; # Sun clear + 01n) icon="";; # Moon clear + 02d) icon="";; # Sun cloudy + 02n) icon="";; # Moon cloudy + 03*) icon="";; # Cloud + 04*) icon="";; # Clouds + 09d) icon="";; # Day, cloud, rain, wind + 09n) icon="";; # Night, cloud, rain, wind + 10d) icon="";; # Day rain + 10n) icon="";; # Night rain + 11d) icon="";; # Day, rain, lightning + 11n) icon="";; # Night, rain, lightning + 13d) icon="";; # Day, snow + 13n) icon="";; # Night, snow + 50d) icon="";; # Day, fog + 50n) icon="";; # Night, fog + *) icon=""; # Sun clear, default icon + esac + + echo $icon +} + +get_duration() { + + osname=$(uname -s) + + case $osname in + *BSD) date -r "$1" -u +%H:%M;; + *) date --date="@$1" -u +%H:%M;; + esac + +} + +KEY="3fbb0282dfd1ba8295bcb93b329fd152" +CITY="5149222" +UNITS="imperial" +SYMBOL="°" + +API="https://api.openweathermap.org/data/2.5" + +if [ -n "$CITY" ]; then + if [ "$CITY" -eq "$CITY" ] 2>/dev/null; then + CITY_PARAM="id=$CITY" + else + CITY_PARAM="q=$CITY" + fi + + current=$(curl -sf "$API/weather?appid=$KEY&$CITY_PARAM&units=$UNITS") + forecast=$(curl -sf "$API/forecast?appid=$KEY&$CITY_PARAM&units=$UNITS&cnt=1") +else + location=$(curl -sf https://location.services.mozilla.com/v1/geolocate?key=geoclue) + + if [ -n "$location" ]; then + location_lat="$(echo "$location" | jq '.location.lat')" + location_lon="$(echo "$location" | jq '.location.lng')" + + current=$(curl -sf "$API/weather?appid=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS") + forecast=$(curl -sf "$API/forecast?appid=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS&cnt=1") + fi +fi + +if [ -n "$current" ] && [ -n "$forecast" ]; then + current_temp=$(echo "$current" | jq ".main.temp" | cut -d "." -f 1) + current_icon=$(echo "$current" | jq -r ".weather[0].icon") + + forecast_temp=$(echo "$forecast" | jq ".list[].main.temp" | cut -d "." -f 1) + forecast_icon=$(echo "$forecast" | jq -r ".list[].weather[0].icon") + + + if [ "$current_temp" -gt "$forecast_temp" ]; then + trend="" # Arrow, down-right slant + elif [ "$forecast_temp" -gt "$current_temp" ]; then + trend="" # Arrow, up-right slant + else + trend="" # Arrow, left-to-right + fi + + + sun_rise=$(echo "$current" | jq ".sys.sunrise") + sun_set=$(echo "$current" | jq ".sys.sunset") + now=$(date +%s) + + if [ "$sun_rise" -gt "$now" ]; then + daytime=" $(get_duration "$((sun_rise-now))")" # Sun rise + elif [ "$sun_set" -gt "$now" ]; then + daytime=" $(get_duration "$((sun_set-now))")" # Sun set + else + daytime=" $(get_duration "$((sun_rise-now))")" # Unknown + fi + + echo "$(get_icon "$current_icon") $current_temp$SYMBOL $trend $(get_icon "$forecast_icon") $forecast_temp$SYMBOL $daytime" +fi diff --git a/bin/.local/bin/i3scripts/xoffee b/bin/.local/bin/i3scripts/xoffee new file mode 100755 index 0000000..436862d --- /dev/null +++ b/bin/.local/bin/i3scripts/xoffee @@ -0,0 +1,23 @@ +#!/bin/bash +## Author: Shaun Reed | Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ## +## ## +## A script to toggle and notify xautolock via polybar menus ## +############################################################################### +# xoffee.sh + +if [ $# -ne 1 ]; then + echo "Incorrect number of arguments provided" + exit 1 +fi + +if [ "$1" = "enable" ]; then + xautolock -disable + notify-send "Caffeine enabled" +elif [ "$1" = "disable" ]; then + xautolock -enable + notify-send "Caffeine disabled" +else + notify-send "Error: Incorrect arg for xoffee script" + exit 1 +fi + diff --git a/polybar/.config/polybar/config b/polybar/.config/polybar/config index 13df9d5..1d3689d 100644 --- a/polybar/.config/polybar/config +++ b/polybar/.config/polybar/config @@ -17,10 +17,10 @@ font-2 = Font Awesome 5 Free Solid:size=8;0 font-3 = Font Awesome 5 Brands Regular:size=8;0 font-4 = Unifont:size=8;0 font-5 = MaterialIcons-Regular:size=10;0 -;font-5 = Icons:size=8;0 -modules-left = powermenu +font-6 = Weather Icons:size=8;0 +modules-left = powermenu weather modules-center = ki3 -modules-right = keyboard volume wired-network clock +modules-right = info-hackspeed keyboard volume wired-network clock [bar/bottom] monitor = ${env:MONITOR} @@ -42,14 +42,25 @@ font-1 = Font Awesome 5 Free Regular:size=10;0 font-2 = Font Awesome 5 Free Solid:size=10;0 font-3 = Font Awesome 5 Brands Regular:size=10;0 font-4 = unifont:size=6;0 -;font-0 = NotoSans-Regular:size=8;0 -;font-1 = unifont:size=6;0 -;font-2 = Font Awesome 5 Free Regular:size=10;0 -;font-3 = Font Awesome 5 Free Solid:size=10;0 -;font-4 = Font Awesome 5 Brands Regular:size=10;0 modules-left = temp-cpu cpu modules-center = network filesystem -modules-right = temp-gpu memory +modules-right = info-idle caffeine temp-gpu memory + +[module/weather] +type = custom/script +exec = ~/.local/bin/i3scripts/weathermap +interval = 600 +;label-font = Font Awesome 5 Free Solid:size=40;0 + +[module/info-hackspeed] +type = custom/script +exec = ~/.local/bin/i3scripts/info-hackspeed +tail = true + +[module/info-idle] +type = custom/script +exec = ~/.local/bin/i3scripts/info-idle +tail = true [module/filesystem] type = internal/fs @@ -248,21 +259,25 @@ menu-0-1-exec = i3 restart menu-0-2 = " Reboot " menu-0-2-foreground = #fba922 menu-0-2-exec = sudo /sbin/reboot -menu-0-3 =  Power off +menu-0-3 = " Power off " menu-0-3-foreground = #fba922 menu-0-3-exec = sudo poweroff -menu-1-0 = Cancel -menu-1-0-foreground = #fba922 -menu-1-0-exec = menu_open-0 -menu-1-1 = Reboot -menu-1-1-foreground = #fff -menu-1-1-exec = xterm -menu-2-0 = Power off -menu-2-0-foreground = #fba922 -menu-2-0-exec = sudo poweroff -menu-2-1 = Cancel -menu-2-1-foreground = #ffffff -menu-2-1-exec = menu_open-0 +menu-0-4 = " Lock " +menu-0-4-foreground = #fba922 +menu-0-4-exec = i3lock -c 000000 + +[module/caffeine] +type = custom/menu +format = +label-open =  +label-close =  +label-open-padding = 0 +label-close-padding = 1 +label-separator = " " +menu-0-0 =  +menu-0-0-exec = source /home/kapper/.local/bin/i3scripts/xoffee enable +menu-0-1 =  +menu-0-1-exec = source /home/kapper/.local/bin/i3scripts/xoffee disable [module/cpu] type = internal/cpu