login-app: use tcell/tview to make the form and add cinematic

This commit is contained in:
nemunaire 2021-02-17 15:03:06 +01:00
commit 4ce6f09a8d
8 changed files with 520 additions and 332 deletions

View file

@ -1,19 +1,45 @@
package main
import (
"math/rand"
"os"
"time"
ui "github.com/VladimirMarkelov/clui"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
const URLLogin = "https://auth.adlin.nemunai.re/login"
var logged = false
var (
loggedAs = ""
)
func askLogin() (lgd *LoginDialog) {
lgd = CreateLoginDialog(" SRS AdLin - Login ")
func modal(p tview.Primitive, width, height int) tview.Primitive {
return tview.NewFlex().
AddItem(nil, 0, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(p, height, 1, false).
AddItem(nil, 0, 1, false), width, 1, false).
AddItem(nil, 0, 1, false)
}
lgd.beforeClose = func() {
func askLogin(app *tview.Application) {
CreateLoginDialog(app, func(username, password string) {
// Display check dialog
CreateCheckDialog(app)
go func() {
if ok, err := checkLogin(username, password); ok {
loggedAs = username
app.Stop()
} else {
CreateErrMsgDialog(app, err)
}
}()
})
/*lgd.beforeClose = func() {
// Display next dialoag
ckd := CreateCheckDialog(" SRS AdLin - Login ", lgd.Username, lgd.Password)
@ -28,20 +54,31 @@ func askLogin() (lgd *LoginDialog) {
}
}
}
}
return
}*/
}
func main() {
ui.InitLibrary()
defer ui.DeinitLibrary()
// seed the rand package with time
rand.Seed(time.Now().UnixNano())
askLogin()
app := tview.NewApplication()
ui.MainLoop()
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyCtrlQ {
app.Stop()
}
return event
})
if !logged {
askLogin(app)
if err := app.Run(); err != nil {
panic(err)
}
if loggedAs == "" {
os.Exit(1)
}
runCinematic(loggedAs)
}