lockbox

password manager
Log | Files | Refs | README | LICENSE

commit bf45572721bb9e54318ae99fd12f85c8fa12f7b8
parent 2622ccb7cce761231474c2d845d4ef99ff653267
Author: Sean Enck <sean@ttypty.com>
Date:   Tue,  5 Oct 2021 18:27:22 -0400

support turning colors off

Diffstat:
Mcmd/lb-totp/main.go | 8++++++--
Mcmd/lb/main.go | 6+++++-
Minternal/utils.go | 24++++++++++++++++++++----
Mtests/expected.log | 4++--
Mtests/run.sh | 1+
5 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/cmd/lb-totp/main.go b/cmd/lb-totp/main.go @@ -46,6 +46,10 @@ func clear() { } func display(token string, clip bool) error { + redStart, redEnd, err := internal.GetColor(internal.ColorRed) + if err != nil { + return err + } tok := strings.TrimSpace(token) store := filepath.Join(getEnv(), tok+internal.Extension) if !stock.PathExists(store) { @@ -92,8 +96,8 @@ func display(token string, clip bool) error { startColor := "" endColor := "" if left < 10 { - startColor = internal.TermBeginRed - endColor = internal.TermEndRed + startColor = redStart + endColor = redEnd } if !clip { outputs = append(outputs, fmt.Sprintf("%s\n %s", tok, code)) diff --git a/cmd/lb/main.go b/cmd/lb/main.go @@ -187,6 +187,10 @@ func main() { stock.Die("cannot glob to clipboard", internal.NewLockboxError("bad glob request")) } } + startColor, endColor, err := internal.GetColor(internal.ColorRed) + if err != nil { + stock.Die("unable to get color for terminal", err) + } for _, entry := range entries { if !stock.PathExists(entry) { stock.Die("invalid entry", internal.NewLockboxError("entry not found")) @@ -207,7 +211,7 @@ func main() { fileName = fileName[1:] } fileName = strings.ReplaceAll(fileName, internal.Extension, "") - fmt.Printf("%s%s:%s\n", internal.TermBeginRed, fileName, internal.TermEndRed) + fmt.Printf("%s%s:%s\n", startColor, fileName, endColor) } fmt.Println(value) continue diff --git a/internal/utils.go b/internal/utils.go @@ -10,15 +10,31 @@ import ( "voidedtech.com/stock" ) +type ( + // Color are terminal colors for dumb terminal coloring. + Color int +) + const ( // Extension is the lockbox file extension. Extension = ".lb" - // TermBeginRed will turn terminal text red. - TermBeginRed = "\033[1;31m" - // TermEndRed will end red terminal text. - TermEndRed = "\033[0m" + termBeginRed = "\033[1;31m" + termEndRed = "\033[0m" + // ColorRed will get red terminal coloring. + ColorRed = iota ) +// GetColor will retrieve start/end terminal coloration indicators. +func GetColor(color Color) (string, string, error) { + if color != ColorRed { + return "", "", NewLockboxError("bad color") + } + if os.Getenv("LOCKBOX_NOCOLOR") == "yes" { + return "", "", nil + } + return termBeginRed, termEndRed, nil +} + // GetStore gets the lockbox directory. func GetStore() string { return os.Getenv("LOCKBOX_STORE") diff --git a/tests/expected.log b/tests/expected.log @@ -1,8 +1,8 @@ -keys/one: +keys/one: test -keys/one2: +keys/one2: test2 keys/one diff --git a/tests/run.sh b/tests/run.sh @@ -5,6 +5,7 @@ TESTS="$PWD/bin" export LOCKBOX_STORE="$TESTS/lb" export LOCKBOX_KEYMODE="plaintext" export LOCKBOX_TOTP="totp" +export LOCKBOX_NOCOLOR="yes" export PWGEN_SOURCE="$PWD" export PWGEN_SPECIAL="u" export PWGEN_SED="s/[[:alnum:]]/u/g;s/\./u/g"