/
stdUi.go
98 lines (78 loc) · 2.42 KB
/
stdUi.go
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
package akevitt
import (
"unicode"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/uaraven/logview"
)
func AppendText(message string, chatlog *logview.LogView) {
ev := logview.NewLogEvent("message", message)
ev.Level = logview.LogLevelInfo
chatlog.AppendEvent(ev)
chatlog.SetFocusFunc(func() {
chatlog.Blur()
})
chatlog.ScrollToBottom()
}
func ErrorBox(message string, app *tview.Application, back tview.Primitive) {
result := tview.NewModal().SetText("Error!").SetText(message).SetTextColor(tcell.ColorRed).
SetBackgroundColor(tcell.ColorBlack).
AddButtons([]string{"Close"}).SetDoneFunc(func(buttonIndex int, buttonLabel string) {
app.SetRoot(back, true)
})
result.SetBorder(true).SetBorderColor(tcell.ColorDarkRed)
app.SetRoot(result, true)
}
func RegistrationScreen(engine *Akevitt, session *ActiveSession, nextScreen UIFunc) tview.Primitive {
username := ""
password := ""
repeatPassword := ""
form := tview.NewForm()
form.AddInputField("Username", "", 0, func(textToCheck string, lastChar rune) bool {
if !unicode.IsLetter(lastChar) && !unicode.IsDigit(lastChar) || lastChar > unicode.MaxASCII {
return false
}
username = textToCheck
return true
}, nil).
AddPasswordField("Repeat password", "", 0, '*', func(text string) {
password = text
}).
AddPasswordField("Repeat password", "", 0, '*', func(text string) {
repeatPassword = text
}).
AddButton("Register", func() {
err := engine.Register(username, password, repeatPassword, session)
if err != nil {
ErrorBox(err.Error(), session.Application, form)
return
}
session.Application.SetRoot(nextScreen(engine, session), true)
})
return form
}
func LoginScreen(engine *Akevitt, session *ActiveSession, nextScreen UIFunc) tview.Primitive {
username := ""
password := ""
form := tview.NewForm()
form.AddInputField("Username", "", 0, func(textToCheck string, lastChar rune) bool {
if !unicode.IsLetter(lastChar) && !unicode.IsDigit(lastChar) || lastChar > unicode.MaxASCII {
return false
}
username = textToCheck
return true
}, nil).
AddPasswordField("Password", "", 0, '*', func(text string) {
password = text
}).
AddButton("Login", func() {
err := engine.Login(username, password, session)
if err != nil {
ErrorBox(err.Error(), session.Application, form)
return
}
session.Application.SetRoot(nextScreen(engine, session), true)
})
form.SetTitle("Login")
return form
}