File: app.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (508 lines) | stat: -rw-r--r-- 12,361 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// Package cli implements a generic interactive line editor.
package cli

import (
	"io"
	"os"
	"sort"
	"sync"
	"syscall"

	"src.elv.sh/pkg/cli/term"
	"src.elv.sh/pkg/cli/tk"
	"src.elv.sh/pkg/sys"
	"src.elv.sh/pkg/ui"
)

// App represents a CLI app.
type App interface {
	// ReadCode requests the App to read code from the terminal by running an
	// event loop. This function is not re-entrant.
	ReadCode() (string, error)

	// MutateState mutates the state of the app.
	MutateState(f func(*State))
	// CopyState returns a copy of the a state.
	CopyState() State

	// PushAddon pushes a widget to the addon stack.
	PushAddon(w tk.Widget)
	// PopAddon pops the last widget from the addon stack. If the widget
	// implements interface{ Dismiss() }, the Dismiss method is called
	// first. This method does nothing if the addon stack is empty.
	PopAddon()

	// ActiveWidget returns the currently active widget. If the addon stack is
	// non-empty, it returns the last addon. Otherwise it returns the main code
	// area widget.
	ActiveWidget() tk.Widget
	// FocusedWidget returns the currently focused widget. It is searched like
	// ActiveWidget, but skips widgets that implement interface{ Focus() bool }
	// and return false when .Focus() is called.
	FocusedWidget() tk.Widget

	// CommitEOF causes the main loop to exit with EOF. If this method is called
	// when an event is being handled, the main loop will exit after the handler
	// returns.
	CommitEOF()
	// CommitCode causes the main loop to exit with the current code content. If
	// this method is called when an event is being handled, the main loop will
	// exit after the handler returns.
	CommitCode()

	// Redraw requests a redraw. It never blocks and can be called regardless of
	// whether the App is active or not.
	Redraw()
	// RedrawFull requests a full redraw. It never blocks and can be called
	// regardless of whether the App is active or not.
	RedrawFull()
	// Notify adds a note and requests a redraw.
	Notify(note ui.Text)
}

type app struct {
	loop    *loop
	reqRead chan struct{}

	TTY               TTY
	MaxHeight         func() int
	RPromptPersistent func() bool
	BeforeReadline    []func()
	AfterReadline     []func(string)
	Highlighter       Highlighter
	Prompt            Prompt
	RPrompt           Prompt
	GlobalBindings    tk.Bindings

	StateMutex sync.RWMutex
	State      State

	codeArea tk.CodeArea
}

// State represents mutable state of an App.
type State struct {
	// Notes that have been added since the last redraw.
	Notes []ui.Text
	// The addon stack. All widgets are shown under the codearea widget. The
	// last widget handles terminal events.
	Addons []tk.Widget
}

// NewApp creates a new App from the given specification.
func NewApp(spec AppSpec) App {
	lp := newLoop()
	a := app{
		loop:              lp,
		TTY:               spec.TTY,
		MaxHeight:         spec.MaxHeight,
		RPromptPersistent: spec.RPromptPersistent,
		BeforeReadline:    spec.BeforeReadline,
		AfterReadline:     spec.AfterReadline,
		Highlighter:       spec.Highlighter,
		Prompt:            spec.Prompt,
		RPrompt:           spec.RPrompt,
		GlobalBindings:    spec.GlobalBindings,
		State:             spec.State,
	}
	if a.TTY == nil {
		a.TTY = NewTTY(os.Stdin, os.Stderr)
	}
	if a.MaxHeight == nil {
		a.MaxHeight = func() int { return -1 }
	}
	if a.RPromptPersistent == nil {
		a.RPromptPersistent = func() bool { return false }
	}
	if a.Highlighter == nil {
		a.Highlighter = dummyHighlighter{}
	}
	if a.Prompt == nil {
		a.Prompt = NewConstPrompt(nil)
	}
	if a.RPrompt == nil {
		a.RPrompt = NewConstPrompt(nil)
	}
	if a.GlobalBindings == nil {
		a.GlobalBindings = tk.DummyBindings{}
	}
	lp.HandleCb(a.handle)
	lp.RedrawCb(a.redraw)

	a.codeArea = tk.NewCodeArea(tk.CodeAreaSpec{
		Bindings:    spec.CodeAreaBindings,
		Highlighter: a.Highlighter.Get,
		Prompt:      a.Prompt.Get,
		RPrompt:     a.RPrompt.Get,
		QuotePaste:  spec.QuotePaste,
		OnSubmit:    a.CommitCode,
		State:       spec.CodeAreaState,

		SimpleAbbreviations:    spec.SimpleAbbreviations,
		CommandAbbreviations:   spec.CommandAbbreviations,
		SmallWordAbbreviations: spec.SmallWordAbbreviations,
	})

	return &a
}

func (a *app) MutateState(f func(*State)) {
	a.StateMutex.Lock()
	defer a.StateMutex.Unlock()
	f(&a.State)
}

