fixed some bugs with the prompt
This commit is contained in:
parent
6771337d4a
commit
2974663454
|
@ -3,7 +3,7 @@ setopt prompt_subst # enable command substitution in prompt
|
|||
PROMPT='$(prompt_cmd)'
|
||||
RPROMPT=''
|
||||
|
||||
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[white]%} on %{$fg[cyan]%}"
|
||||
ZSH_THEME_GIT_PROMPT_PREFIX="%{$FG[008]%} on %{$fg[cyan]%}"
|
||||
ZSH_THEME_GIT_PROMPT_SUFFIX=""
|
||||
|
||||
ZSH_THEME_GIT_PROMPT_ADDED="%{$fg_bold[green]%}+"
|
||||
|
@ -17,7 +17,7 @@ ZSH_THEME_GIT_PROMPT_BEHIND="%{$fg_bold[white]%}↓"
|
|||
|
||||
|
||||
# Evaluate if root user
|
||||
is_root() {
|
||||
__is_root() {
|
||||
if uname -s | egrep '^(CYGWIN)|(MINGW)|(MSYS)' 2>&1 >/dev/null; then
|
||||
id -G | grep 544 2>&1 >/dev/null
|
||||
else
|
||||
|
@ -25,13 +25,22 @@ is_root() {
|
|||
fi
|
||||
}
|
||||
|
||||
# Get the tmp directory
|
||||
function __get_runtime_dir() {
|
||||
if [[ -n "$XDG_RUNTIME_DIR" ]]; then
|
||||
echo -n "$XDG_RUNTIME_DIR"
|
||||
else
|
||||
echo -n "/tmp/"
|
||||
fi
|
||||
}
|
||||
|
||||
prompt_cmd() {
|
||||
# exit code
|
||||
local exit_code=$?
|
||||
local exit_code_hex=$(printf '(%02x)' $exit_code)
|
||||
|
||||
if [[ exit_code -eq 0 ]]; then
|
||||
exit_code_hex="%{$fg[white]%}$exit_code_hex"
|
||||
exit_code_hex="%{$FG[008]%}$exit_code_hex"
|
||||
else
|
||||
exit_code_hex="%{$fg[red]%}$exit_code_hex"
|
||||
fi
|
||||
|
@ -39,37 +48,24 @@ prompt_cmd() {
|
|||
# name@hostname
|
||||
local default_color="green"
|
||||
local user_color="$default_color"
|
||||
if is_root; then
|
||||
if __is_root; then
|
||||
local user_color="red"
|
||||
fi
|
||||
local name_hostname="%{$fg[$user_color]%}$USER%{$fg[$default_color]%}@%m"
|
||||
|
||||
# working directory
|
||||
local wd_arr=($(grep -Eo '[^/]+' <<<"${PWD/$HOME/~}"))
|
||||
local dir_count=${#wd_arr[@]}
|
||||
local dir_abbr=$((dir_count-3))
|
||||
for ((i=dir_count; i >= 1 ; i--)); do
|
||||
local dir="${wd_arr[i]}"
|
||||
if [ $i -lt $dir_abbr ]; then
|
||||
wd="${dir:0:1}/$wd"
|
||||
else
|
||||
wd="$dir/$wd"
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -z "$wd" ]; then
|
||||
wd="//"
|
||||
elif [ "${wd:0:1}" != "~" ]; then
|
||||
wd="/$wd"
|
||||
fi
|
||||
|
||||
wd="%{$fg[yellow]%}${wd:0:-1}"
|
||||
local wd_base="${PWD/$HOME/~}"
|
||||
local wd_post=$(echo "$wd_base" | grep -Eo '(^~|/[^/]+){1,4}$')
|
||||
local wd_pre=$(echo "${wd_base/$wd_post/}" | grep -Eo '^~|/[^/]{2}' | tr -d '\n' )
|
||||
local wd="%{$fg[yellow]%}$wd_pre$wd_post"
|
||||
|
||||
# prompt
|
||||
if [ "$IS_ROOT" = true ]; then
|
||||
if __is_root; then
|
||||
# #
|
||||
local prompt="%{$fg[red]%}#"
|
||||
else
|
||||
local prompt="%{$fg[white]%}$"
|
||||
# $
|
||||
local prompt="%{$FG[008]%}$"
|
||||
fi
|
||||
|
||||
printf "%s %s %s\n%s %s" "$exit_code_hex" "$name_hostname" "$wd" "$prompt" "%{$reset_color%}"
|
||||
|
@ -87,7 +83,7 @@ rprompt_cmd() {
|
|||
|
||||
local prompt_status="$(git_prompt_status)"
|
||||
if [[ -n "$prompt_status" ]]; then
|
||||
local git_status="$(printf "%s[%s%s]" "%{$fg[white]%}" "$prompt_status" "%{$reset_color%}%{$fg[white]%}")"
|
||||
local git_status="$(printf "%s[%s%s]" "%{$FG[008]%}" "$prompt_status" "%{$reset_color%}%{$FG[008]%}")"
|
||||
fi
|
||||
fi
|
||||
|
||||
|
@ -99,7 +95,7 @@ ASYNC_PROC=0
|
|||
function precmd() {
|
||||
function async() {
|
||||
# save to temp file
|
||||
printf "%s" "$(rprompt_cmd)" > "/tmp/zsh_prompt_$$"
|
||||
printf "%s" "$(rprompt_cmd)" > "$(__get_runtime_dir)/zsh_prompt_$$"
|
||||
|
||||
# signal parent
|
||||
kill -s USR1 $$
|
||||
|
@ -118,8 +114,9 @@ function precmd() {
|
|||
}
|
||||
|
||||
function TRAPUSR1() {
|
||||
# read from temp file
|
||||
RPROMPT="$(cat /tmp/zsh_prompt_$$)"
|
||||
# read from tmp file
|
||||
RPROMPT="$(cat $(__get_runtime_dir)/zsh_prompt_$$)"
|
||||
rm "/tmp/zsh_prompt_$$" &>/dev/null
|
||||
|
||||
# reset proc number
|
||||
ASYNC_PROC=0
|
|
@ -13,7 +13,7 @@ export ZSH="$HOME/.config/zsh/oh-my-zsh/"
|
|||
# Set name of the theme to load. Optionally, if you set this to "random"
|
||||
# it'll load a random theme each time that oh-my-zsh is loaded.
|
||||
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
|
||||
ZSH_THEME="noglyph"
|
||||
ZSH_THEME="custom"
|
||||
|
||||
# Uncomment the following line to use case-sensitive completion.
|
||||
# CASE_SENSITIVE="true"
|
||||
|
@ -35,7 +35,7 @@ DISABLE_AUTO_UPDATE="true"
|
|||
# DISABLE_AUTO_TITLE="true"
|
||||
|
||||
# Uncomment the following line to enable command auto-correction.
|
||||
# ENABLE_CORRECTION="true"
|
||||
ENABLE_CORRECTION="true"
|
||||
|
||||
# Uncomment the following line to display red dots whilst waiting for completion.
|
||||
# COMPLETION_WAITING_DOTS="true"
|
||||
|
|
Loading…
Reference in New Issue