Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Dec 19, 2023
1 parent 6bca89c commit 5c30378
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 42 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 36 additions & 28 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
Expand All @@ -436,19 +435,28 @@ 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
}

// 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 <https://www.apache.org/licenses/LICENSE-2.0>",
BugTracker: "https://github.com/essentialkaos/uc",
UpdateChecker: usage.UpdateChecker{"essentialkaos/uc", update.GitHubChecker},
Expand Down
49 changes: 36 additions & 13 deletions cli/support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -83,37 +88,38 @@ 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)
}

systemInfo, err := system.GetSystemInfo()

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)
printInfo(12, "Kernel", systemInfo.Kernel)

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()
Expand All @@ -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 {
Expand All @@ -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 == "" {
Expand Down
6 changes: 5 additions & 1 deletion common/uc.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,6 +65,10 @@ rm -rf %{buildroot}
################################################################################

%changelog
* Tue Dec 19 2023 Anton Novojilov <[email protected]> - 2.0.1-0
- Dependencies update
- Code refactoring

* Wed May 03 2023 Anton Novojilov <[email protected]> - 2.0.0-0
- Better standard input handling

Expand Down

0 comments on commit 5c30378

Please sign in to comment.