Skip to content

Commit d3ceb5b

Browse files
committed
Avoid coloring repeated letters
1 parent d352bd6 commit d3ceb5b

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func main() {
2828

2929
func runCli() {
3030
model := &model{}
31-
program := tea.NewProgram(model, tea.WithAltScreen(), tea.WithOutput(os.Stderr))
31+
program := tea.NewProgram(model, teaOptions...)
3232

3333
exitCode := 0
3434
if err := program.Start(); err != nil {

model.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,29 @@ func (m *model) viewGrid() string {
274274
// viewGridRowFilled renders a filled-in grid row. It chooses the appropriate
275275
// color for each key.
276276
func (m *model) viewGridRowFilled(word [numChars]byte) string {
277-
var keys [numChars]string
277+
var keyStates [numChars]keyState
278+
letters := m.word
279+
280+
// Mark keyStatusCorrect.
278281
for i := 0; i < numChars; i++ {
279-
key := word[i]
280-
keyStatus := keyStateAbsent
281-
if key == m.word[i] {
282-
keyStatus = keyStateCorrect
283-
} else if bytes.IndexByte(m.word[:], key) != -1 {
284-
keyStatus = keyStatePresent
282+
if word[i] == m.word[i] {
283+
keyStates[i] = keyStateCorrect
284+
letters[i] = 0
285+
}
286+
}
287+
288+
// Mark keyStatusPresent.
289+
for i := 0; i < numChars; i++ {
290+
if foundIdx := bytes.IndexByte(letters[:], word[i]); foundIdx != -1 {
291+
keyStates[i] = keyStatePresent
292+
letters[foundIdx] = 0
285293
}
286-
keys[i] = m.viewKey(string(key), keyStatus.color())
294+
}
295+
296+
// Render keys.
297+
var keys [numChars]string
298+
for i := 0; i < numChars; i++ {
299+
keys[i] = m.viewKey(string(word[i]), keyStates[i].color())
287300
}
288301
return lipgloss.JoinHorizontal(lipgloss.Bottom, keys[:]...)
289302
}

0 commit comments

Comments
 (0)