From 5c30378a196710bd0f655d86e0598ca5cb325611 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 19 Dec 2023 14:46:24 +0300 Subject: [PATCH] Code refactoring --- README.md | 3 ++ cli/cli.go | 64 ++++++++++++++++++++++++------------------ cli/support/support.go | 49 +++++++++++++++++++++++--------- common/uc.spec | 6 +++- 4 files changed, 80 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index f568ac6..f4b10da 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,9 @@ Examples cat file.txt | uc Count unique lines in stdin data + + uc -m 100 < file.txt + Count unique lines in stdin data with 100 uniq lines max ``` ### Build Status diff --git a/cli/cli.go b/cli/cli.go index c768c60..f0319c3 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -25,6 +25,7 @@ import ( "github.com/essentialkaos/ek/v12/options" "github.com/essentialkaos/ek/v12/signal" "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" @@ -40,7 +41,7 @@ import ( // Application basic info const ( APP = "uc" - VER = "2.0.0" + VER = "2.0.1" DESC = "Tool for counting unique lines" ) @@ -117,6 +118,12 @@ var stats *Stats // rawMode is raw mode flag var rawMode bool +// colorTagApp contains color tag for app name +var colorTagApp string + +// colorTagVer contains color tag for app version +var colorTagVer string + // ////////////////////////////////////////////////////////////////////////////////// // // Run is main application function @@ -162,27 +169,10 @@ 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 - } - } - - if !fsutil.IsCharacterDevice("/dev/stdout") && os.Getenv("FAKETTY") == "" { + if !tty.IsTTY() { fmtc.DisableColors = true rawMode = true } - - if os.Getenv("NO_COLOR") != "" { - fmtc.DisableColors = true - } } // configureUI configures user interface @@ -194,6 +184,13 @@ func configureUI() { if options.GetB(OPT_NO_PROGRESS) { rawMode = true } + + switch { + case fmtc.Is256ColorsSupported(): + colorTagApp, colorTagVer = "{*}{#168}", "{#168}" + default: + colorTagApp, colorTagVer = "{*}{m}", "{m}" + } } // processData starts data processing @@ -398,11 +395,11 @@ func printError(f string, a ...interface{}) { func printCompletion() int { switch options.GetS(OPT_COMPLETION) { case "bash": - fmt.Printf(bash.Generate(genUsage(), APP)) + fmt.Print(bash.Generate(genUsage(), APP)) case "fish": - fmt.Printf(fish.Generate(genUsage(), APP)) + fmt.Print(fish.Generate(genUsage(), APP)) case "zsh": - fmt.Printf(zsh.Generate(genUsage(), optMap, APP)) + fmt.Print(zsh.Generate(genUsage(), optMap, APP)) default: return 1 } @@ -424,6 +421,8 @@ func printMan() { func genUsage() *usage.Info { info := usage.NewInfo(APP, "file") + info.AppNameColorTag = colorTagApp + info.AddOption(OPT_DISTRIBUTION, "Show number of occurrences for every line") info.AddOption(OPT_MAX_LINES, "Max number of unique lines", "num") info.AddOption(OPT_NO_PROGRESS, "Disable progress output") @@ -436,7 +435,11 @@ func genUsage() *usage.Info { info.AddExample("-d file.txt", "Show distribution for file.txt") info.AddExample("-d -m 5k file.txt", "Show distribution for file.txt with 5,000 uniq lines max") info.AddRawExample("cat file.txt | "+APP, "Count unique lines in stdin data") - info.AddRawExample(APP+" -m 100 < file.txt", "Count unique lines in stdin data with 100 uniq lines max") + + info.AddRawExample( + APP+" -m 100 < file.txt", + "Count unique lines in stdin data with 100 uniq lines max", + ) return info } @@ -444,11 +447,16 @@ func genUsage() *usage.Info { // genAbout generates info about version func genAbout(gitRev string) *usage.About { about := &usage.About{ - App: APP, - Version: VER, - Desc: DESC, - Year: 2009, - Owner: "ESSENTIAL KAOS", + App: APP, + Version: VER, + Desc: DESC, + Year: 2009, + Owner: "ESSENTIAL KAOS", + + AppNameColorTag: colorTagApp, + VersionColorTag: colorTagVer, + DescSeparator: "{s}—{!}", + License: "Apache License, Version 2.0 ", BugTracker: "https://github.com/essentialkaos/uc", UpdateChecker: usage.UpdateChecker{"essentialkaos/uc", update.GitHubChecker}, diff --git a/cli/support/support.go b/cli/support/support.go index c5d80fd..74070a5 100644 --- a/cli/support/support.go +++ b/cli/support/support.go @@ -11,14 +11,15 @@ import ( "fmt" "os" "runtime" + "runtime/debug" "strings" "github.com/essentialkaos/ek/v12/fmtc" "github.com/essentialkaos/ek/v12/fmtutil" - "github.com/essentialkaos/ek/v12/fsutil" "github.com/essentialkaos/ek/v12/hash" "github.com/essentialkaos/ek/v12/strutil" "github.com/essentialkaos/ek/v12/system" + "github.com/essentialkaos/ek/v12/system/container" "github.com/essentialkaos/depsy" ) @@ -55,6 +56,10 @@ func showApplicationInfo(app, ver, gitRev string) { runtime.GOOS, runtime.GOARCH, )) + if gitRev == "" { + gitRev = extractGitRevFromBuildInfo() + } + if gitRev != "" { if !fmtc.DisableColors && fmtc.IsTrueColorSupported() { printInfo(7, "Git SHA", gitRev+getHashColorBullet(gitRev)) @@ -83,13 +88,14 @@ func showOSInfo() { if err == nil { fmtutil.Separator(false, "OS INFO") - printInfo(12, "Name", osInfo.Name) - printInfo(12, "Pretty Name", osInfo.PrettyName) - printInfo(12, "Version", osInfo.VersionID) + printInfo(12, "Name", osInfo.ColoredName()) + printInfo(12, "Pretty Name", osInfo.ColoredPrettyName()) + printInfo(12, "Version", osInfo.Version) printInfo(12, "ID", osInfo.ID) printInfo(12, "ID Like", osInfo.IDLike) printInfo(12, "Version ID", osInfo.VersionID) printInfo(12, "Version Code", osInfo.VersionCodename) + printInfo(12, "Platform ID", osInfo.PlatformID) printInfo(12, "CPE", osInfo.CPEName) } @@ -97,11 +103,9 @@ func showOSInfo() { if err != nil { return - } else { - if osInfo == nil { - fmtutil.Separator(false, "SYSTEM INFO") - printInfo(12, "Name", systemInfo.OS) - } + } else if osInfo == nil { + fmtutil.Separator(false, "SYSTEM INFO") + printInfo(12, "Name", systemInfo.OS) } printInfo(12, "Arch", systemInfo.Arch) @@ -109,11 +113,13 @@ func showOSInfo() { containerEngine := "No" - switch { - case fsutil.IsExist("/.dockerenv"): + switch container.GetEngine() { + case container.DOCKER: containerEngine = "Yes (Docker)" - case fsutil.IsExist("/run/.containerenv"): + case container.PODMAN: containerEngine = "Yes (Podman)" + case container.LXC: + containerEngine = "Yes (LXC)" } fmtc.NewLine() @@ -140,6 +146,23 @@ func showDepsInfo(gomod []byte) { } } +// extractGitRevFromBuildInfo extracts git SHA from embedded build info +func extractGitRevFromBuildInfo() string { + info, ok := debug.ReadBuildInfo() + + if !ok { + return "" + } + + for _, s := range info.Settings { + if s.Key == "vcs.revision" && len(s.Value) > 7 { + return s.Value[:7] + } + } + + return "" +} + // getHashColorBullet return bullet with color from hash func getHashColorBullet(v string) string { if len(v) > 6 { @@ -151,7 +174,7 @@ func getHashColorBullet(v string) string { // printInfo formats and prints info record func printInfo(size int, name, value string) { - name = name + ":" + name += ":" size++ if value == "" { diff --git a/common/uc.spec b/common/uc.spec index 88209b3..8d2ec89 100644 --- a/common/uc.spec +++ b/common/uc.spec @@ -6,7 +6,7 @@ Summary: Simple utility for counting unique lines Name: uc -Version: 2.0.0 +Version: 2.0.1 Release: 0%{?dist} Group: Applications/System License: Apache License, Version 2.0 @@ -65,6 +65,10 @@ rm -rf %{buildroot} ################################################################################ %changelog +* Tue Dec 19 2023 Anton Novojilov - 2.0.1-0 +- Dependencies update +- Code refactoring + * Wed May 03 2023 Anton Novojilov - 2.0.0-0 - Better standard input handling