Skip to content

Instantly share code, notes, and snippets.

@AkBKukU
Last active July 17, 2018 21:27
Show Gist options
  • Save AkBKukU/9d7d82d1c8114d6cdb5c6c52805c4aec to your computer and use it in GitHub Desktop.
Save AkBKukU/9d7d82d1c8114d6cdb5c6c52805c4aec to your computer and use it in GitHub Desktop.
My MOTD and the needed battery and temp scripts. It needs lm-sensors at least.
#!/bin/bash
BATTERY=$(upower -e | grep battery )
BATSTAT="$(upower -i $BATTERY)"
TERMWIDTH=$(tput cols)
percent="$(echo "$BATSTAT" | grep percentage | awk '{print $2}')"
percent=${percent:0:$(expr ${#percent} - 1)}
if [ "t$1" != "t" ]
then
TERMWIDTH=$1
fi
if [ "$1" == "-b" ]
then
echo "$percent"
exit
fi
CRES="\e[0;39m"
CBLA="\e[0;30m"
CRED="\e[0;31m"
CGRE="\e[0;32m"
CYEL="\e[0;33m"
BGRA="\e[100m"
BRED="\e[41m"
BGRE="\e[42m"
BYEL="\e[43m"
nump=$(echo "0.$percent")
left=$(echo $(bc <<< "scale=0; $nump * $TERMWIDTH") | tr '.' ' '| awk '{print $1}' )
half=$(expr $TERMWIDTH / 2)
half=$(expr $half - 1)
if [ "$percent" -gt "66" ]
then
BCOLOR=$(echo $BGRE)
PCOLOR=$(echo $CBLA)
elif [ "$percent" -gt "50" ]
then
BCOLOR=$(echo $BYEL)
PCOLOR=$(echo $CBLA)
else
BCOLOR=$(echo $BGRA)
PCOLOR=$(echo $CRES)
fi
if [ "$percent" -gt "66" ]
then
COLOR=$(echo $CGRE)
elif [ "$percent" -gt "33" ]
then
COLOR=$(echo $CYEL)
else
COLOR=$(echo $CRED)
fi
perl=${#percent}
for ((i = 0 ; i < ( $TERMWIDTH ) ; i++))
do
if [[ "$i" -eq "$half" ]]
then
echo -ne "$PCOLOR$BCOLOR$percent%"
i=$(expr $i + $perl)
elif [ $i -lt "$left" ] || [[ "100" -eq "$percent" ]]
then
echo -ne "$COLOR\xe2\x96\x88"
else
echo -ne "$CRES\xe2\x96\x91"
fi
done
echo -e $CRES
#!/bin/bash
#--Strings
CBLUE="\e[0;34m"
TERMWIDTH=$(tput cols)
BATEXISTS=$(upower -e | grep -c battery.sh)
longest="0"
#--Functions
#--colorIso
#-Colors an iso 8601 timestamp
function colorIso
{
IN=$1
K="\e[0;32m"
C="\e[1;30m"
TEST="bob"
echo "$K${IN:0:4}$C${IN:4:1}$K${IN:5:2}$C${IN:7:1}$K${IN:8:2}$C${IN:10:1}$K${IN:11:2}$C${IN:13:1}$K${IN:14:2}$C${IN:16:1}$K${IN:17:2}\e[0m"
}
#--buildLine
#-Builds the leading and following lines
function buildLine ()
{
line="\e[1;30m|"
other=1
for ((i = 0 ; i < ( $TERMWIDTH - 2 ) ; i++))
do
if [ $other == "0" ]
then
line="$(echo $line)\e[0;31m="
other=1
elif [ $other == "1" ]
then
line="$(echo $line)\e[1;31m="
other=0
fi
done
line="$(echo $line)\e[1;30m|\e[0m"
}
#--buildLine
#-Builds the leading and following lines
function centerColor ( )
{
plain=$(echo -e $1 | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g")
length=${#plain}
half=$(expr $TERMWIDTH / 2 - $length / 2 )
testlong $length
echo -e "$(printf "%$(echo $half)s" "")$1"
}
#--buildLine
#-Builds the leading and following lines
function centerBlock ( )
{
IFS=$'\n' array=($1)
lines=${#array[@]}
plain=$(echo -e ${array[0]} | sed -r "s/\t/ /g")
length=${#plain}
half=$(expr $TERMWIDTH / 2 - $length / 2 )
testlong $length
for ((i = 0 ; i < $lines ; i++))
do
echo -e "$(printf "%$(echo $half)s" "")${array[i]}"
done
}
#--buildLine
#-Builds the leading and following lines
function centerChars ( )
{
plain=$(echo -e $1 | sed -r "s/\t/ /g")
length=${#plain}
half=$(expr $TERMWIDTH / 2 - $length / 2 )
testlong $length
echo -e "$(printf "%$(echo $half)s" "")$1"
}
#--drvCol
#-Drive Color Indicator
function drvColor
{
noerror="yeah"
}
#--batteryline
#-Prints batery info
function batteryline ()
{
if [ "$BATEXISTS" == "1" ]
then
echo
centerChars "Battery"
centerColor "$(battery.sh $longest)"
fi
}
#--testlong
#-Stores the longest line length
function testlong ()
{
if [ "$1" -gt $longest ]
then
longest="$1"
fi
}
#--gettemp
#-Returns the current CPU temp
function gettempline ()
{
echo
centerColor "Temperature"
centerColor "$(temp.sh)"
}
buildLine
echo -e $line
centerColor "Welcome ${CBLUE}User\e[0m!"
centerColor "It's $(colorIso $(date "+%Y:%m:%dT%H:%M:%S"))"
echo
centerColor "You were last on at $(colorIso $(cat .lastlogin) )"
echo $(date "+%Y:%m:%dT%H:%M:%S") > ~/.lastlogin
echo
centerBlock "$(df -h 2> /dev/null | grep -v "tmpfs\|none\|udev" | sed -r 's/Use%/Usage/g')"
echo
centerChars " Memory Used / Cached / Total"
centerBlock "$(free -mh |awk '{print $1"\t\t"$3" / "$7" / "$2}' | grep -v "buff\|total")"
batteryline
gettempline
echo -e $line
echo -e "\e[0m "
#--Ideas
# Color code usages for amount taken
#!/bin/bash
CRES="\e[0;39m"
CBLA="\e[0;30m"
CRED="\e[0;31m"
CGRE="\e[0;32m"
CYEL="\e[0;33m"
BGRA="\e[100m"
BRED="\e[41m"
BGRE="\e[42m"
BYEL="\e[43m"
temp="$(sensors | grep 'id 0:' | awk '{print $4}' | tr -d '+' | cut -c 1-2 )"
tjmax="$(sensors | grep 'id 0:' | awk '{print $7}' | tr -d '+' | tr -d ',' | cut -c 1-2 )"
tempdiv="$(echo $temp)00"
percent="$(expr $tempdiv / $tjmax )"
if [[ "$percent" -gt "88" ]]
then
COLOR=$(echo $CGRE)
elif [[ "$percent" -gt "66" ]]
then
COLOR=$(echo $CYEL)
else
COLOR=$(echo $CGRA)
fi
echo -e "$COLOR$temp/$tjmax °C"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment