Created
December 19, 2019 04:53
-
-
Save Omnikron13/7467917f818fad3fa1df1e15ae382b48 to your computer and use it in GitHub Desktop.
Condensed uptime output for, e.g, status bars etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prints out a much more condensed output than the uptime command, intended for | |
# use in things like status bars where space is a big restriction. | |
function uptime-short | |
# Extract a single unit (minutes/hours/days/weeks) from 'uptime -p' string | |
# Arguments: uptime string, unit to extract | |
function extract | |
# 'uptime -p' omits units entirely if they are 0... | |
set -l X (string match --regex "\d+(?= $argv[2])" "$argv[1]") | |
# ...so we have to put the actual 0 back in if we want to do maths | |
if test $X | |
echo $X | |
else | |
echo "0" | |
end | |
end | |
# Separates the output units, for prettier & clearer output | |
set -l SEPARATOR "⋅" | |
# 'pretty' output is easier to slice the actual numbers out of | |
set -l UPTIME (uptime --pretty) | |
# These are the only units I have seen. No months, I assume because months are a pain | |
# in the arse. There could be years? Probably not worth worrying about. | |
set -l MINUTES (extract $UPTIME 'minutes') | |
set -l HOURS (extract $UPTIME 'hours') | |
set -l DAYS (extract $UPTIME 'days') | |
set -l WEEKS (extract $UPTIME 'weeks') | |
# Merge weeks into days, for shorter output | |
set -l DAYS (math "$WEEKS * 7 + $DAYS") | |
# There are a few different ways output could be formatted other than this. | |
# E.g. X number of significant units, specific single unit (with decimals?), etc. | |
# TODO: rework so there is some flexibility | |
echo "$DAYS"d⋅"$HOURS"h⋅"$MINUTES"m | |
# Cleanup 'local' function(s), as fish doesn't really scope them | |
functions --erase extract | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment