Misc. optimizations to copy menu

This commit is contained in:
2025-12-26 18:26:56 -05:00
parent 12167b1eed
commit aaf69eb590
+19 -34
View File
@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"slices"
"syscall" "syscall"
"time" "time"
@@ -39,22 +40,20 @@ func CopyMenu(vanityPath string, decSlice []string, oldPassword string) {
// determine populated fields in entry // determine populated fields in entry
var fieldStrings = []string{"Username", "Password", "TOTP Code", "URL", "Note (first line)"} var fieldStrings = []string{"Username", "Password", "TOTP Code", "URL", "Note (first line)"}
var indices = []int{1, 0, 2, 3, 4} var indices = []int{1, 0, 2, 3, 4}
var fields []string var fieldOptions []string
var mainFieldPopulated bool
for i := range indices { for i := range indices {
if len(decSlice) > indices[i] && decSlice[indices[i]] != "" { if len(decSlice) > indices[i] && decSlice[indices[i]] != "" {
fields = append(fields, fieldStrings[i]) fieldOptions = append(fieldOptions, fieldStrings[i])
mainFieldPopulated = true
if indices[i] == 0 && oldPassword != "" { if indices[i] == 0 && oldPassword != "" {
fields = append(fields, "Old Password") fieldOptions = append(fieldOptions, "Old Password")
} }
} }
} }
// if no non-first-line notes are populated, render notes and exit // if no copyable lines are populated, render notes and exit
if !mainFieldPopulated { if len(fieldOptions) < 1 {
EntryReader(vanityPath, decSlice, true) EntryReader(vanityPath, decSlice, true)
fmt.Println("Entry has no copyable fields, exiting...") fmt.Printf("\r%sNo copyable fields present, exiting copy menu...%s\n", back.AnsiWarning, back.AnsiReset)
os.Exit(0) os.Exit(0)
} }
@@ -73,16 +72,12 @@ func CopyMenu(vanityPath string, decSlice []string, oldPassword string) {
// and is not desired) // and is not desired)
const timeoutSeconds uint8 = 5 const timeoutSeconds uint8 = 5
selectedChan := make(chan bool, 1) selectedChan := make(chan bool, 1)
var selected bool
go func() { go func() {
var i uint8 for i := uint8(1); i <= timeoutSeconds; i++ {
for i < timeoutSeconds {
time.Sleep(1 * time.Second)
select { select {
case <-selectedChan: case <-selectedChan:
return return
default: case <-time.After(1 * time.Second):
i++
if i == timeoutSeconds { if i == timeoutSeconds {
fmt.Printf("\r%sNo field selected, exiting copy menu...%s\n", back.AnsiWarning, back.AnsiReset) fmt.Printf("\r%sNo field selected, exiting copy menu...%s\n", back.AnsiWarning, back.AnsiReset)
os.Exit(0) os.Exit(0)
@@ -94,33 +89,24 @@ func CopyMenu(vanityPath string, decSlice []string, oldPassword string) {
// copy selected field to clipboard // copy selected field to clipboard
var choice int var choice int
var selectedField string
for { for {
fmt.Println() fmt.Println()
if selected { if selectedField != "" {
choice = front.InputMenuGen("Field to copy:", fields) choice = front.InputMenuGen("Field to copy:", fieldOptions)
} else { } else {
choice = front.InputMenuGen("Field to copy (exiting in 5 seconds):", fields) choice = front.InputMenuGen("Field to copy (exiting in 5 seconds):", fieldOptions)
selectedChan <- true
} }
selected = true selectedField = fieldOptions[choice-1]
selectedChan <- selected if selectedField == "Old Password" {
switch fields[choice-1] { err = clip.CopyString(false, oldPassword)
case "Username":
choice = 1
case "Password":
choice = 0
case "Old Password":
err := clip.CopyString(false, oldPassword)
if err != nil { if err != nil {
other.PrintError("Failed to copy old password to clipboard: "+err.Error(), global.ErrorClipboard) other.PrintError("Failed to copy old password to clipboard: "+err.Error(), global.ErrorClipboard)
} }
continue continue
case "TOTP Code":
choice = 2
case "URL":
choice = 3
case "Note (first line)":
choice = 4
} }
choice = indices[slices.Index(fieldStrings, selectedField)]
if choice == 2 { if choice == 2 {
fmt.Println(back.AnsiWarning + "[Starting]" + back.AnsiReset + " TOTP clipboard refresher") fmt.Println(back.AnsiWarning + "[Starting]" + back.AnsiReset + " TOTP clipboard refresher")
errorChan := make(chan error) errorChan := make(chan error)
@@ -142,8 +128,7 @@ func CopyMenu(vanityPath string, decSlice []string, oldPassword string) {
close(done) close(done)
fmt.Println(back.AnsiBlue + "\n[Stopped]" + back.AnsiReset + " TOTP clipboard refresher") fmt.Println(back.AnsiBlue + "\n[Stopped]" + back.AnsiReset + " TOTP clipboard refresher")
} else { } else {
err := clip.CopyString(false, decSlice[choice]) if err = clip.CopyString(false, decSlice[choice]); err != nil {
if err != nil {
other.PrintError("Failed to copy field to clipboard: "+err.Error(), global.ErrorClipboard) other.PrintError("Failed to copy field to clipboard: "+err.Error(), global.ErrorClipboard)
} }
} }