commit a520b1693fda626aaee61df4a78fe81361f08083
parent a2d393ceee4db3649fced5db511c4b4745477199
Author: Sean Enck <sean@ttypty.com>
Date: Tue, 11 Oct 2022 20:24:19 -0400
defaults for between window
Diffstat:
2 files changed, 55 insertions(+), 41 deletions(-)
diff --git a/internal/inputs/env.go b/internal/inputs/env.go
@@ -54,20 +54,68 @@ const (
// WindowsLinuxPlatform for WSL subsystems
WindowsLinuxPlatform = "wsl"
defaultMaxClipboard = 45
- // TOTPDefaultBetween handles colorized time bounds for TOTP display
- TOTPDefaultBetween = "0:5,30:35"
+ colorWindowDelimiter = ","
+ colorWindowSpan = ":"
)
var (
isYesNoArgs = []string{isYes, isNo}
+ // TOTPDefaultColorWindow is the default coloring rules for totp
+ TOTPDefaultColorWindow = []ColorWindow{{Start: 0, End: 5}, {Start: 30, End: 35}}
+ // TOTPDefaultBetween is the default color window as a string
+ TOTPDefaultBetween = toString(TOTPDefaultColorWindow)
)
type (
environmentOutput struct {
showValues bool
}
+ // ColorWindow for handling terminal colors based on timing
+ ColorWindow struct {
+ Start int
+ End int
+ }
)
+func toString(windows []ColorWindow) string {
+ var results []string
+ for _, w := range windows {
+ results = append(results, fmt.Sprintf("%d%s%d", w.Start, colorWindowSpan, w.End))
+ }
+ return strings.Join(results, colorWindowDelimiter)
+}
+
+// ParseColorWindow will handle parsing a window of colors for TOTP operations
+func ParseColorWindow(windowString string) ([]ColorWindow, error) {
+ var rules []ColorWindow
+ for _, item := range strings.Split(windowString, colorWindowDelimiter) {
+ line := strings.TrimSpace(item)
+ if line == "" {
+ continue
+ }
+ parts := strings.Split(line, colorWindowSpan)
+ if len(parts) != 2 {
+ return nil, fmt.Errorf("invalid colorization rule found: %s", line)
+ }
+ s, err := strconv.Atoi(parts[0])
+ if err != nil {
+ return nil, err
+ }
+ e, err := strconv.Atoi(parts[1])
+ if err != nil {
+ return nil, err
+ }
+ if s < 0 || e < 0 || e < s || s > 59 || e > 59 {
+ return nil, fmt.Errorf("invalid time found for colorization rule: %s", line)
+ }
+ rules = append(rules, ColorWindow{Start: s, End: e})
+ }
+ if len(rules) == 0 {
+ return nil, errors.New("invalid colorization rules for totp, none found")
+ }
+ return rules, nil
+}
+
// EnvOrDefault will get the environment value OR default if env is not set.
func EnvOrDefault(envKey, defaultValue string) string {
val := os.Getenv(envKey)
diff --git a/internal/totp/core.go b/internal/totp/core.go
@@ -6,7 +6,6 @@ import (
"fmt"
"os"
"os/exec"
- "strconv"
"strings"
"time"
@@ -30,10 +29,6 @@ const (
)
type (
- colorWhen struct {
- start int
- end int
- }
arguments struct {
Clip bool
Once bool
@@ -54,41 +49,12 @@ func clear() {
}
}
-func colorWhenRules() ([]colorWhen, error) {
+func colorWhenRules() ([]inputs.ColorWindow, error) {
envTime := inputs.EnvOrDefault(inputs.ColorBetweenEnv, inputs.TOTPDefaultBetween)
- if envTime == "" || envTime == inputs.TOTPDefaultBetween {
- return []colorWhen{
- {start: 0, end: 5},
- {start: 30, end: 35},
- }, nil
- }
- var rules []colorWhen
- for _, item := range strings.Split(envTime, ",") {
- line := strings.TrimSpace(item)
- if line == "" {
- continue
- }
- parts := strings.Split(line, ":")
- if len(parts) != 2 {
- return nil, fmt.Errorf("invalid colorization rule found: %s", line)
- }
- s, err := strconv.Atoi(parts[0])
- if err != nil {
- return nil, err
- }
- e, err := strconv.Atoi(parts[1])
- if err != nil {
- return nil, err
- }
- if s < 0 || e < 0 || e < s || s > 59 || e > 59 {
- return nil, fmt.Errorf("invalid time found for colorization rule: %s", line)
- }
- rules = append(rules, colorWhen{start: s, end: e})
- }
- if len(rules) == 0 {
- return nil, errors.New("invalid colorization rules for totp, none found")
+ if envTime == inputs.TOTPDefaultBetween {
+ return inputs.TOTPDefaultColorWindow, nil
}
- return rules, nil
+ return inputs.ParseColorWindow(envTime)
}
func (w totpWrapper) generateCode() (string, error) {
@@ -183,7 +149,7 @@ func display(token string, args arguments) error {
startColor := ""
endColor := ""
for _, when := range colorRules {
- if left < when.end && left >= when.start {
+ if left < when.End && left >= when.Start {
startColor = coloring.Start
endColor = coloring.End
}