Skip to content

Commit 3940be0

Browse files
Keep message headers within the available terminal space
1 parent d856490 commit 3940be0

6 files changed

Lines changed: 145 additions & 52 deletions

File tree

‎docs/tui.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,9 @@ takes you to the Imbox, as the web app does.
224224
Space opens a bigger preview of the selected sender's most recent email: the whole message,
225225
not just the first line the list has room for. The header names the address it arrived
226226
through and the To, Cc and Bcc lines, the same way an opened thread does. Arrow keys and
227-
PgUp/PgDn scroll it, `y` and `n` answer for that sender straight from it, and Space or
228-
Escape closes it again. Opening a preview only reads the email; it doesn't screen the
227+
PgUp/PgDn scroll it; on short terminals the headers scroll with the message so every
228+
address remains accessible. `y` and `n` answer for that sender straight from it, and
229+
Space or Escape closes it again. Opening a preview only reads the email; it doesn't screen the
229230
sender or tell them anything.
230231

231232
The preview supports the same `/` find prompt and `n`/`N` match navigation as the thread

‎internal/mail/entry_test.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ func TestNewEntryKeepsTheEnvelopeApartFromTheResolvedContact(t *testing.T) {
8686
}},
8787
})
8888

89-
if got, want := entry.Recipients.To[0], (Contact{ID: 21, Name: "Elija Sorensen", EmailAddress: "eli@example.com"}); got != want {
89+
if len(entry.Recipients.To) != 1 {
90+
t.Errorf("to = %+v, want one recipient", entry.Recipients.To)
91+
} else if got, want := entry.Recipients.To[0], (Contact{ID: 21, Name: "Elija Sorensen", EmailAddress: "eli@example.com"}); got != want {
9092
t.Errorf("to = %+v, want %+v", got, want)
9193
}
9294
if len(entry.Recipients.Cc) != 1 || entry.Recipients.Cc[0].EmailAddress != "sam@example.com" {

‎internal/tui/mail.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3787,19 +3787,20 @@ func (v *mailView) renderEntriesWithLinks(entries []mail.Entry) (string, []int,
37873787
offsets = append(offsets, lineCount)
37883788

37893789
from, tag, internal := headingFrom(e)
3790-
date := v.vc.styles.entryDate.Render(formatDisplayDateTime(e.CreatedAt))
3790+
date := formatDisplayDateTime(e.CreatedAt)
37913791
if internal {
3792-
date += v.vc.styles.entryDate.Render(" · " + tag)
3792+
date += " · " + tag
37933793
}
3794-
heading := v.vc.styles.entryFrom.Render(from)
3794+
date = truncateStr(date, max(v.vc.width-2, 0))
3795+
heading := v.vc.styles.entryFrom.Render(truncateStr(from, max(v.vc.width-2-displayWidth(date), 0)))
37953796

37963797
// Each arm below opens with a blank line, which is what separates the header
37973798
// from whatever follows it. The summary is HEY's ~105-character preview of the
37983799
// body, so it stands in only where HEY served no body at all — the same ladder
37993800
// as printThreadStyled in internal/cmd/topic.go. Printed beside a body it repeats
38003801
// the message's opening line; printed for a body that was read and rendered to
38013802
// nothing, or for one that was not read, it passes a preview off as the message.
3802-
write(fmt.Sprintf("%s %s\n", heading, date))
3803+
write(fmt.Sprintf("%s %s\n", heading, v.vc.styles.entryDate.Render(date)))
38033804
if !internal {
38043805
for _, line := range envelopeLines(e.Recipients, e.ReceivedVia, max(v.vc.width, 1)) {
38053806
write(v.vc.styles.entryDate.Render(line) + "\n")

‎internal/tui/mail_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4950,3 +4950,19 @@ func TestMailViewTrashFailureKeepsSelection(t *testing.T) {
49504950
t.Errorf("postings = %d selected = %v, want both rows still selected", len(v.postingList.postings), v.postingList.selectedIDs())
49514951
}
49524952
}
4953+
4954+
func TestRenderEntriesFitsLongSenderBesideDate(t *testing.T) {
4955+
v := newMailView(testVC())
4956+
entry := mail.Entry{
4957+
Creator: mail.Contact{Name: strings.Repeat("Maria Gonzalez ", 8), EmailAddress: "maria.gonzalez@example.org"},
4958+
BodyState: string(threadload.StateHydrated),
4959+
}
4960+
for _, width := range []int{20, 40, 80} {
4961+
v.vc.width = width
4962+
rendered := ansi.Strip(mustRender(t, v, []mail.Entry{entry}))
4963+
heading := strings.Split(strings.TrimLeft(rendered, "\n"), "\n")[0]
4964+
if got := displayWidth(heading); got > width {
4965+
t.Errorf("sender heading is %d columns with %d available: %q", got, width, heading)
4966+
}
4967+
}
4968+
}

‎internal/tui/screener_preview.go‎

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,19 @@ type screenerPreviewLoadedMsg struct {
3232
// recent email in full, over the queue, where a row only has room for its first line.
3333
// It is read-only — the answer is still Yes or No, and either one closes it.
3434
type screenerPreview struct {
35-
row screenerRow
36-
loading bool
37-
failure string
38-
body htmlutil.Markdown
39-
recipients mail.Recipients
40-
receivedVia []string
41-
width int // the width body was last rendered at, 0 when it needs rendering again
42-
viewport viewport.Model
43-
find bodyFind
44-
lines []string
45-
linkRuns []markdown.LinkTextRun
35+
row screenerRow
36+
loading bool
37+
failure string
38+
body htmlutil.Markdown
39+
recipients mail.Recipients
40+
receivedVia []string
41+
width int // the width body was last rendered at, 0 when it needs rendering again
42+
scrollHeader string // header included in the viewport on short terminals
43+
bodyStart int // body offset inside the viewport's lines
44+
viewport viewport.Model
45+
find bodyFind
46+
lines []string
47+
linkRuns []markdown.LinkTextRun
4648
}
4749

4850
// openPreview reads the most recent email of the sender under the cursor. HEY serves its
@@ -159,9 +161,6 @@ func (v *screenerView) handlePreviewKey(msg tea.KeyPressMsg) tea.Cmd {
159161
}
160162
return v.screenRow(row, status)
161163
}
162-
if v.preview.loading || v.preview.failure != "" {
163-
return nil
164-
}
165164
v.layoutPreview()
166165
var cmd tea.Cmd
167166
v.preview.viewport, cmd = v.preview.viewport.Update(msg)
@@ -205,60 +204,72 @@ func (v *screenerView) previewHeader() string {
205204
return b.String()
206205
}
207206

208-
// layoutPreview fits the viewport under the header and renders the body at the width
209-
// there is, again only when the width changed since the last time.
207+
// layoutPreview keeps the header fixed when there is room for it and a body row.
208+
// On short terminals the header scrolls with the body so every address stays reachable.
210209
func (v *screenerView) layoutPreview() {
211210
preview := v.preview
212-
headerLines := strings.Count(v.previewHeader(), "\n")
213-
if v.notice != "" {
214-
headerLines++
211+
header := v.previewHeader()
212+
headerLines := strings.Count(header, "\n")
213+
height := max(v.vc.height, 1)
214+
if v.notice != "" && height > 1 {
215+
height--
216+
}
217+
scrollHeader := ""
218+
if headerLines >= height {
219+
scrollHeader = header
220+
headerLines = 0
221+
}
222+
if preview.scrollHeader != scrollHeader {
223+
preview.scrollHeader = scrollHeader
224+
preview.width = 0
215225
}
216226
preview.viewport.SetWidth(max(v.vc.width, 1))
217227
oldHeight := preview.viewport.Height()
218-
preview.viewport.SetHeight(max(v.vc.height-headerLines, 1))
228+
preview.viewport.SetHeight(height - headerLines)
219229
width := max(v.vc.width-4, 40)
220-
if preview.loading || preview.failure != "" {
221-
return
222-
}
223-
if preview.width == width {
230+
if !preview.loading && preview.failure == "" && preview.width == width {
224231
if oldHeight != preview.viewport.Height() {
225232
v.revealPreviewMatch()
226233
}
227234
return
228235
}
229-
preview.width = width
230-
linked := markdown.RenderLinked(preview.body, width, -1)
231-
content := linked.Text
236+
var content string
232237
preview.linkRuns = nil
233-
for _, link := range linked.Links {
234-
preview.linkRuns = append(preview.linkRuns, link.TextRuns...)
235-
}
236-
if preview.body.IsEmpty() {
237-
content = styleMuted.Render("(this email has no text)")
238+
switch {
239+
case preview.loading:
240+
content = styleMuted.Render(" Loading…")
241+
case preview.failure != "":
242+
content = styleMuted.Render(" " + preview.failure)
243+
default:
244+
preview.width = width
245+
linked := markdown.RenderLinked(preview.body, width, -1)
246+
content = linked.Text
247+
for _, link := range linked.Links {
248+
preview.linkRuns = append(preview.linkRuns, link.TextRuns...)
249+
}
250+
if preview.body.IsEmpty() {
251+
content = styleMuted.Render("(this email has no text)")
252+
}
238253
}
239254
offset := preview.viewport.YOffset()
240-
preview.lines = strings.Split(content, "\n")
255+
preview.bodyStart = strings.Count(scrollHeader, "\n")
256+
preview.lines = strings.Split(scrollHeader+content, "\n")
241257
v.scanPreviewFind()
242258
preview.viewport.SetContentLines(preview.find.highlight(preview.lines))
243259
preview.viewport.SetYOffset(offset)
244260
v.revealPreviewMatch()
245261
}
246262

247263
func (v *screenerView) previewView() string {
264+
v.layoutPreview()
248265
var b strings.Builder
249-
if v.notice != "" {
266+
if v.notice != "" && v.vc.height > 1 {
250267
b.WriteString(v.vc.styles.title.Render(truncateStr(v.notice, max(v.vc.width, 1))) + "\n")
251268
}
252-
b.WriteString(v.previewHeader())
253-
switch {
254-
case v.preview.loading:
255-
b.WriteString(styleMuted.Render(" Loading…"))
256-
case v.preview.failure != "":
257-
b.WriteString(styleMuted.Render(" " + v.preview.failure))
258-
default:
259-
v.layoutPreview()
260-
b.WriteString(v.preview.viewport.View())
269+
if v.preview.scrollHeader == "" {
270+
b.WriteString(v.previewHeader())
261271
}
272+
b.WriteString(v.preview.viewport.View())
262273
return b.String()
263274
}
264275

@@ -268,7 +279,7 @@ func (v *screenerView) scanPreviewFind() {
268279
preview.find.scan()
269280
return
270281
}
271-
preview.find.scan(findDocument{lines: preview.lines, links: preview.linkRuns})
282+
preview.find.scan(findDocument{lines: preview.lines[preview.bodyStart:], start: preview.bodyStart, links: preview.linkRuns})
272283
}
273284

274285
func (v *screenerView) showPreviewMatch() {

‎internal/tui/screener_preview_test.go‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package tui
22

33
import (
4+
"fmt"
45
"net/http"
56
"strings"
67
"testing"
78

89
"github.com/charmbracelet/x/ansi"
10+
11+
"github.com/basecamp/hey-cli/internal/mail"
912
)
1013

1114
// openedPreview presses space on the first sender and lands the email it reads.
@@ -181,7 +184,7 @@ func TestScreenerPreviewHidesTheEnvelopeUntilTheMessageArrives(t *testing.T) {
181184
// A long email scrolls inside the preview; the header stays put.
182185
func TestScreenerPreviewScrolls(t *testing.T) {
183186
view, _ := openedPreview(t)
184-
view.vc.height = 6
187+
view.vc.height = 10
185188
view.preview.body = htmlToMarkdown(strings.Repeat("<p>Another paragraph about the quarterly budget.</p>", 30))
186189
view.preview.width = 0
187190

@@ -295,3 +298,62 @@ func TestScreenerFindDoesNotSearchTheEmptyBodyPlaceholder(t *testing.T) {
295298
view.layoutPreview()
296299
assertNoPlaceholderMatch()
297300
}
301+
302+
func TestScreenerPreviewFitsShortTerminals(t *testing.T) {
303+
view, _ := openedPreview(t)
304+
view.preview.recipients.Bcc = []mail.Contact{{EmailAddress: "audit@example.org"}}
305+
for _, height := range []int{1, 3, 6, 7, 8, 20, 6, 20} {
306+
for _, notice := range []string{"", "Screening decision saved"} {
307+
t.Run(fmt.Sprintf("height=%d/notice=%s", height, notice), func(t *testing.T) {
308+
view.vc.height = height
309+
view.notice = notice
310+
view.preview.viewport.GotoTop()
311+
screen := view.previewView()
312+
if rows := strings.Count(screen, "\n") + 1; rows > height {
313+
t.Fatalf("preview uses %d rows with %d available:\n%s", rows, height, plainText(screen))
314+
}
315+
seen := ansi.Strip(screen)
316+
for !view.preview.viewport.AtBottom() {
317+
view.HandleContentKey(keyPress("down"))
318+
seen += ansi.Strip(view.previewView())
319+
}
320+
for _, want := range []string{"Via eli+screen@example.com", "To Elija Sorensen", "Cc Sam Rivera", "Bcc audit@example.org", "walk through the budget"} {
321+
if !strings.Contains(seen, want) {
322+
t.Errorf("preview lost %q while scrolling:\n%s", want, seen)
323+
}
324+
}
325+
})
326+
}
327+
}
328+
}
329+
330+
func TestScreenerFindWithScrollingEnvelopeHeaders(t *testing.T) {
331+
view, _ := openedPreview(t)
332+
view.preview.recipients.Bcc = []mail.Contact{{EmailAddress: "audit@example.org"}}
333+
view.preview.body = htmlToMarkdown("<p>Monthly newsletter.</p>" + strings.Repeat("<p>News from the team.</p>", 10) +
334+
`<p><a href="https://example.org/newsletter/unsubscribe">Unsubscribe preferences</a></p>`)
335+
view.preview.width = 0
336+
for _, height := range []int{6, 20, 6} {
337+
view.vc.height = height
338+
view.layoutPreview()
339+
for _, key := range []string{"/", "unsubscribe", "enter"} {
340+
view.HandleContentKey(keyPress(key))
341+
}
342+
match, found := view.preview.find.selected()
343+
if !found || !strings.Contains(strings.ToLower(ansi.Strip(view.preview.lines[match.line])), "unsubscribe") {
344+
t.Fatalf("height %d: find coordinates do not identify the body match", height)
345+
}
346+
if match.line < view.preview.viewport.YOffset() || match.line >= view.preview.viewport.YOffset()+view.preview.viewport.Height() {
347+
t.Fatalf("height %d: selected match is outside the viewport", height)
348+
}
349+
view.HandleContentKey(keyPress("esc"))
350+
view.preview.viewport.GotoTop()
351+
for _, key := range []string{"/", "audit@example.org", "enter"} {
352+
view.HandleContentKey(keyPress(key))
353+
}
354+
if len(view.preview.find.matches) != 0 {
355+
t.Fatalf("height %d: find searched the envelope header", height)
356+
}
357+
view.HandleContentKey(keyPress("esc"))
358+
}
359+
}

0 commit comments

Comments
 (0)