diff --git a/README.md b/README.md index e111ce5..4ec32e1 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,7 @@ Composer keys are fixed, not part of `[keys]`: | `delete` | `d` | Move to Trash; inside Trash, delete permanently after a confirmation. | | `archive` | `A` | Move to the archive mailbox. | | `junk` | `!` | Move to the junk mailbox. | -| `move` | `m` | Move to a mailbox chosen from a picker. | +| `move` | `m` | Move to a mailbox chosen from a picker; type to filter the folder list. | | `empty_trash` | `X` | Delete everything in Trash, after a confirmation. Only works inside Trash. | | `collapse` | `z` | Fold or unfold the conversation under the cursor. | | `toggle_flag` | `f` | Flag or unflag. | @@ -202,7 +202,7 @@ Composer keys are fixed, not part of `[keys]`: | `delete` | `d` | Move to Trash; inside Trash, delete permanently after a confirmation. | | `archive` | `A` | Move to the archive mailbox. | | `junk` | `!` | Move to the junk mailbox. | -| `move` | `m` | Move to a mailbox chosen from a picker. | +| `move` | `m` | Move to a mailbox chosen from a picker; type to filter the folder list. | | `toggle_flag` | `f` | Flag or unflag. | | `toggle_read` | `u` | Mark read or unread. | | `next_thread_message` | `n` | Next message in the same conversation. | diff --git a/tui/footer.go b/tui/footer.go index 5793766..0998286 100644 --- a/tui/footer.go +++ b/tui/footer.go @@ -161,6 +161,7 @@ func (m Model) footerHints() []footerHint { } case m.moveOpen: return []footerHint{ + literalHint("type", "filter"), hintPair(actionUp, actionDown, "choose"), hint(actionOpen, "move"), hint(actionBack, "close"), @@ -174,11 +175,14 @@ func (m Model) footerHints() []footerHint { case m.visualActive(): return []footerHint{ hint(actionDelete, "delete"), - hint(actionToggleFlag, "flag"), + hint(actionArchive, "archive"), + hint(actionJunk, "junk"), hint(actionMove, "move"), - hint(actionQuit, "quit"), + hint(actionToggleFlag, "flag"), + hint(actionToggleRead, "read"), hint(actionBack, "cancel"), hintPair(actionUp, actionDown, "extend"), + hint(actionQuit, "quit"), } case m.focus == focusReader: hints := []footerHint{ diff --git a/tui/help.go b/tui/help.go index 77488c6..b7f78ee 100644 --- a/tui/help.go +++ b/tui/help.go @@ -85,6 +85,20 @@ func helpGroups() []helpGroup { helpAction(actionAttachments, "attachments"), }, }, + { + title: "selection", + rows: []helpRow{ + helpAction(actionVisualRange, "start or clear range"), + helpPair(actionUp, actionDown, "extend range"), + helpAction(actionDelete, "delete selected"), + helpAction(actionArchive, "archive selected"), + helpAction(actionJunk, "junk selected"), + helpAction(actionMove, "move selected"), + helpAction(actionToggleFlag, "flag selected"), + helpAction(actionToggleRead, "read/unread selected"), + helpAction(actionBack, "cancel range"), + }, + }, { title: "reader", rows: []helpRow{ @@ -122,6 +136,8 @@ func helpGroups() []helpGroup { helpAction(actionOpen, "confirm"), helpAction(actionBack, "cancel"), helpAction(actionQuit, "close"), + helpLiteral("text", "filter folders"), + helpLiteral("backspace", "erase a letter"), }, }, } diff --git a/tui/help_test.go b/tui/help_test.go index d68d813..6d8b2c0 100644 --- a/tui/help_test.go +++ b/tui/help_test.go @@ -72,10 +72,10 @@ func closeHelp(t *testing.T, m Model, key string) Model { // terminal is tall enough that the whole map renders at once; the 60x20 scroll test // covers the case where it does not fit. func TestHelpOpensAndListsTheContexts(t *testing.T) { - m := openHelp(t, sized(140, 60)) + m := openHelp(t, sized(140, 80)) view := m.View() - for _, want := range []string{"keys", "global", "list", "reader", "composer", "modals"} { + for _, want := range []string{"keys", "global", "list", "selection", "reader", "composer", "modals"} { if !strings.Contains(view, want) { t.Errorf("help is missing %q:\n%s", want, view) } diff --git a/tui/messages.go b/tui/messages.go index f4afffe..5624d69 100644 --- a/tui/messages.go +++ b/tui/messages.go @@ -276,15 +276,15 @@ func (m Model) messageLine(index, width int) string { line := row.String() + // Bold is the selection indicator and nothing else (#97). Unread mail keeps + // the marker column's "*", which survives in monochrome and does not compete + // with the cursor or a range for the one bold attribute. if index == m.messageCursor { return SelectedStyle.Render(line) } if m.inVisual(index) { return SelectedStyle.Render(line) } - if !msg.Seen() { - return UnreadStyle.Render(line) - } return NormalStyle.Render(line) } diff --git a/tui/model.go b/tui/model.go index c66011f..2892430 100644 --- a/tui/model.go +++ b/tui/model.go @@ -89,6 +89,8 @@ type Model struct { moveOpen bool moveCursor int moveFolders []mail.Folder + moveFilter string + moveMatches []mail.Folder accountsOpen bool accountCursor int @@ -669,6 +671,8 @@ func (m Model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) { m.moveOpen = false m.moveFolders = nil m.moveCursor = 0 + m.moveFilter = "" + m.moveMatches = nil m.attachOpen = false m.attachCursor = 0 @@ -1085,12 +1089,44 @@ func (m Model) renderPanels(layout Layout) string { return m.renderPanel(panelTitle(m.focus), layout.Reader, body, true) } +// selectionText names how many messages the active range covers, so a bulk +// action is never confirmed blind. It is empty when no range is active, which +// leaves an ordinary footer unchanged. +func (m Model) selectionText() string { + if !m.visualActive() { + return "" + } + + n := len(m.actionUIDs()) + if n == 0 { + return "" + } + + return "selected " + countLabel(n, "message") +} + +// hintSeparatorLen is the cells one separator costs, used when the selection +// prefix takes part of the footer's width. +const hintSeparatorLen = len(hintSeparator) + // renderFooter shows the keys that work right now, in priority order, as many as // fit. The hints come from the binding table (tui/footer.go), so a rebound key // cannot leave a stale hint behind, and the footer drops whole hints rather than -// cutting one in half. +// cutting one in half. A live range leads with its count (#96), because the +// range is invisible in the list once the unread bold is gone. func (m Model) renderFooter() string { - return MutedStyle.Render(sanitizeDisplay(packHints(m.keys, m.footerHints(), ContentWidth(m.width)))) + width := ContentWidth(m.width) + + text := packHints(m.keys, m.footerHints(), width) + if prefix := m.selectionText(); prefix != "" { + rest := width - lipgloss.Width(prefix) - hintSeparatorLen + text = prefix + if hints := packHints(m.keys, m.footerHints(), max(0, rest)); hints != "" { + text += hintSeparator + hints + } + } + + return MutedStyle.Render(sanitizeDisplay(TruncateDisplay(text, width))) } // statusForBar is the status as the bar shows it. Unknown errors keep their wrapped text, diff --git a/tui/movefilter_test.go b/tui/movefilter_test.go new file mode 100644 index 0000000..666925b --- /dev/null +++ b/tui/movefilter_test.go @@ -0,0 +1,189 @@ +package tui + +import ( + "strings" + "testing" + + "github.com/renderorange/chute/mail" +) + +// visibleFolders lists the destinations the move picker offers right now. +func visibleFolders(m Model) []string { + names := make([]string, 0, len(m.moveMatches)) + for _, folder := range m.moveMatches { + names = append(names, folder.Name) + } + + return names +} + +// TestMovePickerFiltersByTypedQuery is the headline of #95: typing narrows the +// folder list, and enter moves to the folder that survived the filter. +func TestMovePickerFiltersByTypedQuery(t *testing.T) { + m, move := withFolderMove(t, 3) + + m = press(t, m, keyMsg("m")) + m = press(t, m, keyMsg("p")) + m = press(t, m, keyMsg("r")) + + if got := visibleFolders(m); len(got) != 1 || got[0] != "Projects" { + t.Fatalf("picker = %v, want only Projects", got) + } + + m = press(t, m, namedKey("enter")) + + if move.calls != 1 || move.dest != "Projects" { + t.Errorf("moved %d times to %q, want once to Projects", move.calls, move.dest) + } + if m.moveOpen { + t.Error("the picker stayed open after moving") + } +} + +// TestMovePickerFuzzyMatchesTheMiddleOfTheName proves the match is fuzzy, not a +// prefix search, so a two-letter query finds a folder whose letters are scattered. +func TestMovePickerFuzzyMatchesTheMiddleOfTheName(t *testing.T) { + m := loaded(t, 140, 40, []mail.Folder{ + {Name: "INBOX", Delimiter: '/'}, + {Name: "Archive", Delimiter: '/'}, + {Name: "Projects", Delimiter: '/'}, + {Name: "Sent", Delimiter: '/'}, + }, nil) + m.loadMessages = fakeMessages(sampleMessages(3), nil) + m = openInbox(t, m) + + m = press(t, m, keyMsg("m")) + m = press(t, m, keyMsg("a")) + m = press(t, m, keyMsg("c")) + m = press(t, m, keyMsg("h")) + + if got := visibleFolders(m); len(got) != 1 || got[0] != "Archive" { + t.Errorf("picker = %v, want only Archive", got) + } +} + +// TestMovePickerSaysWhenNothingMatches keeps an empty picker honest: enter does +// nothing, the picker stays open, and the status says why. +func TestMovePickerSaysWhenNothingMatches(t *testing.T) { + m, move := withFolderMove(t, 3) + + m = press(t, m, keyMsg("m")) + for _, r := range []string{"z", "z", "z"} { + m = press(t, m, keyMsg(r)) + } + + if got := visibleFolders(m); len(got) != 0 { + t.Fatalf("picker = %v, want no matches", got) + } + + m = press(t, m, namedKey("enter")) + + if move.calls != 0 { + t.Errorf("loader calls = %d, want 0 with no match", move.calls) + } + if !m.moveOpen { + t.Error("enter closed the picker with nothing to choose") + } + if want := "no folders match"; m.status != want { + t.Errorf("status = %q, want %q", m.status, want) + } +} + +// TestMovePickerBackspaceWidensTheList proves the query is editable in place. +func TestMovePickerBackspaceWidensTheList(t *testing.T) { + m, _ := withFolderMove(t, 3) + + m = press(t, m, keyMsg("m")) + m = press(t, m, keyMsg("p")) + m = press(t, m, keyMsg("r")) + + if got := len(visibleFolders(m)); got != 1 { + t.Fatalf("visible = %d, want 1 after typing", got) + } + + m = press(t, m, namedKey("backspace")) + m = press(t, m, namedKey("backspace")) + + if got := len(visibleFolders(m)); got != 2 { + t.Errorf("visible = %d, want both targets after erasing the query", got) + } +} + +// TestMovePickerKeepsItsKeysBound proves the picker did not take the quit key +// hostage: with the shipped keymap q still closes it and does not type. +func TestMovePickerKeepsItsKeysBound(t *testing.T) { + m, _ := withFolderMove(t, 3) + + m = press(t, m, keyMsg("m")) + m = press(t, m, keyMsg("q")) + + if m.moveOpen { + t.Fatal("q did not close the picker") + } + if m.moveFilter != "" { + t.Errorf("query = %q, want empty", m.moveFilter) + } +} + +// TestMovePickerHonoursAReboundKey is the reason the handler resolves the +// keymap first: a rebound open key still moves, and the letters around it type. +func TestMovePickerHonoursAReboundKey(t *testing.T) { + cfg := testConfig() + cfg.Keys = map[string][]string{"open": {"o"}} + + m := loadedWith(t, cfg, moveFolders()) + m.loadMessages = fakeMessages(sampleMessages(3), nil) + m = openInboxAnyMode(t, m) + + move := &fakeFolderMove{} + m.moveToFolder = move.load + + m = press(t, m, keyMsg("m")) + m = press(t, m, keyMsg("p")) + m = press(t, m, keyMsg("r")) + m = press(t, m, keyMsg("o")) + + if move.calls != 1 || move.dest != "Projects" { + t.Errorf("moved %d times to %q, want once to Projects from the rebound o", move.calls, move.dest) + } +} + +// TestMovePickerTypesLettersBoundElsewhere covers the other half: a letter bound +// to an action this picker does not honour is text, not a command. +func TestMovePickerTypesLettersBoundElsewhere(t *testing.T) { + m, _ := withFolderMove(t, 3) + + m = press(t, m, keyMsg("m")) + m = press(t, m, keyMsg("d")) + + if !m.moveOpen { + t.Fatal("d closed the picker instead of typing") + } + if m.moveFilter != "d" { + t.Errorf("query = %q, want d", m.moveFilter) + } + + m = press(t, m, namedKey("esc")) + + if m.moveOpen { + t.Error("esc left the picker open") + } + if m.moveFilter != "" { + t.Errorf("query = %q after closing, want empty", m.moveFilter) + } +} + +// TestMovePickerFilterFitsTheFrame pins the geometry with a query typed in: a +// typed string has no natural bound, so the dialog must clip it. +func TestMovePickerFilterFitsTheFrame(t *testing.T) { + m := loaded(t, 60, 20, moveFolders(), nil) + m.loadMessages = fakeMessages(sampleMessages(3), nil) + m = openInboxAnyMode(t, m) + + m = press(t, m, keyMsg("m")) + for _, r := range strings.Repeat("long-query", 10) { + m = press(t, m, keyMsg(string(r))) + } + + assertExactFrame(t, m, "move picker with a long query") +} diff --git a/tui/movefolder.go b/tui/movefolder.go index aebbea2..cf09a17 100644 --- a/tui/movefolder.go +++ b/tui/movefolder.go @@ -9,6 +9,7 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/emersion/go-imap/v2" "github.com/renderorange/chute/mail" + "github.com/sahilm/fuzzy" ) // folderMoveMsg reports a named move to an arbitrary mailbox. @@ -52,28 +53,91 @@ func (m Model) beginMove() (tea.Model, tea.Cmd) { m.moveOpen = true m.moveCursor = 0 m.moveFolders = folders + m.moveFilter = "" + m.moveMatches = folders m.status = "" return m, nil } -// handleMoveKey applies a keypress while the move picker is open. +// matchFolders narrows the destination list to the folders matching query, in +// folder order. The match is fuzzy and case-insensitive, the same rules the list +// filter uses (#95), so "pr" finds "Projects" and "ach" finds "Archive". +func matchFolders(folders []mail.Folder, query string) []mail.Folder { + query = strings.TrimSpace(query) + if query == "" { + return folders + } + + names := make([]string, len(folders)) + for i, folder := range folders { + names[i] = folder.Name + } + + matched := make([]mail.Folder, 0, len(folders)) + for _, hit := range fuzzy.FindNoSort(query, names) { + matched = append(matched, folders[hit.Index]) + } + + return matched +} + +// applyMoveFilter re-derives the visible destinations after the query changed and +// puts the cursor back at the top, because the row it pointed at may be gone. +func (m *Model) applyMoveFilter() { + m.moveMatches = matchFolders(m.moveFolders, m.moveFilter) + m.moveCursor = 0 + m.status = "" +} + +// closeMove dismisses the picker and forgets the query, so reopening it starts +// from the whole list. +func (m *Model) closeMove() { + m.moveOpen = false + m.moveFilter = "" + m.moveMatches = nil +} + +// handleMoveKey applies a keypress while the move picker is open. A key bound to +// an action this picker honours still does that action, so a rebound esc/enter/ +// m/quit works here like everywhere else. Every other printable key belongs to +// the filter query, which is why typing a folder name is possible at all; the +// alternative — only unbound letters type — would make filtering useless when +// almost every letter carries a binding. func (m Model) handleMoveKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) { switch m.keys.resolve(msg.String()) { case actionForceQuit: return m, tea.Quit case actionQuit, actionBack, actionMove: - m.moveOpen = false + m.closeMove() + + return m, nil + + case actionOpen: + return m.moveSelectionToFolder() case actionUp: m.moveCursor = max(0, m.moveCursor-1) case actionDown: - m.moveCursor = min(m.moveCursor+1, len(m.moveFolders)-1) + m.moveCursor = max(0, min(m.moveCursor+1, len(m.moveMatches)-1)) + } - case actionOpen: - return m.moveSelectionToFolder() + switch msg.Type { + case tea.KeyBackspace: + if runes := []rune(m.moveFilter); len(runes) > 0 { + m.moveFilter = string(runes[:len(runes)-1]) + m.applyMoveFilter() + } + + case tea.KeySpace: + m.moveFilter += " " + m.applyMoveFilter() + + case tea.KeyRunes: + m.moveFilter += string(msg.Runes) + m.applyMoveFilter() } return m, nil @@ -81,16 +145,22 @@ func (m Model) handleMoveKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) { // moveSelectionToFolder starts the move to the mailbox under the picker cursor. func (m Model) moveSelectionToFolder() (tea.Model, tea.Cmd) { - if !m.moveOpen || len(m.moveFolders) == 0 { + if !m.moveOpen { + return m, nil + } + if len(m.moveMatches) == 0 { + m.status = "no folders match" + return m, nil } - if m.moveCursor < 0 || m.moveCursor >= len(m.moveFolders) { - m.moveOpen = false + if m.moveCursor < 0 || m.moveCursor >= len(m.moveMatches) { + m.closeMove() + return m, nil } - dest := m.moveFolders[m.moveCursor].Name - m.moveOpen = false + dest := m.moveMatches[m.moveCursor].Name + m.closeMove() uids := m.actionUIDs() if len(uids) == 0 { @@ -144,20 +214,31 @@ func (m Model) afterRemoval(uids []imap.UID, status string) (tea.Model, tea.Cmd) return next, cmd } -// renderMoveModal centres the folder picker in the content area. +// renderMoveModal centres the folder picker in the content area. The query line +// appears only once something is typed, so the default dialog keeps its rows. func (m Model) renderMoveModal(width, height int) string { - if len(m.moveFolders) == 0 { - return "" + inner := max(10, width-14) + + chrome := 6 + lines := []string{TitleStyle.Render("move to folder")} + if m.moveFilter != "" { + chrome++ + lines = append(lines, NormalStyle.Render(TruncateDisplay(m.moveFilter+"_", inner))) } - rows := max(1, min(len(m.moveFolders), height-6)) - start := clamp(m.moveCursor-rows/2, 0, max(0, len(m.moveFolders)-rows)) - end := min(start+rows, len(m.moveFolders)) + switch { + case len(m.moveMatches) == 0: + lines = append(lines, MutedStyle.Render("no folders match")) + default: + rows := max(1, min(len(m.moveMatches), height-chrome)) + start := clamp(m.moveCursor-rows/2, 0, max(0, len(m.moveMatches)-rows)) + end := min(start+rows, len(m.moveMatches)) - lines := []string{TitleStyle.Render("move to folder")} - for i := start; i < end; i++ { - lines = append(lines, folderRow(m.moveFolders[i], i == m.moveCursor, max(10, width-14))) + for i := start; i < end; i++ { + lines = append(lines, folderRow(m.moveMatches[i], i == m.moveCursor, inner)) + } } + lines = append(lines, "", MutedStyle.Render("enter:move esc:close")) modal := ModalStyle.Render(strings.Join(lines, "\n")) diff --git a/tui/movefolder_test.go b/tui/movefolder_test.go index 11ea279..c181495 100644 --- a/tui/movefolder_test.go +++ b/tui/movefolder_test.go @@ -89,8 +89,8 @@ func TestMovePickerMovesToTheChosenFolder(t *testing.T) { } m = press(t, m, keyMsg("m")) - m = press(t, m, keyMsg("down")) // Archive -> Projects - m = press(t, m, keyMsg("enter")) + m = press(t, m, namedKey("down")) // Archive -> Projects + m = press(t, m, namedKey("enter")) if move.calls != 1 { t.Fatalf("loader calls = %d, want 1", move.calls) @@ -117,7 +117,7 @@ func TestMovePickerEscClosesWithoutMoving(t *testing.T) { m, move := withFolderMove(t, 3) m = press(t, m, keyMsg("m")) - m = press(t, m, keyMsg("esc")) + m = press(t, m, namedKey("esc")) if m.moveOpen { t.Error("esc left the picker open") diff --git a/tui/palette.go b/tui/palette.go index 55cf4c3..cc7ce0c 100644 --- a/tui/palette.go +++ b/tui/palette.go @@ -79,11 +79,6 @@ var ( NormalStyle = lipgloss.NewStyle(). Foreground(ColorTextNormal) - // UnreadStyle marks unread messages: the normal colour, in bold. - UnreadStyle = lipgloss.NewStyle(). - Foreground(ColorTextNormal). - Bold(true) - // ErrorStyle is for failures. ErrorStyle = lipgloss.NewStyle(). Foreground(ColorTextError) diff --git a/tui/selection_test.go b/tui/selection_test.go new file mode 100644 index 0000000..7271ce0 --- /dev/null +++ b/tui/selection_test.go @@ -0,0 +1,102 @@ +package tui + +import ( + "strings" + "testing" + + "github.com/charmbracelet/lipgloss" + "github.com/renderorange/chute/mail" +) + +// withSelection opens INBOX with n sample messages, all unread, and the list +// focused, which is the state a range selection starts from. +func withSelection(t *testing.T, n int) Model { + t.Helper() + + m := loaded(t, 140, 40, []mail.Folder{{Name: "INBOX", Delimiter: '/'}}, nil) + m.loadMessages = fakeMessages(sampleMessages(n), nil) + + return openInbox(t, m) +} + +// TestFooterReportsTheSelectedCount is the indicator the issue asked for: the +// footer names how many messages the active range covers, singular and plural. +func TestFooterReportsTheSelectedCount(t *testing.T) { + m := press(t, withSelection(t, 3), keyMsg("v")) + + if got := m.renderFooter(); !strings.Contains(got, "selected 1 message") { + t.Errorf("footer = %q, want it to name one selected message", got) + } + + m = press(t, m, namedKey("down")) + m = press(t, m, namedKey("down")) + + got := m.renderFooter() + if !strings.Contains(got, "selected 3 messages") { + t.Errorf("footer = %q, want it to name three selected messages", got) + } + if strings.Contains(got, "selected 1 message") { + t.Errorf("footer = %q, still claims one message", got) + } +} + +// TestFooterReportsNoCountWithoutARange proves the count is a selection +// indicator, not a permanent label: an ordinary list footer has no prefix. +func TestFooterReportsNoCountWithoutARange(t *testing.T) { + m := withSelection(t, 3) + + if got := m.renderFooter(); strings.Contains(got, "selected") { + t.Errorf("footer = %q, want no selection prefix before v", got) + } +} + +// TestFooterSelectionListsEveryBulkAction is #96's "all of the options": every +// action the range supports is named while the range is active, and the whole +// set still fits the tablet width. +func TestFooterSelectionListsEveryBulkAction(t *testing.T) { + m := press(t, withSelection(t, 3), keyMsg("v")) + + footer := m.renderFooter() + for _, want := range []string{ + "d:delete", "A:archive", "!:junk", "m:move", "f:flag", "u:read", + "esc:cancel", "up/down:extend", + } { + if !strings.Contains(footer, want) { + t.Errorf("footer is missing %q: %q", want, footer) + } + } + + if got, limit := lipgloss.Width(footer), ContentWidth(m.width); got > limit { + t.Errorf("footer is %d cells, want at most %d: %q", got, limit, footer) + } +} + +// TestFooterSelectionFitsTheMinimumFrame keeps the new prefix from breaking the +// frame at the 60x20 floor: the footer is one line and never wider than the app. +func TestFooterSelectionFitsTheMinimumFrame(t *testing.T) { + m := loaded(t, 60, 20, []mail.Folder{{Name: "INBOX", Delimiter: '/'}}, nil) + m.loadMessages = fakeMessages(sampleMessages(3), nil) + m = openInboxAnyMode(t, m) + m = press(t, m, keyMsg("v")) + + assertExactFrame(t, m, "visual range at 60x20") + + if got, limit := lipgloss.Width(m.renderFooter()), ContentWidth(m.width); got > limit { + t.Errorf("footer is %d cells, want at most %d", got, limit) + } +} + +// TestHelpListsTheSelectionAndMoveFilterOptions covers #96's help half: the +// overlay names the keys a range adds and the move picker's typing filter. +func TestHelpListsTheSelectionAndMoveFilterOptions(t *testing.T) { + m := openHelp(t, sized(140, 80)) + view := m.View() + + for _, want := range []string{ + "selection", "extend range", "delete selected", "filter folders", + } { + if !strings.Contains(view, want) { + t.Errorf("help is missing %q:\n%s", want, view) + } + } +} diff --git a/tui/unread_style_test.go b/tui/unread_style_test.go new file mode 100644 index 0000000..c22ff58 --- /dev/null +++ b/tui/unread_style_test.go @@ -0,0 +1,73 @@ +package tui + +import ( + "regexp" + "strings" + "testing" + + "github.com/charmbracelet/lipgloss" + "github.com/muesli/termenv" +) + +// boldSGR matches the bold attribute inside any SGR sequence: lipgloss packs +// bold and colour into one escape ("\x1b[1;38;5;238m"), so a literal "\x1b[1m" +// never appears. +var boldSGR = regexp.MustCompile("\x1b\\[[0-9;]*1[;m]") + +// boldRows renders the list with colour forced on, so "is this row bold" is a +// real question under `go test`'s piped stdout. The profile is restored before +// the test returns; no test in this package uses t.Parallel. +func boldRows(t *testing.T, m Model) []string { + t.Helper() + + profile := lipgloss.ColorProfile() + lipgloss.SetColorProfile(termenv.TrueColor) + t.Cleanup(func() { lipgloss.SetColorProfile(profile) }) + + return m.messageLines(3, 60) +} + +// TestOnlySelectedRowsAreBold is #97: bold marks the cursor and the active +// range, and nothing else. Every fixture message is unread, so an unread row +// outside the selection is the row that must stay plain. +func TestOnlySelectedRowsAreBold(t *testing.T) { + m := withSelection(t, 3) + + lines := boldRows(t, m) + if !boldSGR.MatchString(lines[0]) { + t.Error("the cursor row is not bold") + } + for i := 1; i < len(lines); i++ { + if boldSGR.MatchString(lines[i]) { + t.Errorf("row %d is bold while only the cursor is selected", i) + } + } + + m = press(t, m, keyMsg("v")) + m = press(t, m, namedKey("down")) + + lines = boldRows(t, m) + for i := 0; i < 2; i++ { + if !boldSGR.MatchString(lines[i]) { + t.Errorf("row %d is in the range but not bold", i) + } + } + if boldSGR.MatchString(lines[2]) { + t.Error("an unread row outside the range is bold") + } +} + +// TestUnreadRowsKeepTheStarMarker proves removing the bold did not remove the +// unread signal itself: the marker column still carries it. +func TestUnreadRowsKeepTheStarMarker(t *testing.T) { + m := withSelection(t, 2) + + for i, line := range m.messageLines(2, 60) { + if i == 0 { + continue // the cursor row shows > in the marker column + } + if !strings.HasPrefix(strings.TrimLeft(line, " "), "*") { + t.Errorf("row %d lost its unread marker: %q", i, line) + } + } +}