Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/scripts/test-windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ $required = @(
'TestAnalyzeTerminalAndUnsearchedResultsAreExplicit',
'TestAnalyzeRefusalsDoNotWriteState',
'TestAnalyzeDoesNotRedirectAnUnreadableExactGame',
'TestPlayNowCutsAMaxSearchShort'
'TestPlayNowCutsAMaxSearchShort',
'TestStudyNoteSurvivesLeavingAndResizing'
)
$missing = @($required | Where-Object { -not $passes.ContainsKey($_) })
$missingStorage = @()
Expand Down Expand Up @@ -109,7 +110,9 @@ if ($Scope -eq 'all') {
'gamestore/TestOnlyOneFinishOfAGameIsStored',
'gamestore/TestReadingAStoreNothingMayWriteTo',
'gamestore/TestASaveThatCannotReplaceTheGameKeepsTheStoredOne',
'gamestore/TestWindowsDeviceGameIDsAreRejected'
'gamestore/TestWindowsDeviceGameIDsAreRejected',
'study/TestTwoProcessesSavingOneStudyLoseNothing',
'study/TestSaveWaitsForTheLockAndSeesTheSaveBeforeIt'
)
$missingStorage = @($requiredStorage | Where-Object {
-not $allPasses.ContainsKey('github.com/BAKocska/twixtui/internal/' + $_)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ jobs:
# job nobody reads.
run: |
go test -race -count=1 ./internal/netplay/ ./internal/app/ ./internal/cli/ \
./internal/leaderboard/ ./internal/profile/
./internal/leaderboard/ ./internal/profile/ ./internal/study/
go test -race -count=1 -run '^TestProgress' ./internal/bot/

# Native Windows executes the same product scenarios through ConPTY. The
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
move: completed depth, nodes and time in all, and stop reason. A status line
too narrow for all of them gives up whole items, the stop reason before the
time and the nodes, and always keeps the move and its completed depth.
- Study notes and bookmarks in the replay viewer: `m` bookmarks the selected
entry and `e` writes a one-line note on it, shown in the panel and marked in
the entry list with `*` and `#`. They are kept per game, record digest and
entry in `study/<game id>.json` beside the saved game (study format 1); the
canonical record is never rewritten. Notes are bounded to 8 KiB of UTF-8 and
study files to 1 MiB, and stored text is drawn inert.
- Saves are checked against the revision the viewer loaded: a save another
window has overtaken is refused with the typed note kept, and it can replace
the stored note only once the viewer has shown that version whole. A changed
record, a newer or damaged study file and unfinished games refuse edits
visibly and leave the file as it was.

### Fixed

Expand Down
54 changes: 51 additions & 3 deletions docs/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ Correspondence keeps `c` available to retrieve the final code, including after
reopening the saved game. `ctrl+c` still ends the whole program, even when
`enter` or `q` would return to the menu.

The replay viewer reuses the movement keys above for stepping through a record
and adds keys of its own, listed under [Reviewing saved games](#reviewing-saved-games):
`:` jumps to an entry, `m` bookmarks the selected entry and `e` writes a note on
it. While a note is being written every printable key is text, and the key
bound to `enter` above is the one that saves it.

## Playing a bot

```
Expand Down Expand Up @@ -893,7 +899,9 @@ shows both the entry counter and the number of moves played.
| `k` / `up`, `j` / `down` | Back or forward five entries. |
| `g` / `G` | Initial position / final entry. |
| `:` | Type an entry number; `enter` jumps and `esc` cancels the input. |
| `q` / `esc` | Leave the viewer when not editing the entry number. |
| `m` | Bookmark the selected entry, or take its bookmark off. Saved at once. |
| `e` | Write or edit the selected entry's note; `enter` saves and `esc` cancels without saving. |
| `q` / `esc` | Leave the viewer when not editing the entry number or a note. |
| `ctrl+c` | End the program. |

Numbers outside `0..last entry`, non-digits and oversized input are refused,
Expand All @@ -910,10 +918,50 @@ remains the record's result while stepping through earlier positions.
Replay validates the whole record before opening and retains one mutable board,
not a board snapshot per entry. Small backward steps use undo; longer backward
seeks rebuild the requested prefix to avoid repeatedly scanning draw-offer
history. Reviewing never changes the stored record or ratings. Notes and
variations are not part of this viewer; engine analysis is available through the
history. Reviewing never changes the stored record or ratings. Variations
are not part of this viewer; engine analysis is available through the
separate `analyze` commands below, not as an interactive replay action.

#### Notes and bookmarks

A finished game can carry a note and a bookmark on any entry, including `0`.
They are kept in `study/<game id>.json` in the configuration directory, beside
the saved game rather than in it: the canonical record is never rewritten, and
an exported record carries no notes. An imported game can have notes of its own
on this machine while its record stays exactly as it was imported.

The panel shows the selected entry's bookmark and note. The entry list gives
marked entries a column of their own once any exist: `*` for a bookmark and `#`
for a note, as characters rather than colours. A note is one line of at most
8 KiB of UTF-8; a longer paste is refused whole rather than cut, and line
breaks in a paste become spaces. While the note input is open every printable
key is text, so `q`, `h` or `:` are typed rather than acted on. Text read from
the file is drawn inert: control characters and escape sequences are shown as
plain text or dropped, never obeyed, and a note written with line breaks by
another program keeps them unless you edit them away.

Notes are attached to the record's digest as well as to the game ID, and the
file is limited to 1 MiB. If the record has changed since the notes were
written, or the file was written by a newer twixtui or is damaged, the viewer
says so and the notes are read-only; the file is left exactly as it was, never
moved onto the new record or rewritten. Notes can be saved on finished games
only, so an unfinished imported game can be watched but `m` and `e` explain why
they do nothing. In a terminal with no room for the panel, the status line
gives the reason for a refused `m`, `e` or save in a few words.

Every save is checked against the revision the viewer last read, and a
successful save advances it. If another window saved first, the save is refused
instead of overwriting it: the input stays open with your text, the stored
note is shown beneath it, and saving again replaces it while `esc` keeps the
stored one. Saving again is taken only while the whole stored note is on
screen. In a terminal too small to show it — with no panel, or a panel too
short or narrow for all of it — the save is refused again and nothing is
written until the terminal is enlarged; `esc` still keeps the stored note. A
bookmark refused the same way shows what is stored, and pressing `m` again
decides. A file another program has left at the highest revision there is
still shows its notes but can take no further save: saving is refused as for a
damaged file, and the file is left as it was.

## Position analysis

Analyse a saved game, a record file (or standard input), or a position written in
Expand Down
6 changes: 6 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/BAKocska/twixtui/internal/leaderboard"
"github.com/BAKocska/twixtui/internal/netplay"
"github.com/BAKocska/twixtui/internal/profile"
"github.com/BAKocska/twixtui/internal/study"
"github.com/BAKocska/twixtui/internal/theme"
"github.com/BAKocska/twixtui/internal/ui"
)
Expand All @@ -28,6 +29,11 @@ type Deps struct {
Profiles *profile.Store
Board *leaderboard.Board
Games *gamestore.Store
// Study keeps the notes and bookmarks a player writes on the entries of a
// replayed game, in a file beside the game rather than in its record. Nil
// means notes are not kept: the replay still opens, and says so when it
// is asked for one.
Study *study.Store

Theme theme.Theme
Styles *ui.Styles
Expand Down
81 changes: 64 additions & 17 deletions internal/app/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type ReplayScreen struct {
stuck string
// jump is the entry-number input, open only while a number is being typed.
jump entryJump
// notes are the player's notes and bookmarks on this game's entries, and
// the note input while one is being written. See replay_study.go.
notes replayNotes
// winning is the run of pegs that won the game, worked out once when the
// screen opens: it cannot change, and it belongs to one position — the last
// one, where the chain the record ends in is actually on the board.
Expand Down Expand Up @@ -196,6 +199,8 @@ var replayKeys = []replayKey{
{ui.ActMoveUp, nil, "five", func(s *ReplayScreen) tea.Cmd { s.seek(s.step() - replayJump); return nil }},
{ui.ActEdgeTop, nil, "ends", func(s *ReplayScreen) tea.Cmd { s.seek(0); return nil }},
{ui.ActEdgeBottom, nil, "ends", func(s *ReplayScreen) tea.Cmd { s.seek(s.entries()); return nil }},
{ui.ActNone, []string{"m"}, "mark", func(s *ReplayScreen) tea.Cmd { s.toggleBookmark(); return nil }},
{ui.ActNone, []string{"e"}, "note", func(s *ReplayScreen) tea.Cmd { s.openNoteEditor(); return nil }},
{ui.ActQuit, []string{"esc"}, "leaves", func(*ReplayScreen) tea.Cmd { return Back() }},
}

Expand Down Expand Up @@ -263,6 +268,7 @@ func NewReplayScreen(d Deps, saved gamestore.Saved) (Screen, error) {
// reaches. A View that looked for it per frame would walk the link
// graph of a 48-hole board on every keypress to draw the same holes.
winning: winningChain(cursor.current()),
notes: openNotes(d.Study, saved, record, cursor.entries()),
}, nil
}

Expand All @@ -277,10 +283,15 @@ func (s *ReplayScreen) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyPressMsg:
return s, s.press(m)
case tea.PasteMsg:
// The entry input is the only field on this screen, so a paste is a
// number for it or nothing at all. The spaces are what a paste out of a
// chat window brings with it; the digits are bounded and checked before
// any of them are kept.
// A paste goes to whichever field is open. The note input bounds and
// cleans what it is given itself.
if s.notes.editor.open {
s.notes.editor.insert(m.Content)
return s, nil
}
// The entry input takes a number or nothing at all. The spaces are
// what a paste out of a chat window brings with it; the digits are
// bounded and checked before any of them are kept.
if s.jump.open {
if len(m.Content) > entryPasteMax {
s.jump.problem = fmt.Sprintf("max %d digits", s.jump.digits)
Expand All @@ -305,6 +316,12 @@ func (s *ReplayScreen) press(m tea.KeyPressMsg) tea.Cmd {
s.editEntry(m)
return nil
}
// The note input owns it for the same reason, and more of it: the letters
// of a note are every key this screen answers.
if s.notes.editor.open {
s.editNote(m)
return nil
}
action := ui.ActNone
// The control forms of a binding are the shell's: it answers them before
// this screen is asked, so the footer does not name them and dispatch does
Expand Down Expand Up @@ -439,45 +456,68 @@ func (s *ReplayScreen) focus() (game.Point, bool) {
// keys do, dropped from the end when the terminal is too narrow for all of it.
//
// At the widths that have no room for a panel it is the whole of the interface,
// so the entry input is drawn here as well as there, and what is wrong with a
// number comes before the keys that would fix it.
// so an open input is drawn here as well as there, and what is wrong with a
// number, or what the notes refused, comes before the counter and the keys,
// which are what a narrow line gives up first.
func (s *ReplayScreen) status(width int) string {
// These are record entries rather than moves: a draw offer is an entry that
// changes nothing on the board, so counting them as moves disagreed with
// every other surface's move count.
counter := fmt.Sprintf("step %d of %d", s.step(), s.entries())
if s.jump.open {
parts := make([]string, 0, len(s.hints)+3)
hints := s.hints
switch {
case s.jump.open:
note := s.jump.note()
fieldWidth := max(3, width-ansi.StringWidth(note)-1)
input := note + " " + s.jump.edit.render(shellStyles(s.deps), fieldWidth)
return hintLine(width, input, counter, keyLabel(s.confirm...)+" jump", "esc cancel")
parts = append(parts, note+" "+s.jump.edit.render(shellStyles(s.deps), fieldWidth))
hints = []string{keyLabel(s.confirm...) + " jump", "esc cancel"}
case s.notes.editor.open:
parts = append(parts, s.noteInput(width))
hints = []string{keyLabel(s.confirm...) + " save", "esc cancel"}
}
// A part wider than the row is shortened here, with a mark, rather than
// clipped at the edge of the frame: the frame pulls a marked cut back to a
// whole word, and an unmarked one ends wherever the edge fell.
if note := s.studyStatus(); note != "" {
parts = append(parts, truncateText(note, width))
}
parts := make([]string, 0, len(s.hints)+1)
parts = append(parts, counter)
return hintLine(width, append(parts, s.hints...)...)
return hintLine(width, append(parts, hints...)...)
}

// panel is the record beside the board: where in it the player is, what it came
// to, and the entries themselves as a list to move through.
//
// The list is promised its share of the rows before the prose is given any. The
// panel below a board is four rows at its smallest, and one that spent every row
// on titles and results would leave a review with nothing to review.
// The list is promised its share of the rows before anything else is given
// any. The panel below a board is four rows at its smallest, and one that spent
// every row on titles and results would leave a review with nothing to review.
// The study block comes next: it is built for every row the list leaves it and
// sets aside the rows it needs, so an open note input or a refusal is drawn
// whole wherever there is room for it, and the prose has what is left.
func (s *ReplayScreen) panel(width, height int) []string {
if width <= 0 || height <= 0 {
return nil
}
out := make([]string, 0, height)
// The input goes first because it is what the player is doing.
out = append(out, clampLines(s.jump.lines(shellStyles(s.deps), width), height)...)
list := min(s.entries()+1, max(1, height/2))
if room := height - len(out) - list; room > 1 {
list := s.listShare(height)
study := s.studyLines(width, s.studyRoom(width, height))
if room := height - len(out) - list - s.studyReserve(width, study); room > 1 {
out = append(out, clampLines(s.describe(width), room-1)...)
out = append(out, "")
}
if room := height - len(out) - list; room > 1 && len(study) > 0 {
out = append(out, cutRows(study, room-1, width)...)
out = append(out, "")
}
return append(out, s.entryList(width, height-len(out))...)
}

// listShare is the entry list's promised share of a panel height rows tall.
func (s *ReplayScreen) listShare(height int) int { return min(s.entries()+1, max(1, height/2)) }

// describe is the panel's prose, most useful line first, because a short panel
// keeps the beginning of it and drops the end.
func (s *ReplayScreen) describe(width int) []string {
Expand Down Expand Up @@ -520,6 +560,9 @@ func (s *ReplayScreen) entryList(width, n int) []string {
// they are padded on the right: the number belongs with the marker in front
// of it, and a marker followed by blanks reads as a gap.
digits := len(strconv.Itoa(s.entries()))
// The study's marks get a column of their own between the number and the
// label, and only once there is a mark to put in it.
flagged := s.notes.flagged()
rows := make([]string, 0, n)
for i := start; i < start+n; i++ {
label := "opening position"
Expand All @@ -532,7 +575,11 @@ func (s *ReplayScreen) entryList(width, n int) []string {
if i == s.step() {
marker = paint(st, &st.Cursor, "> ")
}
rows = append(rows, marker+truncateText(padTo(strconv.Itoa(i), digits)+" "+label, max(0, width-2)))
number := padTo(strconv.Itoa(i), digits)
if flagged {
number += " " + s.notes.flags(i)
}
rows = append(rows, marker+truncateText(number+" "+label, max(0, width-2)))
}
return rows
}
Expand Down
Loading
Loading