File: edit.go

package info (click to toggle)
golang-github-gcla-gowid 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,456 kB
  • sloc: makefile: 4
file content (688 lines) | stat: -rw-r--r-- 16,630 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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// Copyright 2019-2022 Graham Clark. All rights reserved.  Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.

// Package edit provides an editable text field widget with support for password hiding.
package edit

import (
	"fmt"
	"io"
	"unicode"
	"unicode/utf8"

	"github.com/gcla/gowid"
	"github.com/gcla/gowid/gwutil"
	"github.com/gcla/gowid/widgets/text"
	tcell "github.com/gdamore/tcell/v2"
	"github.com/pkg/errors"
)

//======================================================================

// IEdit is an interface to be implemented by a text editing widget. A suitable implementation
// will be able to defer to RenderEdit() in its Render() function.
type IEdit interface {
	text.ISimple
	IMask
	text.ICursor
	Caption() string
	MakeText() text.IWidget
}

type IMask interface {
	UseMask() bool
	MaskChr() rune
}

type IPaste interface {
	PasteState(...bool) bool
	AddKey(*tcell.EventKey)
	GetKeys() []*tcell.EventKey
}

type Mask struct {
	Chr    rune
	Enable bool
}

// For callback registration
type Text struct{}
type Caption struct{}
type Cursor struct{}

func DisabledMask() Mask {
	return Mask{Chr: 'x', Enable: false}
}

func MakeMask(chr rune) Mask {
	return Mask{Chr: chr, Enable: true}
}

func (m Mask) UseMask() bool {
	return m.Enable
}

func (m Mask) MaskChr() rune {
	return m.Chr
}

type IWidget interface {
	IEdit
	text.IChrAt
	LinesFromTop() int
	SetLinesFromTop(int, gowid.IApp)
	UpLines(size gowid.IRenderSize, doPage bool, app gowid.IApp) bool
	DownLines(size gowid.IRenderSize, doPage bool, app gowid.IApp) bool
}

type IReadOnly interface {
	IsReadOnly() bool
}

type Widget struct {
	IMask
	caption      string
	text         string
	paste        bool
	readonly     bool
	pastedKeys   []*tcell.EventKey
	cursorPos    int
	linesFromTop int
	Callbacks    *gowid.Callbacks
	gowid.IsSelectable
}

var _ fmt.Stringer = (*Widget)(nil)
var _ io.Reader = (*Widget)(nil)
var _ gowid.IWidget = (*Widget)(nil)
var _ IPaste = (*Widget)(nil)
var _ IReadOnly = (*Widget)(nil)

// Writer embeds an EditWidget and provides the io.Writer interface. An gowid.IApp needs to
// be provided too because the widget's SetText() function requires it in order to issue
// callbacks when the text changes.
type Writer struct {
	*Widget
	gowid.IApp
}

type Options struct {
	Caption  string
	Text     string
	Mask     IMask
	ReadOnly bool
}

func New(args ...Options) *Widget {
	var opt Options
	if len(args) > 0 {
		opt = args[0]
	}
	if opt.Mask == nil {
		opt.Mask = DisabledMask()
	}
	res := &Widget{
		IMask:        opt.Mask,
		caption:      opt.Caption,
		text:         opt.Text,
		readonly:     opt.ReadOnly,
		cursorPos:    len(opt.Text),
		pastedKeys:   make([]*tcell.EventKey, 0, 100),
		linesFromTop: 0,
		Callbacks:    gowid.NewCallbacks(),
	}
	return res
}

func (w *Widget) String() string {
	return fmt.Sprintf("edit")
}

func (w *Widget) IsReadOnly() bool {
	return w.readonly
}

func (w *Widget) SetReadOnly(v bool, _ gowid.IApp) {
	w.readonly = v
}

// Set content from array
func (w *Writer) Write(p []byte) (n int, err error) {
	w.SetText(string(p), w.IApp)
	w.cursorPos = 0
	w.linesFromTop = 0
	return len(p), nil
}

// Set array from widget content
func (w *Widget) Read(p []byte) (n int, err error) {
	pl := len(p)
	num := copy(p, w.text[:])
	if num < pl {
		return num, io.EOF
	} else {
		return num, nil
	}
}

func (w *Widget) Text() string {
	return w.text
}

var InvalidRuneIndex error = errors.New("Invalid rune index for string")

// TODO - this isn't ideal- if called in a loop, it would be quadratic.
func (w *Widget) ChrAt(i int) rune {
	j := 0
	for _, char := range w.caption {
		if j == i {
			return char
		}
		j++
	}
	for _, char := range w.text {
		if j == i {
			return char
		}
		j++
	}
	panic(errors.WithStack(gowid.WithKVs(InvalidRuneIndex, map[string]interface{}{"index": i, "text": w.text})))
}

func (w *Widget) SetText(text string, app gowid.IApp) {
	w.text = text
	wid := utf8.RuneCountInString(w.text)
	if w.cursorPos > wid {
		w.SetCursorPos(wid, app)
	}
	gowid.RunWidgetCallbacks(w.Callbacks, Text{}, app, w)
}

func (w *Widget) LinesFromTop() int {
	return w.linesFromTop
}

func (w *Widget) SetLinesFromTop(l int, app gowid.IApp) {
	w.linesFromTop = l
}

func (w *Widget) Caption() string {
	return w.caption
}

func (w *Widget) SetCaption(text string, app gowid.IApp) {
	w.caption = text
	gowid.RunWidgetCallbacks(w.Callbacks, Caption{}, app, w)
}

//func (w *Widget) PasteState(b ...bool) []*tcell.EventKey {
func (w *Widget) PasteState(b ...bool) bool {
	if len(b) > 0 {
		w.paste = b[0]
		//if w.paste {
		w.pastedKeys = w.pastedKeys[:0]
		//}
	}
	return w.paste
}

func (w *Widget) AddKey(ev *tcell.EventKey) {
	w.pastedKeys = append(w.pastedKeys, ev)
}

func (w *Widget) GetKeys() []*tcell.EventKey {
	return w.pastedKeys
}

func (w *Widget) CursorEnabled() bool {
	return w.cursorPos != -1
}

func (w *Widget) SetCursorDisabled() {
	w.cursorPos = -1
}

// TODO - weird that you could call set to 0, then get and it would be > 0...
func (w *Widget) CursorPos() int {
	if !w.CursorEnabled() {
		panic(errors.New("Cursor is disabled, cannot return!"))
	}
	return w.cursorPos
}

func (w *Widget) SetCursorPos(pos int, app gowid.IApp) {
	pos = gwutil.Min(pos, utf8.RuneCountInString(w.Text()))
	w.cursorPos = pos
	gowid.RunWidgetCallbacks(w.Callbacks, Cursor{}, app, w)
}

func (w *Widget) OnTextSet(cb gowid.IWidgetChangedCallback) {
	gowid.AddWidgetCallback(w.Callbacks, Text{}, cb)
}

func (w *Widget) RemoveOnTextSet(cb gowid.IIdentity) {
	gowid.RemoveWidgetCallback(w.Callbacks, Text{}, cb)
}

func (w *Widget) OnCaptionSet(cb gowid.IWidgetChangedCallback) {
	gowid.AddWidgetCallback(w.Callbacks, Caption{}, cb)
}

func (w *Widget) RemoveOnCaptionSet(cb gowid.IIdentity) {
	gowid.RemoveWidgetCallback(w.Callbacks, Caption{}, cb)
}

func (w *Widget) OnCursorPosSet(cb gowid.IWidgetChangedCallback) {
	gowid.AddWidgetCallback(w.Callbacks, Cursor{}, cb)
}

func (w *Widget) RemoveOnCursorPosSet(cb gowid.IIdentity) {
	gowid.RemoveWidgetCallback(w.Callbacks, Cursor{}, cb)
}

func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
	return gowid.CalculateRenderSizeFallback(w, size, focus, app)
}

func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
	return Render(w, size, focus, app)
}

func (w *Widget) MakeText() text.IWidget {
	return MakeText(w)
}

func (w *Widget) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
	return UserInput(w, ev, size, focus, app)
}

