Debian Codegraph @

app/getpasswd.go

raw
1 package app
2
3 import (
4 "fmt"
5
6 "git.sr.ht/~rjarry/aerc/config"
7 "git.sr.ht/~rjarry/aerc/lib/ui"
8 "go.rockorager.dev/vaxis"
9 )
10
11 type GetPasswd struct {
12 callback func(string, error)
13 title string
14 prompt string
15 input *ui.TextInput
16 }
17
18 func NewGetPasswd(
19 title string, prompt string, cb func(string, error),
20 ) *GetPasswd {
21 getpasswd := &GetPasswd{
22 callback: cb,
23 title: title,
24 prompt: prompt,
25 input: ui.NewTextInput("", config.Ui()).Password(true).Prompt("Password: "),
26 }
27 getpasswd.input.Focus(true)
28 return getpasswd
29 }
30
31 func (gp *GetPasswd) Draw(ctx *ui.Context) {
32 defaultStyle := config.Ui().GetStyle(config.STYLE_DEFAULT)
33 titleStyle := config.Ui().GetStyle(config.STYLE_TITLE)
34
35 ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', defaultStyle)
36 ctx.Fill(0, 0, ctx.Width(), 1, ' ', titleStyle)
37 ctx.Printf(1, 0, titleStyle, "%s", gp.title)
38 ctx.Printf(1, 1, defaultStyle, "%s", gp.prompt)
39 gp.input.Draw(ctx.Subcontext(1, 3, ctx.Width()-2, 1))
40 }
41
42 func (gp *GetPasswd) Invalidate() {
43 ui.Invalidate()
44 }
45
46 func (gp *GetPasswd) Event(event vaxis.Event) bool {
47 switch event := event.(type) {
48 case vaxis.Key:
49 switch {
50 case event.Matches(vaxis.KeyEnter):
51 gp.input.Focus(false)
52 gp.callback(gp.input.String(), nil)
53 case event.Matches(vaxis.KeyEsc):
54 gp.input.Focus(false)
55 gp.callback("", fmt.Errorf("no password provided"))
56 default:
57 gp.input.Event(event)
58 }
59 default:
60 gp.input.Event(event)
61 }
62 return true
63 }
64
65 func (gp *GetPasswd) Focus(f bool) {
66 // Who cares
67 }