The timer_stop operate additionally has the job of changing the timer right into a human-readable format, and it’s in all probability messier than it must be. I’m no developer, although, so that is what Previous Lee settled on after a couple of hours of looking out via examples.
Doing it in fish for people like me
That’s for bash once I’m ssh’d into one among my Linux hosts, however I run fish on MacOS. I’ve a separate fish operate for getting the identical outcomes there, full with gross hacks for turning the measurement into human-readable type. I made this code, and I’m unapologetic. Witness my cobbled-together StackOverflow-sourced kludge.
operate fish_prompt --description 'Write out the immediate'
# Save the final standing
set -l last_status $standing
# Calculate the command length if accessible
set -l cmd_duration ""
if set -q CMD_DURATION
# Convert milliseconds to microseconds for extra exact comparability
set -l duration_us (math "$CMD_DURATION * 1000")
# Calculate completely different time models
set -l us (math "$duration_us % 1000")
set -l ms (math "ground($duration_us / 1000) % 1000")
set -l s (math "ground($duration_us / 1000000) % 60")
set -l m (math "ground($duration_us / 60000000) % 60")
set -l h (math "ground($duration_us / 3600000000)")
# Format length string
if take a look at $h -gt 0
set cmd_duration (string be part of '' "(" $h "h" $m "m)")
else if take a look at $m -gt 0
set cmd_duration (string be part of '' "(" $m "m" $s "s)")
else if take a look at $s -ge 10
set -l fraction (math "ground($ms / 100)")
set cmd_duration (string be part of '' "(" $s "." $fraction "s)")
else if take a look at $s -gt 0
set cmd_duration (string be part of '' "(" $s "." (printf "%03d" $ms) "s)")
else if take a look at $ms -ge 100
set cmd_duration (string be part of '' "(" $ms "ms)")
else if take a look at $ms -gt 0
set -l fraction (math "ground($us / 100)")
set cmd_duration (string be part of '' "(" $ms "." $fraction "ms)")
else
set cmd_duration (string be part of '' "(" $us "us)")
finish
finish
# Outline unicode symbols for standing
set -l checkmark "✓"
set -l cross "✗"
# Colours
set -l regular (set_color regular)
set -l dark_gray (set_color 555555)
set -l blue (set_color -o blue)
set -l crimson (set_color crimson)
set -l inexperienced (set_color inexperienced)
set -l purple (set_color -o purple)
# First line
echo # New line
echo -n -s $dark_gray "["(date +%T)"] $last_status " # Time in brackets and exit standing
# Standing indicator with exit standing
if take a look at $last_status -eq 0
echo -n -s $inexperienced $checkmark
else
echo -n -s $crimson $cross
finish
# Really echo the length
echo -n -s $dark_gray " $cmd_duration"
# Do the remainder of the immediate
echo
set -l host_color $purple
echo -n -s $host_color $USER "@" (prompt_hostname) $regular ":" $blue (prompt_pwd) $regular " $ "
finish
A splash of colour
Spending my early life immersed in ANSI BBS graphics has in all probability made me somewhat extra fond of colourful textual content in my terminal than the common frumpy, button-downed admin. Look, I do know some of us really feel that syntax highlighting and colours usually kill comprehension and encourage skimming, however what can I say? I really like them and depend on them. Maybe I skim an excessive amount of, however so be it. You may take my colourful shell instruments from my chilly, useless fingers.
To that finish, I lean on somewhat program referred to as GRC (for Generic Colorizer) so as to add highlighting and coloration to different instruments. It’s broadly accessible and works with none extra configuration.
Nothing fallacious with somewhat colour!
Lee Hutchinson
Nothing fallacious with somewhat colour!
Lee Hutchinson
There’s a little bit of aliasing (which I hold in .bash_aliases like a superb citizen) to make colourful output the defaults on some widespread instructions:
alias ls="ls --color=auto"
alias ll="ls -AlFh --group-directories-first"
alias df="grc df -h"
alias du='grc du -h'
alias free="grc free -h"
alias ping='grc ping'
alias traceroute="grc traceroute"
alias ip='grc ip'
I’m additionally a giant fan of constructing my numbers human-readable, and the -h swap is due to this fact utilized liberally.
(Do observe that wrapping instructions like ip in GRC can generally do bizarre issues when you’re piping its output into one thing else. Use warning. Or don’t! It’s your laptop, knock your self out!)
The terminal itself
Sharp-eyed readers will observe from the screenshots that I’m utilizing MacOS’s Terminal.app for my terminal program, regardless of there being much better choices. I suppose the excuse I’ve is that I’m comfortable with Terminal.app and nothing has pulled me off of it. I’ve test-driven the standard suspects—Ghostty, Alacritty, the mighty iTerm2 with its superior tmux windowing integration, and even fancy new reinterpretations of the terminal expertise like Warp.


