about summary refs log tree commit diff stats
path: root/html/037abandon.cc.html
Commit message (Collapse)AuthorAgeFilesLines
* 4003Kartik K. Agaram2017-09-231-56/+56
|
* 3971Kartik K. Agaram2017-08-191-3/+3
|
* 3927Kartik K. Agaram2017-06-191-10/+10
|
* 3900Kartik K. Agaram2017-06-021-1/+1
|
* 3897 - various updates to documentationKartik K. Agaram2017-05-291-11/+11
|
* 3895Kartik K. Agaram2017-05-291-10/+10
|
* 3875Kartik K. Agaram2017-05-211-5/+5
|
* 3867Kartik K. Agaram2017-05-191-3/+3
|
* 3823Kartik K. Agaram2017-04-141-7/+7
|
* 3820Kartik K. Agaram2017-04-131-7/+7
|
* 3764 - better colors for cross-linksKartik K. Agaram2017-03-081-4/+5
|
* 3761Kartik K. Agaram2017-03-071-25/+26
|
* 3750Kartik K. Agaram2017-03-021-6/+6
|
* 3749Kartik K. Agaram2017-03-021-6/+6
|
* 3746Kartik K. Agaram2017-02-071-2/+2
|
* 3716Kartik K. Agaram2016-12-261-0/+2
| | | | Make hyperlinks less salient in the rendered html since there's so many of them.
* 3713 - cross-link calls with definitions in htmlKartik K. Agaram2016-12-261-86/+86
|
* 3710Kartik K. Agaram2016-12-261-246/+246
| | | | | Turns out we don't need to explicitly add anchors for each line. Vim's TOhtml has magic for that out of the box.
* 3709 - line numbers in htmlKartik K. Agaram2016-12-261-250/+274
| | | | | | Each line number also gets an anchor name, but I'm not hyperlinking them for now because I don't want to encourage bookmarking these links just yet. They aren't permalinks because every revision may change what's at any given line number.
* 3707Kartik K. Agaram2016-12-121-2/+1
| | | | | | | | | | | | | | | | | | Be more disciplined about tagging 2 different concepts in the codebase: a) Use the phrase "later layers" to highlight places where a layer doesn't have the simplest possible self-contained implementation. b) Use the word "hook" to point out functions that exist purely to provide waypoints for extension by future layers. Since both these only make sense in the pre-tangled representation of the codebase, using '//:' and '#:' comments to get them stripped out of tangled output. (Though '#:' comments still make it to tangled output at the moment. Let's see if we use it enough to be worth supporting. Scenarios are pretty unreadable in tangled output anyway.)
* 3667Kartik K. Agaram2016-11-111-1/+23
|
* 3544Kartik K. Agaram2016-10-221-1/+1
|
* 3543Kartik K. Agaram2016-10-221-1/+1
|
* 3524Kartik K. Agaram2016-10-201-3/+3
|
* 3431Kartik K. Agaram2016-09-301-31/+33
| | | | | Improvements to syntax highlighting, particularly for Mu code in C++ files.
* 3395Kartik K. Agaram2016-09-171-43/+43
|
* 3315Kartik K. Agaram2016-09-101-1/+1
|
* 3266Kartik K. Agaram2016-08-271-2/+2
|
* 3219Kartik K. Agaram2016-08-171-18/+10
| | | | | | | | Trying keeping html in the master branch: https://github.com/blog/2228-simpler-github-pages-publishing Let's see if https://akkartik.github.io/mu updates after I push this commit to just the master branch.
* 3102Kartik K. Agaram2016-07-051-9/+4
|
* 2996Kartik K. Agaram2016-05-211-0/+277
bd19020f'>^
0b26241 ^
be2918a ^
2925bdf ^
1170893 ^

1170893 ^

16c3f0a ^
960d11c ^
1170893 ^
bd71787 ^


f3d3e0e ^
bd71787 ^



f3d3e0e ^








bd71787 ^









1170893 ^


28f393b ^
0b26241 ^
28f393b ^
















dee0f89 ^
700dea2 ^


dee0f89 ^
dee0f89 ^









1170893 ^

55ad16b ^
1170893 ^

fd27a2b ^













1170893 ^
2925bdf ^


1da3239 ^
2925bdf ^
1da3239 ^
2925bdf ^




5685a17 ^
1170893 ^


fd27a2b ^
dee0f89 ^


fd27a2b ^
15b856a ^
fd27a2b ^




bd71787 ^
fd27a2b ^














bd71787 ^
bd71787 ^

fd27a2b ^


f37508a ^
1da3239 ^
f37508a ^
1da3239 ^
fd27a2b ^
700dea2 ^
bd71787 ^

1170893 ^
1170893 ^








1da3239 ^
1170893 ^


399d014 ^
1170893 ^










bd71787 ^
1170893 ^



55ad16b ^
4bc8ea3 ^
1da3239 ^
4bc8ea3 ^
fd27a2b ^
a602891 ^






55ad16b ^
1170893 ^


700dea2 ^


1170893 ^
e591221 ^






f37508a ^
e591221 ^
a602891 ^
1170893 ^

28f393b ^
















1170893 ^
71c13c9 ^




700dea2 ^


28f393b ^



















1170893 ^


bd71787 ^


1170893 ^
bd71787 ^

1170893 ^
bd71787 ^

























1170893 ^



1da3239 ^
1170893 ^
1da3239 ^
2925bdf ^
1170893 ^


1170893 ^


0b26241 ^





2925bdf ^
1170893 ^

16c3f0a ^






0b26241 ^

2925bdf ^
16c3f0a ^


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


               
                             

                 
              
 
                                         
 
                                 
                                         
                                  

 










































































                                                                         
                      






                                               





                                    
 


                                                 
                               
                                            
                      
                                  


                                                    


                                  
                      
                                      
                                
                                           
                                           
                   
                            
                                         

                                                    
                                                      


                                                                                           


                                                          
                                               
                                      
                         
                                      
                                            
                                         

                 

                                               
                                                 
                                    
                          


                                                       
                                     



                                          








                                                                                       









                                                                                     


                        
                                       
                                 
















                                                
                                        


                        
                      









                                                       

                          
                             

 













                                     
                                    


                                                          
                                 
                                                        
                                   




                                    
                               


                                             
                           


                      
                               
 




                                                   
 














                                                                         
                               

                              


                                                                     
                                                                              
                                         
                                                                
                                           
                              
                 

         
                                           








                                               
                         


                                                                                        
                                                                                                 










                                                                   
                                                                 



                                                                                   
 
                         
                           
 
                                       






                                                         
         


                                         


                        
                          






                                                         
                                         
                 
         

 
















                                                     
                                                     




                                        


                            



















                                                                                


                    


                                                                         
                               

                                                             
                               

























                                                             



                                                      
                         
                                                
                           
                         


                


                                                  





                                                   
                         

                






                                                                          

                                              
                                 


                
package widgets

import (
	gocolor "image/color"
	"os"
	"os/exec"
	"sync"

	"git.sr.ht/~sircmpwn/aerc/lib/ui"

	"git.sr.ht/~sircmpwn/pty"
	"github.com/ddevault/go-libvterm"
	"github.com/gdamore/tcell"
)

type vtermKey struct {
	Key  vterm.Key
	Rune rune
	Mod  vterm.Modifier
}

var keyMap map[tcell.Key]vtermKey

func directKey(key vterm.Key) vtermKey {
	return vtermKey{key, 0, vterm.ModNone}
}

func runeMod(r rune, mod vterm.Modifier) vtermKey {
	return vtermKey{vterm.KeyNone, r, mod}
}

func keyMod(key vterm.Key, mod vterm.Modifier) vtermKey {
	return vtermKey{key, 0, mod}
}

