From 50234ede04e04b83aab4e832ce26cfb2cc19b271 Mon Sep 17 00:00:00 2001 From: PandaWill Date: Wed, 9 Sep 2020 23:31:13 +0100 Subject: [PATCH] Add option to insert newline when prompt is long Add new setting LP_MINIMUM_SPACE_AFTER_PROMPT. A new line is added when the prompt would leave less space than this in the terminal. See feature request #599. --- CONTRIBUTORS.md | 1 + README.md | 1 + liquidprompt | 43 +++++++++++++++++++++++++++++++++++++++++-- liquidpromptrc-dist | 4 ++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 6c673c22..482085fa 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -96,6 +96,7 @@ Thanks again to everyone for all of the help and support! * Tristan Miller (@logological) * Markus Gebert (@mgeb) * Tore Anderson (@toreanderson) +* William S. (@PandaWill) ### Code cleanup * Aurélien Requiem (@aureq) diff --git a/README.md b/README.md index 2a1d18e6..c88266aa 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,7 @@ You can configure some variables in the `~/.config/liquidpromptrc` file: showing it only when connected via a remote shell (0) or never showing it (-1). * `LP_USER_ALWAYS`, a choice between always displaying the user or showing it only when he is different from the one that logged in +* `LP_MINIMUM_SPACE_AFTER_PROMPT`, the minimum number of free columns required after a prompt, a new line is added if the number of free columns is below this. You can also force some features to be disabled, to save some time in the prompt-building process: diff --git a/liquidprompt b/liquidprompt index 682d9bd1..4d79c2a7 100755 --- a/liquidprompt +++ b/liquidprompt @@ -299,6 +299,7 @@ _lp_source_config() LP_PS1=${LP_PS1:-""} LP_PS1_PREFIX=${LP_PS1_PREFIX:-""} LP_PS1_POSTFIX=${LP_PS1_POSTFIX:-""} + LP_MINIMUM_SPACE_AFTER_PROMPT=${LP_MINIMUM_SPACE_AFTER_PROMPT:-0} LP_ENABLE_PERM=${LP_ENABLE_PERM:-1} LP_ENABLE_SHORTEN_PATH=${LP_ENABLE_SHORTEN_PATH:-1} @@ -1594,6 +1595,23 @@ _lp_as_text() echo -nE "$1" | sed -$_LP_SED_EXTENDED "s,$_LP_CLEAN_ESC,,g" } +# Get length of a given string, after evaluating escape codes (\u for user, etc) +_lp_length() +{ + typeset len evaluated_ps1 + typeset printable_ps1=$(_lp_as_text "$1") + + if $_LP_SHELL_bash; then + evaluated_ps1=$(printf '%s' "${printable_ps1@P}") + else # zsh + evaluated_ps1=$(print -P $printable_ps1) + fi + + len=${#evaluated_ps1} + + echo $len +} + _lp_title() { (( LP_ENABLE_TITLE )) || return @@ -1849,8 +1867,18 @@ _lp_set_prompt() # is set. PS1+="${LP_VCS}" - # add return code and prompt mark - PS1+="${LP_RUNTIME}${LP_ERR}${LP_MARK_PREFIX}${LP_COLOR_MARK}${LP_MARK}${LP_PS1_POSTFIX}" + # add return code + PS1+="${LP_RUNTIME}${LP_ERR}" + + # determine if we need to add new line before prompt mark + if (( LP_MINIMUM_SPACE_AFTER_PROMPT )); then + if [[ $(_remaining_space) -lt "$LP_MINIMUM_SPACE_AFTER_PROMPT" ]]; then + PS1+=$'\n' + fi + fi + + # add prompt mark + PS1+="${LP_MARK_PREFIX}${LP_COLOR_MARK}${LP_MARK}${LP_PS1_POSTFIX}" # "invisible" parts # Get the current prompt on the fly and make it a title @@ -1868,6 +1896,17 @@ _lp_set_prompt() fi } +# Calculates the remaining space on a terminal line after prompt has been printed +_remaining_space() +{ + typeset len=$(_lp_length "$PS1") + + # Modulo terminal width, so we handle the case where prompt is already on a new line + remaining_space=$(( $COLUMNS - ( $len % $COLUMNS ) )) + + echo $remaining_space +} + prompt_tag() { export LP_PS1_PREFIX="$(_lp_sr "$1")" diff --git a/liquidpromptrc-dist b/liquidpromptrc-dist index d81d7273..d905dfde 100644 --- a/liquidpromptrc-dist +++ b/liquidpromptrc-dist @@ -176,4 +176,8 @@ LP_ENABLE_SSH_COLORS=0 # will be disabled LP_DISABLED_VCS_PATH="" +# Require minimum number of columns in the terminal after the prompt. +# Default is 0 (off), sensible values are around 20 or 30. +LP_MINIMUM_SPACE_AFTER_PROMPT=0 + # vim: set et sts=4 sw=4 tw=120 ft=sh: