Repository navigation
Keep Move, label, and collection actions in compact modals #671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WhiskeyTuesday
wants to merge
4
commits into
basecamp:main
Choose a base branch
from
WhiskeyTuesday:tui-action-modals
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
470e122
Show mail actions in compact modal pickers
WhiskeyTuesday 6285b2d
Show wrapped thread subjects in action menus
WhiskeyTuesday ce03e30
Preserve thread scroll through modal layout changes
WhiskeyTuesday 9bf1f84
Exercise action picker preservation through the root model
WhiskeyTuesday File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| 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") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 (
vandb) and Escape throughmodel.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 coverv,b, andnfrom 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.