# 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}"; } # directory function directory() { local color="%{$fg_no_bold[cyan]%}"; local directory="$(pwd)"; local color_reset="%{$reset_color%}"; echo "${color}${directory}${color_reset}"; } # git ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_no_bold[blue]%}git(%{$fg_no_bold[red]%}"; ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "; ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg_no_bold[blue]%}) 🔥"; ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_no_bold[blue]%})"; function update_git_status() { GIT_STATUS=$(git_prompt_info); } function git_status() { echo "${GIT_STATUS}" } # command function update_command_status() { local arrow=""; local color_reset="%{$reset_color%}"; local reset_font="%{$fg_no_bold[white]%}"; if $1; then arrow="%{$fg_bold[red]%}❱%{$fg_bold[yellow]%}❱%{$fg_bold[green]%}❱"; else arrow="%{$fg_bold[red]%}❱❱❱"; fi COMMAND_STATUS="${arrow}${reset_font}${color_reset}"; } update_command_status true; function command_status() { echo "${COMMAND_STATUS}" } # output command execute after output_command_execute_after() { if [ "$COMMAND_TIME_BEIGIN" = "-20200325" ] || [ "$COMMAND_TIME_BEIGIN" = "" ]; then return 1; fi # cmd local cmd="${$(fc -l | tail -1)#* }"; local color_cmd=""; if $1; then color_cmd="$fg_no_bold[green]"; else color_cmd="$fg_bold[red]"; fi local color_reset="$reset_color"; cmd="${color_cmd}${cmd}${color_reset}" # time local time="[$(date +%H:%M:%S)]" local color_time="$fg_no_bold[cyan]"; time="${color_time}${time}${color_reset}"; # cost local time_end="$(gdate +%s.%3N)"; local cost=$(bc -l <<<"${time_end}-${COMMAND_TIME_BEIGIN}"); COMMAND_TIME_BEIGIN="-20200325" local length_cost=${#cost}; if [ "$length_cost" = "4" ]; then cost="0${cost}" fi cost="[cost ${cost}s]" local color_cost="$fg_no_bold[cyan]"; cost="${color_cost}${cost}${color_reset}"; echo -e "${time} ${cost} ${cmd}"; echo -e ""; } # command execute before # REF: http://zsh.sourceforge.net/Doc/Release/Functions.html preexec() { COMMAND_TIME_BEIGIN="$(gdate +%s.%3N)"; } # command execute after # REF: http://zsh.sourceforge.net/Doc/Release/Functions.html precmd() { # last_cmd local last_cmd_return_code=$?; local last_cmd_result=true; if [ "$last_cmd_return_code" = "0" ]; then last_cmd_result=true; else last_cmd_result=false; fi # update_git_status update_git_status; # update_command_status update_command_status $last_cmd_result; # output command execute after output_command_execute_after $last_cmd_result; } # set option setopt PROMPT_SUBST; # timer #REF: https://stackoverflow.com/questions/26526175/zsh-menu-completion-causes-problems-after-zle-reset-prompt 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 } # prompt PROMPT='$(real_time) $(directory) $(git_status)$(command_status) ';