feat: add clibboard

This commit is contained in:
Adrien Waksberg 2019-10-20 22:14:50 +02:00
parent f6bb6789f6
commit 6a2f4aaa3e

View file

@ -9,6 +9,7 @@ import (
ui "github.com/gizak/termui/v3" ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets" "github.com/gizak/termui/v3/widgets"
"github.com/atotto/clipboard"
) )
// Options // Options
@ -271,6 +272,7 @@ func (c *Cli) AddEntry() bool {
func (c *Cli) ListEntries(ch chan<- bool) { func (c *Cli) ListEntries(ch chan<- bool) {
var pattern, group string var pattern, group string
var entries []Entry var entries []Entry
var selected bool
refresh := true refresh := true
index := -1 index := -1
@ -297,15 +299,20 @@ func (c *Cli) ListEntries(ch chan<- bool) {
} }
if len(entries) > 0 && index >= 0 && index < len(entries) { if len(entries) > 0 && index >= 0 && index < len(entries) {
selected = true
} else {
selected = false
}
if selected {
c.EntryBox(entries[index]) c.EntryBox(entries[index])
} }
ui.Render(l) ui.Render(l)
e := <-uiEvents e := <-uiEvents
switch e.ID { switch e.ID {
case "t":
c.ChoiceBox("test", false)
case "q": case "q":
clipboard.WriteAll("")
ch <- true ch <- true
case "<Enter>": case "<Enter>":
index = l.SelectedRow index = l.SelectedRow
@ -316,11 +323,11 @@ func (c *Cli) ListEntries(ch chan<- bool) {
case "n": case "n":
refresh = c.AddEntry() refresh = c.AddEntry()
case "u": case "u":
if len(entries) > 0 && index >= 0 && index < len(entries) { if selected {
refresh = c.UpdateEntry(entries[index]) refresh = c.UpdateEntry(entries[index])
} }
case "d": case "d":
if len(entries) > 0 && index >= 0 && index < len(entries) { if selected {
refresh = c.DeleteEntry(entries[index]) refresh = c.DeleteEntry(entries[index])
} }
case "/": case "/":
@ -337,6 +344,20 @@ func (c *Cli) ListEntries(ch chan<- bool) {
if len(entries) > 0 { if len(entries) > 0 {
l.ScrollUp() l.ScrollUp()
} }
case "<C-b>":
if selected {
clipboard.WriteAll(entries[index].User)
}
case "<C-c>":
if selected {
clipboard.WriteAll(entries[index].Password)
}
case "<C-o>":
if selected {
code, time, _ := entries[index].OTPCode()
c.NotificationBox(fmt.Sprintf("the OTP code is available for %d seconds", time), false)
clipboard.WriteAll(code)
}
} }
} }
} }
@ -429,7 +450,7 @@ func Run() {
select { select {
case <-c1: case <-c1:
return return
case <-time.After(10 * time.Second): case <-time.After(300 * time.Second):
return return
} }
} }