2021-01-28 23:23:03 +00:00
|
|
|
#!/usr/bin/env zsh
|
|
|
|
|
2021-08-13 10:31:10 +00:00
|
|
|
# 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
|
|
|
|
|
|
|
|
|
2021-03-23 20:18:34 +00:00
|
|
|
# hide EOL sign ('%')
|
|
|
|
export PROMPT_EOL_MARK=""
|
|
|
|
|
2021-04-28 23:05:25 +00:00
|
|
|
# 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
|
2021-01-28 23:23:03 +00:00
|
|
|
RESET="%f"
|
|
|
|
|
2021-04-20 06:29:47 +00:00
|
|
|
# Signals git status of CWD repository (if any)
|
2021-01-28 23:23:03 +00:00
|
|
|
git_prompt() {
|
2021-01-28 23:39:51 +00:00
|
|
|
ref=$(command git symbolic-ref HEAD 2> /dev/null) || ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
|
2021-01-29 09:38:37 +00:00
|
|
|
echo -n " $ORANGE${ref#refs/heads/}"
|
2021-01-28 23:23:03 +00:00
|
|
|
|
|
|
|
if [ ! -z "$(git status --short)" ]; then
|
2021-01-28 23:39:51 +00:00
|
|
|
echo "$RED+"
|
2021-01-28 23:23:03 +00:00
|
|
|
fi
|
|
|
|
}
|
2021-01-28 23:39:51 +00:00
|
|
|
|
2021-04-20 06:29:47 +00:00
|
|
|
# Adds @chroot or @ssh
|
|
|
|
foreign_prompt() {
|
|
|
|
if [ "$(awk '$5=="/" {print $1}' </proc/1/mountinfo)" != "$(awk '$5=="/" {print $1}' </proc/$$/mountinfo)" ]; then
|
|
|
|
echo -n "@${ORANGE}chroot"
|
2021-05-03 17:08:00 +00:00
|
|
|
elif [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
|
2021-04-20 06:29:47 +00:00
|
|
|
echo -n "@${ORANGE}ssh"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-05-03 17:08:00 +00:00
|
|
|
# Prints appropriate working directory
|
|
|
|
working_directory() {
|
|
|
|
# By default up to 5 directories will be tolerated before shortening
|
|
|
|
# After we surpass that, first directory (or ~) will be printed together with last 3
|
|
|
|
# This feature uses special symbol '…', but this isn't aviable when in TTY. Because
|
|
|
|
# of this, when we are in TTY, we fall back to longer '...'
|
|
|
|
|
|
|
|
if [ $USE_SHORTENED_WORKDIR != 1 ]; then
|
|
|
|
echo -n " $BLUE$~"
|
|
|
|
elif [ $TERM = "linux" ]; then
|
|
|
|
echo -n " $BLUE%(5~|%-1~/.../%3~|%4~)"
|
|
|
|
else
|
|
|
|
echo -n " $BLUE%(5~|%-1~/…/%3~|%4~)"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-08-13 10:31:10 +00:00
|
|
|
# Execution time tracking hooks, this is unique to zsh, as it can add
|
|
|
|
# preexec and precmd hooks. We can utilize this to keep track of the
|
|
|
|
# amount of time it took to run certain command. We store the start time
|
|
|
|
# within a variable: PROMPT_EXEC_TIME_START, which we then compare and
|
|
|
|
# unset after the command was finished. In here, we simply set the
|
|
|
|
# PROMPT_EXEC_TIME_DURATION, which is then used in the actual prompt
|
|
|
|
# This will only be enabled if SHOW_CMD_TIME is 1.
|
|
|
|
exec_time_preexec_hook() {
|
|
|
|
[[ $SHOW_CMD_TIME == 0 ]] && return
|
|
|
|
PROMPT_EXEC_TIME_START=$(date +%s)
|
|
|
|
}
|
|
|
|
exec_time_precmd_hook() {
|
|
|
|
[[ $SHOW_CMD_TIME == 0 ]] && return
|
|
|
|
[[ -z $PROMPT_EXEC_TIME_START ]] && return
|
|
|
|
local PROMPT_EXEC_TIME_STOP=$(date +%s)
|
|
|
|
PROMPT_EXEC_TIME_DURATION=$(( $PROMPT_EXEC_TIME_STOP - $PROMPT_EXEC_TIME_START ))
|
|
|
|
unset PROMPT_EXEC_TIME_START
|
|
|
|
}
|
|
|
|
format_time() {
|
|
|
|
# Do some formatting to get nice time (e.g. 2m 12s)
|
|
|
|
# from seconds only
|
|
|
|
local T=$1
|
|
|
|
local D=$((T/60/60/24))
|
|
|
|
local H=$((T/60/60%24))
|
|
|
|
local M=$((T/60%60))
|
|
|
|
local S=$((T%60))
|
|
|
|
[[ $D > 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)"
|
|
|
|
}
|
|
|
|
|
2021-03-23 20:18:34 +00:00
|
|
|
setopt promptsubst # enable command substitution in prompt
|
2021-01-28 23:23:03 +00:00
|
|
|
|
2021-08-13 10:31:10 +00:00
|
|
|
# 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
|
|
|
|
|
2021-01-28 23:23:03 +00:00
|
|
|
# Primary Prompt
|
2021-04-20 06:29:47 +00:00
|
|
|
[ "$EUID" -eq 0 ] && PS1="$RED%n$RESET" || PS1="$GREEN%n$RESET" # user
|
|
|
|
PS1+="$(foreign_prompt)"
|
2021-05-03 17:08:00 +00:00
|
|
|
PS1+="$(working_directory)"
|
2021-05-03 17:10:47 +00:00
|
|
|
PS1+="\$(git_prompt)"
|
2021-08-13 10:31:10 +00:00
|
|
|
#PS1+="\$(display_cmd_time)"
|
2021-05-13 12:48:28 +00:00
|
|
|
PS1+=" $PURPLE%(!.#.$)$RESET " # Final symbol (# or $/»)
|
2021-01-28 23:23:03 +00:00
|
|
|
|
|
|
|
# Next line prompt
|
|
|
|
PS2="$RED\ $RESET"
|
|
|
|
|
|
|
|
# Right side prompt (on error)
|
2021-05-06 15:20:31 +00:00
|
|
|
if [ $TERM = "linux" ]; then
|
2021-08-13 10:31:10 +00:00
|
|
|
RPS1="\$(display_cmd_time)%(?..${RED}%? X$RESET)"
|
2021-05-06 15:20:31 +00:00
|
|
|
else
|
2021-08-13 10:31:10 +00:00
|
|
|
RPS1="\$(display_cmd_time)%(?..${RED}%? ↵$RESET)"
|
2021-05-06 15:20:31 +00:00
|
|
|
fi
|
2021-04-19 08:47:00 +00:00
|
|
|
|