ohmyzsh/plugins/responsive-prompt/responsive-prompt.plugin.zsh
Nikhil Gupta 01949f462a added plugin for displaying prompt responsively
(cherry picked from commit 25a04de1e51358fc8b119c047343a060ee34ced6)
(cherry picked from commit 3f47be12e38c82d09ddaa4ba59269fa642d18840)
2014-11-08 18:18:31 +05:30

49 lines
1.5 KiB
Bash

#!/usr/env/bin zsh
# Make your Terminal Prompt responsive :)
#
# Author: Nikhil Gupta <me@nikhgupta.com>
# Version: 0.0.1
#
# Please, read the README.md
#
[[ -n "${PROMPT_FILE}" ]] || PROMPT_FILE="${HOME}/.zshrc"
[[ -n "${PROMPT_BREAKPOINTS}" ]] || PROMPT_BREAKPOINTS=(120 90 60 0)
[[ -n "${PROMPT_NEWLINE_AFTER}" ]] || PROMPT_NEWLINE_AFTER=40
# Calculate length of the prompt being rendered. Additionally, set
# a variable that adds a newline character when prompt length exceeds
# $PROMPT_NEWLINE_AFTER
_calculate_prompt_length() {
local prompt_exp="${(%%)PROMPT}"
local prompt_stripped="$(echo $prompt_exp | sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g')"
local prompt_length="${#prompt_stripped}"
unset prompt_newline
if [[ $prompt_length -gt $PROMPT_NEWLINE_AFTER ]]; then
prompt_newline=$'\n'
else
prompt_newline=$''
fi
}
# Look for change in $COLUMNS, and loop through all breakpoints to setup
# the appropriate prompt for the new column size.
_responsive_prompt(){
_calculate_prompt_length
[[ $COLUMNS -eq $last_columns ]] && return
for breakpoint in $PROMPT_BREAKPOINTS; do
[[ $COLUMNS -lt $breakpoint ]] && continue
if which "_prompt_$breakpoint" &>/dev/null; then
eval "_prompt_$breakpoint"
break
else
source $PROMPT_FILE
fi
done
_calculate_prompt_length
last_columns=$COLUMNS
}
# this function should run when prompt is about to be rendered.
precmd_functions+=( _responsive_prompt )