widget

prompt

linux/osx

  • now | now

    #!/usr/bin/env bash
    
    # NAME   : now
    # PATH   : $HOME/bin
    # DESC   : Display current weather, calendar and time
    # CALL   : Called from terminal or ~/.bashrc
    # DATE   : Apr 6, 2017. Modified: May 24, 2019.
    # CREDIT : https://askubuntu.com/a/1020693/92979
    
    # UPDT   : 2019-05-24 If Weather unavailable nicely formatted error message.
    # UPDT   : 2024-01-26 fix the "today" position offset issue due to extra space in `cal`
    
    # 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 "   "   "   "
    curlOpt='-skg -x http://proxy.sample.com:80'
    
    #------------------------------ WEATHER -------------------------------------
    
    # current weather, already in color so no need to override
    # replace edmonton with your city name, gps, etc. see: curl wttr.in/:help
    # shellcheck disable=SC2086
    if ! curl ${curlOpt} wttr.in/sanjose?QmM0 --fail --silent --max-time 3 > /tmp/now-weather; then
    #                          timeout #. increase for slow connection---^
      ~/.marslo/bin/iweather > /tmp/now-weather
    fi
    
    # was valid weather report found or an error message?
    if grep '°C' /tmp/now-weather >/dev/null 2>&1; then
      weatherDone=true
      cat /tmp/now-weather
    else
      weatherDone=false
      dateColumn=1                                    # show data as first column if got weather failed
      timeColumn=27                                   # move 34 column right if got weather failed
    fi
    [[ -f /tmp/now-weather ]] && rm -rf /tmp/now-weather
    
    #------------------------------- DATA ---------------------------------------
    
    # 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.
    if [[ "$weatherDone" == true ]] ; then
        tput cuu 6                                  # move up 6 lines
        # 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 '%10s' ''                            # blank out ", country" with 10 spaces
    else
        tput cuf $dateColumn                        # position to column 27 for date display
    fi
    
    # -h needed to turn off formatting: https://askubuntu.com/questions/1013954/bash-substring-stringoffsetlength-error/1013960#1013960
    # -h not supported in Ubuntu 18.04. Use second answer: https://askubuntu.com/a/1028566/307523
    # to fit for both macOS and Linux
    #   - linux: cal have 2 more extra whitespace in each line, 22 chars per line
    #   - osx: cal has no more extra whitespace in each line, 20 chars per line
    #                    cal -h                 remove trailing spaces     remove empty line
    #                       v                              v                       v
    cal | tr -cd '\11\12\15\40\60-\136\140-\176' | sed 's/[ \t]*$//' | sed '/^[[:space:]]*$/d' > /tmp/terminal
    calLineCnt=1
    today=$(date +"%e")
    printf "\033[32m"                               # color green -- see list above.
    
    while IFS= read -r cal; do
      printf "%s" "$cal"
      if [[ $calLineCnt -gt 2 ]] ; then
        tput cub "$(awk '{print length($0)}' <<< "${cal}")"
        for (( j=0 ; j <= 18 ; j += 3 )) ; do       # see if today is on current line & invert background
          day=${cal:$j:2}                           # current day on calendar line
          if [[ "$day" == "$today" ]] ; then
            printf "\033[7m"                        # reverse: [7m
            printf "%s" "$today"
            printf "\033[0m"                        # normal: [0m
            printf "\033[32m"                       # color green -- see list above.
            tput cuf 1
          else
            tput cuf 3
          fi
        done
      fi
    
      tput cud1                                     # move one line down
      tput cuf $dateColumn                          # move 27 columns right
      calLineCnt=$((++calLineCnt))
    done < /tmp/terminal
    
    printf "\033[00m"                               # color -- bright white (default)
    tput rc                                         # restore saved cursor position.
    
    #------------------------------- TIME ---------------------------------------
    
    tput sc                                         # save cursor position.
    tput cuu 6                                      # move up 9 lines
    tput cuf $timeColumn                            # move 49 columns right
    
    if hash toilet 2>/dev/null; then                # if has toilet
       date +"%I:%M %P" | toilet -f future > /tmp/terminal
    elif hash figlet 2>/dev/null; then              # if has figlet
       date +"%I:%M %P" | figlet -f /usr/local/share/figlet/future.tlf > /tmp/terminal
    else
       date +"%I:%M %P" > /tmp/terminal
    fi
    
    while IFS= read -r time; do
      printf "\033[01;36m"                          # color cyan
      printf "%s" "$time"
      tput cud1                                     # up one line
      tput cuf $timeColumn                          # move 49 columns right
    done < /tmp/terminal
    
    [[ -f /tmp/terminal ]] && rm -rf /tmp/terminal
    tput rc                                         # restore saved cursor position.
    exit 0
    
    # vim:tabstop=2:softtabstop=2:shiftwidth=2:expandtab:filetype=sh
  • another now

    # 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
    screenfetch and now

windows

weather

iweather and now

widget

others

[!NOTE]

figlet

[!NOTE|label:references:]

  • list all fonts

    • result

    • others

toilet

  • fonts

  • list all fonts

Last updated

Was this helpful?