dotfiles/roles/shell-dotfiles/files/zsh_custom_themes/custom.zsh

160 lines
5.4 KiB
Bash

# Del tema bullet-train
BULLETTRAIN_EXEC_TIME_ELAPSED="5"
BULLETTRAIN_EXEC_TIME_BG="yellow"
BULLETTRAIN_EXEC_TIME_FG="black"
preexec() {
cmd_timestamp=`date +%s`
}
precmd() {
local stop=`date +%s`
local start=${cmd_timestamp:-$stop}
let BULLETTRAIN_last_exec_duration=$stop-$start
cmd_timestamp=''
}
prompt_cmd_exec_time() {
[ $BULLETTRAIN_last_exec_duration -gt $BULLETTRAIN_EXEC_TIME_ELAPSED ] && prompt_segment $BULLETTRAIN_EXEC_TIME_BG $BULLETTRAIN_EXEC_TIME_FG "$(displaytime $BULLETTRAIN_last_exec_duration)"
}
# Based on http://stackoverflow.com/a/32164707/3859566
function displaytime {
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
[[ $D > 0 ]] && printf '%dd' $D
[[ $H > 0 ]] && printf '%dh' $H
[[ $M > 0 ]] && printf '%dm' $M
printf '%ds' $S
}
# Begin a segment
# Takes three arguments, background, foreground and text. All of them can be omitted,
# rendering default background/foreground and no text.
prompt_segment() {
local bg fg
SEGMENT_SEPARATOR=''
CURRENT_BG='NONE'
[[ -n $1 ]] && bg="%K{$1}" || bg="%k"
[[ -n $2 ]] && fg="%F{$2}" || fg="%f"
echo -n "%{%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
CURRENT_BG=$1
[[ -n $3 ]] && echo -n $3
}
POWERLEVEL9K_CUSTOM_EXEC_TIME="prompt_cmd_exec_time"
# Cosas de la bateria
prompt_battery() {
# The battery can have four different states - default to 'unknown'.
local current_state="unknown"
typeset -AH battery_states
battery_states=(
'low' 'red'
'charging' 'yellow'
'charged' 'green'
'disconnected' "$DEFAULT_COLOR_INVERTED"
)
# Set default values if the user did not configure them
set_default POWERLEVEL9K_BATTERY_LOW_THRESHOLD 10
if [[ $OS =~ OSX && -f /usr/sbin/ioreg && -x /usr/sbin/ioreg ]]; then
# Pre-Grep as much information as possible to save some memory and
# avoid pollution of the xtrace output.
local raw_data="$(ioreg -n AppleSmartBattery | grep -E "MaxCapacity|TimeRemaining|CurrentCapacity|ExternalConnected|IsCharging")"
# return if there is no battery on system
[[ -z $(echo $raw_data | grep MaxCapacity) ]] && return
# Convert time remaining from minutes to hours:minutes date string
local time_remaining=$(echo $raw_data | grep TimeRemaining | awk '{ print $5 }')
if [[ -n $time_remaining ]]; then
# this value is set to a very high number when the system is calculating
[[ $time_remaining -gt 10000 ]] && local tstring="..." || local tstring=${(f)$(/bin/date -u -r $(($time_remaining * 60)) +%k:%M)}
fi
# Get charge values
local max_capacity=$(echo $raw_data | grep MaxCapacity | awk '{ print $5 }')
local current_capacity=$(echo $raw_data | grep CurrentCapacity | awk '{ print $5 }')
if [[ -n "$max_capacity" && -n "$current_capacity" ]]; then
typeset -i 10 bat_percent
bat_percent=$(( (current_capacity * 100) / max_capacity ))
fi
local remain=""
# Logic for string output
if [[ $(echo $raw_data | grep ExternalConnected | awk '{ print $5 }') =~ "Yes" ]]; then
# Battery is charging
if [[ $(echo $raw_data | grep IsCharging | awk '{ print $5 }') =~ "Yes" ]]; then
current_state="charging"
remain=" ($tstring)"
else
current_state="charged"
fi
else
[[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && current_state="low" || current_state="disconnected"
remain=" ($tstring)"
fi
fi
if [[ $OS =~ Linux ]]; then
local sysp="/sys/class/power_supply"
# Reported BAT0 or BAT1 depending on kernel version
[[ -a $sysp/BAT0 ]] && local bat=$sysp/BAT0
[[ -a $sysp/BAT1 ]] && local bat=$sysp/BAT1
# Return if no battery found
[[ -z $bat ]] && return
local capacity=$(cat $bat/capacity)
local battery_status=$(cat $bat/status)
[[ $capacity -gt 100 ]] && local bat_percent=100 || local bat_percent=$capacity
[[ $battery_status =~ Charging || $battery_status =~ Full ]] && local connected=true
if [[ -z $connected ]]; then
[[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && current_state="low" || current_state="disconnected"
else
[[ $bat_percent =~ 100 ]] && current_state="charged"
[[ $bat_percent -lt 100 ]] && current_state="charging"
fi
if [[ -f /usr/bin/acpi ]]; then
local time_remaining=$(acpi | awk '{ print $5 }')
if [[ $time_remaining =~ rate ]]; then
local tstring="..."
elif [[ $time_remaining =~ "[:digit:]+" ]]; then
local tstring=${(f)$(date -u -d "$(echo $time_remaining)" +%k:%M 2> /dev/null)}
fi
fi
[[ -n $tstring ]] && local remain=" ($tstring)"
fi
local message
# Default behavior: Be verbose!
set_default POWERLEVEL9K_BATTERY_VERBOSE true
if [[ "$POWERLEVEL9K_BATTERY_VERBOSE" == true ]]; then
message="$bat_percent%%$remain"
else
message="$bat_percent%%"
fi
# Draw the prompt_segment
if [[ $current_state != "charged" ]]
then
# Draw the prompt_segment
if [[ -n $bat_percent && $bat_percent -ne 94 && $bat_percent -ne 98 ]]; then
"$1_prompt_segment" "${0}_${current_state}" "$2" "$DEFAULT_COLOR" "${battery_states[$current_state]}" "$message" 'BATTERY_ICON'
fi
fi
}
# K8s
POWERLEVEL9K_KUBECTL_CONTEXT="prompt_kubectl_context"
prompt_kubectl_context() {
if [[ -n $KUBECONFIG ]]
then
prompt_segment $DEFAULT_COLOR blue_COLOR "k8s-$KUBE_PROFILE"
fi
}