diff --git a/main.go b/main.go index 4e12c4e..7a21a35 100644 --- a/main.go +++ b/main.go @@ -77,7 +77,7 @@ func main() { case "note", "-n": field = 3 case "totp", "-t": - fmt.Println("TOTP code copied to clipboard - until this process is closed, your clipboard will be kept up to date with the current TOTP code") + fmt.Println("TOTP code will be copied to clipboard - your clipboard will be kept up to date with the current TOTP code until this process is closed") field = 5 // TODO Update field after removed from notes (breaking sshyp entry compatibility) default: cli.HelpCopy() diff --git a/src/offline/copy.go b/src/offline/copy.go index 9763439..0019081 100644 --- a/src/offline/copy.go +++ b/src/offline/copy.go @@ -28,19 +28,19 @@ func CopyArgument(targetLocation string, field int, executableName string) { copySubject = decryptedEntry[field] } else { // TOTP mode var err error - var currentSecond int // track current second for TOTP code refresh - var copied bool // track whether the TOTP code has been copied to the clipboard for the first time - for { // keep field copied to clipboard, refresh on 30-second intervals - currentSecond = time.Now().Second() - if currentSecond == 0 || currentSecond == 30 || !copied { - copySubject, err = totp.GenerateCode(decryptedEntry[5], time.Now()) - if err != nil { - fmt.Println(AnsiError + "Error generating TOTP code" + AnsiReset) - os.Exit(1) - } - copyField(copySubject, "") + var copied bool // track whether the TOTP code has been copied to the clipboard for the first time + for { // keep field copied to clipboard, refresh on 30-second intervals + copySubject, err = totp.GenerateCode(decryptedEntry[5], time.Now()) + if err != nil { + fmt.Println(AnsiError + "Error generating TOTP code" + AnsiReset) + os.Exit(1) } - time.Sleep(1 * time.Second) + copyField(copySubject, "") + if !copied { + copied = true + } + // sleep until next 30-second interval + time.Sleep(time.Duration(30-(time.Now().Second()%30)) * time.Second) } } } else {