commit 0d2802cc6ad9e24df235ada92864fe7bc4df809d
parent de1ccc0c62fde3bd45ec42cf2d4e5d4932a715d2
Author: Sean Enck <sean@ttypty.com>
Date: Tue, 25 Jul 2023 20:31:11 -0400
migrate coloring back to platform
Diffstat:
5 files changed, 121 insertions(+), 121 deletions(-)
diff --git a/internal/app/colors.go b/internal/app/colors.go
@@ -1,54 +0,0 @@
-// Package app manages definitions within lockbox for colorization.
-package app
-
-import (
- "errors"
-
- "github.com/enckse/lockbox/internal/inputs"
-)
-
-const (
- // Red will get red terminal coloring.
- Red = iota
-)
-
-type (
- // Color are terminal colors for dumb terminal coloring.
- Color int
-)
-
-const (
- termBeginRed = "\033[1;31m"
- termEndRed = "\033[0m"
-)
-
-type (
- // Terminal represents terminal coloring information.
- Terminal struct {
- Start string
- End string
- }
-)
-
-// NewTerminal will retrieve start/end terminal coloration indicators.
-func NewTerminal(color Color) (Terminal, error) {
- if color != Red {
- return Terminal{}, errors.New("bad color")
- }
- interactive, err := inputs.IsInteractive()
- if err != nil {
- return Terminal{}, err
- }
- colors := interactive
- if colors {
- isColored, err := inputs.IsNoColorEnabled()
- if err != nil {
- return Terminal{}, err
- }
- colors = !isColored
- }
- if colors {
- return Terminal{Start: termBeginRed, End: termEndRed}, nil
- }
- return Terminal{}, nil
-}
diff --git a/internal/app/colors_test.go b/internal/app/colors_test.go
@@ -1,66 +0,0 @@
-package app_test
-
-import (
- "os"
- "testing"
-
- "github.com/enckse/lockbox/internal/app"
-)
-
-func TestHasColoring(t *testing.T) {
- os.Setenv("LOCKBOX_INTERACTIVE", "yes")
- os.Setenv("LOCKBOX_NOCOLOR", "no")
- term, err := app.NewTerminal(app.Red)
- if err != nil {
- t.Errorf("color was valid: %v", err)
- }
- if term.Start != "\033[1;31m" || term.End != "\033[0m" {
- t.Error("bad resulting color")
- }
-}
-
-func TestBadColor(t *testing.T) {
- _, err := app.NewTerminal(app.Color(5))
- if err == nil || err.Error() != "bad color" {
- t.Errorf("invalid color error: %v", err)
- }
-}
-
-func TestNoColoring(t *testing.T) {
- os.Setenv("LOCKBOX_INTERACTIVE", "no")
- os.Setenv("LOCKBOX_NOCOLOR", "yes")
- term, err := app.NewTerminal(app.Red)
- if err != nil {
- t.Errorf("color was valid: %v", err)
- }
- if term.Start != "" || term.End != "" {
- t.Error("should have no color")
- }
- os.Setenv("LOCKBOX_INTERACTIVE", "yes")
- os.Setenv("LOCKBOX_NOCOLOR", "yes")
- term, err = app.NewTerminal(app.Red)
- if err != nil {
- t.Errorf("color was valid: %v", err)
- }
- if term.Start != "" || term.End != "" {
- t.Error("should have no color")
- }
- os.Setenv("LOCKBOX_INTERACTIVE", "no")
- os.Setenv("LOCKBOX_NOCOLOR", "no")
- term, err = app.NewTerminal(app.Red)
- if err != nil {
- t.Errorf("color was valid: %v", err)
- }
- if term.Start != "" || term.End != "" {
- t.Error("should have no color")
- }
- os.Setenv("LOCKBOX_INTERACTIVE", "yes")
- os.Setenv("LOCKBOX_NOCOLOR", "no")
- term, err = app.NewTerminal(app.Red)
- if err != nil {
- t.Errorf("color was valid: %v", err)
- }
- if term.Start == "" || term.End == "" {
- t.Error("should have color")
- }
-}
diff --git a/internal/app/totp.go b/internal/app/totp.go
@@ -106,7 +106,7 @@ func (args *TOTPArguments) display(opts TOTPOptions) error {
if !interactive && clip {
return errors.New("clipboard not available in non-interactive mode")
}
- coloring, err := NewTerminal(Red)
+ coloring, err := platform.NewTerminal(platform.Red)
if err != nil {
return err
}
diff --git a/internal/platform/colors.go b/internal/platform/colors.go
@@ -0,0 +1,54 @@
+// Package platform manages definitions within lockbox for colorization.
+package platform
+
+import (
+ "errors"
+
+ "github.com/enckse/lockbox/internal/inputs"
+)
+
+const (
+ // Red will get red terminal coloring.
+ Red = iota
+)
+
+type (
+ // Color are terminal colors for dumb terminal coloring.
+ Color int
+)
+
+const (
+ termBeginRed = "\033[1;31m"
+ termEndRed = "\033[0m"
+)
+
+type (
+ // Terminal represents terminal coloring information.
+ Terminal struct {
+ Start string
+ End string
+ }
+)
+
+// NewTerminal will retrieve start/end terminal coloration indicators.
+func NewTerminal(color Color) (Terminal, error) {
+ if color != Red {
+ return Terminal{}, errors.New("bad color")
+ }
+ interactive, err := inputs.IsInteractive()
+ if err != nil {
+ return Terminal{}, err
+ }
+ colors := interactive
+ if colors {
+ isColored, err := inputs.IsNoColorEnabled()
+ if err != nil {
+ return Terminal{}, err
+ }
+ colors = !isColored
+ }
+ if colors {
+ return Terminal{Start: termBeginRed, End: termEndRed}, nil
+ }
+ return Terminal{}, nil
+}
diff --git a/internal/platform/colors_test.go b/internal/platform/colors_test.go
@@ -0,0 +1,66 @@
+package platform_test
+
+import (
+ "os"
+ "testing"
+
+ "github.com/enckse/lockbox/internal/platform"
+)
+
+func TestHasColoring(t *testing.T) {
+ os.Setenv("LOCKBOX_INTERACTIVE", "yes")
+ os.Setenv("LOCKBOX_NOCOLOR", "no")
+ term, err := platform.NewTerminal(platform.Red)
+ if err != nil {
+ t.Errorf("color was valid: %v", err)
+ }
+ if term.Start != "\033[1;31m" || term.End != "\033[0m" {
+ t.Error("bad resulting color")
+ }
+}
+
+func TestBadColor(t *testing.T) {
+ _, err := platform.NewTerminal(platform.Color(5))
+ if err == nil || err.Error() != "bad color" {
+ t.Errorf("invalid color error: %v", err)
+ }
+}
+
+func TestNoColoring(t *testing.T) {
+ os.Setenv("LOCKBOX_INTERACTIVE", "no")
+ os.Setenv("LOCKBOX_NOCOLOR", "yes")
+ term, err := platform.NewTerminal(platform.Red)
+ if err != nil {
+ t.Errorf("color was valid: %v", err)
+ }
+ if term.Start != "" || term.End != "" {
+ t.Error("should have no color")
+ }
+ os.Setenv("LOCKBOX_INTERACTIVE", "yes")
+ os.Setenv("LOCKBOX_NOCOLOR", "yes")
+ term, err = platform.NewTerminal(platform.Red)
+ if err != nil {
+ t.Errorf("color was valid: %v", err)
+ }
+ if term.Start != "" || term.End != "" {
+ t.Error("should have no color")
+ }
+ os.Setenv("LOCKBOX_INTERACTIVE", "no")
+ os.Setenv("LOCKBOX_NOCOLOR", "no")
+ term, err = platform.NewTerminal(platform.Red)
+ if err != nil {
+ t.Errorf("color was valid: %v", err)
+ }
+ if term.Start != "" || term.End != "" {
+ t.Error("should have no color")
+ }
+ os.Setenv("LOCKBOX_INTERACTIVE", "yes")
+ os.Setenv("LOCKBOX_NOCOLOR", "no")
+ term, err = platform.NewTerminal(platform.Red)
+ if err != nil {
+ t.Errorf("color was valid: %v", err)
+ }
+ if term.Start == "" || term.End == "" {
+ t.Error("should have color")
+ }
+}