From d3bb8a1ecb741723a014da90f5c5798d1da0f62d Mon Sep 17 00:00:00 2001 From: David Luevano <55825613+luevano@users.noreply.github.com> Date: Tue, 11 Feb 2020 00:51:35 -0700 Subject: Add blocklets and py --- .local/bin/blocklets/calendar | 41 ++++++++++ .local/bin/blocklets/cpu | 62 +++++++++++++++ .local/bin/blocklets/gpu | 71 +++++++++++++++++ .local/bin/blocklets/memory | 69 +++++++++++++++++ .local/bin/blocklets/time | 100 ++++++++++++++++++++++++ .local/bin/blocklets/volume | 175 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 518 insertions(+) create mode 100755 .local/bin/blocklets/calendar create mode 100755 .local/bin/blocklets/cpu create mode 100755 .local/bin/blocklets/gpu create mode 100755 .local/bin/blocklets/memory create mode 100755 .local/bin/blocklets/time create mode 100755 .local/bin/blocklets/volume (limited to '.local/bin/blocklets') diff --git a/.local/bin/blocklets/calendar b/.local/bin/blocklets/calendar new file mode 100755 index 0000000..eb8e210 --- /dev/null +++ b/.local/bin/blocklets/calendar @@ -0,0 +1,41 @@ +#! /bin/sh + +WIDTH=${WIDTH:-200} +HEIGHT=${HEIGHT:-200} +DATEFMT=${DATEFMT:-"+%a %d.%m.%Y %H:%M:%S"} +SHORTFMT=${SHORTFMT:-"+%H:%M:%S"} + +OPTIND=1 +while getopts ":f:W:H:" opt; do + case $opt in + f) DATEFMT="$OPTARG" ;; + W) WIDTH="$OPTARG" ;; + H) HEIGHT="$OPTARG" ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument." >&2 + exit 1 + ;; + esac +done + +case "$BLOCK_BUTTON" in + 1|2|3) + + # the position of the upper left corner of the popup + # The size is hardcoded for a 1080p monitor. + posX=$((1899 - $WIDTH)) + posY=$((35)) + + i3-msg -q "exec yad --calendar \ + --width=$WIDTH --height=$HEIGHT \ + --undecorated --fixed \ + --close-on-unfocus --no-buttons \ + --posx=$posX --posy=$posY \ + > /dev/null" +esac +echo "$LABEL$(date "$DATEFMT")" +echo "$LABEL$(date "$SHORTFMT")" diff --git a/.local/bin/blocklets/cpu b/.local/bin/blocklets/cpu new file mode 100755 index 0000000..44c1189 --- /dev/null +++ b/.local/bin/blocklets/cpu @@ -0,0 +1,62 @@ +#!/usr/bin/perl +# +# Copyright 2014 Pierre Mavro +# Copyright 2014 Vivien Didelot +# Copyright 2014 Andreas Guldstrand +# +# 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} // ""; + +sub help { + print "Usage: cpu_usage [-w ] [-c ] [-d ]\n"; + print "-w : warning threshold to become yellow\n"; + print "-c : critical threshold to become red\n"; + print "-d : Use 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 () { + 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 "#FF0000\n"; + exit 33; +} elsif ($cpu_usage >= $t_warn) { + print "#FFFC00\n"; +} + +exit 0; diff --git a/.local/bin/blocklets/gpu b/.local/bin/blocklets/gpu new file mode 100755 index 0000000..e5e460b --- /dev/null +++ b/.local/bin/blocklets/gpu @@ -0,0 +1,71 @@ +#! /usr/bin/perl + +# 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 . + +use strict; +use warnings; +use utf8; +use Getopt::Long; + +# default values +my $t_warn = $ENV{T_WARN} || 70; +my $t_crit = $ENV{T_CRIT} || 90; +my $gpu_usage = -1; +my $gpu_mem = -1; +my $gpu_video = -1; +my $gpu_pcie = -1; +my $label = $ENV{LABEL} // ""; + +sub help { + print "Usage: gpu-load [-w ] [-c ]\n"; + print "-w : warning threshold to become amber\n"; + print "-c : critical threshold to become red\n"; + exit 0; +} + +GetOptions("help|h" => \&help, + "w=i" => \$t_warn, + "c=i" => \$t_crit); + +# Get GPU usage from nvidia-settings +open (NVS, 'nvidia-settings -q GPUUtilization -t |') or die; +while () { + if (/^[a-zA-Z]*=(\d+), [a-zA-Z]*=(\d+), [a-zA-Z]*=(\d+), [a-zA-Z]*=(\d+)$/) { + $gpu_usage = $1; + $gpu_mem = $2; + $gpu_video = $3; + $gpu_pcie = $4; + last; + } +} +close(NVS); + +$gpu_usage eq -1 and die 'Can\'t find GPU information'; + +# I edited this to only show the gpu_usage and gpu_mem. +# Also fixed the label thing. +# Print full_text, short_text +# printf "%.0f%% %.0f%% %.0f%% %.0f%%\n", $gpu_usage, $gpu_mem, $gpu_video, $gpu_pcie; +# printf "%.0f%%\n", $gpu_usage; +printf "${label} %.0f%% %.0f%%\n", $gpu_usage, $gpu_mem; + +# Print color, if needed +if ($gpu_usage >= $t_crit || $gpu_mem >= $t_crit || $gpu_video >= $t_crit || $gpu_pcie >= $t_crit) { + print "#FF0000\n"; + exit 33; +} elsif ($gpu_usage >= $t_warn || $gpu_mem >= $t_warn || $gpu_video >= $t_warn || $gpu_pcie >= $t_warn) { + print "#FFBF00\n"; +} + +exit 0; diff --git a/.local/bin/blocklets/memory b/.local/bin/blocklets/memory new file mode 100755 index 0000000..90eb2c6 --- /dev/null +++ b/.local/bin/blocklets/memory @@ -0,0 +1,69 @@ +#!/bin/sh +# Copyright (C) 2014 Julien Bonjean + +# 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 . + +TYPE="${BLOCK_INSTANCE:-mem}" + +awk -v type=$TYPE ' +/^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 + printf("%.1fG/%.1fG (%.f%%)\n", used, total, pct) + + # 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/.local/bin/blocklets/time b/.local/bin/blocklets/time new file mode 100755 index 0000000..725fc68 --- /dev/null +++ b/.local/bin/blocklets/time @@ -0,0 +1,100 @@ +#!/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}}; + +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/.local/bin/blocklets/volume b/.local/bin/blocklets/volume new file mode 100755 index 0000000..ad1b2d1 --- /dev/null +++ b/.local/bin/blocklets/volume @@ -0,0 +1,175 @@ +#!/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 + pacmd list-sink-inputs | grep index: | grep -o '[0-9]\+' | while read SINK + do + pacmd move-sink-input $SINK $DEFAULT_SINK + done +} + +function set_default_playback_device_next { + inc=${1:-1} + num_devices=$(pacmd list-sinks | grep -c index:) + sink_arr=($(pacmd list-sinks | grep index: | grep -o '[0-9]\+')) + default_sink_index=$(( $(pacmd list-sinks | grep index: | grep -no '*' | grep -o '^[0-9]\+') - 1 )) + default_sink_index=$(( ($default_sink_index + $num_devices + $inc) % $num_devices )) + default_sink=${sink_arr[$default_sink_index]} + pacmd 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=$(pacmd list-sinks | grep "state\: RUNNING" -B4 -A7 | grep "index:\|name:\|volume: front\|muted:") + [ -z "$ACTIVE" ] && ACTIVE=$(pacmd list-sinks | grep "index:\|name:\|volume: front\|muted:" | grep -A3 '*') + for name in INDEX NAME VOL MUTED; do + read $name + done < <(echo "$ACTIVE") + INDEX=$(echo "$INDEX" | grep -o '[0-9]\+') + VOL=$(echo "$VOL" | grep -o "[0-9]*%" | head -1 ) + VOL="${VOL%?}" + + NAME=$(echo "$NAME" | sed \ +'s/.*<.*\.\(.*\)>.*/\1/; t;'\ +'s/.*<\(.*\)>.*/\1/; t;'\ +'s/.*/unknown/') + + if [[ $USE_ALSA_NAME == 1 ]] ; then + ALSA_NAME=$(pacmd 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=$(pacmd list-sinks |\ +awk '/^\s*\*/{f=1}/^\s*index:/{f=0}f' |\ +grep "device.description" |\ +head -n1 |\ +sed 's/.*= "\(.*\)".*/\1/') + 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 [[ $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 -- cgit v1.2.3-54-g00ecf