From 34c3a51114451043f3608d803c99bbbcc9aaa60b Mon Sep 17 00:00:00 2001 From: Osvaldo Ortega <48293249+osortega@users.noreply.github.com> Date: Mon, 19 Aug 2024 12:05:32 -0700 Subject: [PATCH] Modify scripts to stream terminal title changes (#1095) * Modify scripts to stream terminal title changes Adding the capabilities to signal terminal title changes based on the command that is being executed * Fixes * Review comments * Update src/common-utils/devcontainer-feature.json Co-authored-by: Samruddhi Khandale * Update src/common-utils/scripts/bash_theme_snippet.sh Co-authored-by: Samruddhi Khandale * Update src/common-utils/scripts/rc_snippet.sh Co-authored-by: Samruddhi Khandale * Remove RC * Adding tests * Update test/common-utils/scenarios.json * Update src/common-utils/scripts/bash_theme_snippet.sh * Test fixes --------- Co-authored-by: Samruddhi Khandale Co-authored-by: Samruddhi Khandale --- src/common-utils/devcontainer-feature.json | 2 +- .../scripts/bash_theme_snippet.sh | 21 ++++++++++++++++++- .../scripts/devcontainers.zsh-theme | 19 +++++++++++++++++ src/common-utils/scripts/rc_snippet.sh | 1 - .../no-terminal-title-by-default.sh | 18 ++++++++++++++++ test/common-utils/scenarios.json | 15 +++++++++++++ test/common-utils/terminal-title-on-xterm.sh | 18 ++++++++++++++++ 7 files changed, 91 insertions(+), 3 deletions(-) create mode 100755 test/common-utils/no-terminal-title-by-default.sh create mode 100755 test/common-utils/terminal-title-on-xterm.sh diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index c403c2644..153e2503d 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -1,6 +1,6 @@ { "id": "common-utils", - "version": "2.4.7", + "version": "2.5.0", "name": "Common Utilities", "documentationURL": "https://github.com/devcontainers/features/tree/main/src/common-utils", "description": "Installs a set of common command line utilities, Oh My Zsh!, and sets up a non-root user.", diff --git a/src/common-utils/scripts/bash_theme_snippet.sh b/src/common-utils/scripts/bash_theme_snippet.sh index 491e4c047..6e80952a4 100644 --- a/src/common-utils/scripts/bash_theme_snippet.sh +++ b/src/common-utils/scripts/bash_theme_snippet.sh @@ -1,4 +1,3 @@ - # bash theme - partly inspired by https://github.com/ohmyzsh/ohmyzsh/blob/master/themes/robbyrussell.zsh-theme __bash_prompt() { local userpart='`export XIT=$? \ @@ -23,3 +22,23 @@ __bash_prompt() { } __bash_prompt export PROMPT_DIRTRIM=4 + +# Check if the terminal is xterm +if [[ "$TERM" == "xterm" ]]; then + # Function to set the terminal title to the current command + preexec() { + local cmd="${BASH_COMMAND}" + echo -ne "\033]0;${USER}@${HOSTNAME}: ${cmd}\007" + } + + # Function to reset the terminal title to the shell type after the command is executed + precmd() { + echo -ne "\033]0;${USER}@${HOSTNAME}: ${SHELL}\007" + } + + # Trap DEBUG signal to call preexec before each command + trap 'preexec' DEBUG + + # Append to PROMPT_COMMAND to call precmd before displaying the prompt + PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }precmd" +fi \ No newline at end of file diff --git a/src/common-utils/scripts/devcontainers.zsh-theme b/src/common-utils/scripts/devcontainers.zsh-theme index ff11c917e..d12993a67 100644 --- a/src/common-utils/scripts/devcontainers.zsh-theme +++ b/src/common-utils/scripts/devcontainers.zsh-theme @@ -24,3 +24,22 @@ __zsh_prompt() { unset -f __zsh_prompt } __zsh_prompt + +# Check if the terminal is xterm +if [[ "$TERM" == "xterm" ]]; then + # Function to set the terminal title to the current command + preexec() { + local cmd=${1} + echo -ne "\033]0;${USER}@${HOSTNAME}: ${cmd}\007" + } + + # Function to reset the terminal title to the shell type after the command is executed + precmd() { + echo -ne "\033]0;${USER}@${HOSTNAME}: ${SHELL}\007" + } + + # Add the preexec and precmd functions to the corresponding hooks + autoload -Uz add-zsh-hook + add-zsh-hook preexec preexec + add-zsh-hook precmd precmd +fi \ No newline at end of file diff --git a/src/common-utils/scripts/rc_snippet.sh b/src/common-utils/scripts/rc_snippet.sh index 4810cd993..42249ecbd 100644 --- a/src/common-utils/scripts/rc_snippet.sh +++ b/src/common-utils/scripts/rc_snippet.sh @@ -1,4 +1,3 @@ - if [ -z "${USER}" ]; then export USER=$(whoami); fi if [[ "${PATH}" != *"$HOME/.local/bin"* ]]; then export PATH="${PATH}:$HOME/.local/bin"; fi diff --git a/test/common-utils/no-terminal-title-by-default.sh b/test/common-utils/no-terminal-title-by-default.sh new file mode 100755 index 000000000..83fe7d221 --- /dev/null +++ b/test/common-utils/no-terminal-title-by-default.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +set -e + +# Optional: Import test library +source dev-container-features-test-lib + +# Definition specific tests +. /etc/os-release + +# Make sure bashrc is applied +source /root/.bashrc + +check "check_term_is_not_set" test !"$TERM" +check "check_prompt_command_not_set" test !"$PROMPT_COMMAND" + +# Report result +reportResults \ No newline at end of file diff --git a/test/common-utils/scenarios.json b/test/common-utils/scenarios.json index 744f16dc1..e5529ea04 100644 --- a/test/common-utils/scenarios.json +++ b/test/common-utils/scenarios.json @@ -259,5 +259,20 @@ "features": { "common-utils": {} } + }, + "terminal-title-on-xterm": { + "image": "node", + "features": { + "common-utils": {} + }, + "containerEnv": { + "TERM": "xterm" + } + }, + "no-terminal-title-by-default": { + "image": "node", + "features": { + "common-utils": {} + } } } \ No newline at end of file diff --git a/test/common-utils/terminal-title-on-xterm.sh b/test/common-utils/terminal-title-on-xterm.sh new file mode 100755 index 000000000..9b483651f --- /dev/null +++ b/test/common-utils/terminal-title-on-xterm.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +set -e + +# Optional: Import test library +source dev-container-features-test-lib + +# Definition specific tests +. /etc/os-release + +# Make sure bashrc is applied +source /root/.bashrc + +check "check_term_is_set" test "$TERM" = "xterm" +check "check_term_is_set" test "$PROMPT_COMMAND" = "precmd" + +# Report result +reportResults \ No newline at end of file