| 1 |
package app |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"os/exec" |
| 6 |
"sync/atomic" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/config" |
| 9 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 10 |
"git.sr.ht/~rjarry/aerc/lib/state" |
| 11 |
"git.sr.ht/~rjarry/aerc/lib/ui" |
| 12 |
"go.rockorager.dev/vaxis" |
| 13 |
"go.rockorager.dev/vaxis/widgets/term" |
| 14 |
) |
| 15 |
|
| 16 |
type HasTerminal interface { |
| 17 |
Terminal() *Terminal |
| 18 |
} |
| 19 |
|
| 20 |
type Terminal struct { |
| 21 |
// Bell was rung in the terminal since it last had focus. |
| 22 |
bell bool |
| 23 |
closed int32 |
| 24 |
visible int32 // visible if >0 |
| 25 |
cmd *exec.Cmd |
| 26 |
ctx *ui.Context |
| 27 |
focus bool |
| 28 |
vterm *term.Model |
| 29 |
running bool |
| 30 |
tab *ui.Tab |
| 31 |
// The window title set by the application running in the terminal. |
| 32 |
title string |
| 33 |
|
| 34 |
OnClose func(err error) |
| 35 |
OnEvent func(event vaxis.Event) bool |
| 36 |
OnStart func() |
| 37 |
OnTitle func(title string) |
| 38 |
} |
| 39 |
|
| 40 |
func NewTerminal(cmd *exec.Cmd) (*Terminal, error) { |
| 41 |
var vterm *term.Model |
| 42 |
if config.General().InheritHostTTYFeatures { |
| 43 |
vterm = term.New(term.WithVaxis(ui.Vaxis())) |
| 44 |
} else { |
| 45 |
vterm = term.New() |
| 46 |
} |
| 47 |
term := &Terminal{ |
| 48 |
cmd: cmd, |
| 49 |
vterm: vterm, |
| 50 |
visible: 1, |
| 51 |
} |
| 52 |
term.vterm.TERM = config.General().Term |
| 53 |
return term, nil |
| 54 |
} |
| 55 |
|
| 56 |
func (term *Terminal) Close() { |
| 57 |
term.closeErr(nil) |
| 58 |
} |
| 59 |
|
| 60 |
// TODO: replace with atomic.Bool when min go version will have it (1.19+) |
| 61 |
const closed int32 = 1 |
| 62 |
|
| 63 |
func (term *Terminal) isClosed() bool { |
| 64 |
return atomic.LoadInt32(&term.closed) == closed |
| 65 |
} |
| 66 |
|
| 67 |
func (term *Terminal) closeErr(err error) { |
| 68 |
if atomic.SwapInt32(&term.closed, closed) == closed { |
| 69 |
return |
| 70 |
} |
| 71 |
if term.vterm != nil { |
| 72 |
// Stop receiving events |
| 73 |
term.vterm.Detach() |
| 74 |
term.vterm.Close() |
| 75 |
if term.ctx != nil { |
| 76 |
term.ctx.HideCursor() |
| 77 |
} |
| 78 |
} |
| 79 |
if term.OnClose != nil { |
| 80 |
term.OnClose(err) |
| 81 |
} |
| 82 |
ui.Invalidate() |
| 83 |
} |
| 84 |
|
| 85 |
func (term *Terminal) Destroy() { |
| 86 |
// If we destroy, we don't want to call the OnClose callback |
| 87 |
term.OnClose = nil |
| 88 |
term.closeErr(nil) |
| 89 |
} |
| 90 |
|
| 91 |
func (term *Terminal) Invalidate() { |
| 92 |
ui.Invalidate() |
| 93 |
} |
| 94 |
|
| 95 |
func (term *Terminal) Draw(ctx *ui.Context) { |
| 96 |
if ctx.Width() == 0 || ctx.Height() == 0 { |
| 97 |
return |
| 98 |
} |
| 99 |
term.ctx = ctx |
| 100 |
if !term.running && term.cmd != nil { |
| 101 |
term.vterm.Attach(term.HandleEvent) |
| 102 |
w, h := ctx.Window().Size() |
| 103 |
if err := term.vterm.StartWithSize(term.cmd, w, h); err != nil { |
| 104 |
log.Errorf("error running terminal: %v", err) |
| 105 |
term.closeErr(err) |
| 106 |
return |
| 107 |
} |
| 108 |
term.running = true |
| 109 |
if term.OnStart != nil { |
| 110 |
term.OnStart() |
| 111 |
} |
| 112 |
} |
| 113 |
w, h := ctx.Window().Size() |
| 114 |
term.vterm.Update(vaxis.Resize{Cols: w, Rows: h}) |
| 115 |
term.vterm.Draw(ctx.Window()) |
| 116 |
} |
| 117 |
|
| 118 |
func (term *Terminal) Show(visible bool) { |
| 119 |
if visible { |
| 120 |
atomic.StoreInt32(&term.visible, 1) |
| 121 |
} else { |
| 122 |
atomic.StoreInt32(&term.visible, 0) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func (term *Terminal) Terminal() *Terminal { |
| 127 |
return term |
| 128 |
} |
| 129 |
|
| 130 |
func (term *Terminal) MouseEvent(localX int, localY int, event vaxis.Event) { |
| 131 |
ev, ok := event.(vaxis.Mouse) |
| 132 |
if !ok { |
| 133 |
return |
| 134 |
} |
| 135 |
if term.OnEvent != nil { |
| 136 |
term.OnEvent(ev) |
| 137 |
} |
| 138 |
if term.isClosed() { |
| 139 |
return |
| 140 |
} |
| 141 |
ev.Row = localY |
| 142 |
ev.Col = localX |
| 143 |
term.vterm.Update(ev) |
| 144 |
} |
| 145 |
|
| 146 |
func (term *Terminal) Focus(focus bool) { |
| 147 |
if term.isClosed() { |
| 148 |
return |
| 149 |
} |
| 150 |
term.focus = focus |
| 151 |
term.bell = false |
| 152 |
if term.focus { |
| 153 |
term.vterm.Focus() |
| 154 |
} else { |
| 155 |
term.vterm.Blur() |
| 156 |
} |
| 157 |
term.setTitle() |
| 158 |
ui.Invalidate() |
| 159 |
} |
| 160 |
|
| 161 |
// HandleEvent is used to watch the underlying terminal events |
| 162 |
func (t *Terminal) HandleEvent(ev vaxis.Event) { |
| 163 |
if t.isClosed() { |
| 164 |
return |
| 165 |
} |
| 166 |
switch ev := ev.(type) { |
| 167 |
case vaxis.Redraw: |
| 168 |
if atomic.LoadInt32(&t.visible) > 0 { |
| 169 |
ui.Invalidate() |
| 170 |
} |
| 171 |
case term.EventTitle: |
| 172 |
t.title = string(ev) |
| 173 |
t.setTitle() |
| 174 |
ui.Invalidate() |
| 175 |
if t.OnTitle != nil { |
| 176 |
t.OnTitle(string(ev)) |
| 177 |
} |
| 178 |
case term.EventClosed: |
| 179 |
t.Close() |
| 180 |
ui.Invalidate() |
| 181 |
case term.EventBell: |
| 182 |
if !t.focus { |
| 183 |
t.bell = true |
| 184 |
t.setTitle() |
| 185 |
ui.Invalidate() |
| 186 |
} |
| 187 |
aerc.Beep() |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
func (term *Terminal) Event(event vaxis.Event) bool { |
| 192 |
if term.OnEvent != nil { |
| 193 |
if term.OnEvent(event) { |
| 194 |
return true |
| 195 |
} |
| 196 |
} |
| 197 |
if term.isClosed() { |
| 198 |
return false |
| 199 |
} |
| 200 |
term.vterm.Update(event) |
| 201 |
return true |
| 202 |
} |
| 203 |
|
| 204 |
// Renders the terminal tab title based on the template, falls back to the |
| 205 |
// unformatted title in case of errors. |
| 206 |
func (t *Terminal) RenderTitle() string { |
| 207 |
title := t.title |
| 208 |
if title == "" { |
| 209 |
title = t.cmd.Path |
| 210 |
} |
| 211 |
|
| 212 |
data := state.NewDataSetter() |
| 213 |
data.SetTitle(title) |
| 214 |
data.SetBell(t.bell) |
| 215 |
var buf bytes.Buffer |
| 216 |
if err := config.Ui().TabTitleTerminal.Execute(&buf, data); err != nil { |
| 217 |
PushError("failed to render terminal tab title: " + err.Error()) |
| 218 |
return title |
| 219 |
} |
| 220 |
return buf.String() |
| 221 |
} |
| 222 |
|
| 223 |
func (t *Terminal) SetTab(tab *ui.Tab) { |
| 224 |
t.tab = tab |
| 225 |
} |
| 226 |
|
| 227 |
func (t *Terminal) setTitle() { |
| 228 |
if t.tab != nil { |
| 229 |
t.tab.SetTitle(t.RenderTitle()) |
| 230 |
} |
| 231 |
} |