Files
thermal-snake/internal/arcade/records.go
T
2026-11-27 19:04:23 +03:30

99 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package arcade
import (
"fmt"
"strconv"
"strings"
"github.com/charmbracelet/x/ansi"
)
func (m *Model) recordsView(width int) []string {
border := subtle.Render("│")
inside := width - 4
row := func(s string) string {
return border + " " + s + strings.Repeat(" ", max(0, inside-ansi.StringWidth(s))) + " " + border
}
var title, tableHead string
if m.historyTab {
title = "FLIGHT LOG"
tableHead = historyLine(inside, "FINISHED UTC", "OUTCOME", "SCORE")
} else {
title = "SHARED RANKINGS"
tableHead = standingLine(inside, "RANK", "PILOT", "BEST", "RUNS", " ")
}
top := subtle.Render("╭─ ") + accent.Render(title) +
subtle.Render(" "+strings.Repeat("─", width-5-len(title))+"╮")
lines := []string{top, row(muted.Render(tableHead)), row(subtle.Render(strings.Repeat("─", inside)))}
entries, note := m.recordRows(inside)
for i := range pageSize {
var entry string
if i < len(entries) {
entry = entries[i]
}
lines = append(lines, row(entry))
}
page := fmt.Sprintf("PAGE %d / %d", m.page+1, m.pages())
lines = append(lines, row(pair(muted.Render(note), subtle.Render(page), inside)),
subtle.Render("╰"+strings.Repeat("─", width-2)+"╯"), "")
lines = append(lines, controls(control("TAB", "Switch"), control("←/→", "Page"), control("R", "Refresh"), control("ESC", "Back")))
if m.historyTab {
lines = append(lines, muted.Render("Newest completed runs first."))
} else {
lines = append(lines, muted.Render("Earliest best takes a score tie."))
}
return lines
}
func (m *Model) recordRows(width int) ([]string, string) {
if m.readFailed {
return []string{accent.Render(" Records unavailable. Press R to retry.")}, ""
}
if m.historyTab {
var rows []string
for _, run := range m.history {
line := historyLine(width, run.Finished.UTC().Format("2006-01-02 15:04:05"),
outcomeName(run.Outcome), prettyScore(run.Score))
rows = append(rows, rowStyle.Render(line))
}
if len(rows) == 0 {
rows = append(rows, muted.Render(" Finish a run to start your flight log."))
}
return rows, completionCount(m.historyTotal)
}
var rows []string
note := "Complete a run to rank"
for _, standing := range m.standings {
if standing.Callsign == m.pilot.Callsign {
note = fmt.Sprintf("YOUR RANK #%d", standing.Rank)
break
}
}
start := m.page * pageSize
end := min(start+pageSize, len(m.standings))
for _, standing := range m.standings[start:end] {
style, marker := rowStyle, " "
if standing.Callsign == m.pilot.Callsign {
style, marker = selfStyle, "›"
}
line := standingLine(width, fmt.Sprintf("#%d", standing.Rank), standing.Callsign,
prettyScore(standing.Best.Score), strconv.Itoa(standing.Total), marker)
rows = append(rows, style.Render(line))
}
if len(rows) == 0 {
rows = append(rows, muted.Render(" No completed runs yet."))
}
return rows, note
}
func standingLine(width int, rank, pilot, best, runs, marker string) string {
return fmt.Sprintf("%s %-6s %-*s %7s %6s ", marker, rank, width-25, pilot, best, runs)
}
func historyLine(width int, finished, outcome, score string) string {
return fmt.Sprintf(" %-19s %-*s %7s ", finished, width-32, outcome, score)
}