Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Apr 27, 2024
1 parent 640944d commit 0ae76ab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
44 changes: 23 additions & 21 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -162,7 +174,7 @@ func processInput() {

if err != nil {
fmtc.TPrintf("")
printError(err.Error())
terminal.Error(err)
os.Exit(1)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 0ae76ab

Please sign in to comment.