Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions completions/pengwin-setup
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function _pengwin_setup() { # By convention, the function name
mapfile -t COMPREPLY < <(compgen -W 'EXPLORER COLORTOOL LANGUAGE SHELLS' -- "${cur}")
;;
SHELLS)
mapfile -t COMPREPLY < <(compgen -W 'BASH-RL CSH FISH ZSH' -- "${cur}")
mapfile -t COMPREPLY < <(compgen -W 'BASH-RL CSH FISH SHELLINT ZSH' -- "${cur}")
;;
TOOLS)
mapfile -t COMPREPLY < <(compgen -W 'ANSIBLE CLOUDCLI DOCKER FZF HOMEBREW POWERSHELL' -- "${cur}")
Expand All @@ -51,7 +51,7 @@ function _pengwin_setup() { # By convention, the function name
mapfile -t COMPREPLY < <(compgen -W 'AWS AZURE DO IBM KUBERNETES OPENSTACK TERRAFORM' -- "${cur}")
;;
UNINSTALL)
mapfile -t COMPREPLY < <(compgen -W 'ANSIBLE AWS AZURE BASH-RL C++ CASSANDRA COLORTOOL COPILOT-CLI COPILOT-VIM DIGITALOCEAN DOCKER DOTNET FCITX FISH GO GUILIB HIDPI HOMEBREW IBM IBUS JAVA JETBRAINS KEYCHAIN KUBERNETES LAMP MSEDIT NIM NODEJS OPENSTACK POETRY POWERSHELL PYENV RCLOCAL RUBY RUST STARTMENU SSH SYNAPTIC SYSTEMD TERRAFORM VCXSRV VSCODE WINTHEME WSLTTY X410 XFCE' -- "${cur}")
mapfile -t COMPREPLY < <(compgen -W 'ANSIBLE AWS AZURE BASH-RL C++ CASSANDRA COLORTOOL COPILOT-CLI COPILOT-VIM DIGITALOCEAN DOCKER DOTNET FCITX FISH GO GUILIB HIDPI HOMEBREW IBM IBUS JAVA JETBRAINS KEYCHAIN KUBERNETES LAMP MSEDIT NIM NODEJS OPENSTACK POETRY POWERSHELL PYENV RCLOCAL RUBY RUST SHELLINT STARTMENU SSH SYNAPTIC SYSTEMD TERRAFORM VCXSRV VSCODE WINTHEME WSLTTY X410 XFCE' -- "${cur}")
;;
*)
mapfile -t COMPREPLY < <(compgen -W "--debug -d --verbose -v -y --yes --assume-yes --noupdate --norebuildicons -q \
Expand Down
101 changes: 101 additions & 0 deletions pengwin-setup.d/shell-integration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/bin/bash

# shellcheck source=common.sh
source "$(dirname "$0")/common.sh" "$@"

#Imported from common.sh
declare HOME

readonly PENGWIN_SHELL_INTEGRATION_MARKER='### PENGWIN WINDOWS TERMINAL SHELL INTEGRATION'
readonly SHELL_INTEGRATION_DIR='/usr/local/share/pengwin'
readonly SHELL_INTEGRATION_SCRIPT="${SHELL_INTEGRATION_DIR}/wt-shell-integration.sh"

#######################################
# Install Windows Terminal shell integration
# Installs script to /usr/local/share/pengwin and adds source line to ~/.bashrc
# Globals:
# HOME
# PENGWIN_SHELL_INTEGRATION_MARKER
# SHELL_INTEGRATION_DIR
# SHELL_INTEGRATION_SCRIPT
# Arguments:
# None
#######################################
function install_shell_integration() {
echo "Installing Windows Terminal shell integration"

local bashrc="${HOME}/.bashrc"

if [[ ! -f "${bashrc}" ]]; then
touch "${bashrc}"
fi

# Check if already installed
if grep -q "${PENGWIN_SHELL_INTEGRATION_MARKER}" "${bashrc}" 2>/dev/null; then
echo "Previous Pengwin Windows Terminal shell integration detected. Cancelling install..."
message --title "Warning!" --msgbox "Previous install of Windows Terminal shell integration detected. To reinstall, please run the uninstaller first or manually edit \"${bashrc}\" and remove the lines between:\n${PENGWIN_SHELL_INTEGRATION_MARKER}" 10 95
return 1
fi

# Create the directory if it doesn't exist
sudo mkdir -p "${SHELL_INTEGRATION_DIR}"

# Install shell integration script
# Based on: https://learn.microsoft.com/en-us/windows/terminal/tutorials/shell-integration
sudo tee "${SHELL_INTEGRATION_SCRIPT}" >/dev/null <<'SCRIPT_EOF'
#!/bin/bash
# Windows Terminal shell integration
# See: https://learn.microsoft.com/en-us/windows/terminal/tutorials/shell-integration
# Also supports WezTerm which implements the same OSC sequences

# Only run for bash in Windows Terminal or WezTerm
if [[ -z "${BASH_VERSION}" ]]; then
return 0
fi

if [[ "${TERM_PROGRAM}" == "WezTerm" || -n "${WT_SESSION}" ]]; then
__wt_osc() { printf '\e]%s\e\\' "$1"; }

__wt_mark_prompt_start() { __wt_osc '133;A'; }
__wt_mark_command_start() { __wt_osc '133;B'; }
__wt_mark_command_executed() { __wt_osc '133;C'; }
__wt_mark_command_finished() { __wt_osc "133;D;$1"; }
__wt_set_cwd() { __wt_osc "9;9;${PWD}"; }

__wt_update_prompt() {
local last_exit_code="$?"
__wt_mark_command_finished "${last_exit_code}"
__wt_set_cwd
__wt_mark_prompt_start
PS1="\[\$(__wt_mark_command_start)\]${__wt_original_ps1}"
return "${last_exit_code}"
}

if [[ -z "${__wt_original_ps1}" ]]; then
__wt_original_ps1="${PS1}"
PROMPT_COMMAND="__wt_update_prompt${PROMPT_COMMAND:+;}${PROMPT_COMMAND}"
PS1="\[\$(__wt_mark_command_start)\]${PS1}"
# PS0 is executed after reading a command but before executing it
PS0='\[$(__wt_mark_command_executed)\]'
fi
fi
SCRIPT_EOF

# Add minimal source line to .bashrc (since PS1 modifications need to happen after .bashrc sets PS1)
cat >>"${bashrc}" <<EOF
${PENGWIN_SHELL_INTEGRATION_MARKER}
# Source Windows Terminal shell integration (needs to run after PS1 is set)
[[ -f "${SHELL_INTEGRATION_SCRIPT}" ]] && source "${SHELL_INTEGRATION_SCRIPT}"
${PENGWIN_SHELL_INTEGRATION_MARKER}
EOF

echo "Windows Terminal shell integration installed successfully."
enable_should_restart
message --title "Shell Integration Installed" --msgbox "Windows Terminal shell integration has been installed.\n\nScript location: ${SHELL_INTEGRATION_SCRIPT}\nSource added to: ${bashrc}\n\nPlease close and re-open Pengwin to apply the changes.\n\nThis provides:\n- Command marks for scroll-to-command\n- Current working directory tracking\n- Command exit status reporting" 16 80
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot put the flag to restart the distro. this is another thing that i need constantly repeating. if the installer needs a reload of the environment like source ~/.bashrc, use the function in commons.sh to signal a distro restart

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c5e2e15. Added enable_should_restart call after successful installation.


if (confirm --title "Windows Terminal Shell Integration" --yesno "Would you like to install Windows Terminal shell integration?\n\nThis adds special escape sequences to your bash prompt that enable:\n- Scroll to command feature in Windows Terminal\n- Current working directory tracking\n- Command exit status tracking\n\nThe changes will be added to ~/.bashrc with markers for easy removal." 15 80); then
install_shell_integration
else
echo "Skipping Windows Terminal shell integration"
fi
8 changes: 7 additions & 1 deletion pengwin-setup.d/shells.sh
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,11 @@ function installAndSetShell() {
# shellcheck disable=SC2155
local menu_choice=$(

menu --title "Shell Menu" --menu "Custom shells and improvements (bash included)\n[ENTER to confirm]:" 14 65 4 \
menu --title "Shell Menu" --menu "Custom shells and improvements (bash included)\n[ENTER to confirm]:" 15 70 5 \
"BASH-RL" "Recommended readline settings for productivity " \
"CSH" "csh" \
"FISH" "fish with oh-my-fish plugin manager" \
"SHELLINT" "Windows Terminal shell integration for bash" \
"ZSH" "zsh"

# shellcheck disable=SC2188
Expand Down Expand Up @@ -205,6 +206,11 @@ function installAndSetShell() {
bash "${SetupDir}"/shell-opts.sh "$@"
fi

if [[ $menu_choice == *"SHELLINT"* ]]; then
echo "SHELLINT"
bash "${SetupDir}"/shell-integration.sh "$@"
fi

}

installAndSetShell "$@"
6 changes: 6 additions & 0 deletions pengwin-setup.d/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function main() {
"RCLOCAL" "Remove rclocal support (the file /etc/rc.local) is kept" ${OFF} \
"RUBY" "Remove rbenv, Ruby version(s) and Rails (if installed)" ${OFF} \
"RUST" "Remove Rust and rustup toolchain installer" ${OFF} \
"SHELLINT" "Remove Windows Terminal shell integration" ${OFF} \
"STARTMENU" "Remove all Pengwin generated Windows Start Menu shortcuts" ${OFF} \
"SSH" "Remove SSH server" ${OFF} \
"SYNAPTIC" "Remove Synaptic package manager" ${OFF} \
Expand Down Expand Up @@ -266,6 +267,11 @@ function main() {
bash "${UninstallDir}"/rust.sh "$@"
fi

if [[ ${menu_choice} == *"SHELLINT"* ]]; then
echo "SHELLINT"
bash "${UninstallDir}"/shell-integration.sh "$@"
fi

if [[ ${menu_choice} == *"STARTMENU"* ]]; then
echo "STARTMENU"
bash "${UninstallDir}"/shortcut.sh "$@"
Expand Down
51 changes: 51 additions & 0 deletions pengwin-setup.d/uninstall/shell-integration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

# shellcheck source=./uninstall-common.sh
source "$(dirname "$0")/uninstall-common.sh" "$@"

#Imported from uninstall-common.sh
declare HOME

# We need to delete everything between 2 instances of this string
readonly PENGWIN_SHELL_INTEGRATION_MARKER='### PENGWIN WINDOWS TERMINAL SHELL INTEGRATION'
readonly SHELL_INTEGRATION_DIR='/usr/local/share/pengwin'
readonly SHELL_INTEGRATION_SCRIPT="${SHELL_INTEGRATION_DIR}/wt-shell-integration.sh"

#######################################
# Uninstall Windows Terminal shell integration
# Removes the source line from ~/.bashrc and the script from /usr/local/share/pengwin
# Globals:
# HOME
# PENGWIN_SHELL_INTEGRATION_MARKER
# SHELL_INTEGRATION_SCRIPT
# Arguments:
# None
#######################################
function main() {
echo "Uninstalling Windows Terminal shell integration"

local bashrc="${HOME}/.bashrc"

# Remove source line from .bashrc
echo "Cleaning ${bashrc}"
if [[ -f "${bashrc}" ]]; then
if grep -q "${PENGWIN_SHELL_INTEGRATION_MARKER}" "${bashrc}" 2>/dev/null; then
inclusive_file_clean "${bashrc}" "${PENGWIN_SHELL_INTEGRATION_MARKER}"
echo "Source line removed from ${bashrc}"
else
echo "... shell integration not found in ${bashrc}!"
fi
else
echo "... ${bashrc} not found!"
fi

# Remove the shell integration script
echo "Removing ${SHELL_INTEGRATION_SCRIPT}"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot use sudo_rem_file for that also save it into the instructions for the repo or your memory because i had to tell it more than once, priorize using the helper functions from uninstall-common.sh

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c5e2e15. Now using sudo_rem_file from uninstall-common.sh.

sudo_rem_file "${SHELL_INTEGRATION_SCRIPT}"

echo "Windows Terminal shell integration uninstalled successfully."
}

if show_warning "Windows Terminal shell integration" "$@"; then
main "$@"
fi
2 changes: 2 additions & 0 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ if [ -z "${CIRCLE_NODE_TOTAL}" ]; then
run_test ./motd_settings.sh
run_test ./kubernetes.sh
run_test ./latex.sh
run_test ./shell-integration.sh
elif [[ ${CIRCLE_NODE_INDEX} == $((i++)) ]]; then #0
run_test ./nodejs_nvm.sh
run_test ./motd_settings.sh
run_test ./shell-integration.sh
elif [[ ${CIRCLE_NODE_INDEX} == $((i++)) ]]; then #1
run_test ./desktop.sh
run_test ./lamp.sh "BUILTIN"
Expand Down
60 changes: 60 additions & 0 deletions tests/shell-integration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

source commons.sh

declare TEST_USER

readonly PENGWIN_SHELL_INTEGRATION_MARKER='### PENGWIN WINDOWS TERMINAL SHELL INTEGRATION'
readonly SHELL_INTEGRATION_SCRIPT='/usr/local/share/pengwin/wt-shell-integration.sh'

function test_main() {
run_pengwinsetup autoinstall SETTINGS SHELLS SHELLINT

# Check that the source line was added to .bashrc
local bashrc="/home/${TEST_USER}/.bashrc"

test -f "${bashrc}"
assertEquals ".bashrc file should exist" "0" "$?"

grep -q "${PENGWIN_SHELL_INTEGRATION_MARKER}" "${bashrc}"
assertEquals "Shell integration marker should be present in .bashrc" "0" "$?"

grep -q "wt-shell-integration.sh" "${bashrc}"
assertEquals "Source line for shell integration script should be present in .bashrc" "0" "$?"

# Check that the script was installed to /usr/local/share/pengwin
test -f "${SHELL_INTEGRATION_SCRIPT}"
assertEquals "Shell integration script should exist" "0" "$?"

grep -q "__wt_update_prompt" "${SHELL_INTEGRATION_SCRIPT}"
assertEquals "Shell integration function should be present in script" "0" "$?"

grep -q "PS0=" "${SHELL_INTEGRATION_SCRIPT}"
assertEquals "PS0 should be set for command executed mark" "0" "$?"

grep -q "WT_SESSION" "${SHELL_INTEGRATION_SCRIPT}"
assertEquals "WT_SESSION check should be present in script" "0" "$?"
}

function test_uninstall() {
# First ensure it's installed
run_pengwinsetup autoinstall SETTINGS SHELLS SHELLINT

# Then uninstall
run_pengwinsetup autoinstall UNINSTALL SHELLINT

local bashrc="/home/${TEST_USER}/.bashrc"

# Verify the source line was removed from .bashrc
if [[ -f "${bashrc}" ]]; then
grep -q "${PENGWIN_SHELL_INTEGRATION_MARKER}" "${bashrc}"
assertNotEquals "Shell integration marker should NOT be present after uninstall" "0" "$?"
fi

# Verify the script was removed
test -f "${SHELL_INTEGRATION_SCRIPT}"
assertNotEquals "Shell integration script should NOT exist after uninstall" "0" "$?"
}

# shellcheck disable=SC1091
source shunit2