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
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ only one action, an action needs at least one key, and an unknown name is a load

## State

chute remembers the last account and the folder each account had open, in
chute remembers the last account, the folder each account had open, and which folders
each account had expanded in the folder tree, in
`$XDG_STATE_HOME/chute/state.toml` or `~/.local/state/chute/state.toml` when
`XDG_STATE_HOME` is unset. It holds names only — no credentials, no addresses, no message
data — is written `0600`, and deleting it is safe: the config remains the authority and a
name that no longer exists is ignored.
name that no longer exists is ignored. A folder absent from the expanded set is folded,
so a new state file opens the tree showing only its top level.

## Flags

Expand Down Expand Up @@ -179,7 +181,7 @@ Composer keys are fixed, not part of `[keys]`:
| `down` | `down` | Move down one message. |
| `top` | `home`, `g` | First message. |
| `bottom` | `end`, `G` | Last message. |
| `open` | `enter` | Open the message, or load the folder under the cursor. |
| `open` | `enter` | Open the message, or load the folder under the cursor. In the folder pane, a collapsed folder expands first (`right` expands without opening); the next `enter` loads it. |
| `filter` | `/` | Fuzzy, case-insensitive filter over the loaded list's sender, subject and recipients. |
| `search` | `S` | Server-side search of the open folder; free text plus `from:`, `subject:`, `text:`, `body:` and `since:YYYY-MM-DD`. `esc` returns to the folder. |
| `visual_range` | `v` | Anchor a selection range for the actions below. |
Expand All @@ -189,6 +191,8 @@ Composer keys are fixed, not part of `[keys]`:
| `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. |
| `expand_folder` | `right` | In the folder pane, expand the folder under the cursor. |
| `collapse_folder` | `left` | In the folder pane, fold the folder under the cursor. |
| `toggle_flag` | `f` | Flag or unflag. |
| `toggle_read` | `u` | Mark read or unread. |
| `attachments` | `s` | List attachments and save them to `download_dir`. |
Expand Down
4 changes: 4 additions & 0 deletions config/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const (
KeyMove = "move"
KeyEmptyTrash = "empty_trash"
KeyCollapse = "collapse"
KeyExpandFolder = "expand_folder"
KeyCollapseFolder = "collapse_folder"
KeySearch = "search"
KeySettings = "settings"
KeyHelp = "help"
Expand Down Expand Up @@ -87,6 +89,8 @@ var keyDefaults = map[string][]string{
KeyMove: {"m"},
KeyEmptyTrash: {"X"},
KeyCollapse: {"z"},
KeyExpandFolder: {"right"},
KeyCollapseFolder: {"left"},
KeySearch: {"S"},
KeySettings: {","},
KeyHelp: {"?"},
Expand Down
12 changes: 9 additions & 3 deletions config/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ const stateFilename = "state.toml"

// State is the last selection, remembered so a restart lands where the user left.
//
// It holds names only: which account was active, and which folder each account had
// open. No credentials, no addresses, no message data. The config remains the
// authority — a name that no longer exists there is simply ignored.
// It holds names only: which account was active, which folder each account had
// open, and which folders each account had expanded in the folder tree. No
// credentials, no addresses, no message data. The config remains the authority — a
// name that no longer exists there is simply ignored.
type State struct {
// Account is the account that was active, by name. Empty means none was recorded.
Account string `toml:"account"`

// Folders maps an account name to the folder it had open. A missing entry means
// the account has no recorded folder.
Folders map[string]string `toml:"folders"`

// Expanded maps an account name to the folders whose subfolders were shown. A
// folder absent from the list is collapsed, which is also the default for a new
// state file: the tree opens showing only its top level.
Expanded map[string][]string `toml:"expanded"`
}

// StatePath is the last-selection file: $XDG_STATE_HOME/chute/state.toml, or
Expand Down
33 changes: 26 additions & 7 deletions config/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ func TestSaveStateRoundTrips(t *testing.T) {
dir := withStateDir(t)

want := State{
Account: "work",
Folders: map[string]string{"work": "Archive", "fastmail": "INBOX"},
Account: "work",
Folders: map[string]string{"work": "Archive", "fastmail": "INBOX"},
Expanded: map[string][]string{"work": {"Work", "Work/2024"}},
}
if err := SaveState(want); err != nil {
t.Fatalf("SaveState: %v", err)
Expand All @@ -92,7 +93,11 @@ func TestSaveStateRoundTrips(t *testing.T) {
}

// A second write replaces the file without changing its mode.
next := State{Account: "fastmail", Folders: map[string]string{"fastmail": "Sent"}}
next := State{
Account: "fastmail",
Folders: map[string]string{"fastmail": "Sent"},
Expanded: map[string][]string{"fastmail": {"Archive"}},
}
if err := SaveState(next); err != nil {
t.Fatalf("second SaveState: %v", err)
}
Expand All @@ -108,11 +113,17 @@ func TestSaveStateRoundTrips(t *testing.T) {
}

// TestStateFileHoldsOnlyNames is the issue's privacy criterion: account and folder
// names, and nothing that could carry a credential or message content.
// names, and nothing that could carry a credential or message content. The expanded
// set is folder names too, so it belongs on the same side of that line.
func TestStateFileHoldsOnlyNames(t *testing.T) {
dir := withStateDir(t)

if err := SaveState(State{Account: "work", Folders: map[string]string{"work": "Archive"}}); err != nil {
state := State{
Account: "work",
Folders: map[string]string{"work": "Archive"},
Expanded: map[string][]string{"work": {"Work", "Work/2024"}},
}
if err := SaveState(state); err != nil {
t.Fatalf("SaveState: %v", err)
}

Expand All @@ -126,8 +137,8 @@ func TestStateFileHoldsOnlyNames(t *testing.T) {
t.Fatalf("decoding state: %v", err)
}

if len(raw) != 2 {
t.Fatalf("top-level keys = %v, want exactly account and folders", raw)
if len(raw) != 3 {
t.Fatalf("top-level keys = %v, want exactly account, folders and expanded", raw)
}
if raw["account"] != "work" {
t.Errorf("account = %v, want work", raw["account"])
Expand All @@ -141,6 +152,14 @@ func TestStateFileHoldsOnlyNames(t *testing.T) {
t.Errorf("folders = %v, want work = Archive", folders)
}

expanded, ok := raw["expanded"].(map[string]any)
if !ok {
t.Fatalf("expanded = %T, want a table", raw["expanded"])
}
if got, want := expanded["work"], []any{"Work", "Work/2024"}; !reflect.DeepEqual(got, want) {
t.Errorf("expanded[work] = %v, want %v", got, want)
}

for _, forbidden := range []string{"password", "username", "imap", "smtp"} {
if strings.Contains(strings.ToLower(string(data)), forbidden) {
t.Errorf("state file mentions %q:\n%s", forbidden, data)
Expand Down
1 change: 1 addition & 0 deletions tui/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (m Model) selectAccount(index int) (tea.Model, tea.Cmd) {
m.folders = nil
m.folderCursor = 0
m.folderScroll = 0
m.folderExpanded = nil

m.openFolder = ""
m.messages = nil
Expand Down
4 changes: 3 additions & 1 deletion tui/collapse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func TestCollapseFitsTheFrame(t *testing.T) {
assertExactFrame(t, m, "collapsed thread")
}

// TestCollapseKeyOutsideTheListIsInert proves the triage guard applies.
// TestCollapseKeyOutsideTheListIsInert proves the triage guard applies: z collapses
// threads, so in the folder pane it does nothing. Folding a folder is left's job
// (TestArrowKeysExpandAndFoldTheFolder), not the thread gesture's.
func TestCollapseKeyOutsideTheListIsInert(t *testing.T) {
m := loaded(t, 140, 40, []mail.Folder{{Name: "INBOX", Delimiter: '/'}}, nil)
m.focus = focusFolders
Expand Down
90 changes: 61 additions & 29 deletions tui/folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,45 @@ func (m Model) folderRows() int {
return rows
}

// moveFolderCursor moves the selection and keeps it inside the scroll window.
// moveFolderCursor moves the selection and keeps it inside the scroll window. The
// cursor indexes the visible tree, not the listing: a hidden subfolder cannot be
// selected, and a collapsed parent keeps the cursor where the user left it.
func (m *Model) moveFolderCursor(delta int) {
if len(m.folders) == 0 {
rows := m.folderTree()
if len(rows) == 0 {
return
}

m.folderCursor = clamp(m.folderCursor+delta, 0, len(m.folders)-1)
m.folderCursor = clamp(m.folderCursor+delta, 0, len(rows)-1)

rows := m.folderRows()
visible := m.folderRows()
if m.folderCursor < m.folderScroll {
m.folderScroll = m.folderCursor
}
if m.folderCursor >= m.folderScroll+rows {
m.folderScroll = m.folderCursor - rows + 1
if m.folderCursor >= m.folderScroll+visible {
m.folderScroll = m.folderCursor - visible + 1
}
}

// clampFolderScroll keeps the stored scroll offset valid after a reload or resize.
// clampFolderScroll keeps the stored scroll offset valid after a reload, a resize or a
// fold that changed how many rows are drawn.
func (m *Model) clampFolderScroll() {
rows := m.folderRows()
maxScroll := max(0, len(m.folders)-rows)
rows := len(m.folderTree())
visible := m.folderRows()
maxScroll := max(0, rows-visible)

m.folderScroll = clamp(m.folderScroll, 0, maxScroll)
m.folderCursor = clamp(m.folderCursor, 0, max(0, len(m.folders)-1))
m.folderCursor = clamp(m.folderCursor, 0, max(0, rows-1))

if m.folderCursor < m.folderScroll {
m.folderScroll = m.folderCursor
}
if m.folderCursor >= m.folderScroll+rows {
m.folderScroll = m.folderCursor - rows + 1
if m.folderCursor >= m.folderScroll+visible {
m.folderScroll = m.folderCursor - visible + 1
}
}

// folderLines renders the visible slice of the folder list.
// folderLines renders the visible slice of the folder tree.
func (m Model) folderLines(rows, width int) []string {
if rows < 1 {
return nil
Expand All @@ -77,24 +82,50 @@ func (m Model) folderLines(rows, width int) []string {
return []string{MutedStyle.Render("no folders")}
}

start := clamp(m.folderScroll, 0, max(0, len(m.folders)-rows))
end := min(start+rows, len(m.folders))
tree := m.folderTree()
start := clamp(m.folderScroll, 0, max(0, len(tree)-rows))
end := min(start+rows, len(tree))

lines := make([]string, 0, end-start)
for i := start; i < end; i++ {
lines = append(lines, m.folderLine(i, width))
lines = append(lines, m.folderTreeLine(tree[i], i == m.folderCursor, width))
}

return lines
}

// folderLine renders one folder, indented by depth and marked when selected.
func (m Model) folderLine(index, width int) string {
return folderRow(m.folders[index], index == m.folderCursor, width)
// folderTreeLine renders one row of the folder tree in the shape imap-notes uses: the
// +/- marker leads the row and the cursor is the bold row, with no separate glyph, so
// the marker is never displaced by the selection. A folder with no subfolders shows a
// blank marker rather than a sign that promises nothing.
func (m Model) folderTreeLine(row folderTreeRow, selected bool, width int) string {
folder := m.folders[row.index]

marker := " "
if row.hasChildren {
if row.expanded {
marker = "- "
} else {
marker = "+ "
}
}

label := strings.Repeat(" ", row.depth) + folder.Leaf()
label = Truncate(label, max(1, width-2))
line := marker + label

switch {
case selected:
return SelectedStyle.Render(line)
case !folder.Selectable():
return MutedStyle.Render(line)
}

return NormalStyle.Render(line)
}

// folderRow renders one folder row: indent, leaf, marker and styling. The label
// is truncated so a long name never wraps onto a second line.
// folderRow renders one folder row for a flat list: the folders overlay and the move
// picker show every mailbox, including the ones the tree currently hides.
func folderRow(folder mail.Folder, selected bool, width int) string {
label := strings.Repeat(" ", folder.Depth()) + folder.Leaf()
label = Truncate(label, max(1, width-2))
Expand All @@ -111,21 +142,22 @@ func folderRow(folder mail.Folder, selected bool, width int) string {

// selectedFolder is the folder under the cursor, if there is one.
func (m Model) selectedFolder() (mail.Folder, bool) {
if m.folderCursor < 0 || m.folderCursor >= len(m.folders) {
row, ok := m.selectedTreeRow()
if !ok {
return mail.Folder{}, false
}

return m.folders[m.folderCursor], true
return m.folders[row.index], true
}

// moveFolderCursorTo puts the folder cursor on folder, if the listing still has it.
// moveFolderCursorTo puts the folder cursor on folder and reveals its path, if the
// listing still has it.
func (m *Model) moveFolderCursorTo(folder mail.Folder) {
for i := range m.folders {
if m.folders[i].Name == folder.Name {
m.folderCursor = i
m.expandTo(folder.Name)

return
}
if index := m.treeRowIndex(folder.Name); index >= 0 {
m.folderCursor = index
m.clampFolderScroll()
}
}

Expand Down
10 changes: 8 additions & 2 deletions tui/folders_overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (m Model) openFolders() (tea.Model, tea.Cmd) {
}

m.foldersOpen = true
m.folderOverlayCursor = m.folderCursor
m.folderOverlayCursor = m.folderIndex()
m.status = ""

return m, nil
Expand All @@ -44,8 +44,14 @@ func (m Model) handleFoldersKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.folderOverlayCursor = min(m.folderOverlayCursor+1, len(m.folders)-1)

case actionOpen:
m.folderCursor = m.folderOverlayCursor
if m.folderOverlayCursor < 0 || m.folderOverlayCursor >= len(m.folders) {
return m, nil
}
// The overlay is a flat picker: it opens a mailbox, it does not fold the tree.
// Reveal the chosen folder's path first so the tree cursor can land on it.
m.moveFolderCursorTo(m.folders[m.folderOverlayCursor])
m.foldersOpen = false

return m.openSelectedFolder()
}

Expand Down
18 changes: 11 additions & 7 deletions tui/folders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,22 @@ func TestResizeClampsFolderScroll(t *testing.T) {
}
}

func TestFolderLineIndentation(t *testing.T) {
func TestFolderTreeIndentation(t *testing.T) {
m := loaded(t, 140, 40, []mail.Folder{
{Name: "Work", Delimiter: '/'},
{Name: "Work/Projects", Delimiter: '/'},
}, nil)
m.setFolderExpanded("Work", true)

nested := m.folderLine(1, 40)
if !strings.Contains(nested, " Projects") {
t.Errorf("nested line = %q, want two spaces before the leaf", nested)
lines := m.folderLines(10, 40)
if len(lines) < 2 {
t.Fatalf("folder lines = %v, want Work and its child", lines)
}
if strings.Contains(nested, "Work/") {
t.Errorf("nested line = %q, want the leaf only", nested)
if !strings.Contains(lines[1], " Projects") {
t.Errorf("nested line = %q, want two spaces before the leaf", lines[1])
}
if strings.Contains(lines[1], "Work/") {
t.Errorf("nested line = %q, want the leaf only", lines[1])
}
}

Expand All @@ -199,7 +203,7 @@ func TestFolderLineTruncates(t *testing.T) {
{Name: "a folder name long enough to need truncation", Delimiter: '/'},
}, nil)

line := m.folderLine(0, 20)
line := m.folderLines(1, 20)[0]
if !strings.Contains(line, "...") {
t.Errorf("folder line %q has no ellipsis", line)
}
Expand Down
Loading
Loading