Skip to content

Commit 0603036

Browse files
add a bash timer, that shows the runtime of the previous command
+ at the beginning of the prompt
1 parent e9c1ff5 commit 0603036

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

debian/changelog

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ byobu (6.6) unreleased; urgency=medium
44
- export PS1
55
* usr/bin/byobu-janitor.in:
66
- add byobu prompt to .bashrc in Wolfi
7+
* usr/share/byobu/profiles/bashrc, usr/share/man/man1/byobu-prompt.1:
8+
- add a bash timer, that shows the runtime of the previous command
9+
at the beginning of the prompt
710

8-
-- Dustin Kirkland <[email protected]> Tue, 16 Jan 2024 19:14:10 -0600
11+
-- Dustin Kirkland <[email protected]> Tue, 16 Jan 2024 20:10:22 -0600
912

1013
byobu (6.5) released; urgency=medium
1114

usr/share/byobu/profiles/bashrc

+41-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,48 @@ esac
2727
[ -z "$USER" ] && export USER=$(whoami)
2828
byobu_prompt_status() { local e=$?; [ $e != 0 ] && echo -e " $e "; }
2929
byobu_prompt_symbol() { [ "$USER" = "root" ] && printf "%s" "#" || printf "%s" "\$"; }
30+
byobu_prompt_runtime() {
31+
# Calculate the approximate runtime of the previous command
32+
# Uses bash 5.0's EPOCHREALTIME
33+
local starttime endtime duration days hours minutes seconds microseconds nanoseconds str
34+
[ ! -r $BYOBU_RUN_DIR/timer.$$ ] && printf "[0.000s]" && return
35+
read starttime < $BYOBU_RUN_DIR/timer.$$ 2>/dev/null || true
36+
endtime=${EPOCHREALTIME/./}
37+
starttime=${starttime/./}
38+
duration=$((endtime - starttime))
39+
days=$((duration/1000000/60/60/24))
40+
hours=$((duration/1000000/60/60%24))
41+
minutes=$((duration/1000000/60%60))
42+
seconds=$((duration/1000000%60))
43+
nanoseconds=$((duration-days*1000000*60*60*24-hours*1000000*60*60-minutes*1000000*60-seconds*1000000))
44+
nanoseconds=$(printf "%.6d" ${nanoseconds})
45+
microseconds=${nanoseconds:0:3}
46+
# Shorten our string as much as possible
47+
if [ "$days" = "0" ]; then
48+
days=
49+
if [ "$hours" = "0" ]; then
50+
hours=
51+
if [ "$minutes" = "0" ]; then
52+
minutes=
53+
else
54+
minutes="${minutes}m "
55+
fi
56+
else
57+
hours="${hours}h "
58+
fi
59+
else
60+
days="${days}d "
61+
fi
62+
str="${days}${hours}${minutes}${seconds}.${microseconds}s"
63+
printf "[%s]" "$str" 1>&2
64+
}
65+
# Requires Bash 4.x
66+
export PS0='$(printf "%s" ${EPOCHREALTIME/./} >"$BYOBU_RUN_DIR/timer.$$")'
67+
3068
case "$BYOBU_DISTRO" in
3169
"Ubuntu")
3270
# Use Ubuntu colors (grey / orange / aubergine)
33-
export PS1="${debian_chroot:+($debian_chroot)}\[\e[38;5;202m\]\$(byobu_prompt_status)\[\e[38;5;245m\]\u\[\e[00m\]@\[\e[38;5;172m\]\h\[\e[00m\]:\[\e[38;5;5m\]\w\[\e[00m\]\$(byobu_prompt_symbol) "
71+
export PS1="${debian_chroot:+($debian_chroot)}\[\e[38;5;202m\]\$(byobu_prompt_status)\[\e[00m\]\$(byobu_prompt_runtime) \[\e[38;5;245m\]\u\[\e[00m\]@\[\e[38;5;172m\]\h\[\e[00m\]:\[\e[38;5;5m\]\w\[\e[00m\]\$(byobu_prompt_symbol) "
3472
export GREP_COLORS="ms=01;38;5;202:mc=01;31:sl=:cx=:fn=01;38;5;132:ln=32:bn=32:se=00;38;5;242"
3573
export LESS_TERMCAP_mb=$(printf '\e[01;31m') # enter blinking mode – red
3674
export LESS_TERMCAP_md=$(printf '\e[01;38;5;180m') # enter double-bright mode – bold light orange
@@ -50,10 +88,10 @@ case "$BYOBU_DISTRO" in
5088
# For reference: https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg
5189
# Convert hex to 256: https://gist.githubusercontent.com/MicahElliott/719710/raw/73d047f0a3ffc35f0655488547e7f24fa3f04ea6/colortrans.py
5290
# Use Wolfi colors (pink=170 / purple=98 / blue=63); flashing error code on previous command non-zero exit
53-
export PS1="\[\e[03;5;15;54m\]\$(byobu_prompt_status)\[\e[00m\]\[\e[38;5;170m\]\u\[\e[00m\]@\[\e[38;5;98m\]\h\[\e[00m\]:\[\e[38;5;63m\]\w\[\e[00m\]\$(byobu_prompt_symbol) "
91+
PS1="\[\e[03;5;15;54m\]\$(byobu_prompt_status)\[\e[00m\]\$(byobu_prompt_runtime) \[\e[38;5;170m\]\u\[\e[00m\]@\[\e[38;5;98m\]\h\[\e[00m\]:\[\e[38;5;63m\]\w\[\e[00m\]\$(byobu_prompt_symbol) "
5492
;;
5593
*)
5694
# Use Googley colors (blue / red / yellow / blue / green / red )
57-
export PS1="${debian_chroot:+($debian_chroot)}\[\e[31m\]\$(byobu_prompt_status)\[\e[38;5;69m\]\u\[\e[38;5;214m\]@\[\e[38;5;167m\]\h\[\e[38;5;214m\]:\[\e[38;5;71m\]\w\[\e[38;5;214m\]\$(byobu_prompt_symbol)\[\e[00m\] "
95+
PS1="${debian_chroot:+($debian_chroot)}\[\e[31m\]\$(byobu_prompt_status)\[\e[38;5;69m\]\u\[\e[38;5;214m\]@\[\e[38;5;167m\]\h\[\e[38;5;214m\]:\[\e[38;5;71m\]\w\[\e[38;5;214m\]\$(byobu_prompt_symbol)\[\e[00m\] "
5896
;;
5997
esac

