feat: add select box for group in a form

This commit is contained in:
Adrien Waksberg 2020-12-22 18:41:04 +01:00
parent 0bf248d70e
commit ce5563d989

View file

@ -114,6 +114,40 @@ func (c *Cli) InputBox(title string, input string, hidden bool) string {
} }
} }
// SelectBox to select an item from a list
func (c *Cli) SelectBox(title string, items []string) string {
l := widgets.NewList()
l.Title = title
l.TextStyle = ui.NewStyle(ui.ColorYellow)
l.SelectedRowStyle = ui.NewStyle(ui.ColorGreen, ui.ColorClear, ui.ModifierBold)
l.WrapText = false
l.SetRect(10, 10, 70, 5)
l.Rows = items
uiEvents := ui.PollEvents()
for {
ui.Render(l)
e := <-uiEvents
switch e.ID {
case "q", "<C-c>", "<Escape>":
return ""
case "<Enter>":
if len(l.Rows) == 0 {
return ""
}
return l.Rows[l.SelectedRow]
case "j", "<Down>":
if len(l.Rows) > 0 {
l.ScrollDown()
}
case "k", "<Up>":
if len(l.Rows) > 0 {
l.ScrollUp()
}
}
}
}
// EntryBox to add a new entry // EntryBox to add a new entry
func (c *Cli) EntryBox(entry Entry) { func (c *Cli) EntryBox(entry Entry) {
p := widgets.NewParagraph() p := widgets.NewParagraph()
@ -139,7 +173,7 @@ func (c *Cli) GroupsBox() string {
l.TextStyle = ui.NewStyle(ui.ColorYellow) l.TextStyle = ui.NewStyle(ui.ColorYellow)
l.SelectedRowStyle = ui.NewStyle(ui.ColorGreen, ui.ColorClear, ui.ModifierBold) l.SelectedRowStyle = ui.NewStyle(ui.ColorGreen, ui.ColorClear, ui.ModifierBold)
l.WrapText = false l.WrapText = false
l.SetRect(0, 0, 25, 23) l.SetRect(25, 0, 80, 20)
l.Rows = append(c.Wallet.Groups(), "No group") l.Rows = append(c.Wallet.Groups(), "No group")
uiEvents := ui.PollEvents() uiEvents := ui.PollEvents()
@ -244,7 +278,14 @@ func (c *Cli) DeleteEntry(entry Entry) bool {
// UpdateEntry to update an existing entry // UpdateEntry to update an existing entry
func (c *Cli) UpdateEntry(entry Entry) bool { func (c *Cli) UpdateEntry(entry Entry) bool {
entry.Name = c.InputBox("Name", entry.Name, false) entry.Name = c.InputBox("Name", entry.Name, false)
entry.Group = c.InputBox("Group", entry.Group, false) if entry.Group == "" || c.ChoiceBox("Change the group ?", false) {
group := c.SelectBox("Group", append(c.Wallet.Groups(), "* Create new group *"))
if group == "* Create new group *" || group == "" {
entry.Group = c.InputBox("Group", "", false)
} else {
entry.Group = group
}
}
entry.URI = c.InputBox("URI", entry.URI, false) entry.URI = c.InputBox("URI", entry.URI, false)
entry.User = c.InputBox("Username", entry.User, false) entry.User = c.InputBox("Username", entry.User, false)
if c.ChoiceBox("Generate a new random password ?", false) { if c.ChoiceBox("Generate a new random password ?", false) {
@ -275,7 +316,12 @@ func (c *Cli) AddEntry() bool {
entry := Entry{} entry := Entry{}
entry.GenerateID() entry.GenerateID()
entry.Name = c.InputBox("Name", "", false) entry.Name = c.InputBox("Name", "", false)
group := c.SelectBox("Group", append(c.Wallet.Groups(), "* Create new group *"))
if group == "* Create new group *" || group == "" {
entry.Group = c.InputBox("Group", "", false) entry.Group = c.InputBox("Group", "", false)
} else {
entry.Group = group
}
entry.URI = c.InputBox("URI", "", false) entry.URI = c.InputBox("URI", "", false)
entry.User = c.InputBox("Username", "", false) entry.User = c.InputBox("Username", "", false)
if c.ChoiceBox("Generate a random password ?", true) { if c.ChoiceBox("Generate a random password ?", true) {