func (w *Widget) DownLines(size gowid.IRenderSize, doPage bool, app gowid.IApp) bool {
	return DownLines(w, size, doPage, app)
}

func (w *Widget) UpLines(size gowid.IRenderSize, doPage bool, app gowid.IApp) bool {
	return UpLines(w, size, doPage, app)
}

func (w *Widget) CalculateTopMiddleBottom(size gowid.IRenderSize) (int, int, int) {
	return CalculateTopMiddleBottom(w, size)
}

//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

func Render(w IWidget, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
	twc := w.MakeText()
	c := twc.Render(size, focus, app)
	return c
}

func MakeText(w IWidget) text.IWidget {
	var txt string
	if w.UseMask() {
		arr := make([]rune, len(w.Text()))
		for i := 0; i < len(arr); i++ {
			arr[i] = w.MaskChr()
		}
		txt = string(arr)
	} else {
		txt = w.Text()
	}

	//txt = w.Caption() + "\u00A0" + txt
	txt = w.Caption() + txt

	tw := text.New(txt)
	tw.SetLinesFromTop(w.LinesFromTop(), nil)

	cu := &text.SimpleCursor{-1}
	cu.SetCursorPos(w.CursorPos()+utf8.RuneCountInString(w.Caption()), nil)
	twc := &text.WidgetWithCursor{tw, cu}

	return twc
}

func CalculateTopMiddleBottom(w IWidget, size gowid.IRenderSize) (int, int, int) {
	twc := w.MakeText()
	return text.CalculateTopMiddleBottom(twc, size)
}

// Return true if done
func DownLines(w IWidget, size gowid.IRenderSize, doPage bool, app gowid.IApp) bool {
	prev := w.CursorPos()

	twc := w.MakeText()
	caplen := utf8.RuneCountInString(w.Caption())
	// This incorporates the caption too
	cols, ok := size.(gowid.IColumns)
	if !ok {
		panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IColumns"})
	}
	layout := text.MakeTextLayout(twc.Content(), cols.Columns(), text.WrapAny, gowid.HAlignLeft{})
	ccol, crow := text.GetCoordsFromCursorPos(w.CursorPos()+caplen, cols.Columns(), layout, w)
	offset := 1
	if rows, ok := size.(gowid.IRows); ok && doPage {
		if crow < w.LinesFromTop()+rows.Rows()-1 {
			// if the cursor is in the middle somewhere, jump to the bottom
			offset = w.LinesFromTop() + rows.Rows() - (crow + 1)
		} else {
			// otherwise jump a "page" worth
			offset = rows.Rows()
		}
	}

	targetRow := crow + offset
	newCursorPos := text.GetCursorPosFromCoords(ccol, targetRow, layout, w) - caplen
	if newCursorPos < 0 {
		return false
	} else {
		w.SetCursorPos(newCursorPos, app)
		// TODO - ugly to check for render type like this
		if box, ok := size.(gowid.IRenderBox); ok && (targetRow >= box.BoxRows()+w.LinesFromTop()) { // assumes we render fixed not flow
			w.SetLinesFromTop(w.LinesFromTop()+offset, app)
		}

		//w.linesFromTop += 1
		return (prev != w.CursorPos())
	}
}

// Return true if done
func UpLines(w IWidget, size gowid.IRenderSize, doPage bool, app gowid.IApp) bool {
	caplen := utf8.RuneCountInString(w.Caption())
	prev := w.CursorPos()
	twc := w.MakeText()
	cols, isColumns := size.(gowid.IColumns)
	if !isColumns {
		panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IColumns"})
	}
	layout := text.MakeTextLayout(twc.Content(), cols.Columns(), text.WrapAny, gowid.HAlignLeft{})
	ccol, crow := text.GetCoordsFromCursorPos(w.CursorPos()+caplen, cols.Columns(), layout, w)

	if crow <= 0 {
		return false
	} else {
		offset := 1
		if rows, ok := size.(gowid.IRows); ok && doPage {
			if crow == w.LinesFromTop() {
				offset = rows.Rows()
			} else {
				offset = crow - w.LinesFromTop()
			}
		}
		targetCol := gwutil.Max(crow-offset, 0)

		newCursorPos := text.GetCursorPosFromCoords(ccol, targetCol, layout, w) - caplen
		if newCursorPos < 0 {
			return false
		} else {
			w.SetCursorPos(newCursorPos, app)

			if targetCol < w.LinesFromTop() {
				w.SetLinesFromTop(targetCol, app)
			}

			return (prev != w.CursorPos())
		}
	}
}

