#!/usr/bin/env zsh # Configuration variables: # Once we are too deep in the filestructure, we can usually afford to shorten # the whole working directory and only print something like ~/.../dir3/dir4/dir5 # instead of ~/dir1/dir2/dir3/dir4/dir5. If this isn't desired, set this to 0 USE_SHORTENED_WORKDIR=1 # Show how much time it took to run a command SHOW_CMD_TIME=1 # hide EOL sign ('%') export PROMPT_EOL_MARK="" # TTY (pure linux) terminal only has 8-bit color support # (unless you change it in kernel), respect this and downgrade # the color scheme accordingly (it won't look best, but it's # still better than no colors) if [ $TERM = "linux" ]; then GREEN="%F{002}" RED="%F{001}" ORANGE="%F{003}" BLUE="%F{004}" LBLUE="%F{006}" PURPLE="%F{005}" else GREEN="%F{047}" RED="%F{196}" ORANGE="%F{214}" BLUE="%F{027}" LBLUE="%F{075}" PURPLE="%F{105}" fi RESET="%f" # Signals git status of CWD repository (if any) git_prompt() { ref=$(command git symbolic-ref HEAD 2> /dev/null) || ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0 echo -n " $ORANGE${ref#refs/heads/}" if [ ! -z "$(git status --short)" ]; then echo "$RED+" fi } # Adds @chroot or @ssh foreign_prompt() { if [ "$(awk '$5=="/" {print $1}' 0 ]] && printf '%dd ' $D [[ $H > 0 ]] && printf '%dh ' $H [[ $M > 0 ]] && printf '%dm ' $M printf '%ds' $S } display_cmd_time() { [[ $SHOW_CMD_TIME == 0 ]] && return [[ -z $PROMPT_EXEC_TIME_DURATION ]] && return # Don't display the time, if it's 0 seconds [[ $PROMPT_EXEC_TIME_DURATION == 0 ]] && return echo -n " ${LBLUE}took $(format_time $PROMPT_EXEC_TIME_DURATION)" } setopt promptsubst # enable command substitution in prompt # Setup ZSH hooks to display the running time of commands autoload -Uz add-zsh-hook add-zsh-hook preexec exec_time_preexec_hook add-zsh-hook precmd exec_time_precmd_hook # Primary Prompt [ "$EUID" -eq 0 ] && PS1="$RED%n$RESET" || PS1="$GREEN%n$RESET" # user PS1+="$(foreign_prompt)" PS1+="$(working_directory)" PS1+="\$(git_prompt)" PS1+="\$(display_cmd_time)" PS1+=" $PURPLE%(!.#.$)$RESET " # Final symbol (# or $/») # Next line prompt PS2="$RED\ $RESET" # Right side prompt RPS1="" if [ $TERM = "linux" ]; then # Displaying cmd time here works, but often causes issues when we # resize the terminal, since right prompts can be annoying to deal # with when resizing. This would run relatively often so it makes # more sense to only use it in PS1 (left prompt), but if desired, # this can be uncommented #RPS1+="\$(display_cmd_time)" # If we find a non-zero return code, print it in the right prompt, # use X here, to avoid issues with TTY not having support for # a nicer unicode character that we use otherwise ("↵") RPS1+="%(?..${RED}%? X$RESET)" else # Read comments for the section above. #RPS+="\$(display_cmd_time)" RPS1="%(?..${RED}%? ↵$RESET)" fi