Skip to content

Commit 18b5a80

Browse files
committed
fix(tui): restore images and refresh screen after editor suspension
1 parent d92ffff commit 18b5a80

4 files changed

Lines changed: 121 additions & 16 deletions

File tree

‎tui/app.go‎

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,20 @@ type App struct {
4444
keys *Keymap
4545
spinner *spinner.Model
4646

47-
running bool
48-
closed atomic.Bool
49-
vxClosed bool // vaxis closed itself (signal); do not close it again
50-
loadingCount int
51-
fatalErr error
52-
prompt *Prompt
53-
message string
54-
hint string
55-
messageStyle vaxis.Style
56-
messageGen int
57-
split int // column of the vertical separator, set by the active view
58-
cellPixW int // terminal cell size in pixels from resize events, 0 when unknown
59-
cellPixH int
47+
running bool
48+
closed atomic.Bool
49+
vxClosed bool // vaxis closed itself (signal); do not close it again
50+
loadingCount int
51+
fatalErr error
52+
prompt *Prompt
53+
message string
54+
hint string
55+
messageStyle vaxis.Style
56+
messageGen int
57+
split int // column of the vertical separator, set by the active view
58+
cellPixW int // terminal cell size in pixels from resize events, 0 when unknown
59+
cellPixH int
60+
refreshOnNextDraw bool
6061

6162
config *config.Config
6263
client *mastodon.Client
@@ -187,7 +188,12 @@ func (app *App) draw() {
187188

188189
app.help.Draw(win, app.keys)
189190

190-
app.vx.Render()
191+
if app.refreshOnNextDraw {
192+
app.refreshOnNextDraw = false
193+
app.vx.Refresh()
194+
} else {
195+
app.vx.Render()
196+
}
191197
}
192198

193199
func (app *App) footerSegments() []vaxis.Segment {
@@ -267,6 +273,31 @@ func (app *App) RequestQuit() {
267273
app.Confirm("Quit?", func() { app.running = false })
268274
}
269275

276+
// Refresh forces a full screen redraw on the next frame.
277+
func (app *App) Refresh() {
278+
app.refreshOnNextDraw = true
279+
app.RequestRedraw()
280+
}
281+
282+
// Suspend takes the UI out of raw/fullscreen mode so an external command
283+
// (like an editor) can take over the terminal.
284+
func (app *App) Suspend() error {
285+
return app.vx.Suspend()
286+
}
287+
288+
// Resume restores the UI state after the terminal was suspended for an
289+
// external command. It re-enters fullscreen, clears the scaled image cache
290+
// (since the terminal discarded image placements and memory while suspended),
291+
// and forces a full refresh.
292+
func (app *App) Resume() error {
293+
if err := app.vx.Resume(); err != nil {
294+
return err
295+
}
296+
utils.ImageCache.ClearScaled()
297+
app.Refresh()
298+
return nil
299+
}
300+
270301
// OpenURL opens url in the system browser and reports failures in the footer.
271302
func (app *App) OpenURL(url string) {
272303
if url == "" {

‎tui/compose.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ func (app *App) EditText(initial string) (string, error) {
6060
return "", err
6161
}
6262

63-
if err := app.vx.Suspend(); err != nil {
63+
if err := app.Suspend(); err != nil {
6464
return "", err
6565
}
6666
cmd := exec.Command(args[0], append(args[1:], path)...)
6767
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
6868
runErr := cmd.Run()
69-
resumeErr := app.vx.Resume()
69+
resumeErr := app.Resume()
7070
if runErr != nil {
7171
return "", fmt.Errorf("editor %q: %w", editor, runErr)
7272
}

‎utils/image.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ func (l *lru[V]) put(key string, value V) {
8787
}
8888
}
8989

90+
func (l *lru[V]) clear() {
91+
if l == nil {
92+
return
93+
}
94+
for el := l.ll.Front(); el != nil; el = el.Next() {
95+
if l.onEvict != nil {
96+
l.onEvict(el.Value.(*lruEntry[V]).value)
97+
}
98+
}
99+
l.ll.Init()
100+
l.items = make(map[string]*list.Element)
101+
}
102+
90103
// GlobalImageCache downloads images in the background and keeps two bounded
91104
// caches: decoded images by URL, and terminal images by URL, cell size, and
92105
// cell geometry.
@@ -166,6 +179,22 @@ func (c *GlobalImageCache) SetCellPixelSize(w, h int) {
166179
c.mu.Unlock()
167180
}
168181

182+
// ClearScaled evicts all cached terminal images (releasing any resources) so
183+
// that images will be re-encoded and re-uploaded on the next draw. This must be
184+
// called whenever terminal state was invalidated (e.g. after suspending for an
185+
// external editor). Decoded raw images are kept, so no network re-downloading
186+
// occurs.
187+
func (c *GlobalImageCache) ClearScaled() {
188+
if c == nil {
189+
return
190+
}
191+
c.mu.Lock()
192+
defer c.mu.Unlock()
193+
if c.scaled != nil {
194+
c.scaled.clear()
195+
}
196+
}
197+
169198
// cellGeometryLocked returns the pixel size vaxis assigns to one cell when it
170199
// measures images. Block renderers always use 1x2; kitty and sixel use the
171200
// terminal's reported cell size, which may not be known yet.

‎utils/image_test.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package utils
33
import (
44
"image"
55
"testing"
6+
7+
"git.sr.ht/~rockorager/vaxis"
68
)
79

810
func ceilDiv(a, b int) int {
@@ -79,3 +81,46 @@ func TestCoverBox(t *testing.T) {
7981
t.Fatalf("wide cover box = %dx%d, want 6x3", w, h)
8082
}
8183
}
84+
85+
func TestLRUClear(t *testing.T) {
86+
evicted := 0
87+
l := newLRU[string](10, func(v string) {
88+
evicted++
89+
})
90+
l.put("a", "1")
91+
l.put("b", "2")
92+
l.put("c", "3")
93+
if l.ll.Len() != 3 {
94+
t.Fatalf("expected 3 items, got %d", l.ll.Len())
95+
}
96+
l.clear()
97+
if l.ll.Len() != 0 || len(l.items) != 0 {
98+
t.Fatalf("expected 0 items, got ll=%d items=%d", l.ll.Len(), len(l.items))
99+
}
100+
if evicted != 3 {
101+
t.Fatalf("expected 3 evictions, got %d", evicted)
102+
}
103+
if _, ok := l.get("a"); ok {
104+
t.Fatal("expected 'a' to not be found after clear")
105+
}
106+
}
107+
108+
func TestGlobalImageCacheClearScaled(t *testing.T) {
109+
var c *GlobalImageCache
110+
// Safe to call on nil
111+
c.ClearScaled()
112+
113+
c = &GlobalImageCache{
114+
scaled: newLRU[vaxis.Image](10, nil),
115+
}
116+
// Calling with non-nil scaled cache
117+
c.scaled.put("img1", nil)
118+
c.scaled.put("img2", nil)
119+
c.ClearScaled()
120+
if _, ok := c.scaled.get("img1"); ok {
121+
t.Fatal("expected img1 to be cleared")
122+
}
123+
if _, ok := c.scaled.get("img2"); ok {
124+
t.Fatal("expected img2 to be cleared")
125+
}
126+
}

0 commit comments

Comments
 (0)