Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Dec 22, 2023
1 parent de29704 commit 13fb4eb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 45 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
################################################################################

# This Makefile generated by GoMakeGen 2.2.0 using next command:
# This Makefile generated by GoMakeGen 2.3.0 using next command:
# gomakegen --mod .
#
# More info: https://kaos.sh/gomakegen
Expand All @@ -13,6 +13,7 @@ ifdef VERBOSE ## Print verbose information (Flag)
VERBOSE_FLAG = -v
endif

COMPAT ?= 1.18
MAKEDIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
GITREV ?= $(shell test -s $(MAKEDIR)/.git && git rev-parse --short HEAD)

Expand Down Expand Up @@ -61,7 +62,7 @@ else
endif

ifdef COMPAT ## Compatible Go version (String)
go mod tidy $(VERBOSE_FLAG) -compat=$(COMPAT)
go mod tidy $(VERBOSE_FLAG) -compat=$(COMPAT) -go=$(COMPAT)
else
go mod tidy $(VERBOSE_FLAG)
endif
Expand Down Expand Up @@ -105,6 +106,6 @@ help: ## Show this info
| sed 's/ifdef //' \
| awk 'BEGIN {FS = " .*?## "}; {printf " \033[32m%-14s\033[0m %s\n", $$1, $$2}'
@echo -e ''
@echo -e '\033[90mGenerated by GoMakeGen 2.2.0\033[0m\n'
@echo -e '\033[90mGenerated by GoMakeGen 2.3.0\033[0m\n'

################################################################################
2 changes: 1 addition & 1 deletion action/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Template(action *recipe.Action) error {
tmplData, err := os.ReadFile(source)

if err != nil {
fmt.Errorf("Can't read template %q: %v", source, err)
return fmt.Errorf("Can't read template %q: %v", source, err)
}

tmpl, err := template.New("").Parse(string(tmplData))
Expand Down
48 changes: 10 additions & 38 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/essentialkaos/ek/v12/options"
"github.com/essentialkaos/ek/v12/req"
"github.com/essentialkaos/ek/v12/strutil"
"github.com/essentialkaos/ek/v12/terminal/tty"
"github.com/essentialkaos/ek/v12/usage"
"github.com/essentialkaos/ek/v12/usage/completion/bash"
"github.com/essentialkaos/ek/v12/usage/completion/fish"
Expand All @@ -40,7 +41,7 @@ import (
// Application info
const (
APP = "bibop"
VER = "8.0.1"
VER = "8.0.2"
DESC = "Utility for testing command-line tools"
)

Expand Down Expand Up @@ -101,8 +102,7 @@ var optMap = options.Map{
OPT_GENERATE_MAN: {Type: options.BOOL},
}

var colorTagApp string
var colorTagVer string
var colorTagApp, colorTagVer string
var rawOutput bool

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -144,23 +144,7 @@ func Run(gitRev string, gomod []byte) {

// preConfigureUI preconfigures UI based on information about user terminal
func preConfigureUI() {
term := os.Getenv("TERM")

fmtc.DisableColors = true

if term != "" {
switch {
case strings.Contains(term, "xterm"),
strings.Contains(term, "color"),
term == "screen":
fmtc.DisableColors = false
}
}

// Check for output redirect using pipes
if fsutil.IsCharacterDevice("/dev/stdin") &&
!fsutil.IsCharacterDevice("/dev/stdout") &&
os.Getenv("FAKETTY") == "" {
if !tty.IsTTY() {
fmtc.DisableColors = true
rawOutput = true
}
Expand All @@ -171,17 +155,13 @@ func preConfigureUI() {
fmtc.DisableColors = false
}

if os.Getenv("NO_COLOR") != "" {
fmtc.DisableColors = true
}

switch {
case fmtc.IsTrueColorSupported():
colorTagApp, colorTagVer = "{#66CC99}", "{#66CC99}"
colorTagApp, colorTagVer = "{*}{#66CC99}", "{#66CC99}"
case fmtc.Is256ColorsSupported():
colorTagApp, colorTagVer = "{#85}", "{#85}"
colorTagApp, colorTagVer = "{*}{#85}", "{#85}"
default:
colorTagApp, colorTagVer = "{c}", "{c}"
colorTagApp, colorTagVer = "{*}{c}", "{c}"
}

fmtutil.SeparatorTitleColorTag = colorTagApp
Expand Down Expand Up @@ -407,15 +387,6 @@ func printError(f string, a ...interface{}) {
}
}

// printError prints warning message to console
func printWarn(f string, a ...interface{}) {
if len(a) == 0 {
fmtc.Fprintln(os.Stderr, "{y}"+f+"{!}")
} else {
fmtc.Fprintf(os.Stderr, "{y}"+f+"{!}\n", a...)
}
}

// printErrorAndExit print error message and exit with exit code 1
func printErrorAndExit(f string, a ...interface{}) {
printError(f, a...)
Expand Down Expand Up @@ -451,7 +422,7 @@ func printMan() {
func genUsage() *usage.Info {
info := usage.NewInfo("", "recipe")

info.AppNameColorTag = "{*}" + colorTagApp
info.AppNameColorTag = colorTagApp

info.AddOption(OPT_DRY_RUN, "Parse and validate recipe")
info.AddOption(OPT_EXTRA, "Number of output lines for failed action {s-}(default: 10){!}", "lines")
Expand Down Expand Up @@ -520,8 +491,9 @@ func genAbout(gitRev string) *usage.About {
Year: 2006,
Owner: "ESSENTIAL KAOS",

AppNameColorTag: "{*}" + colorTagApp,
AppNameColorTag: colorTagApp,
VersionColorTag: colorTagVer,
DescSeparator: "{s}—{!}",

License: "Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>",
BugTracker: "https://github.com/essentialkaos/bibop/issues",
Expand Down
6 changes: 3 additions & 3 deletions render/renderer_terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/essentialkaos/ek/v12/fmtc"
"github.com/essentialkaos/ek/v12/fmtutil"
"github.com/essentialkaos/ek/v12/strutil"
"github.com/essentialkaos/ek/v12/terminal/window"
"github.com/essentialkaos/ek/v12/terminal/tty"

"github.com/essentialkaos/bibop/recipe"
)
Expand Down Expand Up @@ -252,7 +252,7 @@ func (rr *TerminalRenderer) renderTmpMessage(f string, a ...interface{}) {
return
}

ww := window.GetWidth()
ww := tty.GetWidth()

if ww <= 0 {
fmtc.TPrintf(f, a...)
Expand Down Expand Up @@ -313,7 +313,7 @@ func (rr *TerminalRenderer) renderCurrentActionProgress() {

// renderMessage prints message limited by window size
func (rr *TerminalRenderer) renderMessage(f string, a ...interface{}) {
ww := window.GetWidth()
ww := tty.GetWidth()

if ww <= 0 {
fmtc.Printf(f, a...)
Expand Down

0 comments on commit 13fb4eb

Please sign in to comment.