Merge branch 'v0.2'

This commit is contained in:
Alwaysproblem 2022-04-20 17:57:53 +08:00
commit ffb94f161a

View file

@ -1,64 +1,3 @@
# gdate for macOS
# REF: https://apple.stackexchange.com/questions/135742/time-in-milliseconds-since-epoch-in-the-terminal
if [[ "$OSTYPE" == "darwin"* ]]; then
{
gdate
} || {
echo "\n$fg_bold[yellow]passsion.zsh-theme depends on cmd [gdate] to get current time in milliseconds$reset_color"
echo "$fg_bold[yellow][gdate] is not installed by default in macOS$reset_color"
echo "$fg_bold[yellow]to get [gdate] by running:$reset_color"
echo "$fg_bold[green]brew install coreutils;$reset_color";
echo "$fg_bold[yellow]\nREF: https://github.com/ChesterYue/ohmyzsh-theme-passion#macos\n$reset_color"
}
fi
# time
function real_time() {
local color="%{$fg_no_bold[cyan]%}"; # color in PROMPT need format in %{XXX%} which is not same with echo
local time="[$(date +%H:%M:%S)]";
local color_reset="%{$reset_color%}";
echo "${color}${time}${color_reset}";
}
# login_info
function login_info() {
local color="%{$fg_no_bold[cyan]%}"; # color in PROMPT need format in %{XXX%} which is not same with echo
local ip
if [[ "$OSTYPE" == "linux-gnu" ]]; then
# Linux
ip="$(ifconfig | grep ^eth1 -A 1 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | head -1)";
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
ip="$(ifconfig | grep ^en1 -A 4 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | head -1)";
elif [[ "$OSTYPE" == "cygwin" ]]; then
# POSIX compatibility layer and Linux environment emulation for Windows
elif [[ "$OSTYPE" == "msys" ]]; then
# Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
elif [[ "$OSTYPE" == "win32" ]]; then
# I'm not sure this can happen.
elif [[ "$OSTYPE" == "freebsd"* ]]; then
# ...
else
# Unknown.
fi
local color_reset="%{$reset_color%}";
echo "${color}[%n@${ip}]${color_reset}";
}
# directory
function directory() {
# local color="%{$fg_no_bold[cyan]%}";
local color="$FG[111]";
# REF: https://stackoverflow.com/questions/25944006/bash-current-working-directory-with-replacing-path-to-home-folder
local directory="${PWD/#$HOME/~}";
local color_reset="%{$reset_color%}";
echo "${color}[${directory}]${color_reset}";
}
# git
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_no_bold[blue]%}❮%{$fg_no_bold[red]%}";
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}";
ZSH_THEME_GIT_PROMPT_END_SUFFIX="%{$fg_no_bold[blue]%}%{$reset_color%}";
@ -76,11 +15,43 @@ ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg_bold[red]%}⬢%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_UNMERGED="%{$fg_bold[red]%}!%{$reset_color%}"
ZSH_THEME_GIT_FETCH_STATUS=0
ZSH_THEME_GIT_FETCH_STATUS_INTERVAL=60
function strf_real_time() {
local time_str;
local format=${1:='%Y-%m-%d {%u} %H:%M:%S'}
strftime -s time_str ${format} $EPOCHSECONDS
# strftime -s time_str "%Y-%m-%d {%u} %H:%M:%S" $EPOCHSECONDS
local time="[${time_str}]";
echo -e ${time}
}
# time
function real_time() {
local color="%{$fg_no_bold[cyan]%}"; # color in PROMPT need format in %{XXX%} which is not same with echo
# local time_str;
# strftime -s time_str "%Y-%m-%d {%u} %H:%M:%S" $EPOCHSECONDS
# local time="[${time_str}]";
local _time="$(strf_real_time '%H:%M:%S')"
local color_reset="%{$reset_color%}";
echo "${color}${_time}${color_reset}";
}
# directory
function directory() {
# local color="%{$fg_no_bold[cyan]%}";
local color="$FG[111]";
# REF: https://stackoverflow.com/questions/25944006/bash-current-working-directory-with-replacing-path-to-home-folder
local directory="${PWD/#$HOME/~}";
local color_reset="%{$reset_color%}";
echo "${color}[${directory}]${color_reset}";
}
function __git_branch() {
local ref
ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
ref=$(__git_prompt_git symbolic-ref HEAD 2> /dev/null) || \
ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) || return
echo "${ZSH_THEME_GIT_PROMPT_PREFIX}${ref#refs/heads/}${ZSH_THEME_GIT_PROMPT_SUFFIX}"
}
@ -103,21 +74,6 @@ function vscode_git_status() {
'stashed' 'STASHED'
)
# '(?![DAU][DAU])[MTARCD][AMTD ] ' 'STAGED' # exclude the `[DAU][DAU]`
# '(?![DAU][DAU])[MTARCD ][AMTD] ' 'UNSTAGED'
# prefix_constant_map=(
# '\?\? ' 'UNTRACKED'
# '[MTARC][MTD] ' 'STAGEDUNSTAGED'
# '[MTARC] ' 'STAGED'
# 'D ' 'STAGED'
# ' [AMD] ' 'UNSTAGED'
# '[DAU][DAU] ' 'UNMERGED'
# 'ahead' 'AHEAD'
# 'behind' 'BEHIND'
# 'diverged' 'DIVERGED'
# 'stashed' 'STASHED'
# )
# Maps the internal constant to the prompt theme
local -A constant_prompt_map
constant_prompt_map=(
@ -163,8 +119,6 @@ function vscode_git_status() {
# If the tracking line exists, get and parse it
if [[ "$status_lines[1]" =~ "^## [^ ]+ \[(.*)\]" ]]; then
local branch_statuses
local match
local branch_status
branch_statuses=("${(@s/,/)match}")
for branch_status in $branch_statuses; do
if [[ ! $branch_status =~ "(behind|diverged|ahead) ([0-9]+)?" ]]; then
@ -175,7 +129,6 @@ function vscode_git_status() {
done
fi
local status_prefix
# For each status prefix, do a regex comparison
for status_prefix in ${(k)prefix_constant_map}; do
local status_constant="${prefix_constant_map[$status_prefix]}"
@ -216,7 +169,6 @@ function git_status() {
echo "${GIT_STATUS}"
}
# command
function update_command_status() {
local arrow="";
@ -249,6 +201,13 @@ function afmagic_dashes {
echo $((COLUMNS * ratio))
}
current_time_millis() {
local time_millis;
time_millis=$EPOCHREALTIME
echo $time_millis;
}
# output command execute after
output_command_execute_after() {
if [ "$COMMAND_TIME_BEIGIN" = "-20200325" ] || [ "$COMMAND_TIME_BEIGIN" = "" ];
@ -269,16 +228,16 @@ output_command_execute_after() {
cmd="${color_cmd}${cmd}${color_reset}"
# time
local time="[$(date +%H:%M:%S)]"
# you can use the real_time command to replace
local color_time="$fg_no_bold[cyan]";
time="${color_time}${time}${color_reset}";
local _time="${color_time}$(strf_real_time)${color_reset}";
# cost
local time_end="$(current_time_millis)";
local cost=$(bc -l <<<"${time_end}-${COMMAND_TIME_BEIGIN}");
COMMAND_TIME_BEIGIN="-20200325"
local length_cost=${#cost};
if [ "$length_cost" = "4" ];
if [ "$length_cost" = "11" ]; # 11 means the length of cost
then
cost="0${cost}"
fi
@ -286,10 +245,10 @@ output_command_execute_after() {
local color_cost="$fg_no_bold[yellow]";
cost="${color_cost}${cost}${color_reset}";
echo -e "${time} ${cost} ${cmd}";
echo -e "${_time} ${cost} ${cmd}";
echo -e "";
local echo_dark_gray="\033[2;49;39m"
echo -e "$echo_dark_gray${(l.$(afmagic_dashes)..-.)}${color_reset}"
}
@ -300,29 +259,6 @@ preexec() {
COMMAND_TIME_BEIGIN="$(current_time_millis)";
}
current_time_millis() {
local time_millis;
if [[ "$OSTYPE" == "linux-gnu" ]]; then
# Linux
time_millis="$(date +%s.%3N)";
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
time_millis="$(gdate +%s.%3N)";
elif [[ "$OSTYPE" == "cygwin" ]]; then
# POSIX compatibility layer and Linux environment emulation for Windows
elif [[ "$OSTYPE" == "msys" ]]; then
# Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
elif [[ "$OSTYPE" == "win32" ]]; then
# I'm not sure this can happen.
elif [[ "$OSTYPE" == "freebsd"* ]]; then
# ...
else
# Unknown.
fi
echo $time_millis;
}
# command execute after
# REF: http://zsh.sourceforge.net/Doc/Release/Functions.html
dash_passion_precmd() {
@ -347,21 +283,54 @@ dash_passion_precmd() {
}
TMOUT=1;
TRAPALRM() {
# $(git_prompt_info) cost too much time which will raise stutters when inputting. so we need to disable it in this occurence.
# if [ "$WIDGET" != "expand-or-complete" ] && [ "$WIDGET" != "self-insert" ] && [ "$WIDGET" != "backward-delete-char" ]; then
# black list will not enum it completely. even some pipe broken will appear.
# so we just put a white list here.
if [ "$WIDGET" = "" ] || [ "$WIDGET" = "accept-line" ] ; then
zle reset-prompt;
fi
function git_fetch_status() {
[ ${ZSH_THEME_GIT_FETCH_STATUS} != "0" ] && return 0
ZSH_THEME_GIT_FETCH_STATUS=1
__git_prompt_git rev-parse --is-inside-work-tree &>/dev/null || return 0
__git_prompt_git fetch -q --all 2>/dev/null
__git_prompt_git status 12>/dev/null
ZSH_THEME_GIT_FETCH_STATUS=0
}
function chpwd() {
__git_prompt_git rev-parse --is-inside-work-tree &>/dev/null || return 0
__git_prompt_git status 12>/dev/null
}
# real time clock for zsh.
# https://stackoverflow.com/questions/2187829/constantly-updated-clock-in-zsh-prompt
schedprompt() {
emulate -L zsh
zmodload -i zsh/sched
integer i=${"${(@)zsh_scheduled_events#*:*:}"[(I)git_fetch_status]}
# git_fetch_all periodically.
(( i )) || sched +${ZSH_THEME_GIT_FETCH_STATUS_INTERVAL} git_fetch_status
# Remove existing event, so that multiple calls to
# "schedprompt" work OK. (You could put one in precmd to push
# the timer 30 seconds into the future, for example.)
integer i=${"${(@)zsh_scheduled_events#*:*:}"[(I)schedprompt]}
(( i )) && sched -$i
# Test that zle is running before calling the widget (recommended
# to avoid error messages).
# Otherwise it updates on entry to zle, so there's no loss.
if [ "$WIDGET" = "" ] || [ "$WIDGET" = "accept-line" ] ; then
zle && zle reset-prompt;
fi
# This ensures we're not too far off the start of the minute
# update zle for every second.
sched +1 schedprompt
}
zmodload -i zsh/datetime
setopt prompt_subst
PROMPT='$(real_time) $(directory) $(git_status) $(command_status) ';
RPROMPT='%{$FG[242]%}%n@%m $(battery_pct_prompt)${color_reset}';
autoload -Uz add-zsh-hook
add-zsh-hook precmd dash_passion_precmd
schedprompt