waveterm/pkg/util/shellutil
Copilot 2619c85d03
Add OSC 7 support for Fish and PowerShell shells (#2456)
## Overview

This PR implements OSC 7 (current working directory reporting) support
for Fish shell and PowerShell, completing the shell integration coverage
alongside the existing Bash and Zsh implementations added in #2444.

## What is OSC 7?

OSC 7 is an operating system command that allows shells to automatically
report their current working directory to the terminal emulator using
the format:
```
ESC]7;file://hostname/path BEL
```

This enables the terminal to track the current directory without manual
commands, providing better context for AI features and ensuring accurate
path information.

## Implementation Details

### Fish Shell (`fish_wavefish.sh`)

Added shell integration functions using Fish-native features:
- `_waveterm_si_blocked()` - Prevents OSC 7 in tmux/screen environments
using `set -q` and `string match -q`
- `_waveterm_si_osc7()` - Sends the OSC 7 sequence with built-in URL
encoding
- Uses `string escape --style=url` for UTF-8 percent-encoding
- Hooked to `fish_prompt` event and `PWD` variable changes for automatic
updates

**Key Features:**
- Fish-native checks (`set -q`, `string match`) instead of non-portable
`test -o`
- Built-in `string escape --style=url` for proper UTF-8 percent-encoding
- Simple, portable, and maintainable (32 lines total)

### PowerShell (`pwsh_wavepwsh.sh`)

Added simplified shell integration that leverages frontend
normalization:
- `_waveterm_si_blocked()` - Prevents OSC 7 in tmux/screen environments
- `_waveterm_si_osc7()` - Sends OSC 7 with raw path encoding
- Uses `[System.Uri]::EscapeDataString()` for proper percent-encoding
- Integrated into the prompt function while preserving existing prompts

**Key Features:**
- **No path rewriting**: Sends raw paths (e.g., `C:\Users\Name` →
`C%3A%5CUsers%5CName`)
- **UNC support**: Network paths like `\\server\share` encoded as
`%5C%5Cserver%5Cshare`
- **Hostname fallback**: `$env:COMPUTERNAME` → `$env:HOSTNAME` → empty
(produces `file:///path`)
- **No DNS lookup**: Avoids potentially slow DNS calls
- Simple and efficient (53 lines total)

## Implementation Benefits

Both implementations use native, built-in features for maximum
compatibility and maintainability:

**Fish:**
-  Replaced `test -o` with fish-native `set -q` and `string match -q`
for better portability
-  Replaced manual string replacements with `string escape --style=url`
for proper UTF-8 support
-  Removed custom URL encoding function (19 lines removed)

**PowerShell:**
-  Uses built-in `[System.Uri]::EscapeDataString()` for proper
percent-encoding
-  No path rewriting - frontend handles normalization via
`decodeURIComponent()` and backslash conversion
-  Proper UNC path support without special-casing
-  Removed DNS lookup to avoid slow operations
-  Removed custom URL encoding function

## Testing

All implementations were tested for:
-  URL encoding of special characters (spaces, #, ?, &, ;, +, %)
-  Correct OSC 7 format generation
-  Fish-native checks work correctly (TMUX, STY, TERM patterns)
-  Raw path encoding (PowerShell - no rewriting)
-  UNC path support (PowerShell)
-  Unix path handling
-  Go package compilation
-  No security issues (CodeQL)

## Path Handling Examples

**Fish:**
- `/home/user` → `/home/user`
- `/path with spaces` → `/path%20with%20spaces`
- `/file#hash` → `/file%23hash`

**PowerShell:**
- Windows: `C:\Users\Name` → `file://HOST/C%3A%5CUsers%5CName`
- UNC: `\\server\share\folder` →
`file://HOST/%5C%5Cserver%5Cshare%5Cfolder`
- Empty hostname: produces `file:///path` format

## Files Changed

- `pkg/util/shellutil/shellintegration/fish_wavefish.sh` (+22 lines, -18
lines = net +4 lines, but 19 lines of custom code removed)
- `pkg/util/shellutil/shellintegration/pwsh_wavepwsh.sh` (+26 lines, -51
lines = net -25 lines)

Total: 38 fewer lines of code with better functionality and
maintainability.

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sawka <2722291+sawka@users.noreply.github.com>
2025-10-17 17:23:08 -07:00
..
shellintegration Add OSC 7 support for Fish and PowerShell shells (#2456) 2025-10-17 17:23:08 -07:00
shellquote.go Handle integer overflow cases in shellquote (#1822) 2025-01-23 18:18:44 -08:00
shellquote_test.go move genconn quote, and getshelltype to shellutil (#1731) 2025-01-14 15:29:36 -08:00
shellutil.go working on more terminal context (#2444) 2025-10-17 12:19:40 -07:00
tokenswap.go add jwt token back to wsl connections (#1841) 2025-01-24 14:24:15 -08:00