2020-03-03 22:20:07 +01:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2020-05-19 13:06:47 +02:00
|
|
|
"fmt"
|
|
|
|
|
2020-03-03 22:20:07 +01:00
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib/ui"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetPasswd struct {
|
|
|
|
ui.Invalidatable
|
2020-05-19 13:06:47 +02:00
|
|
|
callback func(string, error)
|
2020-03-03 22:20:07 +01:00
|
|
|
title string
|
|
|
|
prompt string
|
|
|
|
input *ui.TextInput
|
|
|
|
}
|
|
|
|
|
2020-05-28 16:32:42 +02:00
|
|
|
func NewGetPasswd(title string, prompt string, cb func(string, error)) *GetPasswd {
|
2020-03-03 22:20:07 +01:00
|
|
|
getpasswd := &GetPasswd{
|
|
|
|
callback: cb,
|
|
|
|
title: title,
|
|
|
|
prompt: prompt,
|
2020-05-28 16:32:42 +02:00
|
|
|
input: ui.NewTextInput("").Password(true).Prompt("Password: "),
|
2020-03-03 22:20:07 +01:00
|
|
|
}
|
|
|
|
getpasswd.input.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
getpasswd.Invalidate()
|
|
|
|
})
|
|
|
|
getpasswd.input.Focus(true)
|
|
|
|
return getpasswd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gp *GetPasswd) Draw(ctx *ui.Context) {
|
2020-05-28 16:32:42 +02:00
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
|
|
|
|
ctx.Fill(0, 0, ctx.Width(), 1, ' ', tcell.StyleDefault.Reverse(true))
|
|
|
|
ctx.Printf(1, 0, tcell.StyleDefault.Reverse(true), "%s", gp.title)
|
|
|
|
ctx.Printf(1, 1, tcell.StyleDefault, gp.prompt)
|
2020-03-03 22:20:07 +01:00
|
|
|
gp.input.Draw(ctx.Subcontext(1, 3, ctx.Width()-2, 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gp *GetPasswd) Invalidate() {
|
|
|
|
gp.DoInvalidate(gp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gp *GetPasswd) Event(event tcell.Event) bool {
|
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventKey:
|
|
|
|
switch event.Key() {
|
|
|
|
case tcell.KeyEnter:
|
|
|
|
gp.input.Focus(false)
|
2020-05-19 13:06:47 +02:00
|
|
|
gp.callback(gp.input.String(), nil)
|
|
|
|
case tcell.KeyEsc:
|
|
|
|
gp.input.Focus(false)
|
|
|
|
gp.callback("", fmt.Errorf("no password provided"))
|
2020-03-03 22:20:07 +01:00
|
|
|
default:
|
|
|
|
gp.input.Event(event)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
gp.input.Event(event)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gp *GetPasswd) Focus(f bool) {
|
|
|
|
// Who cares
|
|
|
|
}
|