From e275ad660721be2dd3c0042e6e47b1f54b806a39 Mon Sep 17 00:00:00 2001 From: Greg Date: Tue, 1 Oct 2019 18:22:38 -0400 Subject: [PATCH] Add functionality to randomly generate passwords on the insert page. --- cmd/passgo-gui/main.go | 86 ++++++++++++++++++++++++++++++++++++++---- cmd/passgo-gui/ui.go | 17 +++++++++ go.mod | 1 + go.sum | 4 ++ 4 files changed, 100 insertions(+), 8 deletions(-) diff --git a/cmd/passgo-gui/main.go b/cmd/passgo-gui/main.go index bfb43b0..6e8574a 100644 --- a/cmd/passgo-gui/main.go +++ b/cmd/passgo-gui/main.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "path" + "strconv" "strings" "sync" "time" @@ -23,6 +24,7 @@ import ( "gopkg.in/yaml.v2" "git.wow.st/gmp/passgo" + "git.wow.st/gmp/rand" ) type conf struct { @@ -470,24 +472,92 @@ func eventLoop() { } var insName, insValue string + genBtn := &SelButton{SelColor: darkgray} + genBtn.Button = Button{Face: face, Label: "generate", Background: gray} + symBtn := &SelButton{SelColor: gray, Selected: true} + numBtn := &SelButton{SelColor: gray, Selected: true} + symBtn.Button = Button{Face: face, Label: "@", Background: darkgray} + numBtn.Button = Button{Face: face, Label: "#", Background: darkgray} + lenEd := &text.Editor{Face: face, SingleLine: true, Alignment: text.End} + lenEd.SetText("15") + lBtn := &Button{Face: face, Label: "<", Background: gray} + rBtn := &Button{Face: face, Label: ">", Background: gray} + + updatePw := func() { + if !genBtn.Selected { + passvalEd.SetText("") + return + } + var gen rand.Generator + switch { + case symBtn.Selected && numBtn.Selected: + gen = rand.Char + case symBtn.Selected: + gen = rand.LettersSymbols + case numBtn.Selected: + gen = rand.LetterDigits + default: + gen = rand.Letter + } + l,_ := strconv.Atoi(lenEd.Text()) + pw,_ := rand.Slice(gen,l) + passvalEd.SetText(string(pw)) + } insertPage = func() { - cs = flex.Rigid() - c2 := flex.End(insertLabel.Layout(ops, cs)) + c2 := flex.End(insertLabel.Layout(ops, flex.Rigid())) c3 := flex.End(passnameLabel.Layout(ops, flex.Rigid())) c4 := flex.End(passnameEd.Layout(c, q, ops, flex.Rigid())) c5 := flex.End(passvalLabel.Layout(ops, flex.Rigid())) c6 := flex.End(passvalEd.Layout(c, q, ops, flex.Rigid())) - cs = flex.Rigid() + al := &layout.Align{Alignment: layout.E} btnflx := &layout.Flex{Axis: layout.Horizontal} - btnflx.Init(ops, al.Begin(ops, cs)) - bc1 := btnflx.End(backBtn.Layout(c, q, ops, btnflx.Rigid())) - bc2 := btnflx.End(saveBtn.Layout(c, q, ops, btnflx.Rigid())) - c7 := flex.End(al.End(btnflx.Layout(bc1, bc2))) - flex.Layout(c1, c2, c3, c4, c5, c6, c7) + btnflx.Init(ops, al.Begin(ops, flex.Rigid())) + bc1 := btnflx.End(lBtn.Layout(c, q, ops, btnflx.Rigid())) + cs := btnflx.Rigid() + cs.Width.Min = 60 + bc2 := btnflx.End(lenEd.Layout(c, q, ops, cs)) + bc3 := btnflx.End(rBtn.Layout(c, q, ops, btnflx.Rigid())) + bc4 := btnflx.End(symBtn.Layout(c, q, ops, btnflx.Rigid())) + bc5 := btnflx.End(numBtn.Layout(c, q, ops, btnflx.Rigid())) + bc6 := btnflx.End(genBtn.Layout(c, q, ops, btnflx.Rigid())) + c7 := flex.End(al.End(btnflx.Layout(bc1, bc2, bc3, bc4, bc5, bc6))) + btnflx.Init(ops, al.Begin(ops, flex.Rigid())) + bc1 = btnflx.End(backBtn.Layout(c, q, ops, btnflx.Rigid())) + bc2 = btnflx.End(saveBtn.Layout(c, q, ops, btnflx.Rigid())) + c8 := flex.End(al.End(btnflx.Layout(bc1, bc2))) + flex.Layout(c1, c2, c3, c4, c5, c6, c7, c8) + + if lBtn.Clicked() { + l,_ := strconv.Atoi(lenEd.Text()) + if l > 0 { + l -= 1 + } + lenEd.SetText(strconv.Itoa(l)) + updatePw() + w.Invalidate() + } + if rBtn.Clicked() { + l,_ := strconv.Atoi(lenEd.Text()) + lenEd.SetText(strconv.Itoa(l+1)) + updatePw() + w.Invalidate() + } + if genBtn.Clicked() { + updatePw() + w.Invalidate() + } + if symBtn.Clicked() { + updatePw() + w.Invalidate() + } + if numBtn.Clicked() { + updatePw() + w.Invalidate() + } if backBtn.Clicked() { w.Invalidate() page = listPage diff --git a/cmd/passgo-gui/ui.go b/cmd/passgo-gui/ui.go index 50a66f5..a50ca08 100644 --- a/cmd/passgo-gui/ui.go +++ b/cmd/passgo-gui/ui.go @@ -56,6 +56,12 @@ func (b *Overlay) Layout(c ui.Config, ops *ui.Ops, cs layout.Constraints) layout return ins.End(dims) } +type SelButton struct { + Button + SelColor color.RGBA + Selected bool +} + type Button struct { Face text.Face Label string @@ -127,3 +133,14 @@ func (b *Button) Layout(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Const func (b *Button) Clicked() bool { return b.clicked } + +func (b *SelButton) Clicked() bool { + if b.clicked { + b.Selected = !b.Selected + b.SelColor, b.Background = b.Background, b.SelColor + return true + } else { + return false + } +} + diff --git a/go.mod b/go.mod index 576526c..ff7fe86 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.12 require ( gioui.org/ui v0.0.0-20190918172808-816f0e901fc6 git.wow.st/gmp/clip v0.0.0-20191001134149-1458ba6a7cf5 + git.wow.st/gmp/rand v0.0.0-20191001220155-a81bebfaf8b0 github.com/fsnotify/fsnotify v1.4.7 github.com/jcmdev0/gpgagent v0.0.0-20180509014935-5601b32d936c golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 diff --git a/go.sum b/go.sum index 86dd242..fe1b3f2 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,10 @@ gioui.org/ui v0.0.0-20190918172808-816f0e901fc6 h1:LvHEYxyOW7g+PhOiAm8Delc3AUv9E gioui.org/ui v0.0.0-20190918172808-816f0e901fc6/go.mod h1:PssKPKlqVIeyaed+0w492Xc2NgX5M3n6oZKOAj5rxoE= git.wow.st/gmp/clip v0.0.0-20191001134149-1458ba6a7cf5 h1:OKeTjZST+/TKvtdA258NXJH+/gIx/xwyZxKrAezNFvk= git.wow.st/gmp/clip v0.0.0-20191001134149-1458ba6a7cf5/go.mod h1:NLdpaBoMQNFqncwP8OVRNWUDw1Kt9XWm3snfT7cXu24= +git.wow.st/gmp/rand v0.0.0-20191001220006-66bfa936ad6b h1:yu3SnygEUgkNJ3xBXLkWHws7tki5qveZ6Kk7ybWj1GQ= +git.wow.st/gmp/rand v0.0.0-20191001220006-66bfa936ad6b/go.mod h1:8+2Gwnrpc5yuk8Wp6cBhxvGcNLumYiPbQ7n0SQ8h29A= +git.wow.st/gmp/rand v0.0.0-20191001220155-a81bebfaf8b0 h1:08wP00wvbDINsct1fzKV1xGGLvvtNsSb2X4CtIdpBzM= +git.wow.st/gmp/rand v0.0.0-20191001220155-a81bebfaf8b0/go.mod h1:8+2Gwnrpc5yuk8Wp6cBhxvGcNLumYiPbQ7n0SQ8h29A= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/jcmdev0/gpgagent v0.0.0-20180509014935-5601b32d936c h1:DCnjNrPDSEslcqqBgcZBxlLUIhk2elQVyf2V+HkyxJI=