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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand All @@ -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. |
Expand Down
8 changes: 6 additions & 2 deletions tui/footer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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{
Expand Down
16 changes: 16 additions & 0 deletions tui/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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"),
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions tui/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions tui/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
40 changes: 38 additions & 2 deletions tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ type Model struct {
moveOpen bool
moveCursor int
moveFolders []mail.Folder
moveFilter string
moveMatches []mail.Folder

accountsOpen bool
accountCursor int
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
189 changes: 189 additions & 0 deletions tui/movefilter_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
Loading
Loading