-
Notifications
You must be signed in to change notification settings - Fork 408
fix(tui): stabilize dynamic-height UI components to reduce jitter #1365
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: main
Are you sure you want to change the base?
Changes from all commits
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,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Fix visible jitter when dynamic-height UI components collapse during rendering. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import type { Component } from '@moonshot-ai/pi-tui'; | ||
|
|
||
| export interface FixedHeightWindowOptions { | ||
| height: number; | ||
| lines?: string[]; | ||
| tail?: boolean; // default true | ||
| } | ||
|
|
||
| export class FixedHeightWindow implements Component { | ||
| private lines: string[]; | ||
| private readonly height: number; | ||
| private readonly tail: boolean; | ||
|
|
||
| constructor(opts: FixedHeightWindowOptions) { | ||
| this.height = Math.max(0, opts.height); | ||
| this.tail = opts.tail ?? true; | ||
| this.lines = opts.lines ?? []; | ||
| } | ||
|
|
||
| setLines(lines: string[]): void { | ||
| this.lines = lines; | ||
| } | ||
|
|
||
| invalidate(): void {} | ||
|
|
||
| render(_width: number): string[] { | ||
| if (this.height === 0) return []; | ||
| const src = this.lines; | ||
| let shown: string[]; | ||
| if (src.length > this.height) { | ||
| shown = this.tail ? src.slice(src.length - this.height) : src.slice(0, this.height); | ||
| } else { | ||
| shown = [...src]; | ||
| } | ||
| while (shown.length < this.height) shown.push(''); | ||
| return shown; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ import { currentTheme } from '#/tui/theme'; | |
|
|
||
| import { formatBashOutputForDisplay, sanitizeShellOutput } from '#/tui/utils/shell-output'; | ||
|
|
||
| import { FixedHeightWindow } from './fixed-height-window'; | ||
|
|
||
| const RUNNING_TAIL_LINES = 5; | ||
| const TIMER_INTERVAL_MS = 1000; | ||
| // Cap the live running buffer so a command that spews output for minutes can't | ||
|
|
@@ -105,26 +107,47 @@ export class ShellRunComponent extends Container { | |
| if (this.backgrounded) { | ||
| return ` ${currentTheme.fg('textDim', 'Moved to background.')}`; | ||
| } | ||
| const elapsed = Math.floor((Date.now() - this.startedAt) / 1000); | ||
| const dim = (s: string): string => currentTheme.fg('textDim', s); | ||
|
|
||
| if (!this.running) { | ||
| return formatBashOutputForDisplay(this.finalStdout, this.finalStderr, this.finalIsError) | ||
| .split('\n') | ||
| // Finished: show the tail of the final output in the same fixed | ||
| // window as the running view so the card height does not change. | ||
| const allLines = formatBashOutputForDisplay( | ||
| this.finalStdout, | ||
| this.finalStderr, | ||
| this.finalIsError, | ||
| ).split('\n'); | ||
| const window = new FixedHeightWindow({ | ||
| height: RUNNING_TAIL_LINES, | ||
| tail: true, | ||
| lines: allLines, | ||
|
Comment on lines
+121
to
+124
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.
When a Useful? React with 👍 / 👎. |
||
| }); | ||
| const body = window | ||
| .render(80) | ||
| .map((line) => ` ${line}`) | ||
| .join('\n'); | ||
| const hidden = Math.max(0, allLines.length - RUNNING_TAIL_LINES); | ||
| const status = ` ${dim( | ||
| `completed${hidden > 0 ? ` · +${String(hidden)} lines` : ''} · ctrl+o to expand`, | ||
| )}`; | ||
| return `${body}\n${status}\n `; | ||
| } | ||
| const elapsed = Math.floor((Date.now() - this.startedAt) / 1000); | ||
| const dim = (s: string): string => currentTheme.fg('textDim', s); | ||
|
|
||
| // Running: dim tail of the combined output + timing + hint. | ||
| const trimmed = sanitizeShellOutput(this.combined).trimEnd(); | ||
| let body: string; | ||
| let extra = 0; | ||
| if (trimmed.length === 0) { | ||
| body = ` ${dim('Running…')}`; | ||
| } else { | ||
| const lines = trimmed.split('\n'); | ||
| const tail = lines.slice(-RUNNING_TAIL_LINES); | ||
| extra = Math.max(0, lines.length - RUNNING_TAIL_LINES); | ||
| body = tail.map((line) => ` ${dim(line)}`).join('\n'); | ||
| } | ||
| const timing = ` ${dim(`${extra > 0 ? `+${extra} lines ` : ''}(${elapsed}s)`)}`; | ||
| const allLines = trimmed.length === 0 ? ['Running…'] : trimmed.split('\n'); | ||
| const window = new FixedHeightWindow({ | ||
| height: RUNNING_TAIL_LINES, | ||
| tail: true, | ||
| lines: allLines, | ||
| }); | ||
| const body = window | ||
| .render(80) | ||
| .map((line) => ` ${dim(line)}`) | ||
| .join('\n'); | ||
| const extra = Math.max(0, allLines.length - RUNNING_TAIL_LINES); | ||
| const timing = ` ${dim(`${extra > 0 ? `+${String(extra)} lines ` : ''}(${String(elapsed)}s)`)}`; | ||
| const hint = ` ${dim('(ctrl+b to run in background)')}`; | ||
| return `${body}\n${timing}\n${hint}`; | ||
| } catch { | ||
|
|
||
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.
This call runs from inside
CustomEditor.render(), whileTUI.doRender()is already rendering the frame.requestRender(true)mutates the TUI diff state synchronously (clearingpreviousLinesand changing previous dimensions), so on the ordinary autocomplete-close path the current render can take the first-render/full-frame path without clearing and then overwrite the scheduled forced render state, leaving the intended cleanup ineffective or duplicating the UI. Trigger the forced redraw from the close/input path after the current render instead of mutating TUI state during render.Useful? React with 👍 / 👎.