-
Notifications
You must be signed in to change notification settings - Fork 774
Fixes #5193. Emit OSC 8 hyperlink sequences in Driver.ToAnsi() #5195
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9fd3c0e
Initial plan
Copilot 9e50d85
Emit OSC 8 hyperlink sequences in BuildAnsiForRegion for ToAnsi() output
Copilot 2e76d09
Potential fix for pull request finding
tig e8b580e
Add fast-path in GetCellUrl to skip lock when no URLs exist, add row-…
Copilot 95eab44
Fix thread-safety: re-check _urlMap inside lock after fast-path null …
Copilot 9c767d6
Merge branch 'develop' into copilot/add-osc8-hyperlink-sequences
tig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # Plan: Emit OSC 8 Hyperlink Sequences in `ToAnsi()` | ||
|
|
||
| ## Problem Statement | ||
|
|
||
| When `Driver.ToAnsi()` is called (e.g., for print mode in `clet --help` or `mdv --print`), the resulting ANSI string includes SGR styling (underline, color) for links but does NOT include [OSC 8 hyperlink sequences](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda). This means links are visually styled but not clickable in terminals that support OSC 8. | ||
|
|
||
| ## Existing Infrastructure | ||
|
|
||
| The codebase already has all the pieces: | ||
|
|
||
| 1. **Cell-level URL storage**: `IOutputBuffer.GetCellUrl(col, row)` returns the URL associated with a cell. `OutputBufferImpl` stores these in a `_urlMap` dictionary keyed by `Point(col, row)`. | ||
|
|
||
| 2. **URL assignment during draw**: Both `Link` view and `Markdown` view set `Driver.CurrentUrl` before drawing cells. The `OutputBufferImpl.AddStr`/`AddRune` methods call `SetCellUrl` when `CurrentUrl` is non-null. | ||
|
|
||
| 3. **OSC 8 utilities**: `EscSeqUtils.OSC_StartHyperlink(url)` and `EscSeqUtils.OSC_EndHyperlink()` already exist. | ||
|
|
||
| 4. **Real-time rendering already works**: `OutputBase.Write(IOutputBuffer)` already queries `buffer.GetCellUrl(col, row)` and emits OSC 8 sequences during live terminal output. | ||
|
|
||
| 5. **`ToAnsi()` does NOT emit OSC 8**: `BuildAnsiForRegion` (called by `ToAnsi`) only handles SGR attribute changes and graphemes — it has no URL tracking. | ||
|
|
||
| ## Design | ||
|
|
||
| ### Approach | ||
|
|
||
| Modify `BuildAnsiForRegion` in `OutputBase.cs` to track the current URL state and emit OSC 8 open/close sequences when the URL changes between cells. | ||
|
|
||
| ### Implementation | ||
|
|
||
| In `BuildAnsiForRegion`, add a `string? lastUrl = null` tracker. For each cell: | ||
| 1. Query `buffer.GetCellUrl(col, row)` to get the cell's URL | ||
| 2. If the URL differs from `lastUrl`: | ||
| - If `lastUrl` was non-null, emit `EscSeqUtils.OSC_EndHyperlink()` to close the previous link | ||
| - If the new URL is non-null, emit `EscSeqUtils.OSC_StartHyperlink(url)` to open the new link | ||
| - Update `lastUrl` | ||
| 3. After the loop completes (or at end of each row), if `lastUrl` is non-null, emit `EscSeqUtils.OSC_EndHyperlink()` | ||
|
|
||
| ### Files Changed | ||
|
|
||
| - `Terminal.Gui/Drivers/Output/OutputBase.cs` — `BuildAnsiForRegion` method | ||
|
|
||
| ## Unit Tests | ||
|
|
||
| Tests in `Tests/UnitTestsParallelizable/Drivers/Output/OutputBaseTests.cs`: | ||
|
|
||
| 1. **`ToAnsi_CellsWithUrl_EmitsOsc8Sequences`**: Create a buffer, set `CurrentUrl`, write text, verify `ToAnsi()` output contains OSC 8 start/end sequences. | ||
|
|
||
| 2. **`ToAnsi_CellsWithDifferentUrls_EmitsCorrectTransitions`**: Verify that transitioning between different URLs properly closes the first and opens the second. | ||
|
|
||
| 3. **`ToAnsi_CellsWithUrl_ThenNoUrl_ClosesHyperlink`**: Verify that when URL cells are followed by non-URL cells, the hyperlink is properly closed. | ||
|
|
||
| 4. **`ToAnsi_LegacyConsole_NoOsc8`**: Verify that legacy console mode does not emit OSC 8. | ||
|
|
||
| ## Verification | ||
|
|
||
| - Existing `LinkTests.Link_Renders_With_OSC8_Hyperlink` test already verifies OSC 8 in live rendering | ||
| - New tests verify OSC 8 in `ToAnsi()` output specifically | ||
| - Run `MarkdownViewTests` to ensure no regressions |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.