Skip to content

Commit

Permalink
Modify scripts to stream terminal title changes (#1095)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update src/common-utils/scripts/bash_theme_snippet.sh

Co-authored-by: Samruddhi Khandale <[email protected]>

* Update src/common-utils/scripts/rc_snippet.sh

Co-authored-by: Samruddhi Khandale <[email protected]>

* 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 <[email protected]>
Co-authored-by: Samruddhi Khandale <[email protected]>
  • Loading branch information
3 people authored Aug 19, 2024
1 parent b45b199 commit 34c3a51
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/common-utils/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
21 changes: 20 additions & 1 deletion src/common-utils/scripts/bash_theme_snippet.sh
Original file line number Diff line number Diff line change
@@ -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=$? \
Expand All @@ -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
19 changes: 19 additions & 0 deletions src/common-utils/scripts/devcontainers.zsh-theme
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion src/common-utils/scripts/rc_snippet.sh
Original file line number Diff line number Diff line change
@@ -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

Expand Down
18 changes: 18 additions & 0 deletions test/common-utils/no-terminal-title-by-default.sh
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions test/common-utils/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
}
}
}
18 changes: 18 additions & 0 deletions test/common-utils/terminal-title-on-xterm.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 34c3a51

Please sign in to comment.