-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiWindow.go
More file actions
111 lines (92 loc) · 2.2 KB
/
Copy pathminiWindow.go
File metadata and controls
111 lines (92 loc) · 2.2 KB
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
package main
import (
"github.com/jonasfreyr/gim/utils"
gc "github.com/rthornton128/goncurses"
)
type MiniWindow struct {
x map[string]int
width int
stdscr *gc.Window
texts map[string]string
}
func NewMiniWindow(y, x, h, w int) (*MiniWindow, error) {
stdscr, err := gc.NewWindow(h, w, y, x)
if err != nil {
return nil, err
//log.Fatal(err)
}
err = stdscr.Keypad(true)
if err != nil {
return nil, err
//log.Fatal(err)
}
return &MiniWindow{width: w, stdscr: stdscr, texts: make(map[string]string), x: make(map[string]int)}, nil
}
func (w *MiniWindow) draw(label string) {
w.stdscr.Erase()
// w.stdscr.Border(gc.ACS_VLINE, gc.ACS_VLINE, gc.ACS_HLINE, gc.A_INVIS, gc.A_INVIS, gc.A_INVIS, gc.A_INVIS, gc.A_INVIS)
w.stdscr.Print(label+":", w.texts[label])
w.stdscr.Move(0, len(label)+1+w.x[label])
}
func (w *MiniWindow) moveX(delta int, label string) {
w.x[label] = utils.Max(utils.Min(w.x[label]+delta, len(w.texts[label])), 0)
}
func (w *MiniWindow) clear(label string) {
w.texts[label] = ""
w.x[label] = 0
}
func (w *MiniWindow) whileRun(clear bool, label string) string {
if clear {
w.clear(label)
}
w.draw(label)
for {
ch := w.stdscr.GetChar()
switch ch {
case gc.KEY_ESC:
return ""
case gc.KEY_ENTER, gc.KEY_RETURN:
return w.texts[label]
default:
w.run(label, ch)
}
}
}
func (w *MiniWindow) run(label string, input gc.Key) string {
if _, ok := w.texts[label]; !ok {
w.clear(label)
}
//w.x = len(w.texts[label])
//w.draw(label)
//k := w.stdscr.GetChar()
switch input {
case gc.KEY_ESC:
return ""
case gc.KEY_LEFT:
w.moveX(-1, label)
case gc.KEY_RIGHT:
w.moveX(1, label)
case gc.KEY_ENTER, gc.KEY_RETURN:
return w.texts[label]
case gc.KEY_END:
w.x[label] = len(w.texts[label])
case gc.KEY_HOME:
w.x[label] = 0
case gc.KEY_BACKSPACE:
if w.x[label] == 0 {
return ""
}
// e.lines[e.y] = e.lines[e.y][:e.x] + e.lines[e.y][e.x+num:]
w.moveX(-1, label)
w.texts[label] = w.texts[label][:w.x[label]] + w.texts[label][w.x[label]+1:]
default:
chr := gc.KeyString(input)
if len(chr) > 1 {
return ""
}
w.texts[label] = w.texts[label][:w.x[label]] + chr + w.texts[label][w.x[label]:]
w.moveX(1, label)
}
w.draw(label)
return ""
}