Skip to content

Instantly share code, notes, and snippets.

@ohcibi
Created April 25, 2018 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohcibi/d1dad0ea9d37f9ef9a1f6c1081db37f7 to your computer and use it in GitHub Desktop.
Save ohcibi/d1dad0ea9d37f9ef9a1f6c1081db37f7 to your computer and use it in GitHub Desktop.
#
# A theme inspired by sorin and nicoulaj and my old theme
#
# Authors:
# Tobias Witt <tobias.witt@hhu.de>
#
# Features:
# - Lambda Prompt Character (Half-Life 3 confirmed!!!)
# - Two lines.
# - VCS information in both prompts
# - If in a repository: show only the repository dirname and its subpaths
# - Crops the path to a defined length
# - Has a last command success indicator
# - Shows if logged in as root or not.
#
# Depends:
# - prezto git module
#
# Screenshots:
# https://www.dropbox.com/s/088v71s787dejfq/Screenshot%202014-07-12%2013.58.20.png
#
# turns seconds into human readable time
# 165392 => 1d 21h 56m 32s
# borrowed from sindresorhus/pure
prompt_ohcibi_human_time() {
local tmp=$1
local days=$(( tmp / 60 / 60 / 24 ))
local hours=$(( tmp / 60 / 60 % 24 ))
local minutes=$(( tmp / 60 % 60 ))
local seconds=$(( tmp % 60 ))
(( $days > 0 )) && echo -n "${days}d "
(( $hours > 0 )) && echo -n "${hours}h "
(( $minutes > 0 )) && echo -n "${minutes}m "
echo "${seconds}s"
}
# displays the exec time of the last command if set threshold was exceeded
# borrowed from sindresorhus/pure
prompt_ohcibi_cmd_exec_time() {
local timeout=5
local stop=$EPOCHSECONDS
local start=${cmd_timestamp:-$stop}
integer elapsed=$stop-$start
(($elapsed > ${PURE_CMD_MAX_EXEC_TIME:=$timeout})) && prompt_ohcibi_human_time $elapsed
}
function prompt_ohcibi_preexec {
# borrowed from sindresorhus/pure
cmd_timestamp=$EPOCHSECONDS
cmd_executed=$2
print -Pn "\e]0;"
echo -nE "$PWD:t: $2"
print -Pn "\a"
}
function is-discharging {
pmset -g batt | fgrep -Fq "discharging"
}
function prompt_ohcibi_precmd {
vcs_info
git-info
ruby-info
python-info
node-info
unset exec_time
typeset -gA exec_time
local elapsed
local elapsed_format
local elapsed_formatted
elapsed="`prompt_ohcibi_cmd_exec_time`"
if [[ -n $elapsed ]]; then
local cmd_format
local cmd_formatted
zstyle -s ':prezto:module:exec_time:elapsed_format' format 'elapsed_format'
zformat -f elapsed_formatted "$elapsed_format" "t:$elapsed"
exec_time[elapsed]="$elapsed_formatted"
zstyle -s ':prezto:module:exec_time:cmd_format' format 'cmd_format'
zformat -f cmd_formatted "$cmd_format" "c:$cmd_executed"
exec_time[cmd]="$cmd_formatted"
fi
# reset value since `preexec` isn't always triggered
unset cmd_timestamp
# battery state
typeset -gA battery_info
local perc=${$(battery-percentage)/\%/%%}
local batteryps
if is-discharging || ! [ $perc = "100%" ]; then
batteryps=" $perc"
fi
if [ -z $NVIM_LISTEN_ADDRESS ]; then
if is-discharging; then
batteryps="${batteryps} 🔋"
else
batteryps="${batteryps} 🔌"
fi
fi
battery_info[rprompt]=$batteryps
# weather
typeset -gA weather_info
local -A symbols=(
Sunny "☀️ "
# TODO:
"Clear" "☀️ "
"Sunny" "☀️ "
"Partly cloudy" "🌤 "
"Cloudy" "☁️ "
"Overcast" "🌥 "
"Mist" "🌫 "
"Patchy rain possible" "🌧 "
# "Patchy snow possible"
# "Patchy sleet possible"
# "Patchy freezing drizzle possible"
# "Thundery outbreaks possible"
# "Blowing snow"
# "Blizzard"
"Fog" "🌫 "
# "Freezing fog"
# "Patchy light drizzle"
"Light drizzle" "🌧 "
# "Freezing drizzle"
# "Heavy freezing drizzle"
# "Patchy light rain"
"Light Rain" "🌦 "
"Light rain" "🌦 "
# "Moderate rain at times"
# "Moderate rain"
# "Heavy rain at times"
# "Heavy rain"
# "Light freezing rain"
# "Moderate or heavy freezing rain"
# "Light sleet"
# "Moderate or heavy sleet"
# "Patchy light snow"
# "Light snow"
# "Patchy moderate snow"
# "Moderate snow"
# "Patchy heavy snow"
# "Heavy snow"
# "Ice pellets"
"Light rain shower" "🌧 "
"Rain Shower" "🌧 "
# "Moderate or heavy rain shower"
# "Torrential rain shower"
# "Light sleet showers"
# "Moderate or heavy sleet showers"
# "Light snow showers"
# "Moderate or heavy snow showers"
# "Patchy light rain with thunder"
# "Moderate or heavy rain with thunder"
# "Patchy light snow with thunder"
# "Moderate or heavy snow with thunder"
)
weatherstr=$(curl -s wttr.in/Düsseldorf"?TQ0" | sed -n '1,2p' | cut -c 16-)
weather=("${(f)weatherstr}")
(( ${+symbols[$weather[1]]} )) && weather[1]=$symbols[$weather[1]]
weather_info[prompt]="$weather[1] $weather[2] $weather[3]"
bogus() {
typeset -gA weather_info
local -A symbols=(
Sunny "☀️ "
"Clear" "☀️ "
"Sunny" "☀️ "
"Partly cloudy" "🌤 "
"Cloudy" "☁️ "
"Overcast" "🌥 "
"Mist" "🌫 "
"Patchy rain possible" "🌧 "
"Fog" "🌫 "
"Light drizzle" "🌧 "
"Light Rain" "🌦 "
"Light rain" "🌦 "
"Light rain shower" "🌧 "
"Rain Shower" "🌧 "
# TODO:
# "Patchy snow possible"
# "Patchy sleet possible"
# "Patchy freezing drizzle possible"
# "Thundery outbreaks possible"
# "Blowing snow"
# "Blizzard"
# "Freezing fog"
# "Patchy light drizzle"
# "Freezing drizzle"
# "Heavy freezing drizzle"
# "Patchy light rain"
# "Moderate rain at times"
# "Moderate rain"
# "Heavy rain at times"
# "Heavy rain"
# "Light freezing rain"
# "Moderate or heavy freezing rain"
# "Light sleet"
# "Moderate or heavy sleet"
# "Patchy light snow"
# "Light snow"
# "Patchy moderate snow"
# "Moderate snow"
# "Patchy heavy snow"
# "Heavy snow"
# "Ice pellets"
# "Moderate or heavy rain shower"
# "Torrential rain shower"
# "Light sleet showers"
# "Moderate or heavy sleet showers"
# "Light snow showers"
# "Moderate or heavy snow showers"
# "Patchy light rain with thunder"
# "Moderate or heavy rain with thunder"
# "Patchy light snow with thunder"
# "Moderate or heavy snow with thunder"
)
weatherstr=$(curl -s wttr.in/Düsseldorf"?TQ0" | sed -n '1,2p' | cut -c 16-)
weather=("${(f)weatherstr}")
(( ${+symbols[$weather[1]]} )) && weather[1]=$symbols[$weather[1]]
weather_info[prompt]="$weather[1] $weather[2] $weather[3]"
sleep 10
}
async_job my_worker bogus
render_prompt
}
function prompt_ohcibi_setup {
# prevent percentage showing up
# if output doesn't end with a newline
# borrowed from sindresorhus/pure
export PROMPT_EOL_MARK=''
setopt LOCAL_OPTIONS
unsetopt XTRACE KSH_ARRAYS
prompt_opts=(cr percent subst)
# Load required functions.
zmodload zsh/datetime
autoload -Uz add-zsh-hook
autoload -Uz vcs_info
autoload -Uz async && async
async_start_worker my_worker -n
arender() {
render_prompt
zle && zle .reset-prompt
}
async_register_callback my_worker arender
add-zsh-hook precmd prompt_ohcibi_precmd
add-zsh-hook preexec prompt_ohcibi_preexec
# Customizable parameters.
local path_color='%F{4}'
local success_color='%F{071}'
local failure_color='%F{124}'
local vcs_info_color='%F{242}'
local def_color='%F{3}'
local user_color='%F{17}'
local branch_color='%F{6}'
local action_color='%F{23}'
local commit_color='%F{23}'
local host_color='%F{52}'
local ruby_color='%F{099}'
# Set vcs_info parameters.
zstyle ':vcs_info:*' enable bzr git hg svn
zstyle ':vcs_info:*' actionformats "%S" "%F{5}(${action_color}%a%F{5})%f ${path_color}%r/${path_color}"
zstyle ':vcs_info:*' formats "%S" "${path_color}%r"
zstyle ':vcs_info:*' nvcsformats "%~" ""
zstyle ':prezto:module:editor:info:completing' format '%B%F{red}...%f%b'
zstyle ':prezto:module:editor:info:keymap:primary' format "%B%(?;${success_color}✔;${failure_color}✗)%f%b"
zstyle ':prezto:module:editor:info:keymap:alternate' format '%F{166}☭%f'
zstyle ':prezto:module:git:info' verbose 'yes'
zstyle ':prezto:module:git:info:action' format ' %%B%F{yellow}%s%f%%b'
zstyle ':prezto:module:git:info:added' format ' %%B%F{green}✚%f%%b'
zstyle ':prezto:module:git:info:ahead' format ' %%B%F{blue}⇡%f%%b'
zstyle ':prezto:module:git:info:behind' format ' %%B%F{green}⇣%f%%b'
zstyle ':prezto:module:git:info:branch' format "${branch_color}%b%f"
zstyle ':prezto:module:git:info:commit' format "${commit_color}%.7c%f"
zstyle ':prezto:module:git:info:deleted' format ' %%B%F{red}✖%f%%b'
zstyle ':prezto:module:git:info:modified' format ' %%B%F{blue}✱%f%%b'
zstyle ':prezto:module:git:info:position' format '%F{red}%p%f'
zstyle ':prezto:module:git:info:renamed' format ' %%B%F{magenta}➜%f%%b'
zstyle ':prezto:module:git:info:stashed' format ' %%B%F{cyan}✭%f%%b'
zstyle ':prezto:module:git:info:unmerged' format ' %%B%F{yellow}═%f%%b'
zstyle ':prezto:module:git:info:untracked' format ' %%B%F{yellow}◼%f%%b'
zstyle ':prezto:module:git:info:keys' format \
'prompt' '%p%b#%c' \
'rprompt' '%A%B%S%s%a%d%m%r%U%u '
zstyle ':prezto:module:ruby:info:version' format "💎${ruby_color}-%v%f"
zstyle ':prezto:module:exec_time:elapsed_format' format "%F{yellow}%t%f"
#zstyle ':prezto:module:exec_time:cmd_for:mat' format "%F{green}\`%c\`:%f "
zstyle ':prezto:module:exec_time:cmd_format' format ""
zstyle ':prezto:module:python:info:virtualenv' format " %F{094}(%v)%f"
zstyle ':prezto:module:node:info:version' format '🔮%F{070}-%v%f'
}
render_prompt() {
local max_path_chars=30
local user_char='λ'
local root_char='G'
local prompt_color='%F{9}'
local path_color='%F{4}'
# Define prompts.
# note to self: if ever a string is needed in the prompt, define it in precmd instead of trying
# to append it right here
local -a preprompt_parts
local -a prompt_parts
if [[ -z $prompt_newline ]]; then
# This variable needs to be set, usually set by promptinit.
typeset -g prompt_newline=$'\n%{\r%}'
fi
preprompt_parts+=('${editor_info[keymap]}')
preprompt_parts+=($path_color'%B${vcs_info_msg_1_}${vcs_info_msg_1_:+@%b${git_info[prompt]} }'$path_color'%B%'$max_path_chars'<...<${vcs_info_msg_0_%%.}%<<%b%f')
#preprompt_parts+=('${git_info[prompt]}')
preprompt_parts+=('${${ruby_info[version]}/-ruby/}')
preprompt_parts+=('${node_info[version]}')
preprompt_parts+=('${weather_info[prompt]}')
[[ -n ${exec_time[cmd]} ]] && prompt_parts+=('${exec_time[cmd]}')
[[ -n ${exec_time[elapsed]} ]] && prompt_parts+=('${exec_time[elapsed]}')
prompt_parts+=($prompt_color'%B%(!.'$root_char'.'$user_char')%f%b%_')
local -ah ps1
ps1=(
$prompt_newline
#'"'
${(j. .)preprompt_parts}
#'"'
$prompt_newline
${(j. .)prompt_parts}
)
PROMPT="${(j..)ps1} "
local -a rprompt_parts
rprompt_parts+=('${git_info[rprompt]}')
if [[ -n $ITERM_PROFILE && $ITERM_PROFILE = "Hotkey Window" ]]; then
rprompt_parts+=('%B%F{249}[%F{120}%*%f%F{249}]%f%b')
else
rprompt_parts+=('%B%F{052}[%F{022}%*%f%F{052}]%f%b')
fi
rprompt_parts+=('${python_info[virtualenv]}')
rprompt_parts+=('%B${battery_info[rprompt]}%b')
RPROMPT="${(j..)rprompt_parts}"
SPROMPT="zsh: correct %F{red}%R%f to %F{green}%r%f [nyae]? "
}
prompt_ohcibi_setup "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment