mirror of
https://github.com/ChesterYue/ohmyzsh-theme-passion.git
synced 2026-07-25 14:01:03 +00:00
replace TMOUT with zsh/sched and aquired time with zsh/datetime
This commit is contained in:
parent
4c7cb8e455
commit
68aa0d6945
1 changed files with 174 additions and 224 deletions
|
|
@ -1,52 +1,23 @@
|
||||||
# gdate for macOS
|
function strf_real_time() {
|
||||||
# REF: https://apple.stackexchange.com/questions/135742/time-in-milliseconds-since-epoch-in-the-terminal
|
local time_str;
|
||||||
if [[ "$OSTYPE" == "darwin"* ]]; then
|
local format=${1:='%Y-%m-%d {%u} %H:%M:%S'}
|
||||||
{
|
strftime -s time_str ${format} $EPOCHSECONDS
|
||||||
gdate
|
# strftime -s time_str "%Y-%m-%d {%u} %H:%M:%S" $EPOCHSECONDS
|
||||||
} || {
|
local time="[${time_str}]";
|
||||||
echo "\n$fg_bold[yellow]passsion.zsh-theme depends on cmd [gdate] to get current time in milliseconds$reset_color"
|
echo -e ${time}
|
||||||
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
|
# time
|
||||||
function real_time() {
|
function real_time() {
|
||||||
local color="%{$fg_no_bold[cyan]%}"; # color in PROMPT need format in %{XXX%} which is not same with echo
|
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 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%}";
|
local color_reset="%{$reset_color%}";
|
||||||
echo "${color}${time}${color_reset}";
|
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
|
# directory
|
||||||
function directory() {
|
function directory() {
|
||||||
# local color="%{$fg_no_bold[cyan]%}";
|
# local color="%{$fg_no_bold[cyan]%}";
|
||||||
|
|
@ -103,21 +74,6 @@ function vscode_git_status() {
|
||||||
'stashed' 'STASHED'
|
'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
|
# Maps the internal constant to the prompt theme
|
||||||
local -A constant_prompt_map
|
local -A constant_prompt_map
|
||||||
constant_prompt_map=(
|
constant_prompt_map=(
|
||||||
|
|
@ -163,8 +119,6 @@ function vscode_git_status() {
|
||||||
# If the tracking line exists, get and parse it
|
# If the tracking line exists, get and parse it
|
||||||
if [[ "$status_lines[1]" =~ "^## [^ ]+ \[(.*)\]" ]]; then
|
if [[ "$status_lines[1]" =~ "^## [^ ]+ \[(.*)\]" ]]; then
|
||||||
local branch_statuses
|
local branch_statuses
|
||||||
local match
|
|
||||||
local branch_status
|
|
||||||
branch_statuses=("${(@s/,/)match}")
|
branch_statuses=("${(@s/,/)match}")
|
||||||
for branch_status in $branch_statuses; do
|
for branch_status in $branch_statuses; do
|
||||||
if [[ ! $branch_status =~ "(behind|diverged|ahead) ([0-9]+)?" ]]; then
|
if [[ ! $branch_status =~ "(behind|diverged|ahead) ([0-9]+)?" ]]; then
|
||||||
|
|
@ -175,7 +129,6 @@ function vscode_git_status() {
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local status_prefix
|
|
||||||
# For each status prefix, do a regex comparison
|
# For each status prefix, do a regex comparison
|
||||||
for status_prefix in ${(k)prefix_constant_map}; do
|
for status_prefix in ${(k)prefix_constant_map}; do
|
||||||
local status_constant="${prefix_constant_map[$status_prefix]}"
|
local status_constant="${prefix_constant_map[$status_prefix]}"
|
||||||
|
|
@ -216,7 +169,6 @@ function git_status() {
|
||||||
echo "${GIT_STATUS}"
|
echo "${GIT_STATUS}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# command
|
# command
|
||||||
function update_command_status() {
|
function update_command_status() {
|
||||||
local arrow="";
|
local arrow="";
|
||||||
|
|
@ -249,6 +201,13 @@ function afmagic_dashes {
|
||||||
echo $((COLUMNS * ratio))
|
echo $((COLUMNS * ratio))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
current_time_millis() {
|
||||||
|
local time_millis;
|
||||||
|
time_millis=$EPOCHREALTIME
|
||||||
|
echo $time_millis;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# output command execute after
|
# output command execute after
|
||||||
output_command_execute_after() {
|
output_command_execute_after() {
|
||||||
if [ "$COMMAND_TIME_BEIGIN" = "-20200325" ] || [ "$COMMAND_TIME_BEIGIN" = "" ];
|
if [ "$COMMAND_TIME_BEIGIN" = "-20200325" ] || [ "$COMMAND_TIME_BEIGIN" = "" ];
|
||||||
|
|
@ -269,16 +228,16 @@ output_command_execute_after() {
|
||||||
cmd="${color_cmd}${cmd}${color_reset}"
|
cmd="${color_cmd}${cmd}${color_reset}"
|
||||||
|
|
||||||
# time
|
# time
|
||||||
local time="[$(date +%H:%M:%S)]"
|
# you can use the real_time command to replace
|
||||||
local color_time="$fg_no_bold[cyan]";
|
local color_time="$fg_no_bold[cyan]";
|
||||||
time="${color_time}${time}${color_reset}";
|
local _time="${color_time}$(strf_real_time)${color_reset}";
|
||||||
|
|
||||||
# cost
|
# cost
|
||||||
local time_end="$(current_time_millis)";
|
local time_end="$(current_time_millis)";
|
||||||
local cost=$(bc -l <<<"${time_end}-${COMMAND_TIME_BEIGIN}");
|
local cost=$(bc -l <<<"${time_end}-${COMMAND_TIME_BEIGIN}");
|
||||||
COMMAND_TIME_BEIGIN="-20200325"
|
COMMAND_TIME_BEIGIN="-20200325"
|
||||||
local length_cost=${#cost};
|
local length_cost=${#cost};
|
||||||
if [ "$length_cost" = "4" ];
|
if [ "$length_cost" = "11" ]; # 11 means the length of cost
|
||||||
then
|
then
|
||||||
cost="0${cost}"
|
cost="0${cost}"
|
||||||
fi
|
fi
|
||||||
|
|
@ -286,10 +245,10 @@ output_command_execute_after() {
|
||||||
local color_cost="$fg_no_bold[yellow]";
|
local color_cost="$fg_no_bold[yellow]";
|
||||||
cost="${color_cost}${cost}${color_reset}";
|
cost="${color_cost}${cost}${color_reset}";
|
||||||
|
|
||||||
echo -e "${time} ${cost} ${cmd}";
|
echo -e "${_time} ${cost} ${cmd}";
|
||||||
|
|
||||||
echo -e "";
|
echo -e "";
|
||||||
local echo_dark_gray="\033[2;49;39m"
|
local echo_dark_gray="\033[2;49;39m"
|
||||||
|
|
||||||
echo -e "$echo_dark_gray${(l.$(afmagic_dashes)..-.)}${color_reset}"
|
echo -e "$echo_dark_gray${(l.$(afmagic_dashes)..-.)}${color_reset}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -300,29 +259,6 @@ preexec() {
|
||||||
COMMAND_TIME_BEIGIN="$(current_time_millis)";
|
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
|
# command execute after
|
||||||
# REF: http://zsh.sourceforge.net/Doc/Release/Functions.html
|
# REF: http://zsh.sourceforge.net/Doc/Release/Functions.html
|
||||||
dash_passion_precmd() {
|
dash_passion_precmd() {
|
||||||
|
|
@ -347,21 +283,35 @@ dash_passion_precmd() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TMOUT=1;
|
# real time clock for zsh.
|
||||||
|
# https://stackoverflow.com/questions/2187829/constantly-updated-clock-in-zsh-prompt
|
||||||
|
schedprompt() {
|
||||||
|
emulate -L zsh
|
||||||
|
zmodload -i zsh/sched
|
||||||
|
|
||||||
TRAPALRM() {
|
# Remove existing event, so that multiple calls to
|
||||||
# $(git_prompt_info) cost too much time which will raise stutters when inputting. so we need to disable it in this occurence.
|
# "schedprompt" work OK. (You could put one in precmd to push
|
||||||
# if [ "$WIDGET" != "expand-or-complete" ] && [ "$WIDGET" != "self-insert" ] && [ "$WIDGET" != "backward-delete-char" ]; then
|
# the timer 30 seconds into the future, for example.)
|
||||||
# black list will not enum it completely. even some pipe broken will appear.
|
integer i=${"${(@)zsh_scheduled_events#*:*:}"[(I)schedprompt]}
|
||||||
# so we just put a white list here.
|
(( 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
|
if [ "$WIDGET" = "" ] || [ "$WIDGET" = "accept-line" ] ; then
|
||||||
zle reset-prompt;
|
zle && zle reset-prompt;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# This ensures we're not too far off the start of the minute
|
||||||
|
sched +1 schedprompt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zmodload -i zsh/datetime
|
||||||
|
|
||||||
setopt prompt_subst
|
setopt prompt_subst
|
||||||
PROMPT='$(real_time) $(directory) $(git_status) $(command_status) ';
|
PROMPT='$(real_time) $(directory) $(git_status) $(command_status) ';
|
||||||
RPROMPT='%{$FG[242]%}%n@%m $(battery_pct_prompt)${color_reset}';
|
RPROMPT='%{$FG[242]%}%n@%m $(battery_pct_prompt)${color_reset}';
|
||||||
|
|
||||||
autoload -Uz add-zsh-hook
|
autoload -Uz add-zsh-hook
|
||||||
add-zsh-hook precmd dash_passion_precmd
|
add-zsh-hook precmd dash_passion_precmd
|
||||||
|
schedprompt
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue