Skip to content
Open
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
8 changes: 8 additions & 0 deletions docs/tui.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ Sending a reply or a forward from a thread closes the thread and returns you to

While reading a thread, the From header shows the actual send-as address when HEY records one separately from the account user.

Move (`v`), labels (`b`), and collection membership (`n`) open compact menus over
the current mail screen. The thread subject wraps above the choices when there
is room; an excerpt is used only when the subject is absent. Use the arrow keys and Enter to choose an action, or Escape
to dismiss it. In labels and collections, type to filter names, Backspace to edit
the filter, and Ctrl+U to clear it. Current memberships have an `[x]` mark. Long
lists scroll within the menu. Creating a label opens a name field in the same
menu; Escape returns to the choices.

While reading a thread, links can be selected without a mouse. Tab selects the next link in document order and Shift+Tab selects the previous one; both wrap at the ends. A fixed row above the shortcut bar shows the selected destination without moving the thread. A destination too long for one row wraps onto as many rows as it needs, so you always see all of it. Press Enter to open it; opening stays unavailable until the terminal is tall enough to show the whole destination. Press Escape once to clear the selection, and Escape again to leave the thread. A thread with no selectable links keeps the normal global Tab focus behavior. Existing OSC 8 mouse links remain available.

Most of those keep working while you are reading a thread, the way the web app's topic
Expand Down
74 changes: 74 additions & 0 deletions internal/tui/action_picker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package tui

import (
"strings"
"unicode/utf8"

tea "charm.land/bubbletea/v2"

"github.com/basecamp/hey-cli/internal/mail"
"github.com/basecamp/hey-cli/internal/terminal"
)

// Keep short choices compact, but let a small terminal use all the space it has.
func actionPickerFrame(title, subject, filter, hint string, names []string, cursor, width, height int) string {
width = min(width, 64)
rows := modalContentRows(min(height, 18))
contentWidth := modalContentWidth(width)
var before, after []string
if filter != "" && rows >= 2 {
before = append(before, truncateToWidth("Filter: "+terminal.SanitizeLine(filter), contentWidth))
}
if hint != "" && rows >= 5 {
after = append(after, truncateToWidth(hint, contentWidth))
}
// Keep the complete subject when it fits; on a tiny terminal the choices take
// priority over a fragment that cannot identify the thread.
if subject != "" && rows >= 5 {
lines := wrapText(terminal.SanitizeLine(subject), contentWidth)
if len(lines) <= rows-len(before)-len(after)-1 {
before = append(lines, before...)
}
}
visible := min(8, max(rows-len(before)-len(after), 1))
list := modalListRows(names, cursor, contentWidth, visible)
if len(names) == 0 {
list = []string{truncateToWidth("No matches", contentWidth)}
}
body := append(before, list...)
body = append(body, after...)
return modalFrame(title, strings.Join(body, "\n"), width)
}

// HEY's Name is the thread subject; Summary is only a message excerpt.
func pickerThreadSubject(posting mail.Posting) string {
subject := strings.TrimSpace(terminal.SanitizeLine(posting.Name))
if subject == "" {
return terminal.SanitizeLine(posting.Summary)
}
return subject
}

func pickerNameMatches(name, filter string) bool {
return strings.Contains(strings.ToLower(terminal.SanitizeLine(name)), strings.ToLower(filter))
}

// These lists only need an append-and-backspace filter; label creation keeps its
// existing text input with cursor movement and editing.
func editPickerFilter(filter *string, msg tea.KeyPressMsg) bool {
previous := *filter
switch {
case msg.Key().Code == tea.KeyBackspace:
if len(*filter) > 0 {
_, size := utf8.DecodeLastRuneInString(*filter)
*filter = (*filter)[:len(*filter)-size]
}
case msg.String() == "ctrl+u":
*filter = ""
case msg.Key().Text != "" && msg.Key().Mod&(tea.ModCtrl|tea.ModAlt|tea.ModSuper) == 0:
text := terminal.SanitizeLine(msg.Key().Text)
runes := []rune(*filter + text)
*filter = string(runes[:min(len(runes), 80)])
}
return previous != *filter
}
277 changes: 277 additions & 0 deletions internal/tui/action_picker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
package tui

import (
"fmt"
"strings"
"testing"

tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"

"github.com/basecamp/hey-cli/internal/mail"
)

func TestActionPickersPreserveTheScreenBehindThem(t *testing.T) {
for _, key := range []string{"v", "b", "n"} {
for _, screen := range []string{"inbox", "thread", "search", "bundle", "seen"} {
t.Run(key+"/"+screen, func(t *testing.T) {
v := modalViewWithPostings(t)
v.Resize(100, 30)
v.postingList.toggleSelected()
switch screen {
case "thread":
v.inThread = true
v.threadPosting = v.postingList.postings[0]
v.topicViewport.SetContent(strings.Repeat("Hotel newsletter body\n", 80))
v.topicViewport.SetYOffset(10)
case "search":
v.searchActive = true
v.searchList.setPostings([]mail.Posting{{ID: 201, TopicID: 301, Summary: "Search result"}})
case "bundle":
v.bundleActive = true
v.bundleList.setPostings([]mail.Posting{{ID: 201, TopicID: 301, Summary: "Bundle thread"}})
case "seen":
v.seenActive = true
v.seenList.setPostings([]mail.Posting{{ID: 201, TopicID: 301, Summary: "Seen thread"}})
}
// Bulk-selection policy is separate from these single-thread presentations.
// Open the picker directly to exercise the same rendering from every screen.
before := v.View()
offset := v.topicViewport.YOffset()
switch key {
case "v":
v.startMove()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ce03e30, with root-model coverage in 9bf1f84. The new tests send the supported thread shortcuts (v and b) and Escape through model.Update, covering scrolling partway down and to the bottom, with and without a selected link whose destination wraps across footer rows. They check the viewport offset and logical link selection, and compare the complete rendered screen before and after dismissal. Separate root-model tests cover v, b, and n from the mail list and preserve its cursor.

These tests reproduced the same modal layout/scroll issue reported in #670, so this independent branch includes the same fix. TUI/mail tests and lint pass; offline PTY checks on the combined build also verified rendered-screen restoration for Move and labels.

case "b":
v.startFolderPicker()
case "n":
v.startCollectionPicker()
}
if v.modal == nil {
t.Fatal("picker did not open")
}
rendered := stripANSI(v.View())
if !strings.Contains(rendered, "╭") {
t.Fatal("picker did not draw a frame")
}
if screen == "thread" && !strings.Contains(rendered, "Hotel newsletter body") {
t.Fatal("thread disappeared behind picker")
}
if key == "v" {
p := moveModal(v)
frame := p.view(v.vc.styles, v.vc.width)
if lipgloss.Width(frame) >= v.vc.width || lipgloss.Height(frame) >= v.vc.height {
t.Fatal("short picker consumed the screen")
}
}
v.HandleContentKey(keyPress("esc"))
if v.modal != nil || v.View() != before || v.topicViewport.YOffset() != offset || len(v.postingList.selectedIDs()) != 1 {
t.Fatal("dismissal changed the underlying screen, scroll, or selection")
}
})
}
}
}

func TestActionPickerFilteringKeepsTheCorrectTarget(t *testing.T) {
v, recorded := mailWithTestServer(t, 200)
v.boxes = append(v.boxes,
mail.Source{ID: 12, Kind: mail.KindFolder, Name: "Hotel receipts"},
mail.Source{ID: 13, Kind: mail.KindFolder, Name: "Travel plans"},
mail.Source{ID: 24, Kind: mail.KindCollection, Name: "Hotel receipts"},
mail.Source{ID: 25, Kind: mail.KindCollection, Name: "Travel plans"})
v.postingList.postings[0].TopicID = 501
v.postingList.postings[0].Folders = []mail.Folder{{ID: 13, Name: "Travel plans"}}
v.HandleContentKey(keyPress("b"))
v.HandleContentKey(keyPress("down"))
v.HandleContentKey(keyPress("HOTEL"))
p := folderModal(v)
if p.cursor != 0 || p.selected().folder.ID != 12 {
t.Fatal("filter kept a stale cursor or wrong label")
}
if screen := stripANSI(v.View()); strings.Contains(screen, "[x] Travel plans") || !strings.Contains(screen, "Filter: HOTEL") {
t.Fatal("filter did not update label choices")
}
cmd := v.HandleContentKey(keyPress("enter"))
done := cmd()
v.Update(done)
if recorded.path != "/postings/filings.json" || recorded.body.FolderID == nil || *recorded.body.FolderID != 12 {
t.Fatalf("wrong label operation: %s", recorded.path)
}
v.HandleContentKey(keyPress("n"))
v.HandleContentKey(keyPress("Travel"))
c := collectionModal(v)
if c.selected() == nil || c.selected().ID != 25 {
t.Fatal("collection filter used an unfiltered index")
}
v.HandleContentKey(keyPress("zzzz"))
if c.selected() != nil || !strings.Contains(stripANSI(v.View()), "No matches") {
t.Fatal("no-match filter retained an actionable collection")
}
if cmd := v.HandleContentKey(keyPress("enter")); cmd != nil || v.modal != c {
t.Fatal("Enter on no matches dispatched or dismissed")
}
v.HandleContentKey(tea.KeyPressMsg{Code: 'u', Mod: tea.ModCtrl})
if c.filter != "" || c.selected().ID != 24 {
t.Fatal("clearing filter failed to restore choices")
}
v.HandleContentKey(keyPress("esc"))
v.postingList.postings[0].Collections = []mail.Collection{{ID: 25, Name: "Travel plans"}}
v.HandleContentKey(keyPress("n"))
v.HandleContentKey(keyPress("travel"))
collection := collectionModal(v)
if collection.selected() == nil || collection.selected().ID != 25 {
t.Fatal("filter targeted the wrong collection")
}
if !strings.Contains(stripANSI(v.View()), "[x] Travel plans") {
t.Fatal("filtered collection lost membership mark")
}
cmd = v.HandleContentKey(keyPress("enter"))
cmd()
if recorded.method != "DELETE" || recorded.path != "/topics/501/collecting" || recorded.rawQueries[len(recorded.rawQueries)-1] != "collection_id=25" {
t.Fatalf("wrong filtered collection operation: %s %s %v", recorded.method, recorded.path, recorded.rawQueries)
}
}

func TestLabelCreationPreservesFilterOnBack(t *testing.T) {
v := modalViewWithPostings(t)
v.startFolderPicker()
v.HandleContentKey(keyPress("missing"))
p := folderModal(v)
if p.selected().kind != folderPickerCreate {
t.Fatal("create is unreachable with no matching labels")
}
v.HandleContentKey(keyPress("enter"))
if !p.creating || !strings.Contains(stripANSI(v.View()), "Create label") {
t.Fatal("create did not stay in modal")
}
p.input.SetValue(strings.Repeat("Holiday receipts ", 10))
p.input.SetCursor(70)
for _, width := range []int{24, 40, 80} {
v.Resize(width, 24)
frame := p.view(newStyles(), width)
if lipgloss.Width(frame) > width {
t.Fatalf("create label exceeds width %d: %d", width, lipgloss.Width(frame))
}
}
v.HandleContentKey(keyPress("esc"))
if v.modal != p || p.creating || p.filter != "missing" || p.selected().kind != folderPickerCreate {
t.Fatal("back lost filtered choices")
}
v.HandleContentKey(keyPress("esc"))
if v.modal != nil {
t.Fatal("second Escape did not dismiss")
}
}

func TestActionPickerFilteringHandlesUnicodeAndControls(t *testing.T) {
filter := ""
editPickerFilter(&filter, keyPress("café"))
editPickerFilter(&filter, tea.KeyPressMsg{Code: tea.KeyBackspace})
if filter != "caf" {
t.Fatalf("backspace split Unicode: %q", filter)
}
editPickerFilter(&filter, keyPress("\x1b]2;owned\a"))
if filter != "caf" {
t.Fatalf("filter retained control sequence: %q", filter)
}
editPickerFilter(&filter, keyPress(strings.Repeat("é", 200)))
if len([]rune(filter)) != 80 {
t.Fatal("filter exceeded its limit")
}
if !pickerNameMatches("Café\x1b]2;owned\a receipts", "CAFÉ") {
t.Fatal("matching differs from displayed names")
}
}

func TestActionPickerFitsSmallScreensAndScrollsAfterResize(t *testing.T) {
sources := make([]mail.Source, 0, 40)
for i := range 40 {
sources = append(sources, mail.Source{ID: int64(i + 100), Kind: mail.KindFolder, Name: fmt.Sprintf("Label %02d", i)})
}
p := newFolderPicker(mail.Posting{Summary: "An estimate for kitchen cabinets"}, sources)
for range 39 {
p.moveCursor(keyPress("down"))
}
for _, size := range [][2]int{{80, 24}, {24, 8}, {16, 7}, {80, 24}} {
p.resize(size[0], size[1])
frame := p.view(newStyles(), size[0])
if lipgloss.Width(frame) > size[0] || lipgloss.Height(frame) > size[1] {
t.Fatalf("frame exceeds %v: %dx%d", size, lipgloss.Width(frame), lipgloss.Height(frame))
}
if size[0] >= 24 && !strings.Contains(stripANSI(frame), "Label 39") {
t.Fatal("resize hid the selected label")
}
}
}

func TestActionPickersShowTheWrappedSubject(t *testing.T) {
const subject = "Your updated travel itinerary and hotel reservation for the long weekend in New Orleans"
posting := mail.Posting{ID: 100, Name: subject, Summary: "Plus, agent controls, single-use cards, and a clearer view of your spending"}
sources := []mail.Source{{ID: 12, Kind: mail.KindFolder, Name: "Travel plans"}, {ID: 24, Kind: mail.KindCollection, Name: "Receipts"}}
folders := newFolderPicker(posting, sources)
collections := newCollectionMembershipPicker(posting, sources)
move := newMovePicker(posting, []mail.Source{{ID: 2, Kind: mail.KindBox, Name: "The Feed"}}, mail.Source{ID: 1, Kind: mail.KindBox})
for _, picker := range []interface {
resize(int, int)
view(styles, int) string
}{folders, collections, move} {
picker.resize(80, 24)
frame := picker.view(newStyles(), 80)
text := strings.Join(strings.Fields(stripANSI(frame)), " ")
for _, line := range wrapText(subject, modalContentWidth(64)) {
if !strings.Contains(text, line) {
t.Fatalf("subject was clipped: %q", text)
}
}
if strings.Contains(text, "agent controls") || strings.Contains(text, "...") {
t.Fatalf("picker used a clipped message excerpt: %q", text)
}
if lipgloss.Width(frame) > 64 || lipgloss.Height(frame) > 24 {
t.Fatal("wrapping overflowed the menu")
}
}
if got := pickerThreadSubject(mail.Posting{Summary: "Hotel reservation confirmed"}); got != "Hotel reservation confirmed" {
t.Fatalf("missing subject fallback: %q", got)
}
}

func TestActionPickerLongContextLeavesChoicesUsable(t *testing.T) {
for _, height := range []int{8, 12, 24} {
frame := actionPickerFrame("Label thread", strings.Repeat("A very long subject ", 100), "travel", "Type to filter", []string{"Travel plans"}, 0, 40, height)
if lipgloss.Height(frame) > height || !strings.Contains(stripANSI(frame), "Travel plans") {
t.Fatal("long subject crowded out the selected action")
}
}
}

func TestActionPickersPreserveScrollThroughModel(t *testing.T) {
for _, key := range []string{"v", "b"} {
t.Run(key, func(t *testing.T) {
testPickerScrollThroughModel(t, key)
})
}
}

func TestActionPickersPreserveListThroughModel(t *testing.T) {
for _, key := range []string{"v", "b", "n"} {
t.Run(key, func(t *testing.T) {
m := modelWithBoxes()
v := m.mailView
v.boxes = append(v.boxes, mail.Source{ID: 12, Kind: mail.KindFolder, Name: "Travel plans"}, mail.Source{ID: 24, Kind: mail.KindCollection, Name: "Hotel receipts"})
updated, _ := m.Update(keyPress("down"))
m = updated.(model)
posting := v.postingList.selectedPosting().ID
before := m.View().Content
updated, _ = m.Update(keyPress(key))
m = updated.(model)
if v.modal == nil {
t.Fatal("picker did not open")
}
updated, _ = m.Update(keyPress("esc"))
m = updated.(model)
if v.modal != nil || v.postingList.selectedPosting().ID != posting || m.View().Content != before {
t.Fatal("dismissal changed the list or cursor")
}
})
}
}
Loading