mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-02-20 03:02:29 +01:00
added plugin for displaying prompt responsively
(cherry picked from commit 25a04de1e51358fc8b119c047343a060ee34ced6) (cherry picked from commit 3f47be12e38c82d09ddaa4ba59269fa642d18840)
This commit is contained in:
parent
df5b09e20b
commit
01949f462a
2 changed files with 105 additions and 0 deletions
56
plugins/responsive-prompt/README.md
Normal file
56
plugins/responsive-prompt/README.md
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
# Responsive Prompt
|
||||
|
||||
`Responsive Prompt` is a `OhMyZSH` plugin, which makes your prompt
|
||||
responsive to your terminal's column size. It does so, by watching
|
||||
`$COLUMNS` environment variables. When it changes, this plugin reloads
|
||||
your ZSH prompt.
|
||||
|
||||
## Different Prompts
|
||||
|
||||
You can define several prompts for your terminal via functions named
|
||||
like `_prompt_60`, which defines the prompt to be used when `$COLUMNS`
|
||||
is greater than `60`. Note that, you must set `$PROMPT_BREAKPOINTS`
|
||||
variable, appropriately, for this to work. Example:
|
||||
|
||||
PROMPT_BREAKPOINTS=(100 50 0)
|
||||
_prompt_100(){ PROMPT="<prompt1>"; } # prompt when $COLUMNS > 100
|
||||
_prompt_50() { PROMPT="<prompt2>"; } # prompt when 100 >= $COLUMNS > 50
|
||||
_prompt_0() { PROMPT="<prompt3>"; } # prompt when 50 >= $COLUMNS > 0
|
||||
|
||||
You can, even, define different prompts using conditional statements in
|
||||
your `$PROMPT_FILE`. In this case, your `$PROMPT_FILE` will be sourced,
|
||||
whenever the value of `$COLUMNS` change, to reload your prompt. Example:
|
||||
|
||||
if [[ $COLUMNS -gt 100 ]]; then
|
||||
PROMPT="<prompt1>"
|
||||
elif [[ $COLUMNS -gt 50 ]]; then
|
||||
PROMPT="<prompt2>"
|
||||
else
|
||||
PROMPT="<prompt3>"
|
||||
fi
|
||||
|
||||
## Variables
|
||||
|
||||
- **`PROMPT_FILE`**: File which will be sourced (in order to reload the
|
||||
`PROMPT`), if no appropriate prompt-setting functions were found.
|
||||
Since, most ZSH users will have prompt defined somewhere inside their
|
||||
`~/.zshrc` file, it is the default. However, if you have a dedicated
|
||||
file for your prompt, you can specify it with this option, which will
|
||||
speed up prompt rendering.
|
||||
|
||||
- **`PROMPT_BREAKPOINTS`**: If you use prompt-setting functions, you can
|
||||
use this variable to define the various `$COLUMNS` values, when the
|
||||
plugin should reload your prompt. Note that, you must define this
|
||||
variable in a decreasing array format. Default value: `(120 90 60 0)`,
|
||||
which means that when `$COLUMNS` is greater than `120`, `_prompt_120`
|
||||
function will be called, and so on. If the plugin is unable to find
|
||||
a function by these names, your `$PROMPT_FILE` will be sourced to
|
||||
reload the prompt.
|
||||
|
||||
|
||||
- **`PROMPT_NEWLINE_AFTER`**: If you prefer to break your prompt into
|
||||
a multi-line prompt when it exceeds a given display length, you can
|
||||
use this variable to specify such length. Once the prompt exceeds this
|
||||
length, a newline character will be inserted in the variable
|
||||
`$prompt_newline`, which you can use inside your prompt definition.
|
||||
Otherwise, this variable will remain empty.
|
||||
49
plugins/responsive-prompt/responsive-prompt.plugin.zsh
Normal file
49
plugins/responsive-prompt/responsive-prompt.plugin.zsh
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#!/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 )
|
||||
Loading…
Add table
Add a link
Reference in a new issue