func init() {
	keyMap = make(map[tcell.Key]vtermKey)
	keyMap[tcell.KeyCtrlSpace] = runeMod(' ', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlA] = runeMod('a', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlB] = runeMod('b', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlC] = runeMod('c', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlD] = runeMod('d', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlE] = runeMod('e', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlF] = runeMod('f', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlG] = runeMod('g', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlH] = runeMod('h', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlI] = runeMod('i', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlJ] = runeMod('j', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlK] = runeMod('k', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlL] = runeMod('l', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlM] = runeMod('m', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlN] = runeMod('n', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlO] = runeMod('o', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlP] = runeMod('p', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlQ] = runeMod('q', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlR] = runeMod('r', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlS] = runeMod('s', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlT] = runeMod('t', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlU] = runeMod('u', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlV] = runeMod('v', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlW] = runeMod('w', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlX] = runeMod('x', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlY] = runeMod('y', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlZ] = runeMod('z', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlBackslash] = runeMod('\\', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlCarat] = runeMod('^', vterm.ModCtrl)
	keyMap[tcell.KeyCtrlUnderscore] = runeMod('_', vterm.ModCtrl)
	keyMap[tcell.KeyEnter] = directKey(vterm.KeyEnter)
	keyMap[tcell.KeyTab] = directKey(vterm.KeyTab)
	keyMap[tcell.KeyBackspace] = directKey(vterm.KeyBackspace)
	keyMap[tcell.KeyEscape] = directKey(vterm.KeyEscape)
	keyMap[tcell.KeyUp] = directKey(vterm.KeyUp)
	keyMap[tcell.KeyDown] = directKey(vterm.KeyDown)
	keyMap[tcell.KeyLeft] = directKey(vterm.KeyLeft)
	keyMap[tcell.KeyRight] = directKey(vterm.KeyRight)
	keyMap[tcell.KeyInsert] = directKey(vterm.KeyIns)
	keyMap[tcell.KeyDelete] = directKey(vterm.KeyDel)
	keyMap[tcell.KeyHome] = directKey(vterm.KeyHome)
	keyMap[tcell.KeyEnd] = directKey(vterm.KeyEnd)
	keyMap[tcell.KeyPgUp] = directKey(vterm.KeyPageUp)
	keyMap[tcell.KeyPgDn] = directKey(vterm.KeyPageDown)
	for i := 0; i < 64; i++ {
		keyMap[tcell.Key(int(tcell.KeyF1)+i)] =
			directKey(vterm.Key(int(vterm.KeyFunction0) + i))
	}
	keyMap[tcell.KeyTAB] = directKey(vterm.KeyTab)
	keyMap[tcell.KeyESC] = directKey(vterm.KeyEscape)
	keyMap[tcell.KeyDEL] = directKey(vterm.KeyBackspace)
}

type Terminal struct {
	ui.Invalidatable
	closed      bool
	cmd         *exec.Cmd
	colors      map[tcell.Color]tcell.Color
	ctx         *ui.Context
	cursorPos   vterm.Pos
	cursorShown bool
	destroyed   bool
	err         error
	focus       bool
	pty         *os.File
	start       chan interface{}
	vterm       *vterm.VTerm

	damage []vterm.Rect // protected by mutex
	mutex  sync.Mutex

	OnClose func(err error)
	OnEvent func(event tcell.Event) bool
	OnStart func()
	OnTitle func(title string)
}

func NewTerminal(cmd *exec.Cmd) (*Terminal, error) {
	term := &Terminal{
		cursorShown: true,
	}
	term.cmd = cmd
	term.vterm = vterm.New(24, 80)
	term.vterm.SetUTF8(true)
	term.start = make(chan interface{})
	screen := term.vterm.ObtainScreen()
	go func() {
		<-term.start
		buf := make([]byte, 4096)
		for {
			n, err := term.pty.Read(buf)
			if err != nil || term.closed {
				// These are generally benine errors when the process exits
				term.Close(nil)
				return
			}
			n, err = term.vterm.Write(buf[:n])
			if err != nil {
				term.Close(err)
				return
			}
			screen.Flush()
			term.flushTerminal()
			term.invalidate()
		}
	}()
	screen.OnDamage = term.onDamage
	screen.OnMoveCursor = term.onMoveCursor
	screen.OnSetTermProp = term.onSetTermProp
	screen.EnableAltScreen(true)
	screen.Reset(true)

	state := term.vterm.ObtainState()
	term.colors = make(map[tcell.Color]tcell.Color)
	for i := 0; i < 256; i += 1 {
		tcolor := tcell.Color(i)
		var r uint8 = 0
		var g uint8 = 0
		var b uint8 = uint8(i + 1)
		if i < 16 {
			// Set the first 16 colors to predictable near-black RGB values
			state.SetPaletteColor(i,
				vterm.NewVTermColorRGB(gocolor.RGBA{r, g, b, 255}))
		} else {
			// The rest use RGB
			vcolor := state.GetPaletteColor(i)
			r, g, b = vcolor.GetRGB()
		}
		term.colors[tcell.NewRGBColor(int32(r), int32(g), int32(b))] = tcolor
	}
	fg, bg := state.GetDefaultColors()
	r, g, b := bg.GetRGB()
	term.colors[tcell.NewRGBColor(
		int32(r), int32(g), int32(b))] = tcell.ColorDefault
	r, g, b = fg.GetRGB()
	term.colors[tcell.NewRGBColor(
		int32(r), int32(g), int32(b))] = tcell.ColorDefault

	return term, nil
}