func keyIsPasteable(ev *tcell.EventKey) bool {
	switch ev.Key() {
	case tcell.KeyEnter, tcell.Key(' '), tcell.KeyRune:
		return true
	default:
		return false
	}
}

func isReadOnly(w interface{}) bool {
	readOnly := false
	if ro, ok := w.(IReadOnly); ok {
		readOnly = ro.IsReadOnly()
	}
	return readOnly
}

func pasteableKeyInput(w IWidget, ev *tcell.EventKey, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
	if isReadOnly(w) {
		return false
	}

	handled := true
	switch ev.Key() {
	case tcell.KeyEnter:
		r := []rune(w.Text())
		w.SetText(string(r[0:w.CursorPos()])+string('\n')+string(r[w.CursorPos():]), app)
		w.SetCursorPos(w.CursorPos()+1, app)
	case tcell.Key(' '):
		r := []rune(w.Text())
		w.SetText(string(r[0:w.CursorPos()])+" "+string(r[w.CursorPos():]), app)
		w.SetCursorPos(w.CursorPos()+1, app)
	case tcell.KeyRune:
		// TODO: this is lame. Inserting a character is O(n) where n is length
		// of text. I should switch this to use the two stack model for edited
		// text.
		txt := w.Text()
		r := []rune(txt)
		cpos := w.CursorPos()
		rhs := make([]rune, len(r)-cpos)
		copy(rhs, r[cpos:])
		w.SetText(string(append(append(r[:cpos], ev.Rune()), rhs...)), app)
		w.SetCursorPos(w.CursorPos()+1, app)

	default:
		handled = false
	}

	return handled
}

