How can the last command's wall time be put in the Bash prompt?

后端 未结 14 2841
無奈伤痛
無奈伤痛 2020-12-04 05:16

Is there a way to embed the last command\'s elapsed wall time in a Bash prompt? I\'m hoping for something that would look like this:

[last: 0s][/my/dir]$ sl         


        
14条回答
  •  星月不相逢
    2020-12-04 06:02

    Using your replies and some other threads, I wrote this prompt which I want to share with you. I took a screenshot in wich you can see :

    • White : Last return code
    • Green and tick mark means success (return code was 0)
    • Red and cross mark means error (return code was >0)
    • (Green or Red) : Last command execution time in parenthesis
    • (Green or Red) : Current date time (\t)
    • (Green if not root, Red if root) : the logged username
    • (Green) : the server name
    • (Blue) : the pwd directory and the usual $

    Here is the code to put in your ~/.bashrc file :

    function timer_now {
        date +%s%N
    }
    
    function timer_start {
        timer_start=${timer_start:-$(timer_now)}
    }
    
    function timer_stop {
        local delta_us=$((($(timer_now) - $timer_start) / 1000))
        local us=$((delta_us % 1000))
        local ms=$(((delta_us / 1000) % 1000))
        local s=$(((delta_us / 1000000) % 60))
        local m=$(((delta_us / 60000000) % 60))
        local h=$((delta_us / 3600000000))
        # Goal: always show around 3 digits of accuracy
        if ((h > 0)); then timer_show=${h}h${m}m
        elif ((m > 0)); then timer_show=${m}m${s}s
        elif ((s >= 10)); then timer_show=${s}.$((ms / 100))s
        elif ((s > 0)); then timer_show=${s}.$(printf %03d $ms)s
        elif ((ms >= 100)); then timer_show=${ms}ms
        elif ((ms > 0)); then timer_show=${ms}.$((us / 100))ms
        else timer_show=${us}us
        fi
        unset timer_start
    }
    
    
    set_prompt () {
        Last_Command=$? # Must come first!
        Blue='\[\e[01;34m\]'
        White='\[\e[01;37m\]'
        Red='\[\e[01;31m\]'
        Green='\[\e[01;32m\]'
        Reset='\[\e[00m\]'
        FancyX='\342\234\227'
        Checkmark='\342\234\223'
    
    
        # Add a bright white exit status for the last command
        PS1="$White\$? "
        # If it was successful, print a green check mark. Otherwise, print
        # a red X.
        if [[ $Last_Command == 0 ]]; then
            PS1+="$Green$Checkmark "
        else
            PS1+="$Red$FancyX "
        fi
    
        # Add the ellapsed time and current date
        timer_stop
        PS1+="($timer_show) \t "
    
        # If root, just print the host in red. Otherwise, print the current user
        # and host in green.
        if [[ $EUID == 0 ]]; then
            PS1+="$Red\\u$Green@\\h "
        else
            PS1+="$Green\\u@\\h "
        fi
        # Print the working directory and prompt marker in blue, and reset
        # the text color to the default.
        PS1+="$Blue\\w \\\$$Reset "
    }
    
    trap 'timer_start' DEBUG
    PROMPT_COMMAND='set_prompt'
    

提交回复
热议问题