The Cookie Machine - Click here to drag window

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

Views: 2,380β€…    Votes:  9β€…    βœ… Solution
Tags: command-line   bash   bashrc  
Link: πŸ” See Original Answer on Ask Ubuntu πŸ”—

URL: https://askubuntu.com/q/901394
Title: When terminal is opened can I get current calendar and time displayed?
ID: /2017/04/08/When-terminal-is-opened-can-I-get-current-calendar-and-time-displayed_
Created: April 8, 2017    Edited:  June 12, 2020
Upload: November 24, 2022    Layout:  post
TOC: false    Navigation:  true    Copy to clipboard:  false


Skip

Improved Version

You can see the improved version here: https://askubuntu.com/a/1020693/307523

It looks like this:

Terminal Splash Screen


Top ToS Skip

Original Version

You can print an introduction banner when the terminal is opened using the ~/.bashrc script. If you have figlet (sudo apt install figlet) you can take advantage of large letters to display the time:

now

In the first instance figlet is used to display the time and in the second a regular font is used.

Top ToS Skip

Additional screen examples using toilet

now 3

This screen uses fonts from the toilet package. The last example uses future font and is used in the code below. Additionally, the calendar is set to cyan color in the code below.

The toilet package allows additional font types and formatting styles over the figlet package which it is forked from. To install the package use sudo apt install toilet after installing figlet as described above.

Top ToS Skip

The code

Here is the code you need to make it all work. It’s recommended to place this script in your /home/user/bin directory as it is automatically added to your path. This script is named now but you can use any unique name you like.

#!/bin/bash

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

# 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

# calendar current month with today higlighted.
# colors 00=bright white, 31=red, 32=green, 33=yellow, 34=blue, 35=purple,
#        36=cyan, 37=white
printf "\033[36m"       # color 36=cyan
echo ""; cal;
printf "\033[00m"       # color bright white (default)
echo ""

tput sc                 # Save cursor position.
# Move up 9 lines
while [ $((++i)) -lt 10 ]; do tput cuu1; done
tput cuf 25             # Move 25 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;32m" # color green
    printf "$Time"
    tput cud1           # Up one line
    tput cuf 25         # Move 25 columns right
done < /tmp/terminal

tput rc                 # Restore saved cursor position.

exit 0

Mark script as executable

Copy this code into your editor and save it to the file now. Next mark it as executable using:

sudo chmod +x now

Add script to ~./bashrc

Once this is completed you can type now in the terminal and you will see the calendar and time. To wrap it all up and have it automatically displayed each time you open the terminal:

Now when opening the terminal you will be greeted with the current day highlighted on current month’s calendar followed by the current time.

Top ToS Skip

Dissecting the code

Here we’ll briefly look at how the code works without discussing every line.

figlet and toilet packages

The script first checks if toilet is installed with the hash command. If so that is used to display time. If not figlet is used if installed. If neither are installed a regular font is used.

In the code above a comment can be copied to your command line and executed to show available figlet and toilet fonts on your terminal:

for i in ${TOILET_FONT_PATH:=/usr/share/figlet}/*.{t,f}lf; do j=${i##*/}; toilet -d "${i%/*}" -f "$j" "${j%.*}"; done

To change the font used for time display search the code for this line:

toilet -f future --filter border > /tmp/terminal

and change the font name future to the font name you choose. Keep in mind some fonts are too large to fit on the display.

Selecting colors

Set the color you want for the calendar and the time separately. In the above code, notice the command:

printf "\033[36m"       # color 36=cyan

Change the last two digits to the color code you want to use. From tinkering with the code I found these values:

00=bright white, 31=red, 32=green, 33=yellow, 34=blue, 35=purple, 36=cyan, 37=white

If you find additional color codes please post a comment below or update this answer.

Top ToS
⇧ HDMI second monitor stopped working Code version control between local files and AU answers  β‡©