func UserInput(w IWidget, ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
	handled := true
	doup := false
	dodown := false
	recalcLinesFromTop := false
	readOnly := isReadOnly(w)

	switch ev := ev.(type) {
	case *tcell.EventMouse:
		switch ev.Buttons() {
		case tcell.WheelUp:
			doup = true
		case tcell.WheelDown:
			dodown = true
		case tcell.Button1:
			twc := w.MakeText()
			cols, isColumns := size.(gowid.IColumns)
			if !isColumns {
				panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IColumns"})
			}
			layout := text.MakeTextLayout(twc.Content(), cols.Columns(), text.WrapAny, gowid.HAlignLeft{})
			mx, my := ev.Position()
			cursorPos := text.GetCursorPosFromCoords(mx, my+w.LinesFromTop(), layout, w) - (utf8.RuneCountInString(w.Caption()))
			if cursorPos < 0 {
				handled = false
			} else {
				w.SetCursorPos(cursorPos, app)
				handled = true
			}
		default:
			handled = false
		}

	case *tcell.EventPaste:
		if wp, ok := w.(IPaste); ok {
			if ev.Start() {
				wp.PasteState(true)
			} else {
				evs := wp.GetKeys()
				wp.PasteState(false)
				for _, ev := range evs {
					pasteableKeyInput(w, ev, size, focus, app)
				}
			}
		}

	case *tcell.EventKey:
		handled = false
		if wp, ok := w.(IPaste); ok {
			if wp.PasteState() && keyIsPasteable(ev) && !readOnly {
				wp.AddKey(ev)
				handled = true
			}
		}

		if !handled {
			handled = pasteableKeyInput(w, ev, size, focus, app)
		}

		if !handled {
			handled = true
			switch ev.Key() {
			case tcell.KeyPgUp:
				handled = w.UpLines(size, true, app)
			case tcell.KeyUp, tcell.KeyCtrlP:
				doup = true
			case tcell.KeyDown, tcell.KeyCtrlN:
				dodown = true
			case tcell.KeyPgDn:
				handled = w.DownLines(size, true, app)
			case tcell.KeyLeft, tcell.KeyCtrlB:
				if w.CursorPos() > 0 {
					w.SetCursorPos(w.CursorPos()-1, app)
				} else {
					handled = false
				}
			case tcell.KeyRight, tcell.KeyCtrlF:
				if w.CursorPos() < utf8.RuneCountInString(w.Text()) {
					w.SetCursorPos(w.CursorPos()+1, app)
				} else {
					handled = false
				}
			case tcell.KeyBackspace, tcell.KeyBackspace2:
				if !readOnly {
					if w.CursorPos() > 0 {
						pos := w.CursorPos()
						w.SetCursorPos(w.CursorPos()-1, app)
						r := []rune(w.Text())
						w.SetText(string(r[0:pos-1])+string(r[pos:]), app)
					}
				}
			case tcell.KeyDelete, tcell.KeyCtrlD:
				if !readOnly {
					if w.CursorPos() < utf8.RuneCountInString(w.Text()) {
						r := []rune(w.Text())
						w.SetText(string(r[0:w.CursorPos()])+string(r[w.CursorPos()+1:]), app)
					}
				}
			case tcell.KeyCtrlK:
				if !readOnly {
					r := []rune(w.Text())
					w.SetText(string(r[0:w.CursorPos()]), app)
				}
			case tcell.KeyCtrlU:
				if !readOnly {
					r := []rune(w.Text())
					w.SetText(string(r[w.CursorPos():]), app)
					w.SetCursorPos(0, app)
				}
			case tcell.KeyHome:
				w.SetCursorPos(0, app)
				w.SetLinesFromTop(0, app)
			case tcell.KeyCtrlW:
				if !readOnly {
					txt := []rune(w.Text())
					origcp := w.CursorPos()
					cp := origcp
					for cp > 0 && unicode.IsSpace(txt[cp-1]) {
						cp--
					}
					for cp > 0 && !unicode.IsSpace(txt[cp-1]) {
						cp--
					}
					if cp != origcp {
						w.SetText(string(txt[0:cp])+string(txt[origcp:]), app)
						w.SetCursorPos(cp, app)
					}
				}
			case tcell.KeyCtrlA:
				// Would be nice to use a slice here, something that doesn't copy
				// TODO: terrible O(n) behavior :-(
				txt := w.Text()

				i := w.CursorPos()
				j := 0
				lastnl := false
				curstart := 0

				for _, ch := range txt {
					if lastnl {
						curstart = j
					}
					lastnl = (ch == '\n')

					if i == j {
						break
					}
					j += 1
				}

				w.SetCursorPos(curstart, app)
				recalcLinesFromTop = true

			case tcell.KeyEnd:
				w.SetCursorPos(utf8.RuneCountInString(w.Text()), app)
				recalcLinesFromTop = true

			case tcell.KeyCtrlE:
				// TODO: terrible O(n) behavior :-(
				txt := w.Text()
				i := w.CursorPos()
				j := 0
				checknl := false
				for _, ch := range txt {
					if i == j {
						checknl = true
					}
					j += 1
					if checknl {
						if ch == '\n' {
							break
						}
						i += 1
					}
				}
				w.SetCursorPos(i, app)
				recalcLinesFromTop = true

			default:
				handled = false
			}
		}
	}

	if doup {
		handled = w.UpLines(size, false, app)
	}
	if dodown {
		handled = w.DownLines(size, false, app)
	}

	box, ok := size.(gowid.IRenderBox)
	if recalcLinesFromTop && ok {
		twc := w.MakeText()
		caplen := utf8.RuneCountInString(w.Caption())
		layout := text.MakeTextLayout(twc.Content(), box.BoxColumns(), text.WrapAny, gowid.HAlignLeft{})
		_, crow := text.GetCoordsFromCursorPos(w.CursorPos()+caplen, box.BoxColumns(), layout, w)
		w.SetLinesFromTop(gwutil.Max(0, crow-(box.BoxRows()-1)), app)
	}

	return handled
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: