forked from gizak/termui
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (66 loc) · 1.53 KB
/
main.go
File metadata and controls
75 lines (66 loc) · 1.53 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
package main
import (
"log"
ui "github.com/metaspartan/gotui/v5"
"github.com/metaspartan/gotui/v5/widgets"
)
func main() {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize gotui: %v", err)
}
defer ui.Close()
i1 := widgets.NewInput()
i1.Title = "Username"
i1.Placeholder = "Enter username"
i1.SetRect(10, 5, 50, 8)
i2 := widgets.NewInput()
i2.Title = "Password"
i2.Placeholder = "Enter password"
i2.EchoMode = widgets.EchoPassword
i2.SetRect(10, 10, 50, 13)
current := 0
inputs := []*widgets.Input{i1, i2}
ui.Render(i1, i2)
uiEvents := ui.PollEvents()
for {
e := <-uiEvents
switch e.ID {
case "q", "<C-c>", "<Escape>":
return
case "<Tab>":
current = (current + 1) % len(inputs)
ui.Render(i1, i2)
case "<Enter>":
// Submit action?
case "<Resize>":
payload := e.Payload.(ui.Resize)
// Resize logic... for this simple example we keep fixed or adjust relative?
// Let's just center them
midX := payload.Width / 2
midY := payload.Height / 2
i1.SetRect(midX-20, midY-5, midX+20, midY-2)
i2.SetRect(midX-20, midY, midX+20, midY+3)
ui.Clear()
ui.Render(i1, i2)
default:
// Pass event to active input
active := inputs[current]
// Handle Input events
switch e.ID {
case "<Backspace>":
active.Backspace()
case "<Left>":
active.MoveCursorLeft()
case "<Right>":
active.MoveCursorRight()
case "<Space>":
active.InsertRune(' ')
default:
if len(e.ID) == 1 {
active.InsertRune([]rune(e.ID)[0])
}
}
ui.Render(i1, i2)
}
}
}