func (a *app) CopyState() State {
	a.StateMutex.RLock()
	defer a.StateMutex.RUnlock()
	return State{
		append([]ui.Text(nil), a.State.Notes...),
		append([]tk.Widget(nil), a.State.Addons...),
	}
}

type dismisser interface {
	Dismiss()
}

func (a *app) PushAddon(w tk.Widget) {
	a.StateMutex.Lock()
	defer a.StateMutex.Unlock()
	a.State.Addons = append(a.State.Addons, w)
}

func (a *app) PopAddon() {
	a.StateMutex.Lock()
	defer a.StateMutex.Unlock()
	if len(a.State.Addons) == 0 {
		return
	}
	if d, ok := a.State.Addons[len(a.State.Addons)-1].(dismisser); ok {
		d.Dismiss()
	}
	a.State.Addons = a.State.Addons[:len(a.State.Addons)-1]
}

func (a *app) ActiveWidget() tk.Widget {
	a.StateMutex.Lock()
	defer a.StateMutex.Unlock()
	if len(a.State.Addons) > 0 {
		return a.State.Addons[len(a.State.Addons)-1]
	}
	return a.codeArea
}

func (a *app) FocusedWidget() tk.Widget {
	a.StateMutex.Lock()
	defer a.StateMutex.Unlock()
	addons := a.State.Addons
	for i := len(addons) - 1; i >= 0; i-- {
		if hasFocus(addons[i]) {
			return addons[i]
		}
	}
	return a.codeArea
}

func (a *app) resetAllStates() {
	a.MutateState(func(s *State) { *s = State{} })
	a.codeArea.MutateState(
		func(s *tk.CodeAreaState) { *s = tk.CodeAreaState{} })
}

func (a *app) handle(e event) {
	switch e := e.(type) {
	case os.Signal:
		switch e {
		case syscall.SIGHUP:
			a.loop.Return("", io.EOF)
		case syscall.SIGINT:
			a.resetAllStates()
			a.triggerPrompts(true)
		case sys.SIGWINCH:
			a.RedrawFull()
		}
	case term.Event:
		target := a.ActiveWidget()
		handled := target.Handle(e)
		if !handled {
			handled = a.GlobalBindings.Handle(target, e)
		}
		if !handled {
			if k, ok := e.(term.KeyEvent); ok {
				a.Notify(ui.T("Unbound key: " + ui.Key(k).String()))
			}
		}
		if !a.loop.HasReturned() {
			a.triggerPrompts(false)
			a.reqRead <- struct{}{}
		}
	}
}

func (a *app) triggerPrompts(force bool) {
	a.Prompt.Trigger(force)
	a.RPrompt.Trigger(force)
}

func (a *app) redraw(flag redrawFlag) {
	// Get the dimensions available.
	height, width := a.TTY.Size()
	if maxHeight := a.MaxHeight(); maxHeight > 0 && maxHeight < height {
		height = maxHeight
	}

	var notes []ui.Text
	var addons []tk.Widget
	a.MutateState(func(s *State) {
		notes = s.Notes
		s.Notes = nil
		addons = append([]tk.Widget(nil), s.Addons...)
	})

	bufNotes := renderNotes(notes, width)
	isFinalRedraw := flag&finalRedraw != 0
	if isFinalRedraw {
		hideRPrompt := !a.RPromptPersistent()
		a.codeArea.MutateState(func(s *tk.CodeAreaState) {
			s.HideTips = true
			s.HideRPrompt = hideRPrompt
		})
		bufMain := renderApp([]tk.Widget{a.codeArea /* no addon */}, width, height)
		a.codeArea.MutateState(func(s *tk.CodeAreaState) {
			s.HideTips = false
			s.HideRPrompt = false
		})
		// Insert a newline after the buffer and position the cursor there.
		bufMain.Extend(term.NewBuffer(width), true)

		a.TTY.UpdateBuffer(bufNotes, bufMain, flag&fullRedraw != 0)
		a.TTY.ResetBuffer()
	} else {
		bufMain := renderApp(append([]tk.Widget{a.codeArea}, addons...), width, height)
		a.TTY.UpdateBuffer(bufNotes, bufMain, flag&fullRedraw != 0)
	}
}

// Renders notes. This does not respect height so that overflow notes end up in
// the scrollback buffer.
func renderNotes(notes []ui.Text, width int) *term.Buffer {
	if len(notes) == 0 {
		return nil
	}
	bb := term.NewBufferBuilder(width)
	for i, note := range notes {
		if i > 0 {
			bb.Newline()
		}
		bb.WriteStyled(note)
	}
	return bb.Buffer()
}

// Renders the codearea, and uses the rest of the height for the listing.
func renderApp(widgets []tk.Widget, width, height int) *term.Buffer {
	heights, focus := distributeHeight(widgets, width, height)
	var buf *term.Buffer
	for i, w := range widgets {
		if heights[i] == 0 {
			continue
		}
		buf2 := w.Render(width, heights[i])
		if buf == nil {
			buf = buf2
		} else {
			buf.Extend(buf2, i == focus)
		}
	}
	return buf
}