func (term *Terminal) flushTerminal() {
	buf := make([]byte, 4096)
	for {
		n, err := term.vterm.Read(buf)
		if err != nil {
			term.Close(err)
			return
		}
		if n == 0 {
			break
		}
		n, err = term.pty.Write(buf[:n])
		if err != nil {
			term.Close(err)
			return
		}
	}
}

func (term *Terminal) Close(err error) {
	if term.closed {
		return
	}
	term.err = err
	if term.pty != nil {
		term.pty.Close()
		term.pty = nil
	}
	if term.cmd != nil && term.cmd.Process != nil {
		term.cmd.Process.Kill()
		term.cmd = nil
	}
	if !term.closed && term.OnClose != nil {
		term.OnClose(err)
	}
	term.closed = true
	term.ctx.HideCursor()
}

func (term *Terminal) Destroy() {
	if term.destroyed {
		return
	}
	if term.vterm != nil {
		term.vterm.Close()
		term.vterm = nil
	}
	if term.ctx != nil {
		term.ctx.HideCursor()
	}
	term.destroyed = true
}

func (term *Terminal) Invalidate() {
	if term.vterm != nil {
		width, height := term.vterm.Size()
		rect := vterm.NewRect(0, width, 0, height)
		term.mutex.Lock()
		term.damage = append(term.damage, *rect)
		term.mutex.Unlock()
	}
	term.invalidate()
}

func (term *Terminal) invalidate() {
	term.DoInvalidate(term)
}

func (term *Terminal) Draw(ctx *ui.Context) {
	if term.destroyed {
		return
	}

	term.ctx = ctx // gross

	if !term.closed {
		winsize := pty.Winsize{
			Cols: uint16(ctx.Width()),
			Rows: uint16(ctx.Height()),
		}

		if term.pty == nil {
			term.vterm.SetSize(ctx.Height(), ctx.Width())
			tty, err := pty.StartWithSize(term.cmd, &winsize)
			term.pty = tty
			if err != nil {
				term.Close(err)
				return
			}
			term.start <- nil
			if term.OnStart != nil {
				term.OnStart()
			}
		}

		rows, cols, err := pty.Getsize(term.pty)
		if err != nil {
			return
		}
		if ctx.Width() != cols || ctx.Height() != rows {
			pty.Setsize(term.pty, &winsize)
			term.vterm.SetSize(ctx.Height(), ctx.Width())
			rect := vterm.NewRect(0, ctx.Width(), 0, ctx.Height())
			term.mutex.Lock()
			term.damage = append(term.damage, *rect)
			term.mutex.Unlock()
			return
		}
	}

	screen := term.vterm.ObtainScreen()

	type coords struct {
		x int
		y int
	}

	// naive optimization
	visited := make(map[coords]interface{})

	term.mutex.Lock()
	for _, rect := range term.damage {
		for x := rect.StartCol(); x < rect.EndCol() && x < ctx.Width(); x += 1 {

			for y := rect.StartRow(); y < rect.EndRow() && y < ctx.Height(); y += 1 {

				coords := coords{x, y}
				if _, ok := visited[coords]; ok {
					continue
				}
				visited[coords] = nil

				cell, err := screen.GetCellAt(y, x)
				if err != nil {
					continue
				}
				style := term.styleFromCell(cell)
				ctx.Printf(x, y, style, "%s", string(cell.Chars()))
			}
		}
	}

	term.damage = nil
	term.mutex.Unlock()

	if term.focus && !term.closed {
		if !term.cursorShown {
			ctx.HideCursor()
		} else {
			state := term.vterm.ObtainState()
			row, col := state.GetCursorPos()
			ctx.SetCursor(col, row)
		}
	}
}

func (term *Terminal) Focus(focus bool) {
	if term.closed {
		return
	}
	term.focus = focus
	if term.ctx != nil {
		if !term.focus {
			term.ctx.HideCursor()
		} else {
			state := term.vterm.ObtainState()
			row, col := state.GetCursorPos()
			term.ctx.SetCursor(col, row)
			term.Invalidate()
		}
	}
}

func convertMods(mods tcell.ModMask) vterm.Modifier {
	var (
		ret  uint = 0
		mask uint = uint(mods)
	)
	if mask&uint(tcell.ModShift) > 0 {
		ret |= uint(vterm.ModShift)
	}
	if mask&uint(tcell.ModCtrl) > 0 {
		ret |= uint(vterm.ModCtrl)
	}
	if mask&uint(tcell.ModAlt) > 0 {
		ret |= uint(vterm.ModAlt)
	}
	return vterm.Modifier(ret)
}

func (term *Terminal) Event(event tcell.Event) bool {
	if term.OnEvent != nil {
		if term.OnEvent(event) {
			return true
		}
	}
	if term.closed {
		return false
	}
	switch event := event.(type) {
	case *tcell.EventKey:
		if event.Key() == tcell.KeyRune {
			term.vterm.KeyboardUnichar(
				event.Rune(), convertMods(event.Modifiers()))
		} else {
			if key, ok := keyMap[event.Key()]; ok {
				if key.Key == vterm.KeyNone {
					term.vterm.KeyboardUnichar(
						key.Rune, key.Mod)
				} else if key.Mod == vterm.ModNone {
					term.vterm.KeyboardKey(key.Key,
						convertMods(event.Modifiers()))
				} else {
					term.vterm.KeyboardKey(key.Key, key.Mod)
				}
			}
		}
		term.flushTerminal()
	}
	return false
}

func (term *Terminal) styleFromCell(cell *vterm.ScreenCell) tcell.Style {
	style := tcell.StyleDefault

	background := cell.Bg()
	r, g, b := background.GetRGB()
	bg := tcell.NewRGBColor(int32(r), int32(g), int32(b))
	foreground := cell.Fg()
	r, g, b = foreground.GetRGB()
	fg := tcell.NewRGBColor(int32(r), int32(g), int32(b))

	if color, ok := term.colors[bg]; ok {
		style = style.Background(color)
	} else {
		style = style.Background(bg)
	}
	if color, ok := term.colors[fg]; ok {
		style = style.Foreground(color)
	} else {
		style = style.Foreground(fg)
	}

	if cell.Attrs().Bold != 0 {
		style = style.Bold(true)
	}
	if cell.Attrs().Underline != 0 {
		style = style.Underline(true)
	}
	if cell.Attrs().Blink != 0 {
		style = style.Blink(true)
	}
	if cell.Attrs().Reverse != 0 {
		style = style.Reverse(true)
	}
	return style
}

func (term *Terminal) onDamage(rect *vterm.Rect) int {
	term.mutex.Lock()
	term.damage = append(term.damage, *rect)
	term.mutex.Unlock()
	term.invalidate()
	return 1
}

func (term *Terminal) onMoveCursor(old *vterm.Pos,
	pos *vterm.Pos, visible bool) int {

	rows, cols, _ := pty.Getsize(term.pty)
	if pos.Row() >= rows || pos.Col() >= cols {
		return 1
	}

	term.cursorPos = *pos
	term.invalidate()
	return 1
}

func (term *Terminal) onSetTermProp(prop int, val *vterm.VTermValue) int {
	switch prop {
	case vterm.VTERM_PROP_TITLE:
		if term.OnTitle != nil {
			term.OnTitle(val.String)
		}
	case vterm.VTERM_PROP_CURSORVISIBLE:
		term.cursorShown = val.Boolean
		term.invalidate()
	}
	return 1
}