From 6811143925384ba1cfda8b3e1b338b0cfb9ac6e3 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 21 May 2019 16:31:04 -0400 Subject: New account wizard, part one --- lib/ui/text.go | 2 +- lib/ui/textinput.go | 31 ++++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/ui/text.go b/lib/ui/text.go index 8aea8eb..2b82598 100644 --- a/lib/ui/text.go +++ b/lib/ui/text.go @@ -77,7 +77,7 @@ func (t *Text) Draw(ctx *Context) { style = style.Reverse(true) } ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', style) - ctx.Printf(x, 0, style, t.text) + ctx.Printf(x, 0, style, "%s", t.text) } func (t *Text) Invalidate() { diff --git a/lib/ui/textinput.go b/lib/ui/textinput.go index 4a5308e..ce443c3 100644 --- a/lib/ui/textinput.go +++ b/lib/ui/textinput.go @@ -10,14 +10,15 @@ import ( type TextInput struct { Invalidatable - cells int - ctx *Context - focus bool - index int - prompt string - scroll int - text []rune - change []func(ti *TextInput) + cells int + ctx *Context + focus bool + index int + password bool + prompt string + scroll int + text []rune + change []func(ti *TextInput) } // Creates a new TextInput. TextInputs will render a "textbox" in the entire @@ -31,6 +32,11 @@ func NewTextInput(text string) *TextInput { } } +func (ti *TextInput) Password(password bool) *TextInput { + ti.password = password + return ti +} + func (ti *TextInput) Prompt(prompt string) *TextInput { ti.prompt = prompt return ti @@ -42,6 +48,7 @@ func (ti *TextInput) String() string { func (ti *TextInput) Set(value string) { ti.text = []rune(value) + ti.index = len(ti.text) } func (ti *TextInput) Invalidate() { @@ -51,7 +58,13 @@ func (ti *TextInput) Invalidate() { func (ti *TextInput) Draw(ctx *Context) { ti.ctx = ctx // gross ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault) - ctx.Printf(0, 0, tcell.StyleDefault, "%s%s", ti.prompt, string(ti.text)) + if ti.password { + x := ctx.Printf(0, 0, tcell.StyleDefault, "%s", ti.prompt) + cells := runewidth.StringWidth(string(ti.text)) + ctx.Fill(x, 0, cells, 1, '*', tcell.StyleDefault) + } else { + ctx.Printf(0, 0, tcell.StyleDefault, "%s%s", ti.prompt, string(ti.text)) + } cells := runewidth.StringWidth(string(ti.text[:ti.index]) + ti.prompt) if cells != ti.cells && ti.focus { ctx.SetCursor(cells, 0) -- cgit v1.2.3