1
0
Fork 0

fixed some bugs with the prompt

This commit is contained in:
Massaki Archambault 2018-09-19 16:47:10 -04:00
parent 6771337d4a
commit 2974663454
2 changed files with 28 additions and 31 deletions

View File

@ -3,7 +3,7 @@ setopt prompt_subst # enable command substitution in prompt
PROMPT='$(prompt_cmd)' PROMPT='$(prompt_cmd)'
RPROMPT='' 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_SUFFIX=""
ZSH_THEME_GIT_PROMPT_ADDED="%{$fg_bold[green]%}+" ZSH_THEME_GIT_PROMPT_ADDED="%{$fg_bold[green]%}+"
@ -17,7 +17,7 @@ ZSH_THEME_GIT_PROMPT_BEHIND="%{$fg_bold[white]%}↓"
# Evaluate if root user # Evaluate if root user
is_root() { __is_root() {
if uname -s | egrep '^(CYGWIN)|(MINGW)|(MSYS)' 2>&1 >/dev/null; then if uname -s | egrep '^(CYGWIN)|(MINGW)|(MSYS)' 2>&1 >/dev/null; then
id -G | grep 544 2>&1 >/dev/null id -G | grep 544 2>&1 >/dev/null
else else
@ -25,13 +25,22 @@ is_root() {
fi 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() { prompt_cmd() {
# exit code # exit code
local exit_code=$? local exit_code=$?
local exit_code_hex=$(printf '(%02x)' $exit_code) local exit_code_hex=$(printf '(%02x)' $exit_code)
if [[ exit_code -eq 0 ]]; then if [[ exit_code -eq 0 ]]; then
exit_code_hex="%{$fg[white]%}$exit_code_hex" exit_code_hex="%{$FG[008]%}$exit_code_hex"
else else
exit_code_hex="%{$fg[red]%}$exit_code_hex" exit_code_hex="%{$fg[red]%}$exit_code_hex"
fi fi
@ -39,37 +48,24 @@ prompt_cmd() {
# name@hostname # name@hostname
local default_color="green" local default_color="green"
local user_color="$default_color" local user_color="$default_color"
if is_root; then if __is_root; then
local user_color="red" local user_color="red"
fi fi
local name_hostname="%{$fg[$user_color]%}$USER%{$fg[$default_color]%}@%m" local name_hostname="%{$fg[$user_color]%}$USER%{$fg[$default_color]%}@%m"
# working directory # working directory
local wd_arr=($(grep -Eo '[^/]+' <<<"${PWD/$HOME/~}")) local wd_base="${PWD/$HOME/~}"
local dir_count=${#wd_arr[@]} local wd_post=$(echo "$wd_base" | grep -Eo '(^~|/[^/]+){1,4}$')
local dir_abbr=$((dir_count-3)) local wd_pre=$(echo "${wd_base/$wd_post/}" | grep -Eo '^~|/[^/]{2}' | tr -d '\n' )
for ((i=dir_count; i >= 1 ; i--)); do local wd="%{$fg[yellow]%}$wd_pre$wd_post"
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}"
# prompt # prompt
if [ "$IS_ROOT" = true ]; then if __is_root; then
# #
local prompt="%{$fg[red]%}#" local prompt="%{$fg[red]%}#"
else else
local prompt="%{$fg[white]%}$" # $
local prompt="%{$FG[008]%}$"
fi fi
printf "%s %s %s\n%s %s" "$exit_code_hex" "$name_hostname" "$wd" "$prompt" "%{$reset_color%}" 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)" local prompt_status="$(git_prompt_status)"
if [[ -n "$prompt_status" ]]; then 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
fi fi
@ -99,7 +95,7 @@ ASYNC_PROC=0
function precmd() { function precmd() {
function async() { function async() {
# save to temp file # save to temp file
printf "%s" "$(rprompt_cmd)" > "/tmp/zsh_prompt_$$" printf "%s" "$(rprompt_cmd)" > "$(__get_runtime_dir)/zsh_prompt_$$"
# signal parent # signal parent
kill -s USR1 $$ kill -s USR1 $$
@ -118,8 +114,9 @@ function precmd() {
} }
function TRAPUSR1() { function TRAPUSR1() {
# read from temp file # read from tmp file
RPROMPT="$(cat /tmp/zsh_prompt_$$)" RPROMPT="$(cat $(__get_runtime_dir)/zsh_prompt_$$)"
rm "/tmp/zsh_prompt_$$" &>/dev/null
# reset proc number # reset proc number
ASYNC_PROC=0 ASYNC_PROC=0

View File

@ -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" # 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. # it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes # 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. # Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true" # CASE_SENSITIVE="true"
@ -35,7 +35,7 @@ DISABLE_AUTO_UPDATE="true"
# DISABLE_AUTO_TITLE="true" # DISABLE_AUTO_TITLE="true"
# Uncomment the following line to enable command auto-correction. # 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. # Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true" # COMPLETION_WAITING_DOTS="true"