usr/share/man/man1/byobu-prompt.1

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
.TH byobu-prompt 1 "26 July 2013" byobu "byobu"
1+
.TH byobu-prompt 1 "16 January 2024" byobu "byobu"
22
.SH NAME
3-
byobu-prompt, byobu-enable-prompt, byobu-disable-prompt \- add and remove a nice color prompt to your shell configuration
3+
byobu-prompt, byobu-enable-prompt, byobu-disable-prompt \- add and remove a nice color prompt with a previous command timer to your shell configuration
44

55
.SH SYNOPSIS
66
\fBbyobu-prompt\fP
@@ -11,7 +11,7 @@ byobu-prompt, byobu-enable-prompt, byobu-disable-prompt \- add and remove a nice
1111

1212
.SH DESCRIPTION
1313

14-
Byobu provides a special PS1 prompt command, compatible with Bash shells. It will display the previous command's exit code, if it's not zero. It will use 3 separate colors for the local username, hostname, and the current working directory.
14+
Byobu provides a special PS0 and PS1 prompt command, compatible with Bash shells. It will display the previous command's total runtime and exit code, if it's not zero. It will use 3 separate colors for the local username, hostname, and the current working directory.
1515

1616
\fBbyobu-enable-prompt\fP will add one line to your \fI~/.bashrc\fP.
1717

0 commit comments

Comments
 (0)