From 0887bd6a7d450a1001ea0ab0d701caef3dddc309 Mon Sep 17 00:00:00 2001 From: Akib Azmain Turja Date: Sun, 4 Dec 2022 15:10:13 +0600 Subject: [PATCH] Update title automatically in shell integration * integration/bash (__eat_prompt_command): Remove extra '\'s. * integration/bash (__eat_in_prompt_command): New variable. * integration/bash (__eat_preexec, __eat_before_prompt_command) (__eat_after_prompt_command, __eat_before_exec): New function. * integration/bash: Modify PS1 to update terminal title when displaying prompt. Add '__eat_before_exec' as 'DEBUG' trap handler to update terminal title just before executing a command. Prepend and append '__eat_before_prompt_command' and '__eat_after_prompt_command' to 'PROMPT_COMMAND' to avoid getting trapped in 'DEBUG' trap. --- integration/bash | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/integration/bash b/integration/bash index bba6539..bd0168a 100644 --- a/integration/bash +++ b/integration/bash @@ -19,14 +19,60 @@ __eat_prompt_command () { # Send the current working directory, for directory tracking. - printf "\\e]7;file://%s%s\\e\\\\" "$HOSTNAME" "$PWD" + printf '\e]7;file://%s%s\e\\' "$HOSTNAME" "$PWD" +} + +__eat_preexec () { + # "${PWD/$HOME/'~'}" converts "/home/akib/org/" to "~/org/". + # The next one is substituted with '$', or '#' if we're "root". + printf '\e]2;%s@%s:%s%s %s\e\\' "$USER" "$HOSTNAME" \ + "${PWD/$HOME/'~'}" \ + "$(test $UID -eq 0 && echo '#' || echo '$')" "$BASH_COMMAND" +} + +__eat_in_prompt_command=no + +__eat_before_prompt_command () +{ + __eat_in_prompt_command=yes +} + +__eat_after_prompt_command () +{ + __eat_in_prompt_command=no +} + +__eat_before_exec () { + if test $__eat_in_prompt_command = no \ + && test "$BASH_COMMAND" != __eat_before_prompt_command + then + __eat_preexec + fi } # Add '__eat_prompt_command' as the last element of 'PROMPT_COMMAND'. if test -z "$__eat_bash_integration_enabled" then __eat_bash_integration_enabled=yes + PS1='\[\e]2;\u@\h:\w$\e\\\]'"$PS1" PROMPT_COMMAND+=(__eat_prompt_command) + trap '__eat_before_exec' DEBUG + # Wrap 'PROMPT_COMMAND' to avoid it getting trapped in 'DEBUG' trap. + # Step 1: Append to PROMPT_COMMAND. + PROMPT_COMMAND+=(__eat_after_prompt_command) + # Step 2: Prepend to PROMPT_COMMAND. + # Step 2.1: Move all elements to make the first index free. + # Fun fact: Microsoft doesn't still know this simple trick. They + # use something as silly and pityful as 'VAR=$PROMPT_COMMAND' to + # copy a Bash array in VSCode Bash integration script, that simply + # won't work ever, and then complain about Bash in the comments! + # LOL. ;D + for i in $(eval "printf {${#PROMPT_COMMAND[*]}..1..-1}") + do + PROMPT_COMMAND[$i]=${PROMPT_COMMAND[$((i-1))]} + done + # Step 2.2: Assign the first element. + PROMPT_COMMAND[0]=__eat_before_prompt_command fi # Local Variables: