From 323662942043ac759c513c76218bfe8afbf5001a Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Wed, 19 Feb 2020 15:55:35 +0000 Subject: [PATCH] Add community scripts for i3blocks --- .local/bin/i3scripts/contrib/README.md | 18 +++++++ .local/bin/i3scripts/contrib/bandwidth | 35 ++++++++++++ .local/bin/i3scripts/contrib/battery | 42 +++++++++++++++ .local/bin/i3scripts/contrib/bauerbill | 44 +++++++++++++++ .local/bin/i3scripts/contrib/brightness | 26 +++++++++ .local/bin/i3scripts/contrib/cpu | 47 ++++++++++++++++ .local/bin/i3scripts/contrib/date | 13 +++++ .local/bin/i3scripts/contrib/disk | 68 +++++++++++++++++++++++ .local/bin/i3scripts/contrib/keyboard | 31 +++++++++++ .local/bin/i3scripts/contrib/memory | 46 ++++++++++++++++ .local/bin/i3scripts/contrib/mpc | 38 +++++++++++++ .local/bin/i3scripts/contrib/network | 36 +++++++++++++ .local/bin/i3scripts/contrib/pacman | 34 ++++++++++++ .local/bin/i3scripts/contrib/playerctl | 50 +++++++++++++++++ .local/bin/i3scripts/contrib/sound | 71 +++++++++++++++++++++++++ .local/bin/i3scripts/contrib/time | 13 +++++ .local/bin/i3scripts/contrib/util.sh | 41 ++++++++++++++ .local/bin/i3scripts/contrib/weather | 49 +++++++++++++++++ .local/bin/i3scripts/contrib/yaourt | 44 +++++++++++++++ 19 files changed, 746 insertions(+) create mode 100644 .local/bin/i3scripts/contrib/README.md create mode 100755 .local/bin/i3scripts/contrib/bandwidth create mode 100755 .local/bin/i3scripts/contrib/battery create mode 100755 .local/bin/i3scripts/contrib/bauerbill create mode 100755 .local/bin/i3scripts/contrib/brightness create mode 100755 .local/bin/i3scripts/contrib/cpu create mode 100755 .local/bin/i3scripts/contrib/date create mode 100755 .local/bin/i3scripts/contrib/disk create mode 100755 .local/bin/i3scripts/contrib/keyboard create mode 100755 .local/bin/i3scripts/contrib/memory create mode 100755 .local/bin/i3scripts/contrib/mpc create mode 100755 .local/bin/i3scripts/contrib/network create mode 100755 .local/bin/i3scripts/contrib/pacman create mode 100755 .local/bin/i3scripts/contrib/playerctl create mode 100755 .local/bin/i3scripts/contrib/sound create mode 100755 .local/bin/i3scripts/contrib/time create mode 100644 .local/bin/i3scripts/contrib/util.sh create mode 100755 .local/bin/i3scripts/contrib/weather create mode 100755 .local/bin/i3scripts/contrib/yaourt diff --git a/.local/bin/i3scripts/contrib/README.md b/.local/bin/i3scripts/contrib/README.md new file mode 100644 index 0000000..db97832 --- /dev/null +++ b/.local/bin/i3scripts/contrib/README.md @@ -0,0 +1,18 @@ +# i3-blocks-contrib + +A collection of useful blocks for [i3blocks](https://github.com/vivien/i3blocks), for the amazing [i3](https://i3wm.org) window manager. + + +## Configuration + +Some examples on how to configure these in your `i3blocks` config file: + +- @rosshadden's [i3blocks.conf](https://github.com/rosshadden/dotfiles/blob/master/src/.config/i3/i3blocks.conf) +- @oppenlander's [i3blocks.conf](https://github.com/oppenlander/dotfiles/blob/master/i3/i3blocks.conf) + + +## Contributing + +As hinted by the name, this is intended to be openly contributed to by the community. +There is no official contributing guide, but please try to fit in to the style and themes found in our existing blocks. +We use a somewhat ad-hock [YUIDoc](http://yui.github.io/yuidoc/) documentation syntax. diff --git a/.local/bin/i3scripts/contrib/bandwidth b/.local/bin/i3scripts/contrib/bandwidth new file mode 100755 index 0000000..87633d0 --- /dev/null +++ b/.local/bin/i3scripts/contrib/bandwidth @@ -0,0 +1,35 @@ +#!/bin/bash + +################################ +# Shows bandwidth +# +# @param {String(tx|rx)} type: The bandwidth type to check +# @return {Number(kB/s)}: Speed of the interface +################################ + +dir=$(dirname $0) +source $dir/util.sh + +type=$BLOCK_INSTANCE + +file=/tmp/i3blocks_bandwidth_$type +touch $file + +prev=$(cat $file) +cur=0 + +netdir=/sys/class/net +for iface in $(ls -1 $netdir); do + # Skip the loopback interface + if [ "$iface" == "lo" ]; then continue; fi + + f=$netdir/$iface/statistics/${type}_bytes + n=$(cat $f) + cur=$(expr $cur + $n) +done + +delta=$(calc "$cur - $prev") +echo "$(calc "$delta / 1000") kB/s" + +# store result +echo $cur > $file diff --git a/.local/bin/i3scripts/contrib/battery b/.local/bin/i3scripts/contrib/battery new file mode 100755 index 0000000..8781975 --- /dev/null +++ b/.local/bin/i3scripts/contrib/battery @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +################################ +# Shows info about connected batteries. +# +# Dependencies: +# - acpi +# +# @return {Number(%)}: Current battery charge +################################ + +dir=$(dirname $0) +source $dir/util.sh + +full="" +short="" +status=0 + +# Exit if no battery was found +if [ "$(acpi)" == "" ]; then exit 0; fi + +state=$(acpi | sed -n 's/Battery [0-9]: \([A-Z]\).*, .*/\1/p') +chg=$(acpi | sed -n 's/Battery [0-9]:.*, \([0-9]\{1,3\}\)%.*/\1/p') + +# Charging or Unknown +if [ $state = "C" ] || [ $state = "U" ]; then + icon="" +else + if [ $chg -le 10 ]; then + icon="" + status=33 + else + icon="" + fi +fi + +full="$icon $chg%" +short="$chg%" + +echo $full +echo $short +exit $status diff --git a/.local/bin/i3scripts/contrib/bauerbill b/.local/bin/i3scripts/contrib/bauerbill new file mode 100755 index 0000000..67c306a --- /dev/null +++ b/.local/bin/i3scripts/contrib/bauerbill @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +################################ +# Shows AUR packages that need updated. +# +# Dependencies: +# - checkupdates +# - bauerbill +# - [notify-send] +# +# @return {Number}: Outdated packages +################################ + +dir=$(dirname "$0") +source $dir/util.sh + +full="" +short="" +status=0 + +archPackages=$(checkupdates) +allPackages=$(bauerbill -Quq --aur) +numArchPackages=$(numLines "$archPackages") +numAllPackages=$(numLines "$allPackages") +numAurPackages=$(calc "$numAllPackages - $numArchPackages") + +if [ "$numAllPackages" -le "$numArchPackages" ]; then + numAurPackages=$numAllPackages +fi + +full=$numAurPackages +short=$full + +case $BLOCK_BUTTON in + # right click: show packages + 3) + aurPackages=$(diff -y <(echo "$archPackages") <(echo "$allPackages") | awk -p '{ print $3 }') + notify-send "AUR packages" "$aurPackages" + ;; +esac + +echo $full +echo $short +exit $status diff --git a/.local/bin/i3scripts/contrib/brightness b/.local/bin/i3scripts/contrib/brightness new file mode 100755 index 0000000..e5bb66c --- /dev/null +++ b/.local/bin/i3scripts/contrib/brightness @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +################################ +# Shows current brightness +# +# dependencies: +# - xbacklight +# +# @return {Number}: Current brightness +################################ + +case $BLOCK_BUTTON in + # right click + # set to `20 + 3) xbacklight -set 20 ;; + + # scroll up + # raise brightness + 4) xbacklight -inc 10 ;; + + # scroll down + # lower brightness + 5) xbacklight -dec 10 ;; +esac + +printf "%.0f" "$(xbacklight -get)" diff --git a/.local/bin/i3scripts/contrib/cpu b/.local/bin/i3scripts/contrib/cpu new file mode 100755 index 0000000..f86805a --- /dev/null +++ b/.local/bin/i3scripts/contrib/cpu @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +################################ +# Shows info about the CPU +# +# Dependencies: +# - [notify-send] +# +# @return {Number(%)}: CPU usage +################################ + +dir=$(dirname $0) +source $dir/util.sh + +full="" +short="" +status=0 + +read cpu a b c previdle rest < /proc/stat +prevtotal=$(calc "$a + $b + $c + $previdle") +sleep 0.5 +read cpu a b c idle rest < /proc/stat +total=$(calc "$a + $b + $c + $idle") + +CPU=$(calc "100 * (($total - $prevtotal) - ($idle - $previdle)) / ($total - $prevtotal)") + +full="$CPU%" +short=$full + +if [ $CPU -ge 90 ]; then + status=33 +fi + +case $BLOCK_BUTTON in + + # right click: show packages + 3) + n=16 + summary=$(ps -eo pcpu,pmem,pid,comm --sort=-pcpu | head -$n) + notify-send "Top $n CPU-eaters" "$summary" + ;; + +esac + +echo $full +echo $short +exit $status diff --git a/.local/bin/i3scripts/contrib/date b/.local/bin/i3scripts/contrib/date new file mode 100755 index 0000000..847004c --- /dev/null +++ b/.local/bin/i3scripts/contrib/date @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +################################ +# Shows current date +# +# @param {String} format: The format of the date +# @return {Date}: Current date +################################ + +format=${BLOCK_INSTANCE:-"%Y-%m-%d"} + +date +"$format" +date +"%m-%d" diff --git a/.local/bin/i3scripts/contrib/disk b/.local/bin/i3scripts/contrib/disk new file mode 100755 index 0000000..e71c968 --- /dev/null +++ b/.local/bin/i3scripts/contrib/disk @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +################################ +# Shows info about a specified mount point +# +# Dependencies: +# - [notify-send] +# +# @param {String} disk: The mount point to check +# @return {Number(%)}: Space used on the disk +################################ + +dir=$(dirname $0) +source $dir/util.sh + +full="" +short="" +status=0 + +disks=$(lsblk | tail -n +2 | awk '{ print $7 }' | sed '/^\s*$/d' | sort) +let numDisks=$(echo "$disks" | wc -l) +let diskNum=$(getCache 1) + +getDisk() { + echo "$disks" | sed -n "$diskNum p" +} +disk=$(getDisk) + +case $BLOCK_BUTTON in + # right click: show details + # TODO: fix + 3) + local summary=$(du -h --max-depth=1 $disk) + notify-send "Disk usage" "$summary" + ;; + # scroll up: cycle disks + 4) + diskNum=$[$(getCache) - 1] + if (( diskNum <= 0 )); then + diskNum=$numDisks + fi + setCache $diskNum + disk=$(getDisk) + ;; + 5) + # scroll down: cycle disks + diskNum=$[$(getCache) + 1] + if (( diskNum >= numDisks + 1 )); then + diskNum=1 + fi + setCache $diskNum + disk=$(getDisk) + ;; +esac + +usage=$(df -h $disk | sed -n '2 p') +usage=($usage) + +if [ ${usage[4]%?} -ge 90 ]; then + status=33 +fi + +full="$disk ${usage[4]}" +short="$full" + +echo $full +echo $short +exit $status diff --git a/.local/bin/i3scripts/contrib/keyboard b/.local/bin/i3scripts/contrib/keyboard new file mode 100755 index 0000000..2987a1b --- /dev/null +++ b/.local/bin/i3scripts/contrib/keyboard @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +################################ +# Shows current keyboard layout. +# Allows layout toggling. +# +# Dependencies: +# - [Ross' key script](https://github.com/rosshadden/dotfiles/blob/master/src/lib/keys.sh) +# +# @return {String}: Current keyboard layout +################################ + +keys=$HOME/bin/keys + +full="" +short="" +status=0 + +case $BLOCK_BUTTON in + # right click: toggle key variant + 3) $keys toggle ;; +esac + +layout=$($keys get) + +full=$layout +short=${layout:0:1} + +echo $full +echo $short +exit $status diff --git a/.local/bin/i3scripts/contrib/memory b/.local/bin/i3scripts/contrib/memory new file mode 100755 index 0000000..4db07d3 --- /dev/null +++ b/.local/bin/i3scripts/contrib/memory @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +################################ +# Shows info about the RAM +# +# Dependencies: +# - [notify-send] +# +# @return {Number(%)}: RAM usage +################################ + +dir=$(dirname $0) +source $dir/util.sh + +full="" +short="" +status=0 + +exec 6< /proc/meminfo +read a total b <&6 +read a free b <&6 +read a b c <&6 +read a buffer b <&6 +read a cached b <&6 + +MEM=$(calc "100 * ($total - ($free + $buffer + $cached)) / $total") + +full="$MEM%" +short=$full + +if [ $MEM -ge 80 ]; then + status=33 +fi + +case $BLOCK_BUTTON in + # right click: show packages + 3) + n=16 + summary=$(ps -eo pmem,pcpu,pid,comm --sort=-pmem | head -$n) + notify-send "Top $n RAM-eaters" "$summary" + ;; +esac + +echo $full +echo $short +exit $status diff --git a/.local/bin/i3scripts/contrib/mpc b/.local/bin/i3scripts/contrib/mpc new file mode 100755 index 0000000..6842dfd --- /dev/null +++ b/.local/bin/i3scripts/contrib/mpc @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +################################ +# Shows info from media players. +# +# Dependencies: +# - mpc +# +# @return {String}: Current media info +################################ + +full="" +short="" +status=0 + +format=${BLOCK_INSTANCE:-'[[%artist% - ]%title%[ \[%album%\]]]|[%file%]'} + +current=$(mpc current) +currentLong=$(mpc current -f "$format") +state=playing + +if [[ "$current" ]]; then + # Make icon mapping + declare -A icons + icons["playing"]="" + icons["paused"]="" + icons["stopped"]="" + + # Determine which icon to use + icon=${icons[$state]} + + full="$currentLong $icon" + short="$current $icon" +fi + +echo "$full" +echo "$short" +exit "$status" diff --git a/.local/bin/i3scripts/contrib/network b/.local/bin/i3scripts/contrib/network new file mode 100755 index 0000000..027e5a4 --- /dev/null +++ b/.local/bin/i3scripts/contrib/network @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +################################ +# Shows IP address of a given interface +# +# @param {String} interface: The network interface to check +# @return {String(IP)}: IP address of the interface +################################ + +dir=$(dirname $0) +source "$dir/util.sh" + +full="" +short="" +status=0 + +interface=${BLOCK_INSTANCE:-eth0} + +netPath=/sys/class/net +interfacePath=$(echo $netPath/$interface) +# Expand wildcard interfaces +interface=${interfacePath#$netPath/} + +state=$(cat $interfacePath/operstate) + +if [ $state == "up" ]; then + ips=$(ip addr show $interface | perl -n -e'/inet (.+)\// && print $1') + ip=${ips:-0.0.0.0} + + full=$ip + short=$full +fi + +echo "$full" +echo "$short" +exit $status diff --git a/.local/bin/i3scripts/contrib/pacman b/.local/bin/i3scripts/contrib/pacman new file mode 100755 index 0000000..daf2693 --- /dev/null +++ b/.local/bin/i3scripts/contrib/pacman @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +################################ +# Shows pacman packages that need updated. +# +# dependencies: +# - checkupdates +# - [notify-send] +# +# @return {Number}: Outdated packages +################################ + +dir=$(dirname "$0") +source $dir/util.sh + +full="" +short="" +status=0 + +packages=$(checkupdates) +numPackages=$(numLines "$packages") + +full=$numPackages +short=$full + +echo "$full" +echo "$short" + +case $BLOCK_BUTTON in + # right click: show packages + 3) notify-send "Pacman packages" "$packages" ;; +esac + +exit $status diff --git a/.local/bin/i3scripts/contrib/playerctl b/.local/bin/i3scripts/contrib/playerctl new file mode 100755 index 0000000..d135461 --- /dev/null +++ b/.local/bin/i3scripts/contrib/playerctl @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +################################ +# Shows info from media players. +# +# TODO: make output format configurable +# +# Dependencies: +# - playerctl +# +# @return {String}: Current media info +################################ + +dir=$(dirname $0) +source $dir/util.sh + +full="" +short="" +status=0 + +# Exit if no player found +players=$(playerctl -l) +if [[ ! $players ]]; then exit 0; fi + +artist=$(playerctl metadata artist) +title=$(playerctl metadata title) +album=$(playerctl metadata album) +state=$(playerctl status) + +if [[ "$title" ]]; then + # Make icon mapping + declare -A icons + icons["Playing"]="" + icons["Paused"]="" + icons["Stopped"]="" + + # Determine which icon to use + icon=${icons[$state]} + + full="$title" + if [[ "$artist" ]]; then full="$artist - $full"; fi + + short="$full $icon" + if [[ "$album" ]]; then full="$full [$album]"; fi + full="$full $icon" +fi + +echo $full +echo $short +exit $status diff --git a/.local/bin/i3scripts/contrib/sound b/.local/bin/i3scripts/contrib/sound new file mode 100755 index 0000000..70d2f86 --- /dev/null +++ b/.local/bin/i3scripts/contrib/sound @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +################################ +# Shows info about sound/volume. +# Allows simple volume controls. +# +# Thanks to [@EliteTK](https://gist.github.com/EliteTK/36d061fa24372fb70312), +# for the big speed gain when switching to `ponymix` +# +# Dependencies: +# - ponymix +# - ttf-font-icons +# +# @return {Number}: Current volume +################################ + +dir=$(dirname $0) +source $dir/util.sh + +full="" +short="" +status=0 + +step=${BLOCK_INSTANCE:-5} + +getVolume() { + ponymix get-volume +} + +isMuted() { + ponymix is-muted +} + +case $BLOCK_BUTTON in + # right click + # mute/unmute + 3) ponymix toggle >/dev/null ;; + + # scroll up + # raise volume + 4) ponymix increase $step >/dev/null ;; + + # scroll down + # lower volume + 5) ponymix decrease $step >/dev/null ;; +esac + +vol=$(getVolume) + +# level-based icon +if (( $vol == 0 )); then + icon="" +elif (( $vol < 34 )); then + icon="" +elif (( $vol < 67 )); then + icon="" +else + icon="" +fi + +# determine mute status +if isMuted; then + status=33 +fi + +full="$icon $vol%" +short=$vol + +echo $full +echo $short +exit $status diff --git a/.local/bin/i3scripts/contrib/time b/.local/bin/i3scripts/contrib/time new file mode 100755 index 0000000..24f78ab --- /dev/null +++ b/.local/bin/i3scripts/contrib/time @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +################################ +# Shows current time +# +# @param {String} format: The format of the time +# @return {Time}: Current time +################################ + +format=${BLOCK_INSTANCE:-"%H:%M:%S"} + +date +"$format" +date +"%H:%M" diff --git a/.local/bin/i3scripts/contrib/util.sh b/.local/bin/i3scripts/contrib/util.sh new file mode 100644 index 0000000..711b874 --- /dev/null +++ b/.local/bin/i3scripts/contrib/util.sh @@ -0,0 +1,41 @@ +################################ +# Calculates a given expression +# +# Dependencies: +# - bc +# +# @param {String} 1: The expression to calculate +# @return {Number}: The result of the expression +################################ +calc() { + echo "$1" | bc +} + +################################ +# Counts lines in text, accounting for empty values +# +# @param {String} 1: The input text +# @return {Number}: The number of lines +################################ +numLines() { + if [[ "$1" == "" ]]; then + echo 0 + else + echo "$1" | wc -l + fi +} + +cacheDir=".cache" +cacheFile="$cacheDir/i3-blocks-contrib-${BLOCK_NAME}" + +if [[ -z $cacheDir ]]; then + mkdir $cacheDir +fi + +getCache() { + cat $cacheFile || echo $1 +} + +setCache() { + echo "$1" > $cacheFile +} diff --git a/.local/bin/i3scripts/contrib/weather b/.local/bin/i3scripts/contrib/weather new file mode 100755 index 0000000..92f78c5 --- /dev/null +++ b/.local/bin/i3scripts/contrib/weather @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +################################ +# Shows info about the weather (in Cincinnati) from accuweather.com +# +# TODO: completely rewrite, probably using openweather APIs +# TODO: make location configurable +# TODO: make temperature unit configurable +# +# @return {Number(degrees Fahrenheit)}: Current temperature in Cincinnati +################################ + +dir=$(dirname $0) +source $dir/util.sh + +full="" +short="" +status=0 + +URL='http://www.accuweather.com/en/us/cincinnati-oh/45212/weather-forecast/350126' +SITE="$(curl -s "$URL")" + +weather="$(echo "$SITE" | awk -F\' '/acm_RecentLocationsCarousel\.push/{print $13 }'| head -1)" +temp="$(echo "$SITE" | awk -F\' '/acm_RecentLocationsCarousel\.push/{print $10 }'| head -1)" + +if [[ $weather == *thunder* || $weather == *Thunder* ]]; then + icon="" +else + if [[ $weather == *rain* || $weather == *Rain* ]]; then + icon="" + else + if [[ $weather == *snow* || $weather == *Snow* ]]; then + icon="❄" + else + if [[ $weather == *cloud* || $weather == *Cloud* ]]; then + icon="" + else + icon="" + fi + fi + fi +fi + +full="$icon $temp°" +short="$temp°" + +echo $full +echo $short +exit $status diff --git a/.local/bin/i3scripts/contrib/yaourt b/.local/bin/i3scripts/contrib/yaourt new file mode 100755 index 0000000..db92684 --- /dev/null +++ b/.local/bin/i3scripts/contrib/yaourt @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +################################ +# Shows AUR packages that need updated. +# +# Dependencies: +# - checkupdates +# - yaourt +# - [notify-send] +# +# @return {Number}: Outdated packages +################################ + +dir=$(dirname $0) +source $dir/util.sh + +full="" +short="" +status=0 + +archPackages=$(checkupdates) +allPackages=$(yaourt -Quaq) +numArchPackages=$(numLines "$archPackages") +numAllPackages=$(numLines "$allPackages") +numAurPackages=$(calc "$numAllPackages - $numArchPackages") + +if [ $numAllPackages -le $numArchPackages ]; then + numAurPackages=$numAllPackages +fi + +full=$numAurPackages +short=$full + +case $BLOCK_BUTTON in + # right click: show packages + 3) + aurPackages=$(diff -y <(echo "$archPackages") <(echo "$allPackages") | awk -p '{ print $3 }') + notify-send "AUR packages" "$aurPackages" + ;; +esac + +echo $full +echo $short +exit $status