Skip to content

Commit

Permalink
Remove subshells from title escapes
Browse files Browse the repository at this point in the history
During startup, title escape sequences were generated with printf in a
subshell. Use the ANSI C string style to save time.

Replace echo with printf when using the escapes in _lp_title().

Replace echo with here-string in _lp_as_text().

We can't change the API of _lp_title(), as it is the one function used
in the default liquid.ps1. There are probably a sizable number of users
using it.

I had wanted to remove the title from PS1 for two reasons: First is that
trying to read or debug your PS1 is much harder when there is a ton of
extra junk in it. Second, the shell reprints PS1 if the terminal is redrawn,
which can happen with ^L or on resize. But the second point is false;
the terminal won't redraw PS1 unless it is waiting on the command line.
I would only want the title to not be overridden when a command is
running (since I want to make the title the running command in the
future). And second, ^L to clear screen also only works when on the
command line.

I do want to remove the sed command that strips color escape codes. It
is complicated, not portable, and is run for every prompt. We need to
remove every such external call, so the best way is to not pass in
colored text to the title. But the structure right now would make that
difficult, so it stays for now.
  • Loading branch information
Rycieos committed Nov 25, 2020
1 parent decaece commit e2ba86e
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,14 @@ lp_activate() {
LP_BRACKET_OPEN="${LP_COLOR_IN_MULTIPLEXER}${LP_MARK_BRACKET_OPEN}${NO_COL}"
LP_BRACKET_CLOSE="${LP_COLOR_IN_MULTIPLEXER}${LP_MARK_BRACKET_CLOSE}${NO_COL}"
(( LP_ENABLE_TITLE = LP_ENABLE_TITLE && LP_ENABLE_SCREEN_TITLE ))
LP_TITLE_OPEN="$(printf '\033k')"
LP_TITLE_OPEN=$'\Ek'
# "\e\" but on bash \ must be escaped
LP_TITLE_CLOSE="$(printf '\033%s' "$_LP_BACKSLASH")"
LP_TITLE_CLOSE=$'\E'"$_LP_BACKSLASH"
else
LP_BRACKET_OPEN="${LP_MARK_BRACKET_OPEN}"
LP_BRACKET_CLOSE="${LP_MARK_BRACKET_CLOSE}"
LP_TITLE_OPEN="$(printf '\e]0;')"
LP_TITLE_CLOSE="$(printf '\a')"
LP_TITLE_OPEN=$'\E]0;'
LP_TITLE_CLOSE=$'\a'
fi

[[ "_$TERM" == _linux* ]] && LP_ENABLE_TITLE=0
Expand Down Expand Up @@ -578,7 +578,7 @@ lp_activate() {
_lp_as_text() {
# Remove all terminal sequences that we wrapped with $_LP_OPEN_ESC and
# $_LP_CLOSE_ESC.
echo -nE "$1" | sed -$_LP_SED_EXTENDED "s,$_LP_CLEAN_ESC,,g"
printf '%s' "$1" | sed -$_LP_SED_EXTENDED "s,$_LP_CLEAN_ESC,,g"
}

# Store $2 (or $?) as a true/false value in variable named $1
Expand Down Expand Up @@ -2119,12 +2119,12 @@ _lp_temperature_color() {
##########

_lp_title() {
(( LP_ENABLE_TITLE )) || return
(( LP_ENABLE_TITLE )) || return 2

# Get the current computed prompt as pure text
echo -nE "${_LP_OPEN_ESC}${LP_TITLE_OPEN}"
_lp_as_text "$1"
echo -nE "${LP_TITLE_CLOSE}${_LP_CLOSE_ESC}"
printf '%s' "${_LP_OPEN_ESC}${LP_TITLE_OPEN}"
_lp_as_text "${1-}"
printf '%s' "${LP_TITLE_CLOSE}${_LP_CLOSE_ESC}"
}

###################
Expand Down

0 comments on commit e2ba86e

Please sign in to comment.