-
Notifications
You must be signed in to change notification settings - Fork 16
Add Windows Terminal shell integration installer #449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from all commits
a87eff4
baf2479
ace9ef0
d91a9f8
f562e00
c74cfcd
71a9a7b
c5e2e15
0178160
e5e903f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } | ||
|
|
||
| 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 | ||
| 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}" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in c5e2e15. Now using |
||
| sudo_rem_file "${SHELL_INTEGRATION_SCRIPT}" | ||
|
|
||
| echo "Windows Terminal shell integration uninstalled successfully." | ||
| } | ||
|
|
||
| if show_warning "Windows Terminal shell integration" "$@"; then | ||
| main "$@" | ||
| fi | ||
| 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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_restartcall after successful installation.