package main import ( "citrons.xyz/talk/tui" "os" ) type loginPrompt struct { input tui.TextInput uid string username string customPrompt string waiting bool callback func(ok bool) } func newLoginPrompt(completeName string) *loginPrompt { var prompt loginPrompt username := completeName if username == "" { username = os.Getenv("USER") } prompt.input.SetText(username) return &prompt } func (p *loginPrompt) Input() *tui.TextInput { if !p.waiting { return &p.input } else { return &tui.TextInput {} } } func (p *loginPrompt) Send(text string) { if p.uid == "" { p.username = text globalApp.authAnon(text, func(success bool, uid string) { if success { globalApp.removePrompt(p) globalApp.cmdWindow.warn( "you have created a temporary account. " + "set a password to keep it.", ) } else if uid == "" { p.username = "" } else { p.uid = uid p.input.SetText("") p.input.Private = true } }) } else { p.input.SetText("") p.waiting = true globalApp.authPassword(p.uid, text, func(success bool) { p.waiting = false if !globalApp.authenticated { p.uid = "" p.input.SetText(p.username) p.input.Private = false return } globalApp.removePrompt(p) if p.callback != nil { p.callback(success) } }) } } func (p *loginPrompt) ShowStatusLine() { tui.Text("[", nil) tui.Text("login", &tui.Style { Bg: tui.White, Fg: tui.Blue, Bold: true, }) tui.Text("] ", nil) if p.customPrompt == "" { if p.uid == "" { tui.Text("username", nil) } else { tui.Text("password", nil) } } else { tui.Text(p.customPrompt, nil) } tui.Text(":", nil) } type passwordChangePrompt struct { input tui.TextInput password string } func (p *passwordChangePrompt) Input() *tui.TextInput { p.input.Private = true return &p.input } func (p *passwordChangePrompt) Send(text string) { if text != p.password { globalApp.cmdWindow.err("passwords do not match") } else { globalApp.setPassword(text, func(ok bool) { if ok { globalApp.cmdWindow.info("password changed") } }) } globalApp.removePrompt(p) } func (p *passwordChangePrompt) ShowStatusLine() { tui.Text("[", nil) tui.Text("login", &tui.Style { Bg: tui.White, Fg: tui.Blue, Bold: true, }) tui.Text("] ", nil) tui.Text("confirm new password:", nil) }