diff --git a/pkg/board.go b/pkg/board.go index 5e12c62..0a4ca5e 100644 --- a/pkg/board.go +++ b/pkg/board.go @@ -484,19 +484,30 @@ func (b *BoardView) Draw(screen tc.Screen) { queueImages(pieceSize, currentPieceSet(), images) queueOverlay(overlay, key) } + // Stepping back through the moves (not in studies, which browse): a + // banner on the row above the board, to the right of the top clock if + // there is one, so that clock (often the one running) stays in sight. + topWidth := 8 * sqW if b.back > 0 && !b.browse && b.oy > y { - label := b.historyLabel() - lx := b.ox + 8*sqW - len([]rune(label)) + room := topWidth + if bars && b.topBar.shown() { + room -= clockRoom + } + label := []rune(b.historyLabel(room)) + lx := b.ox + topWidth - len(label) for i, r := range label { screen.SetContent(lx+i, b.oy-1, r, nil, tc.StyleDefault.Foreground(tc.ColorBlack).Background(tintLastMove)) } + topWidth -= len(label) + 1 } if bars { bg := b.GetBackgroundColor() - if b.back == 0 || b.browse { // the history banner uses this row (not in studies) - drawClockBar(screen, b.ox, b.oy-1, 8*sqW, b.topBar, style == styleBasic, bg) + top := b.topBar + if topWidth < 8*sqW { + top.right = "" // the banner has that end } + drawClockBar(screen, b.ox, b.oy-1, topWidth, top, style == styleBasic, bg) drawClockBar(screen, b.ox, b.oy+8*sqH+labelH, 8*sqW, b.bottomBar, style == styleBasic, bg) } labelStyle := tc.StyleDefault.Foreground(colLabel).Background(b.GetBackgroundColor()) @@ -508,6 +519,10 @@ func (b *BoardView) Draw(screen tc.Screen) { } } +// clockRoom is the cells a clock keeps beside the history banner: its time +// (up to " 1:00:00 ") and a little of its bar. +const clockRoom = 16 + // squareLook is what one square shows. type squareLook struct { bg, fg tc.Color diff --git a/pkg/board_extras.go b/pkg/board_extras.go index 36a6a0e..c602803 100644 --- a/pkg/board_extras.go +++ b/pkg/board_extras.go @@ -158,10 +158,21 @@ func (b *BoardView) notifyShown() { go Root.App.QueueUpdateDraw(func() { f(ply) }) } -// historyLabel describes the position shown while stepping back. -func (b *BoardView) historyLabel() string { - i := len(b.history) - 1 - b.back - return fmt.Sprintf(" move %d of %d · scroll down or ] to return ", i, len(b.history)-1) +// historyLabel describes the position shown while stepping back, in at +// most width cells if it can (shorter forms for a narrow board or a clock +// sharing the row). +func (b *BoardView) historyLabel(width int) string { + i, n := len(b.history)-1-b.back, len(b.history)-1 + for _, l := range []string{ + fmt.Sprintf(" move %d of %d · scroll down or ] to return ", i, n), + fmt.Sprintf(" move %d of %d · ] to return ", i, n), + fmt.Sprintf(" move %d/%d · ] returns ", i, n), + } { + if len([]rune(l)) <= width { + return l + } + } + return fmt.Sprintf(" %d/%d ", i, n) } // toggleShape adds a shape, or removes it if it's already drawn (in any diff --git a/pkg/board_extras_test.go b/pkg/board_extras_test.go index 7c4c3de..f4c2b81 100644 --- a/pkg/board_extras_test.go +++ b/pkg/board_extras_test.go @@ -1,6 +1,7 @@ package pkg import ( + "strings" "testing" tc "github.com/gdamore/tcell/v3" @@ -116,3 +117,37 @@ func TestDrawBoardStyles(t *testing.T) { b.Draw(s) } } + +// Stepping back in a game with clocks: the banner shares the row above the +// board with the top clock (it used to cover it, though that clock is often +// the one running), and is drawn in one piece (the · used to leave a gap). +func TestHistoryBannerKeepsClock(t *testing.T) { + for _, w := range []int{160, 70} { + s := newMockScreen(t, w, 50) + b := NewBoardView(nil) + b.SetRect(0, 0, w, 50) + g, _ := gameFromSAN("", "e4 e5 Nf3 Nc6 Bc4 Nd4 Bxf7+") + b.SetPosition(g.Position(), true) + b.SetHistory(g.Positions(), g.Moves()) + b.SetClockBars(clockBar{label: "9:18", frac: 0.9, right: "10+5", active: true}, clockBar{label: "9:30", frac: 0.95}) + b.mu.Lock() + b.step(2) + b.mu.Unlock() + b.Draw(s) + var row strings.Builder + for x := 0; x < w; x++ { + c, _, _ := s.Get(x, b.oy-1) + row.WriteString(c) + } + text := row.String() + if !strings.Contains(text, "9:18") { + t.Errorf("%d wide: top clock hidden: %q", w, text) + } + if !strings.Contains(text, "move 5/7") && !strings.Contains(text, "move 5 of 7 · ") { + t.Errorf("%d wide: banner %q", w, text) + } + if end := strings.TrimRight(text, " "); len([]rune(end)) > b.ox+8*b.sqW { + t.Errorf("%d wide: banner runs past the board: %q", w, text) + } + } +}