ArchLabs Terminal Info Script


#1

Hello all,

A simple little system info script to share, it can display images if you have w3m and a terminal that supports it. A version without the image display, but with our distro ascii art has been included in our pipemenus package, try it out :slight_smile: al-info

Back on subject

#!/usr/bin/env bash

# extremely simple system info script
# written by Nathaniel Maia, 2017-2018

# set these to try image display
# full path the image you want displayed eg. "~/Pictures/image.png"
IMG=""

# directory where the smaller 200x200 cropped image will be stored
# this will be created if it doesn't exist, eg. "~/.config/al-info"
THUMBD=""


# $FG resets colors
FG=$'\e[0m'

# Set colors as $FG0-7
for n in {0..7}; do printf -v FG${n} "%b" "\e[3${n}m"; done

# repeat $2 n times, where n = $1
rep_char() { nstring="$(printf "%$1s")" ; echo "${nstring// /$2}" ; }

# current window manager
cur_wm() {
    [[ $DISPLAY && ! $WM ]] && {
        ID="$(xprop -root -notype _NET_SUPPORTING_WM_CHECK)"
        WM="$(xprop -id "${ID##* }" -notype -len 100 -f _NET_WM_NAME 8t)"
        WM="${WM/*WM_NAME = }" WM="${WM/\"}" WM="${WM/\"*}"
    } || WM="${WM:-tty${TTY:9}}" # fallback for tty or unsupported WM
    echo "$WM"
}

# hostname, distro and uptime strings
HOSTN=$(hostname)
DIST=$(grep "^ID=" /etc/os-release | awk -F'=' '{print tolower($2)}')
UPT=$(uptime -p | awk -NF'up ' '{print $2}' | sed 's/ minutes\?/m/;s/ hours\?/h/;s/ days\?/d/')

# larger offset when using image display
[[ $IMG ]] && OFFSET="$(rep_char 38 ' ')" || OFFSET=" "

image() {
    # only display images when IMG is set and xorg is running
    # the tty console doesnt have the ability to
    if [[ $DISPLAY && $IMG && -e $IMG ]]; then
        
        # we dont need to crop and resize the image if it already exists
        if [[ $THUMBD && ! -e $THUMBD/${IMG##*/} ]]; then
            [[ ! -d $THUMBD ]] && mkdir -p $THUMBD   # create the folder if needed
            
            # get the original image size for cropping
            if [[ -z "$S" ]]; then
                S="$(identify -format "%w %h" "$IMG")"
                w="${S%% *}" h="${S##* }"
                ((h > w)) && S="$w" || S="$h"
            fi
            c="$(convert "$IMG" -colorspace srgb -format "%[pixel:p{0,0}]" info:)"

            # crop if needed and scale to 200x200 placing it in $THUMBD
            convert -background none "$IMG" -trim +repage -gravity south -background "$c" \
                -extent "$S"x"$S" -scale 200x200 "$THUMBD/${IMG##*/}"
        fi
        sleep 0.05

        # display the image using w3mimgdisplay
        # 1 pixel offsets from edge and 200x200 image size
        printf "%b\n" "0;1;1;1;200;200;;;;;"$THUMBD/${IMG##*/}"\n4;\n3;" | \
            /usr/lib/w3m/w3mimgdisplay -bg none &>/dev/null
    fi
}
clear

# print everything out
cat <<- EOF

${OFFSET}$FG5--->$FG $FG4$USER$FG2@$FG6$HOSTN
${OFFSET}$FG5$(rep_char $((${#USER} + 9 + ${#HOSTN})) "-")>

${OFFSET}$FG6->$FG   terminal$FG1:     $FG4$(sed 's/-256color//g' <<< "$TERM")
${OFFSET}$FG6->$FG   current wm$FG1:   $FG4$(cur_wm)
${OFFSET}$FG6->$FG   login shell$FG1:  $FG4$(basename "$SHELL")
${OFFSET}$FG6->$FG   total pkgs$FG1:   $FG4$(pacman -Qq | wc -l)
${OFFSET}$FG6->$FG   linux kernel$FG1: $FG4$(uname -r | cut -d "-" -f1)
${OFFSET}$FG6->$FG   distribution$FG1: $FG4${DIST}
${OFFSET}$FG6->$FG   total uptime$FG1: $FG4${UPT}

${OFFSET}$FG1β–ˆβ–ˆβ–ˆβ–ˆ$FG2β–ˆβ–ˆβ–ˆβ–ˆ$FG3β–ˆβ–ˆβ–ˆβ–ˆ$FG4β–ˆβ–ˆβ–ˆβ–ˆ$FG5β–ˆβ–ˆβ–ˆβ–ˆ$FG6β–ˆβ–ˆβ–ˆβ–ˆ$FG7β–ˆβ–ˆβ–ˆβ–ˆ$FG

EOF

# draw image if $IMG and $THUMBD are set
image

Edit: I added some more commenting to the script for those looking to hack/customize

Lemme know what you think

Cheers


#2

\o/ i’ll try!
Thanks!


#3

Loving this man.


#4

Thanky you nate! Really nice idea. I just change the arrows.


#5

Very nice, glad people like it.

@subjunkie Feel free to change all you like and share screens I love terminal eye candy :smiley:

This line here: ${OFFSET}$FG5$(rep_char $((${#USER} + 9 + ${#HOSTN})) "-")>
just uses the little rep_char function further up which repeats the character given, in this case a dash β€œ-”. Changing the "-" will change the characters under your hostname, the rest is just basic math to keep the line consistent with how long your user@hostname is.

You could also just change it to be hardcoded, as a user theres no need to make it work for everyone else, so long as it works for you :stuck_out_tongue:


#6

Thank you for the tip. Now i understand a liitle bit more, how those things working.


#7

Also, I’ll edit my original post but I should have mentioned

# full path the image you want displayed eg. "~/Pictures/image.png"
IMG=""

# directory where the smaller 200x200 cropped image will be stored
# this will be created if it doesn't exist, eg. "~/.config/al-info"
THUMBD=""

With urxvt images are displayed properly without issue in my testing, other terminals are hit and miss, some work, some not so great and the image disappears.


#8

Apologies for the lame question, but where can I find the script so I can mess about with it?


#9

@philT, al-info is in /bin


#10

Cheers mate. Got it!


#11

Actually, while Im here…

Is there a simple way just to show the info and remove the ascii art from the terminal?

Looks cool, but I just want to simplify the output.


#12

I put it in the following way @philT . Simple and with an image. In the same way as @natemaia .

# print everything out
cat <<- EOF

${OFFSET}
${OFFSET}$FG6->$FG2 Terminal$FG1:             $FG1$(sed 's/-256color//g' <<< "$TERM")
${OFFSET}$FG6->$FG2 Ventanas WM$FG1:          $FG1$(cur_wm)
${OFFSET}$FG6->$FG2 Mi Shell$FG1:             $FG1$(basename "$SHELL")
${OFFSET}$FG6->$FG2 Paquetes totales$FG1:     $FG1$(pacman -Qq | wc -l)
${OFFSET}$FG6->$FG2 Kernel de linux$FG1:      $FG1$(uname -r | cut -d "-" -f1)
${OFFSET}$FG6->$FG2 Distro$FG1:               $FG1${DIST}
${OFFSET}$FG6->$FG2 En marcha desde hace$FG1: $FG1${UPT}

${OFFSET}$FG1β–ˆβ–ˆβ–ˆβ–ˆ$FG2β–ˆβ–ˆβ–ˆβ–ˆ$FG3β–ˆβ–ˆβ–ˆβ–ˆ$FG4β–ˆβ–ˆβ–ˆβ–ˆ$FG5β–ˆβ–ˆβ–ˆβ–ˆ$FG6β–ˆβ–ˆβ–ˆβ–ˆ$FG7β–ˆβ–ˆβ–ˆβ–ˆ$FG

${OFFSET}$FG3$(rep_char $((${#USER} + 20 + ${#HOSTN})) "_")#!
${OFFSET}$FG3$(rep_char $((${#USER} + 25 + ${#HOSTN})) "_")#!

EOF
    image

#13

Thanks very much! @Negata