This commit is contained in:
katistix committed 2026-10-09 18:50:53 +03:00
1 parent cb0b0f6fb0
commit 914ddd7d12
11 files changed
+833 -33

No files matched your search

+9 -7
View File
@@ -7,6 +7,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/katistix/termitype/internal/config"
"github.com/katistix/termitype/internal/theme"
"github.com/katistix/termitype/internal/typing"
"github.com/katistix/termitype/internal/words"
)
@@ -25,14 +26,15 @@ type Model struct {
engine *typing.Engine
styles Styles
width int
height int
}
// New creates a model with a fresh test from cfg.
func New(cfg config.Config) Model {
// New creates a model with a fresh test from cfg, drawn in theme t.
func New(cfg config.Config, t theme.Theme) Model {
return Model{
cfg: cfg,
engine: newEngine(cfg),
styles: DefaultStyles(),
styles: NewStyles(t),
}
}
@@ -65,7 +67,7 @@ func (m Model) Init() tea.Cmd { return tick() }
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.width, m.height = msg.Width, msg.Height
return m, nil
case tickMsg:
@@ -151,7 +153,7 @@ func (m Model) View() string {
b.WriteString(m.renderText())
}
return s.App.Render(b.String())
return s.fill(s.App.Render(b.String()), m.width, m.height)
}
func (m Model) renderLiveStats() string {
@@ -159,7 +161,7 @@ func (m Model) renderLiveStats() string {
parts := ""
if m.cfg.Mode == config.ModeTime {
secs := int(m.engine.Remaining().Round(time.Second).Seconds())
parts += s.Stat.Render("time ") + s.StatVal.Render(fmt.Sprintf("%d", secs)) + " "
parts += s.Stat.Render("time ") + s.StatVal.Render(fmt.Sprintf("%d", secs)) + s.Stat.Render(" ")
}
wpm := fmt.Sprintf("%.0f", m.engine.WPM())
acc := fmt.Sprintf("%.0f%%", m.engine.Accuracy()*100)
@@ -242,7 +244,7 @@ func (m Model) renderText() string {
sb.WriteString(s.Cursor.Render("·"))
}
} else if i+1 < end {
sb.WriteString(" ")
sb.WriteString(s.Base.Render(" "))
}
}
w := m.width - 8
+240
View File
@@ -0,0 +1,240 @@
package ui
import (
"fmt"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/katistix/termitype/internal/theme"
)
// Picker is a fuzzy theme selector. The whole screen previews the
// highlighted theme; each list row is drawn in its own theme's colors.
type Picker struct {
query string
matches []theme.Match
cursor int // index into matches
offset int // first visible match
current string
width int
height int
chosen *theme.Theme
canceled bool
}
// NewPicker starts with the cursor on the current theme.
func NewPicker(current theme.Theme) Picker {
p := Picker{current: current.Name}
p.refilter()
for i, mt := range p.matches {
if mt.Theme.Name == current.Name {
p.cursor = i
}
}
return p
}
// Chosen returns the selected theme, or false if the picker was canceled.
func (p Picker) Chosen() (theme.Theme, bool) {
if p.chosen == nil {
return theme.Theme{}, false
}
return *p.chosen, true
}
func (p *Picker) refilter() {
p.matches = theme.Filter(theme.All(), p.query)
p.cursor, p.offset = 0, 0
}
func (p Picker) highlighted() theme.Theme {
if len(p.matches) == 0 {
return theme.MustGet(p.current)
}
return p.matches[p.cursor].Theme
}
func (p Picker) Init() tea.Cmd { return nil }
func (p Picker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
p.width, p.height = msg.Width, msg.Height
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc":
p.canceled = true
return p, tea.Quit
case "enter":
if len(p.matches) > 0 {
t := p.matches[p.cursor].Theme
p.chosen = &t
return p, tea.Quit
}
case "up", "ctrl+p", "ctrl+k":
p.move(-1)
case "down", "ctrl+n", "ctrl+j", "tab":
p.move(1)
case "shift+tab":
p.move(-1)
case "pgup":
p.move(-p.listHeight())
case "pgdown":
p.move(p.listHeight())
case "home":
p.move(-len(p.matches))
case "end":
p.move(len(p.matches))
case "backspace":
if r := []rune(p.query); len(r) > 0 {
p.query = string(r[:len(r)-1])
p.refilter()
}
case "ctrl+u", "ctrl+w", "alt+backspace":
p.query = ""
p.refilter()
default:
if msg.Type == tea.KeyRunes || msg.Type == tea.KeySpace {
p.query += string(msg.Runes)
p.refilter()
}
}
}
return p, nil
}
func (p *Picker) move(d int) {
if len(p.matches) == 0 {
return
}
p.cursor = max(0, min(len(p.matches)-1, p.cursor+d))
h := p.listHeight()
if p.cursor < p.offset {
p.offset = p.cursor
} else if p.cursor >= p.offset+h {
p.offset = p.cursor - h + 1
}
}
// listHeight is the number of theme rows that fit on screen.
func (p Picker) listHeight() int {
// App padding (2) + header (2) + prompt (2) + footer (2).
h := p.height - 8
if p.height == 0 {
h = 15
}
return max(1, h)
}
const rowWidth = 28
func (p Picker) View() string {
t := p.highlighted()
s := NewStyles(t)
header := s.Title.Render("termitype") + s.Hint.Render(" theme")
prompt := s.StatVal.Render("> ") + s.Correct.Render(p.query) + s.Cursor.Render(" ")
list := p.renderList(s)
body := list
// Preview only when there's room beside the list.
if p.width == 0 || p.width >= rowWidth+50 {
gap := s.Base.Render(" ")
body = lipgloss.JoinHorizontal(lipgloss.Top, list, gap, renderPreview(s, t))
}
footer := s.Hint.Render(fmt.Sprintf("%d/%d", len(p.matches), len(theme.All()))) +
s.Hint.Render(" ↑/↓ move · enter select · esc cancel")
view := lipgloss.JoinVertical(lipgloss.Left, header, "", prompt, "", body, "", footer)
return s.fill(s.App.Render(view), p.width, p.height)
}
func (p Picker) renderList(s Styles) string {
if len(p.matches) == 0 {
return s.Hint.Width(rowWidth + 2).Render("no matching themes")
}
h := p.listHeight()
end := min(len(p.matches), p.offset+h)
rows := make([]string, 0, h)
for i := p.offset; i < end; i++ {
marker := s.Base.Render(" ")
if i == p.cursor {
marker = s.StatVal.Render("▸ ")
}
rows = append(rows, marker+renderRow(p.matches[i], p.matches[i].Theme.Name == p.current))
}
// Pad so the preview doesn't jump around as the list shrinks.
for len(rows) < h {
rows = append(rows, s.Base.Render(strings.Repeat(" ", rowWidth+2)))
}
return strings.Join(rows, "\n")
}
// renderRow draws a theme name on its own background with matched
// characters in its accent color, plus main/sub/text color dots.
func renderRow(mt theme.Match, isCurrent bool) string {
t := mt.Theme
base := lipgloss.NewStyle().Background(lipgloss.Color(t.Bg))
name := base.Foreground(lipgloss.Color(t.Text))
hit := base.Foreground(lipgloss.Color(t.Main)).Bold(true).Underline(true)
matched := make(map[int]bool, len(mt.Indices))
for _, i := range mt.Indices {
matched[i] = true
}
label := t.Display()
if isCurrent {
label += " *"
}
// Truncate to leave room for padding and the color dots.
maxName := rowWidth - 8
if len(label) > maxName {
label = label[:maxName-1] + "…"
}
var b strings.Builder
b.WriteString(base.Render(" "))
for i, r := range label {
if matched[i] {
b.WriteString(hit.Render(string(r)))
} else {
b.WriteString(name.Render(string(r)))
}
}
pad := rowWidth - 1 - lipgloss.Width(label) - 6
b.WriteString(base.Render(strings.Repeat(" ", max(0, pad))))
for _, c := range []string{t.Main, t.Sub, t.Text} {
b.WriteString(base.Foreground(lipgloss.Color(c)).Render("●"))
b.WriteString(base.Render(" "))
}
return b.String()
}
// renderPreview draws a mock test mid-way through, showing every
// letter state: correct, wrong, extra, caret and pending.
func renderPreview(s Styles, t theme.Theme) string {
type seg struct {
st lipgloss.Style
text string
}
segs := []seg{
{s.Correct, "the quick"}, {s.Base, " "},
{s.Correct, "br"}, {s.Wrong, "w"}, {s.Correct, "wn"}, {s.Base, " "},
{s.Correct, "fox"}, {s.Extra, "xx"}, {s.Base, " "},
{s.Correct, "jum"}, {s.Cursor, "p"}, {s.Pending, "s"}, {s.Base, " "},
{s.Pending, "over the lazy dog and then some more words to type"},
}
var text strings.Builder
for _, sg := range segs {
text.WriteString(sg.st.Render(sg.text))
}
stats := s.Stat.Render("time ") + s.StatVal.Render("12") +
s.Stat.Render(" wpm ") + s.StatVal.Render("87") +
s.Stat.Render(" acc ") + s.StatVal.Render("96%")
title := s.Title.Render(t.Display())
return lipgloss.JoinVertical(lipgloss.Left, title, "", stats, "", s.Box.Width(42).Render(text.String()))
}
+41 -24
View File
@@ -1,22 +1,18 @@
// Package ui contains the bubbletea model and lipgloss styles.
// Package ui contains the bubbletea models and lipgloss styles.
package ui
import (
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/katistix/termitype/internal/theme"
)
var (
// Palette: muted background, monkeytype-ish yellow accent.
accent = lipgloss.Color("#e2b714")
muted = lipgloss.Color("#646669")
fg = lipgloss.Color("#d1d0c5")
bad = lipgloss.Color("#ca4754")
bg = lipgloss.Color("#323437")
cursorBg = lipgloss.Color("#e2b714")
cursorFg = lipgloss.Color("#323437")
)
// Styles are built from a theme. Every style sets the theme background:
// ANSI resets between styled segments would otherwise punch holes in it.
type Styles struct {
Bg lipgloss.Color
Base lipgloss.Style // plain text and gaps on the theme background
App lipgloss.Style
Title lipgloss.Style
Hint lipgloss.Style
@@ -30,18 +26,39 @@ type Styles struct {
StatVal lipgloss.Style
}
func DefaultStyles() Styles {
func NewStyles(t theme.Theme) Styles {
bg := lipgloss.Color(t.Bg)
base := lipgloss.NewStyle().Background(bg)
fg := func(c string) lipgloss.Style { return base.Foreground(lipgloss.Color(c)) }
return Styles{
App: lipgloss.NewStyle().Padding(1, 2),
Title: lipgloss.NewStyle().Bold(true).Foreground(accent),
Hint: lipgloss.NewStyle().Foreground(muted),
Box: lipgloss.NewStyle().Padding(1, 2).Border(lipgloss.RoundedBorder()).BorderForeground(bg),
Pending: lipgloss.NewStyle().Foreground(muted),
Correct: lipgloss.NewStyle().Foreground(fg),
Wrong: lipgloss.NewStyle().Foreground(bad),
Extra: lipgloss.NewStyle().Foreground(bad).Background(lipgloss.Color("#4a2b2e")),
Cursor: lipgloss.NewStyle().Background(cursorBg).Foreground(cursorFg),
Stat: lipgloss.NewStyle().Foreground(muted),
StatVal: lipgloss.NewStyle().Bold(true).Foreground(accent),
Bg: bg,
Base: base,
App: base.Padding(1, 2),
Title: fg(t.Main).Bold(true),
Hint: fg(t.Sub),
Box: base.Padding(1, 2).Border(lipgloss.RoundedBorder()).BorderForeground(lipgloss.Color(t.SubAlt)).BorderBackground(bg),
Pending: fg(t.Sub),
Correct: fg(t.Text),
Wrong: fg(t.Error),
Extra: fg(t.ErrorExtra),
Cursor: lipgloss.NewStyle().Background(lipgloss.Color(t.Caret)).Foreground(bg),
Stat: fg(t.Sub),
StatVal: fg(t.Main).Bold(true),
}
}
// fill paints content over the whole terminal in the theme background.
func (s Styles) fill(content string, width, height int) string {
if width <= 0 || height <= 0 {
return content
}
// Joins and padding emit plain spaces after a style reset; re-apply
// the background after each reset and at each line start.
bgSeq, _, _ := strings.Cut(s.Base.Render("x"), "x")
if bgSeq != "" {
content = strings.ReplaceAll(content, "\x1b[0m", "\x1b[0m"+bgSeq)
content = bgSeq + strings.ReplaceAll(content, "\n", "\n"+bgSeq)
}
return lipgloss.Place(width, height, lipgloss.Left, lipgloss.Top, content,
lipgloss.WithWhitespaceBackground(s.Bg))
}