Unverified Commit 34c3a511 authored by Osvaldo Ortega's avatar Osvaldo Ortega Committed by GitHub

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: 's avatarSamruddhi Khandale <samruddhikhandale@github.com>

* Update src/common-utils/scripts/bash_theme_snippet.sh
Co-authored-by: 's avatarSamruddhi Khandale <samruddhikhandale@github.com>

* Update src/common-utils/scripts/rc_snippet.sh
Co-authored-by: 's avatarSamruddhi Khandale <samruddhikhandale@github.com>

* Remove RC

* Adding tests

* Update test/common-utils/scenarios.json

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

* Test fixes

---------
Co-authored-by: 's avatarSamruddhi Khandale <samruddhikhandale@github.com>
Co-authored-by: 's avatarSamruddhi Khandale <skhandale@microsoft.com>
parent b45b1997
{
"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.",
......
# 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
......@@ -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
if [ -z "${USER}" ]; then export USER=$(whoami); fi
if [[ "${PATH}" != *"$HOME/.local/bin"* ]]; then export PATH="${PATH}:$HOME/.local/bin"; fi
......
#!/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
......@@ -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
#!/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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment