aerc/widgets/getpasswd.go
y0ast 03650474e2 update tcell to v2 and enable TrueColor support
Also update to the tcell v2 PaletteColor api, which should keep the chosen
theme of the user intact.

Note, that if $TRUECOLOR is defined and a truecolor given, aerc will now stop
clipping the value to one of the theme colors.
Generally this is desired behaviour though.
2020-12-18 07:23:22 +01:00

74 lines
1.6 KiB
Go

package widgets
import (
"fmt"
"github.com/gdamore/tcell/v2"
"git.sr.ht/~sircmpwn/aerc/config"
"git.sr.ht/~sircmpwn/aerc/lib/ui"
)
type GetPasswd struct {
ui.Invalidatable
callback func(string, error)
title string
prompt string
input *ui.TextInput
conf *config.AercConfig
}
func NewGetPasswd(title string, prompt string, conf *config.AercConfig,
cb func(string, error)) *GetPasswd {
getpasswd := &GetPasswd{
callback: cb,
title: title,
prompt: prompt,
conf: conf,
input: ui.NewTextInput("", conf.Ui).Password(true).Prompt("Password: "),
}
getpasswd.input.OnInvalidate(func(_ ui.Drawable) {
getpasswd.Invalidate()
})
getpasswd.input.Focus(true)
return getpasswd
}
func (gp *GetPasswd) Draw(ctx *ui.Context) {
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)
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)
gp.callback(gp.input.String(), nil)
case tcell.KeyEsc:
gp.input.Focus(false)
gp.callback("", fmt.Errorf("no password provided"))
default:
gp.input.Event(event)
}
default:
gp.input.Event(event)
}
return true
}
func (gp *GetPasswd) Focus(f bool) {
// Who cares
}