You are not logged in.
Hi! I have a Bash script that has colored output.
I use this function for returning the same message but colored with ansii escape codes (I think that's the name):
## Availible Color shemes
deBlack='\033[0;30m'
Red='\033[0;31m'
Green='\033[0;32m'
BrownOrange='\033[0;33m'
Blue='\033[0;34m'
Purple='\033[0;35m'
Cyan='\033[0;36m'
LightGray='\033[0;37m'
DarkGray='\033[1;30m'
LightRed='\033[1;31m'
LightGreen='\033[1;32m'
Yellow='\033[1;33m'
LightBlue='\033[1;34m'
LightPurple='\033[1;35m'
LightCyan='\033[1;36m'
White='\033[1;37m'
NoColor='\033[0m'
#Red="#FFFF00"
#BrownOrange="#00FF00"
colorize () {
echo "${1}${2}${NoColor}"
#echo "<span foreground='$1'>$2</span>"
#echo "$2"
}I get these strange symbols and some of the color codes in the bar.
it works normal in the terminal.
can I modify that function so that it works or do I need to edit the rest of the file?
here's the rest:
#! /bin/bash
## Availible Color shemes
deBlack='\033[0;30m'
Red='\033[0;31m'
Green='\033[0;32m'
BrownOrange='\033[0;33m'
Blue='\033[0;34m'
Purple='\033[0;35m'
Cyan='\033[0;36m'
LightGray='\033[0;37m'
DarkGray='\033[1;30m'
LightRed='\033[1;31m'
LightGreen='\033[1;32m'
Yellow='\033[1;33m'
LightBlue='\033[1;34m'
LightPurple='\033[1;35m'
LightCyan='\033[1;36m'
White='\033[1;37m'
NoColor='\033[0m'
#Red="#FFFF00"
#BrownOrange="#00FF00"
colorize () {
echo "${1}${2}${NoColor}"
#echo "<span foreground='$1'>$2</span>"
#echo "$2"
}
mem () {
mem="$(free -h)"
swap="$(zramctl)"
mused="$(echo $mem | awk 'NR==1{print $9}')"
mtotal="$(echo $mem | awk 'NR==1{print $8}')"
sused="$(echo $swap | awk 'NR==1{print $12}')"
sunc="$(echo $swap | awk 'NR==1{print $14}')"
stotal="$(echo $swap | awk 'NR==1{print $11}')"
mused="${mused::-1}"
mtotal="${mtotal::-1}"
if [ ${sunc##*[0-9]} = "G" ] && [ ${sunc::-1} -ge 8 ]; then
mused="$(colorize $Red $mused)"
sused="$(colorize $Red $sused)"
fi
out="${out} Mem: ${mused}/${mtotal} | Swap: ${sused}/${stotal} |"
}
timelef () {
a="$(cat ~/.time/time)"
if systemctl --user --quiet is-active timectl.timer; then
b=1
else
b="$(colorize $BrownOrange 0)"
fi
if [ $a -le 3 ]; then
a="$(colorize $Red $a)"
elif [ $a -le 8 ]; then
a="$(colorize $BrownOrange $a)"
fi
out="${out} $a ($b) |"
}
notify-get () {
if ! h="$(cat /tmp/blink)"; then
h=0
echo $h >/tmp/blink
fi
if [ $h = 0 ]; then
out="$(notify -r --nonewlines)"
if [ $(echo $out | wc -L ) -gt 0 ]; then
out="$(colorize $Cyan "$out") |"
fi
elif [ $h = 1 ]; then
out=""
fi
if [ $h = 0 ]; then
echo 1 >/tmp/blink
else
echo 0 >/tmp/blink
fi
}
cpu () {
temp=$(( $(cat /sys/class/thermal/thermal_zone0/temp)/1000 ))
if [ $temp -ge 80 ]; then
temp="$(colorize $Red $temp)"
elif [ $temp -ge 55 ]; then
temp="$(colorize $BrownOrange $temp)"
fi
freq="$(cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq)"
n=0
for i in $freq
do
n=$n+$i
done
n=$(( ($n)/160000))
if [ $n -lt 100 ]; then
n="0${n}"
fi
g="$n"
n="${n::1}.${n:1}"
if [ $g -ge 270 ]; then
n="$(colorize $BrownOrange $n)"
fi
out="$out ${n} Ghz [${temp}°C] |"
}
battery () {
bat="$(cat /sys/class/power_supply/BAT0/capacity)"
if [ $bat -le 8 ]; then
bat="$(colorize $Red $bat)"
elif [ $bat -le 20 ]; then
bat="$(colorize $BrownOrange $bat)"
fi
out="$out Bat: ${bat}% |"
}
wlan () {
name=$(cat /tmp/wifi | awk 'NR==2{print $3}')
strength=$(cat /tmp/wifi | awk 'NR==2{print $8}')
internet=$(cat /tmp/is_online)
strength=${strength:-"none"}
if ! [ $strength = "none" ]; then
if [ $strength -le 20 ]; then
strength="$(colorize $Red $strength)"
elif [ $strength -le 40 ]; then
strength="$(colorize $BrownOrange $strength)"
elif [ $strength -ge 80 ]; then
strength="$(colorize $Green $strength)"
fi
fi
out="$out ${name} ${strength} ($internet) |"
}
notify-get
wlan
cpu
mem
battery
timelef
out="$out $(date "+%I:%M")´´"
echo -e "$out"
#swaymsg -t command "swaymsg 'message \"$out\"'"Last edited by jl2 (2023-06-05 19:18:21)
Why I run Arch? To "BTW I run Arch" the guy one grade younger.
And to let my siblings and cousins laugh at Arsch Linux...
Offline
Does the script behave correctly if you just "echo $2"?
If so you can probably discriminate the behavior based on the result of "tty", "$TERM" or the $PPID (eg. realpath /proc/$PPID/exe)
Don't test that inside colorize, redefine the function based on the result.
Online
yes, but it doesn't have color. the color also works normally in the terminal.
how do you mean discriminate?
Last edited by jl2 (2023-06-05 07:39:06)
Why I run Arch? To "BTW I run Arch" the guy one grade younger.
And to let my siblings and cousins laugh at Arsch Linux...
Offline
https://man.archlinux.org/man/extra/sway/sway-bar.5.en
https://docs.gtk.org/Pango/pango_markup.html
Enable pango_markup and use spans w/ the color attribute.
Edit: you check tty or term or ppid and redifine the color function based on that.
Discrimination is often used in a negative context but just means "treat differently"
Last edited by seth (2023-06-05 07:57:17)
Online
thanks for the first link.
I had already tried that. It was commented out in the OP.
Now it doesn't work in the terminal but I don't care. that's a separate script.
Thanks.
Why I run Arch? To "BTW I run Arch" the guy one grade younger.
And to let my siblings and cousins laugh at Arsch Linux...
Offline
Red='\033[0;31m'
BrownOrange='\033[0;33m'
…
NoColor='\033[0m'
colorize () {
echo "${1}${2}${NoColor}"
}
if is_swaybar; then
Red="#FFFF00"
BrownOrange="#00FF00"
…
colorize () {
echo "<span color='$1'>$2</span>"
}
fiis_swaybar needs to be determined, I don't use sway but would inspect the tty, $PPID or $TERM to see whether this might be an interactive shell or something else.
Online
PPID=8150; changes with each reload
TERM=dumb
janluca 8150 1.6 0.1 246336 15672 tty1 S+ 20:49 0:00 swaybar -b bar-0I'm gonna go with $TERM.
In case you need a sway status bar:
#! /bin/dash
## Availible Color shemes
## doesn't work with dash
#if [ "$TERM" = "dumb" ]; then
Red=#FF0000
Green=#00FF00
BrownOrange=#FFDD00
Cyan=#00FFDD
#else
#Black='\033[0;30m'
#Red='\033[0;31m'
#Green='\033[0;32m'
#BrownOrange='\033[0;33m'
#Blue='\033[0;34m'
#Purple='\033[0;35m'
#Cyan='\033[0;36m'
#LightGray='\033[0;37m'
#DarkGray='\033[1;30m'
#LightRed='\033[1;31m'
#LightGreen='\033[1;32m'
#Yellow='\033[1;33m'
#LightBlue='\033[1;34m'
#LightPurple='\033[1;35m'
#LightCyan='\033[1;36m'
#White='\033[1;37m'
#NoColor='\033[0m'
#fi
colorize () {
#if [ "$TERM" = "dumb" ]; then
echo "<span foreground='$1'>$2</span>"
#else
#echo "${1}${2}${NoColor}"
#d=#FF0000
}
mem () {
mem="$(free -h)"
swap="$(zramctl)"
mused="$(echo $mem | awk 'NR==1{print $9}')"
mtotal="$(echo $mem | awk 'NR==1{print $8}')"
sused="$(echo $swap | awk 'NR==1{print $12}')"
sunc="$(echo $swap | awk 'NR==1{print $14}')"
stotal="$(echo $swap | awk 'NR==1{print $11}')"
mused="${mused%?}"
mtotal="${mtotal%?}"
if [ ${sunc##*[0-9]} = "G" ] && [ ${sunc%?} -ge 8 ]; then
mused="$(colorize $Red $mused)"
sused="$(colorize $Red $sused)"
fi
out="${out} Mem: ${mused}/${mtotal} | Swap: ${sused}/${stotal} |"
}
timelef () {
## see how much screen time I have left...
a="$(cat ~/.time/time)"
if systemctl --user --quiet is-active timectl.timer; then
b=1
else
b="$(colorize $BrownOrange 0)"
fi
if [ $a -le 3 ]; then
a="$(colorize $Red $a)"
elif [ $a -le 8 ]; then
a="$(colorize $BrownOrange $a)"
fi
out="${out} $a ($b) |"
}
notifyget () {
## if you guess how I wrote the other script needed to maintain this function :)
if ! h="$(cat /tmp/blink)"; then
h=0
echo $h >/tmp/blink
fi
if [ $h = 0 ]; then
out="$(notify -r --nonewlines)"
if [ $(echo $out | wc -L ) -gt 0 ]; then
out="$(colorize $Cyan "$out") |"
fi
elif [ $h = 1 ]; then
out=""
fi
if [ $h = 0 ]; then
echo 1 >/tmp/blink
else
echo 0 >/tmp/blink
fi
}
cpu () {
temp=$(( $(cat /sys/class/thermal/thermal_zone0/temp)/1000 ))
if [ $temp -ge 85 ]; then
temp="$(colorize $Red $temp)"
elif [ $temp -ge 65 ]; then
temp="$(colorize $BrownOrange $temp)"
fi
freq="$(cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq)"
n=0
for i in $freq
do
n=$n+$i
done
n=$(( ($n)/160000))
if [ $n -lt 100 ]; then
n="0${n}"
fi
g="$n"
n="${n%??}.${n#?}"
if [ $g -ge 320 ]; then
n="$(colorize $BrownOrange $n)"
fi
out="$out Cpu: ${n} Ghz [${temp}°C] |"
}
battery () {
bat="$(cat /sys/class/power_supply/BAT0/capacity)"
if [ $bat -le 8 ]; then
bat="$(colorize $Red $bat)"
elif [ $bat -le 20 ]; then
bat="$(colorize $BrownOrange $bat)"
fi
out="$out Bat: ${bat}% |"
}
wlan () {
## heavy editing of this section needed if you don't use iwd
iwctl station wlan0 show >/tmp/wifi
if [ "$(cat /tmp/wifi)" = "No station on device: 'wlan0'" ]; then
if [ $(rfkill list wifi | sed -n 2p | awk '{print $3}') = "yes" ]; then
strength="airplane"
fi
else
strength=$(cat /tmp/wifi | grep " RSSI" | awk '{print $2}')
if [ "$strength" = "" ]; then
strength=0
else
strength=$(( $strength + 100))
fi
name="$(cat /tmp/wifi | grep "Connected network" | awk '{print $3}')"
if [ $strength -le 20 ]; then
strength="$(colorize $Red $strength)"
elif [ $strength -le 40 ]; then
strength="$(colorize $BrownOrange $strength)"
elif [ $strength -ge 70 ]; then
strength="$(colorize $Green $strength)"
fi
fi
## check if google.ch is reachable
internet=$(cat /tmp/is_online)
if [ $internet = 0 ] && ! [ $strength = "airplane" ]; then
internet=$(colorize $Red 0)
fi
out="${out}Wlan: ${name} ${strength} ($internet) |"
}
vol () {
## I use wireplumber - edit accordingly
d=$(wpctl get-volume @DEFAULT_AUDIO_SINK@)
if [ "$(echo $d | awk '{print $3}')" = "[MUTED]" ]; then
d="muted"
else
d=$(echo $d | awk '{print $2}')
b=$d
d=${d#??}
if [ ${b%???} -gt 0 ]; then
d=${b%???}${d}
fi
fi
out="$out Vol: ${d}% | "
}
notifyget
wlan
cpu
mem
vol
battery
timelef
out="$out $(date "+%I:%M") ´"
echo "$out"It runs on dash, but feel free to edit it for bash
note dash doesn't seem to support $TERM, so I commented it out.
You made my desktop a little bit more colorful. thanks!
Last edited by jl2 (2023-06-05 19:17:57)
Why I run Arch? To "BTW I run Arch" the guy one grade younger.
And to let my siblings and cousins laugh at Arsch Linux...
Offline