The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

Views: 81,139β€…    Votes:  4β€…
Tags: bash   login   prompt   profile  
Link: πŸ” See Original Answer on Unix & Linux ⧉ πŸ”—

URL: https://unix.stackexchange.com/q/434701
Title: How can I change what's displayed at a login shell?
ID: /2018/03/31/How-can-I-change-what_s-displayed-at-a-login-shell_
Created: March 31, 2018
Upload: April 8, 2024    Layout:  post
TOC: true    Navigation:  true    Copy to clipboard:  false


From Ask Ubuntu:

Ubuntu splash screen.png

Check the Ask Ubuntu link above for comments and possibly new answers from other users.

ToC Skip

now bash script

The heavy lifting is the splash component that shows this:

$ now
 
Weather report: Edmonton               March 2018            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                  Su Mo Tu We Th Fr Sa       β”‚   ┏━┓╺┓  ┏━┓┏━┓   ┏━┓┏┳┓   β”‚
     \   /     Sunny                           1  2  3       β”‚   ┃┃┃ ┃ ╹┏━┛┗━┫   ┣━┛┃┃┃   β”‚
      .-.      -23--14 Β°C          4  5  6  7  8  9 10       β”‚   ┗━┛╺┻╸╹┗━╸┗━┛   β•Ή  β•Ή β•Ή   β”‚
   ― (   ) ―   β†˜ 22 km/h          11 12 13 14 15 16 17       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      `-’      14 km              18 19 20 21 22 23 24  
     /   \     0.9 mm             25 26 27 28 29 30 31  
                                                        

Yes it really is -14 in Edmonton and feels like -23. A good time to spend the long-weekend inside playing the newly arrived Tomb Raider 2013! Maybe brush up on that Resume to move to Vancouver or Montreal…

Here is the code:

#!/bin/bash

# NAME: now
# PATH: $HOME/bin
# DESC: Display current weather, calendar and time
# CALL: Called from terminal or ~/.bashrc
# DATE: Apr 6, 2017. Modified: Mar 30, 2018.

# NOTE: To display all available toilet fonts use this one-liner:
#       for i in ${TOILET_FONT_PATH:=/usr/share/figlet}/*.{t,f}lf; do j=${i##*/}; toilet -d "${i%/*}" -f "$j" "${j%.*}"; done

# Setup for 92 character wide terminal
DateColumn=34 # Default is 27 for 80 character line, 34 for 92 character line
TimeColumn=61 # Default is 49 for   "   "   "   "    61 "   "   "   "

#--------- WEATHER ----------------------------------------------------------

# Current weather, already in color so no need to override
echo " "
# Replace Edmonton with your city name, GPS, etc. See: curl wttr.in/:help
curl wttr.in/Edmonton?0 --silent --max-time 3
# Timeout #. Increase for slow connection---^

echo " "
echo " "                # Pad with blank lines for calendar & time to fit

#--------- DATE -------------------------------------------------------------

# calendar current month with today highlighted.
# colors 00=bright white, 31=red, 32=green, 33=yellow, 34=blue, 35=purple,
#        36=cyan, 37=white

tput sc                 # Save cursor position.
# Move up 9 lines
while [ $((++i)) -lt 10 ]; do tput cuu1; done

# Depending on length of your city name and country name you will:
#   1. Comment out next three lines of code. Uncomment fourth code line.
#   2. Change subtraction value and set number of print spaces to match
#      subtraction value. Then place comment on fourth code line.

Column=$(($DateColumn - 10))
tput cuf $Column        # Move x column number
printf "          "     # Blank out ", country" with x spaces
#tput cuf $DateColumn    # Position to column 27 for date display


# -h needed to turn off formating: https://askubuntu.com/questions/1013954/bash-substring-stringoffsetlength-error/1013960#1013960
cal -h > /tmp/terminal

CalLineCnt=1
Today=$(date +"%d")
# Prefix with space when length < 2
if [[ ${#Today} < 2 ]] ; then
    Today=" "$Today
fi
printf "\033[32m"   # color green -- see list above.

while IFS= read -r Cal; do
    printf "$Cal"
    if [[ $CalLineCnt > 2 ]] ; then
        # See if today is on current line & invert background
        tput cub 22
        for (( j=0 ; j <= 18 ; j += 3 )) ; do
            Test=${Cal:$j:2}            # Current day on calendar line
            if [[ "$Test" == "$Today" ]] ; then
                printf "\033[7m"        # Reverse: [ 7 m
                printf "$Today"
                printf "\033[0m"        # Normal: [ 0 m
                printf "\033[32m"       # color green -- see list above.
                tput cuf 1
            else
                tput cuf 3
            fi
        done
    fi

    tput cud1               # Down one line
    tput cuf $DateColumn    # Move 27 columns right
    CalLineCnt=$((++CalLineCnt))
done < /tmp/terminal

printf "\033[00m"           # color -- bright white (default)
echo ""

tput rc                     # Restore saved cursor position.

#-------- TIME --------------------------------------------------------------

tput sc                 # Save cursor position.
# Move up 9 lines
i=0
while [ $((++i)) -lt 10 ]; do tput cuu1; done
tput cuf $TimeColumn    # Move 49 columns right

# Do we have the toilet package?
if hash toilet 2>/dev/null; then
    echo " "$(date +"%I:%M %P")" " | \
        toilet -f future --filter border > /tmp/terminal
# Do we have the figlet package?
elif hash figlet 2>/dev/null; then
    echo $(date +"%I:%M %P") | figlet > /tmp/terminal
# else use standard font
else
    echo $(date +"%I:%M %P") > /tmp/terminal
fi

while IFS= read -r Time; do
    printf "\033[01;36m"    # color cyan
    printf "$Time"
    tput cud1               # Up one line
    tput cuf $TimeColumn    # Move 49 columns right
done < /tmp/terminal

tput rc                     # Restore saved cursor position.

exit 0

Top ToS Skip

Table of Contents

Top ToS ToC Skip

Prerequisites

For the fancy time display you need to install toilet:

sudo apt install toilet

For another fancy time display (but not quite as fancy) install figlet:

sudo apt install figlet

Otherwise the time will be displayed in β€œnormal” font.

Top ToS ToC Skip

Weather

Weather is provided by the curl wttr.in/cityname?0 command. In your terminal use: curl wttr.in/:help for additional details.

Weather: Change city name

You’ll want to modify this section of code and change Edmonton to your city name:

# Replace Edmonton with your city name, GPS, etc. See: curl wttr.in/:help
curl wttr.in/Edmonton?0 --silent --max-time 3
# Timeout #. Increase for slow connection---^

Unicode is supported for city names like /Москва (Moscow). Airport letters are supported such as YEG for Edmonton.

Weather: Remove country name

When terminal is set for 92 characters wide, the weather displays as β€œEdmonton, Canada.” which is too long for my liking:

Weather with country.png

Worse yet when terminal is set for default 80 characters wide:

Weather country name 80 chars.png

To circumvent the problem, β€œ, Countryname” is blanked out on the screen with this code:

# Depending on length of your city name and country name you will:
#   1. Comment out next three lines of code. Uncomment fourth code line.
#   2. Change subtraction value and set number of print spaces to match
#      subtraction value. Then place comment on fourth code line.
Column=$(($DateColumn - 10))
tput cuf $Column        # Move x column number
printf "          "     # Blank out ", country" with x spaces
#tput cuf $DateColumn    # Position to column 27 for date display

If you need help with this part of the script please post comment below for assistance.

Top ToS ToC Skip

Terminal screen width adjustments

Adjust the spacing to your terminal screen width by changing:

# Setup for 92 character wide terminal
DateColumn=34 # Default is 27 for 80 character line, 34 for 92 character line
TimeColumn=61 # Default is 49 for   "   "   "   "    61 "   "   "   "

Top ToS ToC Skip

Tying it all together in ~/.bashrc

Edit your ~/.bashrc file and add these lines to the bottom:

# Splash Calendar and time
now

# ASCII Linux distribution display
screenfetch

Save the `~/.bashrc” file changes.

To display the Ubuntu information you need screenfetch:

sudo apt install screenfetch

There are similar display packages to screenfetch so shop around!

If you want the same command prompt with β€œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β€ dividing line between commands, change these lines:

if [ "$color_prompt" = yes ]; then
    PS1='───────────────────────────────────────────────────────────────────────────────────────────
${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='───────────────────────────────────────────────────────────────────────────────────────────
${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

Note the length of the separator line coincides with width of screenfetch output. In this case it is 92 characters wide and gnome-terminal preferences are set accordingly.

Top ToS ToC
⇧ How to analyze and defragment drive in ubuntu 16.04 Terminal splash screen with Weather, Calendar, Time & Sysinfo?  β‡©