diff --git a/plugins/responsive-prompt/README.md b/plugins/responsive-prompt/README.md new file mode 100644 index 000000000..0e658f331 --- /dev/null +++ b/plugins/responsive-prompt/README.md @@ -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=""; } # prompt when $COLUMNS > 100 + _prompt_50() { PROMPT=""; } # prompt when 100 >= $COLUMNS > 50 + _prompt_0() { PROMPT=""; } # 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="" + elif [[ $COLUMNS -gt 50 ]]; then + PROMPT="" + else + PROMPT="" + 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. diff --git a/plugins/responsive-prompt/responsive-prompt.plugin.zsh b/plugins/responsive-prompt/responsive-prompt.plugin.zsh new file mode 100644 index 000000000..ddb131650 --- /dev/null +++ b/plugins/responsive-prompt/responsive-prompt.plugin.zsh @@ -0,0 +1,49 @@ +#!/usr/env/bin zsh +# Make your Terminal Prompt responsive :) +# +# Author: Nikhil Gupta +# 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 )