Skip to content

Commit

Permalink
Rewrite using modern Fish
Browse files Browse the repository at this point in the history
  • Loading branch information
ammgws authored and jorgebucaran committed Oct 11, 2020
1 parent 2c338ef commit f0ce905
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions humanize_duration.fish
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
function humanize_duration -d "Make a time interval human readable"
command awk '
function hmTime(time, stamp) {
split("h:m:s:ms", units, ":")
for (i = 2; i >= -1; i--) {
if (t = int( i < 0 ? time % 1000 : time / (60 ^ i * 1000) % 60 )) {
stamp = stamp t units[sqrt((i - 2) ^ 2) + 1] " "
}
}
if (stamp ~ /^ *$/) {
return "0ms"
}
return substr(stamp, 1, length(stamp) - 1)
}
{
print hmTime($0)
}
'
if not string length --quiet $argv
set --erase argv
read --line argv
end
set hours (math --scale=0 $argv/\(3600 \*1000\))
set mins (math --scale=0 $argv/\(60 \*1000\) % 60)
set secs (math --scale=0 $argv/1000 % 60)
if test $hours -gt 0
set --append output $hours"h"
end
if test $mins -gt 0
set --append output $mins"m"
end
if test $secs -gt 0
set --append output $secs"s"
end
if not set --query output
echo $argv"ms"
else
echo $output
end
end

0 comments on commit f0ce905

Please sign in to comment.