aboutsummaryrefslogtreecommitdiffstats
path: root/app/getpasswd.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-10-09 13:52:20 +0200
committerRobin Jarry <robin@jarry.cc>2023-10-10 11:37:56 +0200
commit598e4a5803578ab3e291f232d6aad31b4efd8ea4 (patch)
treec55e16d60e2c3eea2d6de27d1bac18db5670ec77 /app/getpasswd.go
parent61bca76423ee87bd59084a146eca71c6bae085e1 (diff)
downloadaerc-598e4a5803578ab3e291f232d6aad31b4efd8ea4.tar.gz
widgets: rename package to app
This is the central point of all aerc. Having it named widgets is confusing. Rename it to app. It will make a cleaner transition when making the app.Aerc object available globally in the next commit. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'app/getpasswd.go')
-rw-r--r--app/getpasswd.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/app/getpasswd.go b/app/getpasswd.go
new file mode 100644
index 00000000..9a3dbf3b
--- /dev/null
+++ b/app/getpasswd.go
@@ -0,0 +1,68 @@
+package app
+
+import (
+ "fmt"
+
+ "github.com/gdamore/tcell/v2"
+
+ "git.sr.ht/~rjarry/aerc/config"
+ "git.sr.ht/~rjarry/aerc/lib/ui"
+)
+
+type GetPasswd struct {
+ callback func(string, error)
+ title string
+ prompt string
+ input *ui.TextInput
+}
+
+func NewGetPasswd(
+ title string, prompt string, cb func(string, error),
+) *GetPasswd {
+ getpasswd := &GetPasswd{
+ callback: cb,
+ title: title,
+ prompt: prompt,
+ input: ui.NewTextInput("", config.Ui).Password(true).Prompt("Password: "),
+ }
+ getpasswd.input.Focus(true)
+ return getpasswd
+}
+
+func (gp *GetPasswd) Draw(ctx *ui.Context) {
+ defaultStyle := config.Ui.GetStyle(config.STYLE_DEFAULT)
+ titleStyle := config.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() {
+ ui.Invalidate()
+}
+
+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
+}