// Distributes the height among all the widgets. Returns the height for each
// widget, and the index of the widget currently focused.
func distributeHeight(widgets []tk.Widget, width, height int) ([]int, int) {
	var focus int
	for i, w := range widgets {
		if hasFocus(w) {
			focus = i
		}
	}
	n := len(widgets)
	heights := make([]int, n)
	if height <= n {
		// Not enough (or just enough) height to render every widget with a
		// height of 1.
		remain := height
		// Start from the focused widget, and extend downwards as much as
		// possible.
		for i := focus; i < n && remain > 0; i++ {
			heights[i] = 1
			remain--
		}
		// If there is still space remaining, start from the focused widget
		// again, and extend upwards as much as possible.
		for i := focus - 1; i >= 0 && remain > 0; i-- {
			heights[i] = 1
			remain--
		}
		return heights, focus
	}

	maxHeights := make([]int, n)
	for i, w := range widgets {
		maxHeights[i] = w.MaxHeight(width, height)
	}

	// The algorithm below achieves the following goals:
	//
	// 1. If maxHeights[u] > maxHeights[v], heights[u] >= heights[v];
	//
	// 2. While achieving goal 1, have as many widgets s.t. heights[u] ==
	//    maxHeights[u].
	//
	// This is done by allocating the height among the widgets following an
	// non-decreasing order of maxHeights. At each step:
	//
	// - If it's possible to allocate maxHeights[u] to all remaining widgets,
	//   then allocate maxHeights[u] to widget u;
	//
	// - If not, allocate the remaining budget evenly - rounding down at each
	//   step, so the widgets with smaller maxHeights gets smaller heights.

	// TODO: Add a test for this.

	indices := make([]int, n)
	for i := range indices {
		indices[i] = i
	}
	sort.Slice(indices, func(i, j int) bool {
		return maxHeights[indices[i]] < maxHeights[indices[j]]
	})

	remain := height
	for rank, idx := range indices {
		if remain >= maxHeights[idx]*(n-rank) {
			heights[idx] = maxHeights[idx]
		} else {
			heights[idx] = remain / (n - rank)
		}
		remain -= heights[idx]
	}

	return heights, focus
}

func hasFocus(w any) bool {
	if f, ok := w.(interface{ Focus() bool }); ok {
		return f.Focus()
	}
	return true
}

func (a *app) ReadCode() (string, error) {
	for _, f := range a.BeforeReadline {
		f()
	}
	defer func() {
		content := a.codeArea.CopyState().Buffer.Content
		for _, f := range a.AfterReadline {
			f(content)
		}
		a.resetAllStates()
	}()

	restore, err := a.TTY.Setup()
	if err != nil {
		return "", err
	}
	defer restore()

	var wg sync.WaitGroup
	defer wg.Wait()

	// Relay input events.
	a.reqRead = make(chan struct{}, 1)
	a.reqRead <- struct{}{}
	defer close(a.reqRead)
	defer a.TTY.CloseReader()
	wg.Add(1)
	go func() {
		defer wg.Done()
		for range a.reqRead {
			event, err := a.TTY.ReadEvent()
			if err == nil {
				a.loop.Input(event)
			} else if err == term.ErrStopped {
				return
			} else if term.IsReadErrorRecoverable(err) {
				a.loop.Input(term.NonfatalErrorEvent{Err: err})
			} else {
				a.loop.Input(term.FatalErrorEvent{Err: err})
				return
			}
		}
	}()

	// Relay signals.
	sigCh := a.TTY.NotifySignals()
	defer a.TTY.StopSignals()
	wg.Add(1)
	go func() {
		for sig := range sigCh {
			a.loop.Input(sig)
		}
		wg.Done()
	}()

	// Relay late updates from prompt, rprompt and highlighter.
	stopRelayLateUpdates := make(chan struct{})
	defer close(stopRelayLateUpdates)
	relayLateUpdates := func(ch <-chan struct{}) {
		if ch == nil {
			return
		}
		wg.Add(1)
		go func() {
			defer wg.Done()
			for {
				select {
				case <-ch:
					a.Redraw()
				case <-stopRelayLateUpdates:
					return
				}
			}
		}()
	}

	relayLateUpdates(a.Prompt.LateUpdates())
	relayLateUpdates(a.RPrompt.LateUpdates())
	relayLateUpdates(a.Highlighter.LateUpdates())

	// Trigger an initial prompt update.
	a.triggerPrompts(true)

	return a.loop.Run()
}

func (a *app) Redraw() {
	a.loop.Redraw(false)
}

func (a *app) RedrawFull() {
	a.loop.Redraw(true)
}

func (a *app) CommitEOF() {
	a.loop.Return("", io.EOF)
}

func (a *app) CommitCode() {
	code := a.codeArea.CopyState().Buffer.Content
	a.loop.Return(code, nil)
}

func (a *app) Notify(note ui.Text) {
	a.MutateState(func(s *State) { s.Notes = append(s.Notes, note) })
	a.Redraw()
}