Skip to content

Commit 34c3a51

Browse files
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 <[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]>
1 parent b45b199 commit 34c3a51

File tree

7 files changed

+91
-3
lines changed

7 files changed

+91
-3
lines changed

src/common-utils/devcontainer-feature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "common-utils",
3-
"version": "2.4.7",
3+
"version": "2.5.0",
44
"name": "Common Utilities",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/common-utils",
66
"description": "Installs a set of common command line utilities, Oh My Zsh!, and sets up a non-root user.",

src/common-utils/scripts/bash_theme_snippet.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# bash theme - partly inspired by https://github.com/ohmyzsh/ohmyzsh/blob/master/themes/robbyrussell.zsh-theme
32
__bash_prompt() {
43
local userpart='`export XIT=$? \
@@ -23,3 +22,23 @@ __bash_prompt() {
2322
}
2423
__bash_prompt
2524
export PROMPT_DIRTRIM=4
25+
26+
# Check if the terminal is xterm
27+
if [[ "$TERM" == "xterm" ]]; then
28+
# Function to set the terminal title to the current command
29+
preexec() {
30+
local cmd="${BASH_COMMAND}"
31+
echo -ne "\033]0;${USER}@${HOSTNAME}: ${cmd}\007"
32+
}
33+
34+
# Function to reset the terminal title to the shell type after the command is executed
35+
precmd() {
36+
echo -ne "\033]0;${USER}@${HOSTNAME}: ${SHELL}\007"
37+
}
38+
39+
# Trap DEBUG signal to call preexec before each command
40+
trap 'preexec' DEBUG
41+
42+
# Append to PROMPT_COMMAND to call precmd before displaying the prompt
43+
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }precmd"
44+
fi

src/common-utils/scripts/devcontainers.zsh-theme

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,22 @@ __zsh_prompt() {
2424
unset -f __zsh_prompt
2525
}
2626
__zsh_prompt
27+
28+
# Check if the terminal is xterm
29+
if [[ "$TERM" == "xterm" ]]; then
30+
# Function to set the terminal title to the current command
31+
preexec() {
32+
local cmd=${1}
33+
echo -ne "\033]0;${USER}@${HOSTNAME}: ${cmd}\007"
34+
}
35+
36+
# Function to reset the terminal title to the shell type after the command is executed
37+
precmd() {
38+
echo -ne "\033]0;${USER}@${HOSTNAME}: ${SHELL}\007"
39+
}
40+
41+
# Add the preexec and precmd functions to the corresponding hooks
42+
autoload -Uz add-zsh-hook
43+
add-zsh-hook preexec preexec
44+
add-zsh-hook precmd precmd
45+
fi

src/common-utils/scripts/rc_snippet.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
if [ -z "${USER}" ]; then export USER=$(whoami); fi
32
if [[ "${PATH}" != *"$HOME/.local/bin"* ]]; then export PATH="${PATH}:$HOME/.local/bin"; fi
43

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Definition specific tests
9+
. /etc/os-release
10+
11+
# Make sure bashrc is applied
12+
source /root/.bashrc
13+
14+
check "check_term_is_not_set" test !"$TERM"
15+
check "check_prompt_command_not_set" test !"$PROMPT_COMMAND"
16+
17+
# Report result
18+
reportResults

test/common-utils/scenarios.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,20 @@
259259
"features": {
260260
"common-utils": {}
261261
}
262+
},
263+
"terminal-title-on-xterm": {
264+
"image": "node",
265+
"features": {
266+
"common-utils": {}
267+
},
268+
"containerEnv": {
269+
"TERM": "xterm"
270+
}
271+
},
272+
"no-terminal-title-by-default": {
273+
"image": "node",
274+
"features": {
275+
"common-utils": {}
276+
}
262277
}
263278
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Definition specific tests
9+
. /etc/os-release
10+
11+
# Make sure bashrc is applied
12+
source /root/.bashrc
13+
14+
check "check_term_is_set" test "$TERM" = "xterm"
15+
check "check_term_is_set" test "$PROMPT_COMMAND" = "precmd"
16+
17+
# Report result
18+
reportResults

0 commit comments

Comments
 (0)