@@ -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.
3434type 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 .
210209func (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
247263func (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
274285func (v * screenerView ) showPreviewMatch () {
0 commit comments