Repository navigation
fix(mobile): optional toggle to dim agent directory-listing size tokens #10603
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
Open
innocarpe
wants to merge
2
commits into
stablyai:main
Choose a base branch
from
innocarpe:fix/mobile-dim-dir-listing-sizes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
117 changes: 117 additions & 0 deletions
117
mobile/src/terminal/terminal-dir-listing-size-dim-injected.ts
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,117 @@ | ||
| // Visual-only dim of trailing directory-listing size tokens inside the terminal | ||
| // WebView. Injected into XTERM_HTML; mirrors matchTrailingDirListingSize in | ||
| // terminal-dir-listing-size-dim.ts (unit-tested source of truth). | ||
| // | ||
| // Decorations overlay the size cells with the terminal background at partial | ||
| // opacity so the buffer, selection, and copy stay byte-identical to the PTY. | ||
|
|
||
| export const TERMINAL_DIR_LISTING_SIZE_DIM_JS = String.raw` | ||
| var dimDirListingSizesEnabled = false; | ||
| var sizeDimDecorations = []; | ||
| var sizeDimRefreshScheduled = false; | ||
| var SIZE_TOKEN_RE = /(\d+(?:\.\d+)?)([KMGT]B?|B)\s*$/i; | ||
|
|
||
| function matchTrailingDirListingSize(lineText) { | ||
| if (!lineText) return null; | ||
| var match = SIZE_TOKEN_RE.exec(lineText); | ||
| if (!match || match.index === undefined) return null; | ||
| var tokenStart = match.index; | ||
| if (tokenStart === 0) return null; | ||
| if (!/\s/.test(lineText.charAt(tokenStart - 1))) return null; | ||
| var prefixEnd = tokenStart; | ||
| while (prefixEnd > 0 && /\s/.test(lineText.charAt(prefixEnd - 1))) prefixEnd -= 1; | ||
| if (prefixEnd === 0) return null; | ||
| var token = match[1] + match[2]; | ||
| return { start: tokenStart, end: tokenStart + token.length }; | ||
| } | ||
|
|
||
| function stringIndexToCellCol(line, stringIndex) { | ||
| if (!line || stringIndex <= 0) return 0; | ||
| var consumed = 0; | ||
| for (var col = 0; col < line.length; col++) { | ||
| if (consumed >= stringIndex) return col; | ||
| var cell = line.getCell(col); | ||
| if (!cell) return col; | ||
| var chars = cell.getChars(); | ||
| if (chars) consumed += chars.length; | ||
| } | ||
| return line.length; | ||
| } | ||
|
|
||
| function clearSizeDimDecorations() { | ||
| for (var i = 0; i < sizeDimDecorations.length; i++) { | ||
| // Why: IDecoration.dispose leaves the linked IMarker alive in the buffer. | ||
| try { sizeDimDecorations[i].marker.dispose(); } catch (e) {} | ||
| try { sizeDimDecorations[i].dispose(); } catch (e) {} | ||
| } | ||
| sizeDimDecorations = []; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| function refreshSizeDimDecorations() { | ||
| sizeDimRefreshScheduled = false; | ||
| clearSizeDimDecorations(); | ||
| if (!dimDirListingSizesEnabled || !term || typeof term.registerMarker !== 'function') return; | ||
| var buf = term.buffer.active; | ||
| var baseY = buf.baseY; | ||
| var cursorY = buf.cursorY; | ||
| // Why: body.style.background is empty when the theme is only set via xterm options. | ||
| var bg = (typeof terminalTheme !== 'undefined' && terminalTheme && terminalTheme.background) || | ||
| (typeof defaultTheme !== 'undefined' && defaultTheme.background) || | ||
| '#1a1b26'; | ||
| for (var row = 0; row < term.rows; row++) { | ||
| var absRow = baseY + row; | ||
| var line = buf.getLine(absRow); | ||
| if (!line) continue; | ||
| var text = line.translateToString(false); | ||
| var range = matchTrailingDirListingSize(text); | ||
| if (!range) continue; | ||
| var startCol = stringIndexToCellCol(line, range.start); | ||
| var endCol = stringIndexToCellCol(line, range.end); | ||
| var width = Math.max(1, endCol - startCol); | ||
| var marker; | ||
| try { | ||
| marker = term.registerMarker(row - cursorY); | ||
| } catch (e) { | ||
| continue; | ||
| } | ||
| if (!marker) continue; | ||
| var decoration; | ||
| try { | ||
| decoration = term.registerDecoration({ | ||
| marker: marker, | ||
| x: startCol, | ||
| width: width, | ||
| layer: 'top' | ||
| }); | ||
| } catch (e) { | ||
| try { marker.dispose(); } catch (e2) {} | ||
| continue; | ||
| } | ||
| if (!decoration) { | ||
| try { marker.dispose(); } catch (e) {} | ||
| continue; | ||
| } | ||
| decoration.onRender(function (element) { | ||
| element.style.background = bg; | ||
| element.style.opacity = '0.62'; | ||
| element.style.pointerEvents = 'none'; | ||
| }); | ||
| sizeDimDecorations.push(decoration); | ||
| } | ||
| } | ||
|
|
||
| function scheduleSizeDimRefresh() { | ||
| if (!dimDirListingSizesEnabled || sizeDimRefreshScheduled) return; | ||
| sizeDimRefreshScheduled = true; | ||
| requestAnimationFrame(refreshSizeDimDecorations); | ||
| } | ||
|
|
||
| function setDimDirListingSizes(enabled) { | ||
| dimDirListingSizesEnabled = !!enabled; | ||
| if (!dimDirListingSizesEnabled) { | ||
| clearSizeDimDecorations(); | ||
| return; | ||
| } | ||
| scheduleSizeDimRefresh(); | ||
| } | ||
| ` | ||
47 changes: 47 additions & 0 deletions
47
mobile/src/terminal/terminal-dir-listing-size-dim-wiring.test.ts
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,47 @@ | ||
| import { readFileSync } from 'node:fs' | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| const injectedSource = readFileSync( | ||
| new URL('./terminal-dir-listing-size-dim-injected.ts', import.meta.url), | ||
| 'utf8' | ||
| ) | ||
| const webViewSource = readFileSync(new URL('./TerminalWebView.tsx', import.meta.url), 'utf8') | ||
| const htmlSource = readFileSync(new URL('./terminal-webview-html.ts', import.meta.url), 'utf8') | ||
|
|
||
| describe('dir-listing size dim wiring', () => { | ||
| it('disposes the association marker when clearing decorations', () => { | ||
| expect(injectedSource).toMatch( | ||
| /try\s*\{\s*sizeDimDecorations\[i\]\.marker\.dispose\(\);\s*\}\s*catch/ | ||
| ) | ||
| expect(injectedSource).toMatch(/try\s*\{\s*sizeDimDecorations\[i\]\.dispose\(\);\s*\}\s*catch/) | ||
| }) | ||
|
|
||
| it('tints size-dim overlays from terminalTheme.background', () => { | ||
| expect(injectedSource).toMatch(/terminalTheme\.background/) | ||
| expect(injectedSource).not.toMatch(/document\.body\.style\.background/) | ||
| }) | ||
|
|
||
| it('refreshes size-dim decorations after set-theme', () => { | ||
| const setThemeIdx = htmlSource.indexOf("msg.type === 'set-theme'") | ||
| expect(setThemeIdx).toBeGreaterThanOrEqual(0) | ||
| const nextBranch = htmlSource.indexOf("} else if (msg.type ===", setThemeIdx + 1) | ||
| const setThemeBlock = htmlSource.slice(setThemeIdx, nextBranch > setThemeIdx ? nextBranch : undefined) | ||
| expect(setThemeBlock).toContain('scheduleSizeDimRefresh') | ||
| }) | ||
|
|
||
| it('subscribes before load so a live toggle is not overridden by a stale initial read', () => { | ||
| const effectStart = webViewSource.indexOf( | ||
| 'settings toggle must dim already-mounted terminals without a reload' | ||
| ) | ||
| expect(effectStart).toBeGreaterThanOrEqual(0) | ||
| const effectEnd = webViewSource.indexOf('}, [postMessage])', effectStart) | ||
| expect(effectEnd).toBeGreaterThan(effectStart) | ||
| const effect = webViewSource.slice(effectStart, effectEnd) | ||
| const subscribeIdx = effect.indexOf('subscribeTerminalDimDirListingSizes') | ||
| const loadIdx = effect.indexOf('loadTerminalDimDirListingSizes') | ||
| expect(subscribeIdx).toBeGreaterThanOrEqual(0) | ||
| expect(loadIdx).toBeGreaterThan(subscribeIdx) | ||
| expect(effect).toContain('receivedSubscriptionUpdate') | ||
| expect(effect).toMatch(/!cancelled\s*&&\s*!receivedSubscriptionUpdate/) | ||
| }) | ||
| }) |
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.