mirror of
https://github.com/docker/compose.git
synced 2026-11-18 06:07:53 +00:00
The TTY renderer accumulated display-corruption fixes (truncation of details, then progress sizes, then task ids; timer alignment; rune-based measurement) that each patched one symptom of the same structural gap: nothing guaranteed a rendered line fits the terminal, and once a line wraps, cursor arithmetic desyncs and the block corrupts. Replace it with three separable units: - tty_model.go: pure event reducer with an injected clock, preserving first-parent-wins updates, monotonic progress and header counters - tty_layout.go: pure (model, size, now) -> lines function; all widths are measured in terminal cells (go-runewidth, so CJK is correct) on plain text before coloring, and every line is clipped to the terminal width by construction, status text included - tty_screen.go: diff-based repaint; unchanged rows are skipped, identical frames write nothing, a frame is a single Write; a shrinking terminal abandons the block instead of moving the cursor over reflowed rows The writer coordinates them behind a mutex and stops the refresh goroutine through context cancellation, so Done cannot block when the operation context was cancelled first (Ctrl-C during pull). The spinner frame is derived from the clock instead of advancing on every call, and truncation can no longer split multi-byte runes. Visual output is unchanged: the snapshot test reproduces the previous renderer's golden output character for character. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
88 lines
2.6 KiB
Go
88 lines
2.6 KiB
Go
/*
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package display
|
|
|
|
import (
|
|
"io"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/morikuni/aec"
|
|
)
|
|
|
|
// screen owns the terminal region the progress block is painted on.
|
|
//
|
|
// It relies on a single contract with the layout: every line fits the
|
|
// terminal width, so one string is one terminal row and the cursor can move
|
|
// back over the block with plain relative moves. Repainting diffs against
|
|
// the previous frame: unchanged rows are skipped (a bare newline), changed
|
|
// rows are erased and rewritten, leftover rows from a taller previous frame
|
|
// are blanked. Each frame is flushed as a single Write.
|
|
type screen struct {
|
|
out io.Writer
|
|
prev []string
|
|
prevWidth int
|
|
}
|
|
|
|
// reset forgets the painted block: the next paint starts fresh at the
|
|
// current cursor position. Used when foreign output (buildkit, a resize
|
|
// reflow) may have invalidated our notion of where the block lives.
|
|
func (s *screen) reset() {
|
|
s.prev = nil
|
|
s.prevWidth = 0
|
|
}
|
|
|
|
func (s *screen) paint(rows []string, width int) {
|
|
if width < s.prevWidth {
|
|
// The terminal shrank: previously painted rows may have wrapped and
|
|
// the terminal may have reflowed them, so cursor arithmetic against
|
|
// the old block is meaningless. Leave it behind and start a new one.
|
|
s.reset()
|
|
}
|
|
redrawAll := width != s.prevWidth
|
|
if !redrawAll && slices.Equal(rows, s.prev) {
|
|
return // nothing changed, write nothing
|
|
}
|
|
|
|
var b strings.Builder
|
|
b.WriteString(aec.Hide.String())
|
|
if n := len(s.prev); n > 0 {
|
|
b.WriteString(aec.Up(uint(n)).String())
|
|
}
|
|
b.WriteString(aec.Column(0).String())
|
|
for i, row := range rows {
|
|
if !redrawAll && i < len(s.prev) && s.prev[i] == row {
|
|
b.WriteString("\n")
|
|
continue
|
|
}
|
|
b.WriteString(aec.EraseLine(aec.EraseModes.All).String())
|
|
b.WriteString(row)
|
|
b.WriteString("\n")
|
|
}
|
|
if extra := len(s.prev) - len(rows); extra > 0 {
|
|
for range extra {
|
|
b.WriteString(aec.EraseLine(aec.EraseModes.All).String())
|
|
b.WriteString("\n")
|
|
}
|
|
b.WriteString(aec.Up(uint(extra)).String())
|
|
}
|
|
b.WriteString(aec.Show.String())
|
|
_, _ = io.WriteString(s.out, b.String())
|
|
|
|
s.prev = rows
|
|
s.prevWidth = width
|
|
}
|