2020-03-03 22:20:07 +01:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2020-05-19 13:06:47 +02:00
|
|
|
"fmt"
|
|
|
|
|
2020-11-30 23:07:03 +01:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2020-03-03 22:20:07 +01:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/config"
|
|
|
|
"git.sr.ht/~rjarry/aerc/lib/ui"
|
2020-03-03 22:20:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type GetPasswd struct {
|
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-07-27 10:03:55 +02:00
|
|
|
conf *config.AercConfig
|
2020-03-03 22:20:07 +01:00
|
|
|
}
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
func NewGetPasswd(title string, prompt string, conf *config.AercConfig,
|
2022-07-31 22:16:40 +02:00
|
|
|
cb func(string, error),
|
|
|
|
) *GetPasswd {
|
2020-03-03 22:20:07 +01:00
|
|
|
getpasswd := &GetPasswd{
|
|
|
|
callback: cb,
|
|
|
|
title: title,
|
|
|
|
prompt: prompt,
|
2020-07-27 10:03:55 +02:00
|
|
|
conf: conf,
|
2022-07-03 17:11:12 +02:00
|
|
|
input: ui.NewTextInput("", &conf.Ui).Password(true).Prompt("Password: "),
|
2020-03-03 22:20:07 +01:00
|
|
|
}
|
|
|
|
getpasswd.input.Focus(true)
|
|
|
|
return getpasswd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gp *GetPasswd) Draw(ctx *ui.Context) {
|
2020-07-27 10:03:55 +02:00
|
|
|
defaultStyle := gp.conf.Ui.GetStyle(config.STYLE_DEFAULT)
|
|
|
|
titleStyle := gp.conf.Ui.GetStyle(config.STYLE_TITLE)
|
|
|
|
|
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', defaultStyle)
|
|
|
|
ctx.Fill(0, 0, ctx.Width(), 1, ' ', titleStyle)
|
|
|
|
ctx.Printf(1, 0, titleStyle, "%s", gp.title)
|
|
|
|
ctx.Printf(1, 1, defaultStyle, 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() {
|
2022-10-07 18:00:31 +02:00
|
|
|
ui.Invalidate()
|
2020-03-03 22:20:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|