From 0ae76abdea86c9408e8c70733590cf1c08b66eb7 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Sun, 28 Apr 2024 00:10:18 +0300 Subject: [PATCH] Code refactoring --- cli/cli.go | 44 +++++++++++++++++++++++--------------------- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 07a9be0..46d633b 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -23,6 +23,8 @@ import ( "github.com/essentialkaos/ek/v12/support" "github.com/essentialkaos/ek/v12/support/apps" "github.com/essentialkaos/ek/v12/support/deps" + "github.com/essentialkaos/ek/v12/terminal" + "github.com/essentialkaos/ek/v12/terminal/tty" "github.com/essentialkaos/ek/v12/timeutil" "github.com/essentialkaos/ek/v12/usage" "github.com/essentialkaos/ek/v12/usage/completion/bash" @@ -39,7 +41,7 @@ import ( // App info const ( APP = "fz" - VER = "1.1.2" + VER = "1.1.3" DESC = "Tool for formatting go-fuzz output" ) @@ -81,10 +83,13 @@ var colorTagApp, colorTagVer string func Run(gitRev string, gomod []byte) { runtime.GOMAXPROCS(3) + preConfigureUI() + _, errs := options.Parse(optMap) - if len(errs) != 0 { - printError(errs[0].Error()) + if !errs.IsEmpty() { + terminal.Error("Options parsing errors:") + terminal.Error(errs.String()) os.Exit(1) } @@ -116,19 +121,26 @@ func Run(gitRev string, gomod []byte) { processInput() } -// configureUI configures user interface -func configureUI() { - if options.GetB(OPT_NO_COLOR) { +// preConfigureUI preconfigures UI based on information about user terminal +func preConfigureUI() { + if !tty.IsTTY() { fmtc.DisableColors = true } switch { case fmtc.IsTrueColorSupported(): - colorTagApp, colorTagVer = "{*}{#00ADD8}", "{#5DC9E2}" + colorTagApp, colorTagVer = "{*}{&}{#00ADD8}", "{#5DC9E2}" case fmtc.Is256ColorsSupported(): - colorTagApp, colorTagVer = "{*}{#38}", "{#74}" + colorTagApp, colorTagVer = "{*}{&}{#38}", "{#74}" default: - colorTagApp, colorTagVer = "{*}{c}", "{c}" + colorTagApp, colorTagVer = "{*}{&}{c}", "{c}" + } +} + +// configureUI configures user interface +func configureUI() { + if options.GetB(OPT_NO_COLOR) { + fmtc.DisableColors = true } } @@ -162,7 +174,7 @@ func processInput() { if err != nil { fmtc.TPrintf("") - printError(err.Error()) + terminal.Error(err) os.Exit(1) } @@ -255,11 +267,6 @@ func isShutdownMessage(data string) bool { return strings.Contains(data, "shutting down...") } -// printError prints error message to console -func printError(f string, a ...any) { - fmtc.Fprintf(os.Stderr, "{r}"+f+"{!}\n", a...) -} - // ////////////////////////////////////////////////////////////////////////////////// // // checkForGoFuzz checks if go-fuzz binary present on the system @@ -291,12 +298,7 @@ func printCompletion() int { // printMan prints man page func printMan() { - fmt.Println( - man.Generate( - genUsage(), - genAbout(""), - ), - ) + fmt.Println(man.Generate(genUsage(), genAbout(""))) } // genUsage generates usage info diff --git a/go.mod b/go.mod index 4f62b25..f95847f 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/essentialkaos/check v1.4.0 - github.com/essentialkaos/ek/v12 v12.116.0 + github.com/essentialkaos/ek/v12 v12.118.0 ) require ( diff --git a/go.sum b/go.sum index 1bf6365..700a1ad 100644 --- a/go.sum +++ b/go.sum @@ -3,8 +3,8 @@ github.com/essentialkaos/check v1.4.0 h1:kWdFxu9odCxUqo1NNFNJmguGrDHgwi3A8daXX1n github.com/essentialkaos/check v1.4.0/go.mod h1:LMKPZ2H+9PXe7Y2gEoKyVAwUqXVgx7KtgibfsHJPus0= github.com/essentialkaos/depsy v1.1.0 h1:U6dp687UkQwXlZU17Hg2KMxbp3nfZAoZ8duaeUFYvJI= github.com/essentialkaos/depsy v1.1.0/go.mod h1:kpiTAV17dyByVnrbNaMcZt2jRwvuXClUYOzpyJQwtG8= -github.com/essentialkaos/ek/v12 v12.116.0 h1:lEe1pakqCFgbJMBQ+Ymrbd0GvzXdtg9/0nB6lgpuH6o= -github.com/essentialkaos/ek/v12 v12.116.0/go.mod h1:L3l8wkmXO9T72+hXc4nZosl7tj49XiU0gVrwiXyiYBg= +github.com/essentialkaos/ek/v12 v12.118.0 h1:fosk+jCK9GrrnPiua1O6PQSwrlQ25arwespB6fnbi9g= +github.com/essentialkaos/ek/v12 v12.118.0/go.mod h1:VVCk0ZA3CB5lPkQ7bUHo/mOBs02jkjMRlLS8ES2TQpw= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=