diff options
author | Karel Kočí <cynerd@email.cz> | 2022-06-25 20:08:57 +0200 |
---|---|---|
committer | Karel Kočí <cynerd@email.cz> | 2022-06-25 20:08:57 +0200 |
commit | 44fa0d9200d2a0e78e5d2cb5a849d370bc47e45c (patch) | |
tree | de358e64afa892a6374e662b32db6317bbeb80f9 | |
parent | c3cc932f4b1950b1633af02f510d47b9c19d0863 (diff) | |
download | myconfigs-44fa0d9200d2a0e78e5d2cb5a849d370bc47e45c.tar.gz myconfigs-44fa0d9200d2a0e78e5d2cb5a849d370bc47e45c.tar.bz2 myconfigs-44fa0d9200d2a0e78e5d2cb5a849d370bc47e45c.zip |
i3blocks: add missing scripts
-rwxr-xr-x | config/i3blocks/scripts/battery2 | 106 | ||||
-rwxr-xr-x | config/i3blocks/scripts/cpu_usage | 66 | ||||
-rwxr-xr-x | config/i3blocks/scripts/disk | 48 | ||||
-rwxr-xr-x | config/i3blocks/scripts/memory | 73 | ||||
-rwxr-xr-x | config/i3blocks/scripts/ssid | 39 | ||||
-rwxr-xr-x | config/i3blocks/scripts/temperature | 70 | ||||
-rwxr-xr-x | config/i3blocks/scripts/time | 101 | ||||
-rwxr-xr-x | config/i3blocks/scripts/volume-pipewire | 172 | ||||
-rwxr-xr-x | config/i3blocks/scripts/wifi | 59 |
9 files changed, 734 insertions, 0 deletions
diff --git a/config/i3blocks/scripts/battery2 b/config/i3blocks/scripts/battery2 new file mode 100755 index 0000000..4898d3c --- /dev/null +++ b/config/i3blocks/scripts/battery2 @@ -0,0 +1,106 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2016 James Murphy +# Licensed under the GPL version 2 only +# +# A battery indicator blocklet script for i3blocks + +from subprocess import check_output +import os +import re + +config = dict(os.environ) +status = check_output(['acpi'], universal_newlines=True) + +if not status: + # stands for no battery found + color = config.get("color_10", "red") + fulltext = "<span color='{}'><span font='FontAwesome'>\uf00d \uf240</span></span>".format(color) + percentleft = 100 +else: + # if there is more than one battery in one laptop, the percentage left is + # available for each battery separately, although state and remaining + # time for overall block is shown in the status of the first battery + batteries = status.split("\n") + state_batteries=[] + commasplitstatus_batteries=[] + percentleft_batteries=[] + time = "" + for battery in batteries: + if battery!='': + state_batteries.append(battery.split(": ")[1].split(", ")[0]) + commasplitstatus = battery.split(", ") + if not time: + time = commasplitstatus[-1].strip() + # check if it matches a time + time = re.match(r"(\d+):(\d+)", time) + if time: + time = ":".join(time.groups()) + timeleft = " ({})".format(time) + else: + timeleft = "" + + p = int(commasplitstatus[1].rstrip("%\n")) + if p>0: + percentleft_batteries.append(p) + commasplitstatus_batteries.append(commasplitstatus) + state = state_batteries[0] + commasplitstatus = commasplitstatus_batteries[0] + if percentleft_batteries: + percentleft = int(sum(percentleft_batteries)/len(percentleft_batteries)) + else: + percentleft = 0 + + # stands for charging + color = config.get("color_charging", "yellow") + FA_LIGHTNING = "<span color='{}'><span font='FontAwesome'>\uf0e7</span></span>".format(color) + + # stands for plugged in + FA_PLUG = "<span font='FontAwesome'>\uf1e6</span>" + + # stands for using battery + FA_BATTERY = "<span font='FontAwesome'>\uf240</span>" + + # stands for unknown status of battery + FA_QUESTION = "<span font='FontAwesome'>\uf128</span>" + + + if state == "Discharging": + fulltext = FA_BATTERY + " " + elif state == "Full": + fulltext = FA_PLUG + " " + timeleft = "" + elif state == "Unknown": + fulltext = FA_QUESTION + " " + FA_BATTERY + " " + timeleft = "" + else: + fulltext = FA_LIGHTNING + " " + FA_PLUG + " " + + def color(percent): + if percent < 10: + # exit code 33 will turn background red + return config.get("color_10", "#FFFFFF") + if percent < 20: + return config.get("color_20", "#FF3300") + if percent < 30: + return config.get("color_30", "#FF6600") + if percent < 40: + return config.get("color_40", "#FF9900") + if percent < 50: + return config.get("color_50", "#FFCC00") + if percent < 60: + return config.get("color_60", "#FFFF00") + if percent < 70: + return config.get("color_70", "#FFFF33") + if percent < 80: + return config.get("color_80", "#FFFF66") + return config.get("color_full", "#FFFFFF") + + form = '<span color="{}">{}%</span>' + fulltext += form.format(color(percentleft), percentleft) + fulltext += timeleft + +print(fulltext) +print(fulltext) +if percentleft < 10: + exit(33) diff --git a/config/i3blocks/scripts/cpu_usage b/config/i3blocks/scripts/cpu_usage new file mode 100755 index 0000000..b298a97 --- /dev/null +++ b/config/i3blocks/scripts/cpu_usage @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# +# Copyright 2014 Pierre Mavro <deimos@deimos.fr> +# Copyright 2014 Vivien Didelot <vivien@didelot.org> +# Copyright 2014 Andreas Guldstrand <andreas.guldstrand@gmail.com> +# +# Licensed under the terms of the GNU GPL v3, or any later version. + +use strict; +use warnings; +use utf8; +use Getopt::Long; + +# default values +my $t_warn = $ENV{T_WARN} // 50; +my $t_crit = $ENV{T_CRIT} // 80; +my $cpu_usage = -1; +my $decimals = $ENV{DECIMALS} // 2; +my $label = $ENV{LABEL} // ""; +my $color_normal = $ENV{COLOR_NORMAL} // "#EBDBB2"; +my $color_warn = $ENV{COLOR_WARN} // "#FFFC00"; +my $color_crit = $ENV{COLOR_CRIT} // "#FF0000"; + +sub help { + print "Usage: cpu_usage [-w <warning>] [-c <critical>] [-d <decimals>]\n"; + print "-w <percent>: warning threshold to become yellow\n"; + print "-c <percent>: critical threshold to become red\n"; + print "-d <decimals>: Use <decimals> decimals for percentage (default is $decimals) \n"; + exit 0; +} + +GetOptions("help|h" => \&help, + "w=i" => \$t_warn, + "c=i" => \$t_crit, + "d=i" => \$decimals, +); + +# Get CPU usage +$ENV{LC_ALL}="en_US"; # if mpstat is not run under en_US locale, things may break, so make sure it is +open (MPSTAT, 'mpstat 1 1 |') or die; +while (<MPSTAT>) { + if (/^.*\s+(\d+\.\d+)[\s\x00]?$/) { + $cpu_usage = 100 - $1; # 100% - %idle + last; + } +} +close(MPSTAT); + +$cpu_usage eq -1 and die 'Can\'t find CPU information'; + +# Print short_text, full_text +print "${label}"; +printf "%.${decimals}f%%\n", $cpu_usage; +print "${label}"; +printf "%.${decimals}f%%\n", $cpu_usage; + +# Print color, if needed +if ($cpu_usage >= $t_crit) { + print "${color_crit}\n"; +} elsif ($cpu_usage >= $t_warn) { + print "${color_warn}\n"; +} else { + print "${color_normal}\n"; +} + +exit 0; diff --git a/config/i3blocks/scripts/disk b/config/i3blocks/scripts/disk new file mode 100755 index 0000000..9d64cc1 --- /dev/null +++ b/config/i3blocks/scripts/disk @@ -0,0 +1,48 @@ +#!/usr/bin/env sh +# Copyright (C) 2014 Julien Bonjean <julien@bonjean.info> + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +DIR="${DIR:-$BLOCK_INSTANCE}" +DIR="${DIR:-$HOME}" +ALERT_LOW="${ALERT_LOW:-$1}" +ALERT_LOW="${ALERT_LOW:-10}" # color will turn red under this value (default: 10%) + +LOCAL_FLAG="-l" +if [ "$1" = "-n" ] || [ "$2" = "-n" ]; then + LOCAL_FLAG="" +fi + +df -h -P $LOCAL_FLAG "$DIR" | awk -v label="$LABEL" -v alert_low=$ALERT_LOW ' +/\/.*/ { + # full text + print label $4 + + # short text + print label $4 + + use=$5 + + # no need to continue parsing + exit 0 +} + +END { + gsub(/%$/,"",use) + if (100 - use < alert_low) { + # color + print "#FF0000" + } +} +' diff --git a/config/i3blocks/scripts/memory b/config/i3blocks/scripts/memory new file mode 100755 index 0000000..fd814e0 --- /dev/null +++ b/config/i3blocks/scripts/memory @@ -0,0 +1,73 @@ +#!/usr/bin/env sh +# Copyright (C) 2014 Julien Bonjean <julien@bonjean.info> + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +TYPE="${BLOCK_INSTANCE:-mem}" +PERCENT="${PERCENT:-true}" + +awk -v type=$TYPE -v percent=$PERCENT ' +/^MemTotal:/ { + mem_total=$2 +} +/^MemFree:/ { + mem_free=$2 +} +/^Buffers:/ { + mem_free+=$2 +} +/^Cached:/ { + mem_free+=$2 +} +/^SwapTotal:/ { + swap_total=$2 +} +/^SwapFree:/ { + swap_free=$2 +} +END { + if (type == "swap") { + free=swap_free/1024/1024 + used=(swap_total-swap_free)/1024/1024 + total=swap_total/1024/1024 + } else { + free=mem_free/1024/1024 + used=(mem_total-mem_free)/1024/1024 + total=mem_total/1024/1024 + } + + pct=0 + if (total > 0) { + pct=used/total*100 + } + + # full text + if (percent == "true" ) { + printf("%.1fG/%.1fG (%.f%%)\n", used, total, pct) + } else { + printf("%.1fG/%.1fG\n", used, total) + } + # short text + printf("%.f%%\n", pct) + + # color + if (pct > 90) { + print("#FF0000") + } else if (pct > 80) { + print("#FFAE00") + } else if (pct > 70) { + print("#FFF600") + } +} +' /proc/meminfo diff --git a/config/i3blocks/scripts/ssid b/config/i3blocks/scripts/ssid new file mode 100755 index 0000000..be92883 --- /dev/null +++ b/config/i3blocks/scripts/ssid @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# Copyright (C) 2020 hseg <gesh@gesh.uni.cx> +# Copyright (C) 2014 Alexander Keller <github@nycroth.com> + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +#------------------------------------------------------------------------ +if [[ -z "$INTERFACE" ]] ; then + INTERFACE="${BLOCK_INSTANCE:-wlan0}" +fi +#------------------------------------------------------------------------ + +# As per #36 -- It is transparent: e.g. if the machine has no battery or wireless +# connection (think desktop), the corresponding block should not be displayed. +# Similarly, if the wifi interface exists but no connection is active, show +# nothing +[[ ! -d /sys/class/net/"${INTERFACE}"/wireless || \ + "$(cat /sys/class/net/"$INTERFACE"/operstate)" = 'down' ]] && exit + +#------------------------------------------------------------------------ + +SSID=$(iw "$INTERFACE" info | awk '/ssid/ {$1=""; print $0}') + +#------------------------------------------------------------------------ + +echo "$SSID" # full text +echo "$SSID" # short text +echo "#00FF00" # color diff --git a/config/i3blocks/scripts/temperature b/config/i3blocks/scripts/temperature new file mode 100755 index 0000000..97579fb --- /dev/null +++ b/config/i3blocks/scripts/temperature @@ -0,0 +1,70 @@ +#!/usr/bin/env perl +# Copyright 2014 Pierre Mavro <deimos@deimos.fr> +# Copyright 2014 Vivien Didelot <vivien@didelot.org> +# Copyright 2014 Andreas Guldstrand <andreas.guldstrand@gmail.com> +# Copyright 2014 Benjamin Chretien <chretien at lirmm dot fr> + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +use strict; +use warnings; +use utf8; +use Getopt::Long; + +binmode(STDOUT, ":utf8"); + +# default values +my $t_warn = $ENV{T_WARN} || 70; +my $t_crit = $ENV{T_CRIT} || 90; +my $chip = $ENV{SENSOR_CHIP} || ""; +my $temperature = -9999; + +sub help { + print "Usage: temperature [-w <warning>] [-c <critical>] [--chip <chip>]\n"; + print "-w <percent>: warning threshold to become yellow\n"; + print "-c <percent>: critical threshold to become red\n"; + print "--chip <chip>: sensor chip\n"; + exit 0; +} + +GetOptions("help|h" => \&help, + "w=i" => \$t_warn, + "c=i" => \$t_crit, + "chip=s" => \$chip); + +# Get chip temperature +open (SENSORS, "sensors -u $chip 2>/dev/null |") or die; +while (<SENSORS>) { + if (/^\s+temp1_input:\s+[\+]*([\-]*\d+\.\d)/) { + $temperature = $1; + last; + } +} +close(SENSORS); + +$temperature eq -9999 and die 'Cannot find temperature'; + +# Print short_text, full_text +print "$temperature°C\n" x2; + +# Print color, if needed +if ($temperature >= $t_crit) { + print " + #FF0000\n"; + exit 33; +} elsif ($temperature >= $t_warn) { + print "#FFFC00\n"; +} + +exit 0; diff --git a/config/i3blocks/scripts/time b/config/i3blocks/scripts/time new file mode 100755 index 0000000..e552a92 --- /dev/null +++ b/config/i3blocks/scripts/time @@ -0,0 +1,101 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use POSIX qw/strftime/; + +my $click = $ENV{BLOCK_BUTTON} || 0; +my $format = $ENV{BLOCK_INSTANCE} || $ENV{STRFTIME_FORMAT} || "%H:%M"; +my $tz_file = shift || $ENV{TZ_FILE} || "$ENV{HOME}/.tz"; +$tz_file = glob($tz_file); +my $default_tz = get_default_tz(); + +my $tzones = $ENV{TZONES} || '$DEFAULT_TZ'; +$tzones =~ s/\$DEFAULT_TZ/$default_tz/g; +my @tz_list = split(/,/, $tzones); +my @tz_labels = split(/,/, $ENV{TZ_LABELS} || ""); +if (scalar(@tz_list) != scalar(@tz_labels)) { + @tz_labels = @tz_list; +} + +my $current_tz; +if ($click == 1) { + $current_tz = get_tz(); + + my %tzmap; + $tzmap{""} = $tz_list[0]; + my $prev = $tz_list[0]; + foreach my $tz (@tz_list) { + $tzmap{$prev} = $tz; + $prev = $tz; + } + $tzmap{$prev} = $tz_list[0]; + + if (exists $tzmap{$current_tz}) { + set_tz($tzmap{$current_tz}); + $current_tz = $tzmap{$current_tz}; + } +} + +# How each timezone will be displayed in the bar. +my %display_map; +for (my $i=0; $i < scalar(@tz_list); $i++) { + $display_map{$tz_list[$i]} = $tz_labels[$i]; +} + +if (!defined $current_tz) { + $current_tz = get_tz(); + set_tz($current_tz); +} +$ENV{TZ} = $current_tz; +my $tz_display = ""; +if (!exists $display_map{$ENV{TZ}}) { + $ENV{TZ} = $tz_list[0]; + set_tz($tz_list[0]); +} +$tz_display = $display_map{$ENV{TZ}}; + +binmode(STDOUT, ":utf8"); +my $time = strftime($format, localtime()); +if ($tz_display eq "") { + print "$time\n"; +} else { + print "$time ($tz_display)\n"; +} + +sub get_tz { + my $current_tz; + + if (-f $tz_file) { + open my $fh, '<', $tz_file || die "Couldn't open file: $tz_file"; + $current_tz = <$fh>; + chomp $current_tz; + close $fh; + } + + return $current_tz || get_default_tz(); +} + +sub set_tz { + my $tz = shift; + + open my $fh, '>', $tz_file || die "Couldn't open file: $tz_file"; + print $fh $tz; + close $fh; +} + +sub get_default_tz { + my $tz = "Europe/London"; + + if (-f "/etc/timezone") { + open my $fh, '<', "/etc/timezone" || die "Couldn't open file: /etc/timezone"; + $tz = <$fh>; + chomp $tz; + close $fh; + } elsif (-l "/etc/localtime") { + $tz = readlink "/etc/localtime"; + $tz = (split /zoneinfo\//, $tz)[-1]; + } + + return $tz; +} diff --git a/config/i3blocks/scripts/volume-pipewire b/config/i3blocks/scripts/volume-pipewire new file mode 100755 index 0000000..bc7da69 --- /dev/null +++ b/config/i3blocks/scripts/volume-pipewire @@ -0,0 +1,172 @@ +#!/bin/bash +# Displays the default device, volume, and mute status for i3blocks + +set -a + +AUDIO_HIGH_SYMBOL=${AUDIO_HIGH_SYMBOL:-' '} + +AUDIO_MED_THRESH=${AUDIO_MED_THRESH:-50} +AUDIO_MED_SYMBOL=${AUDIO_MED_SYMBOL:-' '} + +AUDIO_LOW_THRESH=${AUDIO_LOW_THRESH:-0} +AUDIO_LOW_SYMBOL=${AUDIO_LOW_SYMBOL:-' '} + +AUDIO_MUTED_SYMBOL=${AUDIO_MUTED_SYMBOL:-' '} + +AUDIO_DELTA=${AUDIO_DELTA:-5} + +DEFAULT_COLOR=${DEFAULT_COLOR:-"#ffffff"} +MUTED_COLOR=${MUTED_COLOR:-"#a0a0a0"} + +LONG_FORMAT=${LONG_FORMAT:-'${SYMB} ${VOL}% [${INDEX}:${NAME}]'} +SHORT_FORMAT=${SHORT_FORMAT:-'${SYMB} ${VOL}% [${INDEX}]'} +USE_ALSA_NAME=${USE_ALSA_NAME:-0} +USE_DESCRIPTION=${USE_DESCRIPTION:-0} + +SUBSCRIBE=${SUBSCRIBE:-0} + +MIXER=${MIXER:-""} +SCONTROL=${SCONTROL:-""} + +while getopts F:Sf:adH:M:L:X:T:t:C:c:i:m:s:h opt; do + case "$opt" in + S) SUBSCRIBE=1 ;; + F) LONG_FORMAT="$OPTARG" ;; + f) SHORT_FORMAT="$OPTARG" ;; + a) USE_ALSA_NAME=1 ;; + d) USE_DESCRIPTION=1 ;; + H) AUDIO_HIGH_SYMBOL="$OPTARG" ;; + M) AUDIO_MED_SYMBOL="$OPTARG" ;; + L) AUDIO_LOW_SYMBOL="$OPTARG" ;; + X) AUDIO_MUTED_SYMBOL="$OPTARG" ;; + T) AUDIO_MED_THRESH="$OPTARG" ;; + t) AUDIO_LOW_THRESH="$OPTARG" ;; + C) DEFAULT_COLOR="$OPTARG" ;; + c) MUTED_COLOR="$OPTARG" ;; + i) AUDIO_INTERVAL="$OPTARG" ;; + m) MIXER="$OPTARG" ;; + s) SCONTROL="$OPTARG" ;; + h) printf \ +"Usage: volume-pulseaudio [-S] [-F format] [-f format] [-p] [-a|-d] [-H symb] [-M symb] + [-L symb] [-X symb] [-T thresh] [-t thresh] [-C color] [-c color] [-i inter] + [-m mixer] [-s scontrol] [-h] +Options: +-F, -f\tOutput format (-F long format, -f short format) to use, with exposed variables: +\${SYMB}, \${VOL}, \${INDEX}, \${NAME} +-S\tSubscribe to volume events (requires persistent block, always uses long format) +-a\tUse ALSA name if possible +-d\tUse device description instead of name if possible +-H\tSymbol to use when audio level is high. Default: '$AUDIO_HIGH_SYMBOL' +-M\tSymbol to use when audio level is medium. Default: '$AUDIO_MED_SYMBOL' +-L\tSymbol to use when audio level is low. Default: '$AUDIO_LOW_SYMBOL' +-X\tSymbol to use when audio is muted. Default: '$AUDIO_MUTED_SYMBOL' +-T\tThreshold for medium audio level. Default: $AUDIO_MED_THRESH +-t\tThreshold for low audio level. Default: $AUDIO_LOW_THRESH +-C\tColor for non-muted audio. Default: $DEFAULT_COLOR +-c\tColor for muted audio. Default: $MUTED_COLOR +-i\tInterval size of volume increase/decrease. Default: $AUDIO_DELTA +-m\tUse the given mixer. +-s\tUse the given scontrol. +-h\tShow this help text +" && exit 0;; + esac +done + +if [[ -z "$MIXER" ]] ; then + MIXER="default" + if amixer -D pulse info >/dev/null 2>&1 ; then + MIXER="pulse" + fi +fi + +if [[ -z "$SCONTROL" ]] ; then + SCONTROL=$(amixer -D "$MIXER" scontrols | sed -n "s/Simple mixer control '\([^']*\)',0/\1/p" | head -n1) +fi + +CAPABILITY=$(amixer -D $MIXER get $SCONTROL | sed -n "s/ Capabilities:.*cvolume.*/Capture/p") + + +function move_sinks_to_new_default { + DEFAULT_SINK=$1 + pactl list sink-inputs | grep 'Sink Input #' | grep -o '[0-9]\+' | while read SINK + do + pactl move-sink-input $SINK $DEFAULT_SINK + done +} + +function set_default_playback_device_next { + inc=${1:-1} + num_devices=$(pactl list sinks | grep -c Name:) + sink_arr=($(pactl list sinks | grep Name: | sed -r 's/\s+Name: (.+)/\1/')) + default_sink=$(pactl get-default-sink) + default_sink_index=$(for i in "${!sink_arr[@]}"; do if [[ "$default_sink" = "${sink_arr[$i]}" ]]; then echo "$i"; fi done) + default_sink_index=$(( ($default_sink_index + $num_devices + $inc) % $num_devices )) + default_sink=${sink_arr[$default_sink_index]} + pactl set-default-sink $default_sink + move_sinks_to_new_default $default_sink +} + +case "$BLOCK_BUTTON" in + 1) set_default_playback_device_next ;; + 2) amixer -q -D $MIXER sset $SCONTROL $CAPABILITY toggle ;; + 3) set_default_playback_device_next -1 ;; + 4) amixer -q -D $MIXER sset $SCONTROL $CAPABILITY $AUDIO_DELTA%+ ;; + 5) amixer -q -D $MIXER sset $SCONTROL $CAPABILITY $AUDIO_DELTA%- ;; +esac + +function print_format { + echo "$1" | envsubst '${SYMB}${VOL}${INDEX}${NAME}' +} + +function print_block { + ACTIVE=$(pactl list sinks | grep "State\: RUNNING" -B4 -A55 | grep "Name:\|Volume: \(front-left\|mono\)\|Mute:\|api.alsa.pcm.card = \|node.nick = ") + for Name in NAME MUTED VOL INDEX NICK; do + read $Name + done < <(echo "$ACTIVE") + INDEX=$(echo "$INDEX" | grep -o '".*"' | sed 's/"//g') + VOL=$(echo "$VOL" | grep -o "[0-9]*%" | head -1 ) + VOL="${VOL%?}" + NAME=$(echo "$NICK" | grep -o '".*"' | sed 's/"//g') + + if [[ $USE_ALSA_NAME == 1 ]] ; then + ALSA_NAME=$(pactl list sinks |\ +awk '/^\s*\*/{f=1}/^\s*index:/{f=0}f' |\ +grep "alsa.name\|alsa.mixer_name" |\ +head -n1 |\ +sed 's/.*= "\(.*\)".*/\1/') + NAME=${ALSA_NAME:-$NAME} + elif [[ $USE_DESCRIPTION == 1 ]] ; then + DESCRIPTION=$(pactl list sinks | grep "State\: RUNNING" -B4 -A55 | grep 'Description: ' | sed 's/^.*: //') + NAME=${DESCRIPTION:-$NAME} + fi + + if [[ $MUTED =~ "no" ]] ; then + SYMB=$AUDIO_HIGH_SYMBOL + [[ $VOL -le $AUDIO_MED_THRESH ]] && SYMB=$AUDIO_MED_SYMBOL + [[ $VOL -le $AUDIO_LOW_THRESH ]] && SYMB=$AUDIO_LOW_SYMBOL + COLOR=$DEFAULT_COLOR + else + SYMB=$AUDIO_MUTED_SYMBOL + COLOR=$MUTED_COLOR + fi + + if [[ $ACTIVE = "" ]] ; then + echo "Sound inactive" + COLOR='#222225' + fi + + if [[ $SUBSCRIBE == 1 ]] ; then + print_format "$LONG_FORMAT" + else + print_format "$LONG_FORMAT" + print_format "$SHORT_FORMAT" + echo "$COLOR" + fi +} + +print_block +if [[ $SUBSCRIBE == 1 ]] ; then + while read -r EVENT; do + print_block + done < <(pactl subscribe | stdbuf -oL grep change) +fi diff --git a/config/i3blocks/scripts/wifi b/config/i3blocks/scripts/wifi new file mode 100755 index 0000000..258ac8c --- /dev/null +++ b/config/i3blocks/scripts/wifi @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +# Copyright (C) 2014 Alexander Keller <github@nycroth.com> + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +#------------------------------------------------------------------------ +if [[ -z "$INTERFACE" ]] ; then + INTERFACE="${BLOCK_INSTANCE:-wlan0}" +fi +#------------------------------------------------------------------------ + +COLOR_GE80=${COLOR_GE80:-#00FF00} +COLOR_GE60=${COLOR_GE60:-#FFF600} +COLOR_GE40=${COLOR_GE40:-#FFAE00} +COLOR_LOWR=${COLOR_LOWR:-#FF0000} +COLOR_DOWN=${COLOR_DOWN:-#FF0000} + +# As per #36 -- It is transparent: e.g. if the machine has no battery or wireless +# connection (think desktop), the corresponding block should not be displayed. +[[ ! -d /sys/class/net/${INTERFACE}/wireless ]] && exit + +# If the wifi interface exists but no connection is active, "down" shall be displayed. +if [[ "$(cat /sys/class/net/$INTERFACE/operstate)" = 'down' ]]; then + echo "down" + echo "down" + echo $COLOR_DOWN + exit +fi + +#------------------------------------------------------------------------ + +QUALITY=$(iw dev ${INTERFACE} link | grep 'dBm$' | grep -Eoe '-[0-9]{2}' | awk '{print ($1 > -50 ? 100 :($1 < -100 ? 0 : ($1+100)*2))}') + +#------------------------------------------------------------------------ + +echo $QUALITY% # full text +echo $QUALITY% # short text + +# color +if [[ $QUALITY -ge 80 ]]; then + echo $COLOR_GE80 +elif [[ $QUALITY -ge 60 ]]; then + echo $COLOR_GE60 +elif [[ $QUALITY -ge 40 ]]; then + echo $COLOR_GE40 +else + echo $COLOR_LOWR +fi |