From ea09330c5d72157d31774b0ac9ebcda47715fbb4 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 26 Apr 2023 13:29:50 +0300 Subject: [PATCH 1/3] Improve README --- README.md | 2 +- fmtc/README.md | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6ae68eaf..25a7aeba 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Currently we support Linux and macOS (_except some packages_). All packages have ### Installation -Make sure you have a working Go 1.18+ workspace (_[instructions](https://golang.org/doc/install)_), then: +Make sure you have a working Go 1.18+ workspace (_[instructions](https://go.dev/doc/install)_), then: ``` go get github.com/essentialkaos/ek/v12 diff --git a/fmtc/README.md b/fmtc/README.md index b4e1508d..e28770c7 100644 --- a/fmtc/README.md +++ b/fmtc/README.md @@ -2,14 +2,14 @@ #### Modificators -| Name | Tag | Code | -|-----------|-------|------| -| Reset | `{!}` | `0` | -| Bold | `{*}` | `1` | -| Dim | `{^}` | `2` | -| Underline | `{_}` | `4` | -| Blink | `{~}` | `5` | -| Reverse | `{@}` | `7` | +| Name | Tag | Reset Tag | Code | +|-----------|-------|-----------|------| +| Reset | `{!}` | — | `0` | +| Bold | `{*}` | `{!*}` | `1` | +| Dim | `{^}` | `{!^}` | `2` | +| Underline | `{_}` | `{!_}` | `4` | +| Blink | `{~}` | `{!~}` | `5` | +| Reverse | `{@}` | `{!@}` | `7` | #### 8/16 Colors From c0ed500aef59e04985aa3a6c5acfae136d5da870 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Thu, 27 Apr 2023 15:39:28 +0300 Subject: [PATCH 2/3] [fmtutil] Add method Align for aligning text with ANSI control sequences --- CHANGELOG.md | 4 ++++ ek.go | 2 +- fmtutil/fmtutil.go | 34 ++++++++++++++++++++++++++++++++++ fmtutil/fmtutil_test.go | 16 ++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e5d1c1f..ba5f9362 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ### 12.65.0 +* `[fmtutil]` Added method `Align` for aligning text with ANSI control sequences (_for example colors_) + +### 12.64.1 + * `[processes]` `ProcessInfo.Childs` renamed to `ProcessInfo.Children` * Fixed typos diff --git a/ek.go b/ek.go index d20b4a11..6ede7ad6 100644 --- a/ek.go +++ b/ek.go @@ -20,7 +20,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "12.64.1" +const VERSION = "12.65.0" // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/fmtutil/fmtutil.go b/fmtutil/fmtutil.go index 31b7ee73..a0fba690 100644 --- a/fmtutil/fmtutil.go +++ b/fmtutil/fmtutil.go @@ -22,6 +22,16 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // +type Alignment uint8 + +const ( + LEFT Alignment = 0 + CENTER Alignment = 1 + RIGHT Alignment = 2 +) + +// ////////////////////////////////////////////////////////////////////////////////// // + const ( _KILO = 1024 _MEGA = 1048576 @@ -50,6 +60,10 @@ var SizeSeparator = "" // ////////////////////////////////////////////////////////////////////////////////// // +var spaces = strings.Repeat(" ", 256) + +// ////////////////////////////////////////////////////////////////////////////////// // + // PrettyNum formats number to "pretty" form (e.g 1234567 -> 1,234,567) func PrettyNum(i any, separator ...string) string { var str string @@ -199,6 +213,26 @@ func Float(f float64) float64 { return mathutil.Round(f, 1) } +// Align can align text with ANSI control sequences (for example colors) +func Align(text string, alignment Alignment, size int) string { + len := strutil.Len(ansi.RemoveCodes(text)) + + if len >= size { + return text + } + + switch alignment { + case RIGHT: + return spaces[:size-len] + text + case CENTER: + pad := (size - len) / 2 + return spaces[:pad] + + text + spaces[:size-(len+pad)] + default: + return text + spaces[:size-len] + } +} + // Wrap wraps text using max line length func Wrap(text, indent string, maxLineLength int) string { var word bytes.Buffer diff --git a/fmtutil/fmtutil_test.go b/fmtutil/fmtutil_test.go index c5e1e072..2d101dcb 100644 --- a/fmtutil/fmtutil_test.go +++ b/fmtutil/fmtutil_test.go @@ -117,6 +117,15 @@ func (s *FmtUtilSuite) TestFloat(c *C) { c.Assert(Float(math.NaN()), Equals, 0.0) } +func (s *FmtUtilSuite) TestAlign(c *C) { + text := "\033[1;33;4;44mTeSt TeXt 12345\033[0m" + + c.Assert(Align(text, LEFT, 20), Equals, "\033[1;33;4;44mTeSt TeXt 12345\033[0m ") + c.Assert(Align(text, CENTER, 20), Equals, " \033[1;33;4;44mTeSt TeXt 12345\033[0m ") + c.Assert(Align(text, RIGHT, 20), Equals, " \033[1;33;4;44mTeSt TeXt 12345\033[0m") + c.Assert(Align(text, RIGHT, 2), Equals, text) +} + func (s *FmtUtilSuite) TestWrap(c *C) { input := `Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam @@ -236,3 +245,10 @@ func (s *FmtUtilSuite) BenchmarkWrap(c *C) { Wrap("0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", " ", 10) } } + +func (s *FmtUtilSuite) BenchmarkAlign(c *C) { + text := "\033[1;33;4;44mYellow Underlined Text on Blue Background\033[0m" + for i := 0; i < c.N; i++ { + Align(text, CENTER, 63) + } +} From 435c92c1bfd53bc4c5f93a028f692de54c04da5e Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Thu, 27 Apr 2023 16:24:14 +0300 Subject: [PATCH 3/3] [usage] Added feature for adding and rendering environment info --- CHANGELOG.md | 1 + usage/example_test.go | 6 ++++++ usage/usage.go | 27 +++++++++++++++++++++++++-- usage/usage_test.go | 17 +++++++++-------- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba5f9362..75911eff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### 12.65.0 * `[fmtutil]` Added method `Align` for aligning text with ANSI control sequences (_for example colors_) +* `[usage]` Added feature for adding and rendering environment info ### 12.64.1 diff --git a/usage/example_test.go b/usage/example_test.go index 17051125..264fc152 100644 --- a/usage/example_test.go +++ b/usage/example_test.go @@ -24,6 +24,12 @@ func ExampleAbout_Render() { License: "MIT", Owner: "John Dow ", + Environment: Environment{ + {"Client", "1.8.1"}, + {"Server", "4.x"}, + {"Encoder", "h265/AV1"}, + }, + AppNameColorTag: "{r*}", // Use custom color for application name VersionColorTag: "{r}", // Use custom color for application version } diff --git a/usage/usage.go b/usage/usage.go index e91407e0..8b2cd94a 100644 --- a/usage/usage.go +++ b/usage/usage.go @@ -41,6 +41,13 @@ const ( // ////////////////////////////////////////////////////////////////////////////////// // +type Environment []EnvironmentInfo + +type EnvironmentInfo struct { + Name string + Version string +} + // About contains info about application type About struct { App string // App is application name @@ -56,6 +63,8 @@ type About struct { AppNameColorTag string // AppNameColorTag contains default app name color tag VersionColorTag string // VersionColorTag contains default app version color tag + Environment Environment // Environment contains info about environment + // Function for checking application updates UpdateChecker UpdateChecker } @@ -473,18 +482,32 @@ func (a *About) Print() { switch { case a.Build != "": fmtc.Printf( - "\n"+nc+"%s{!} "+vc+"%s{!}{s}%s{!} {s-}(%s){!} - %s\n\n", + "\n"+nc+"%s{!} "+vc+"%s{!}{s}%s{!} {s-}(%s){!} - %s\n", a.App, a.Version, a.Release, a.Build, a.Desc, ) default: fmtc.Printf( - "\n"+nc+"%s{!} "+vc+"%s{!}{s}%s{!} - %s\n\n", + "\n"+nc+"%s{!} "+vc+"%s{!}{s}%s{!} - %s\n", a.App, a.Version, a.Release, a.Desc, ) } + if len(a.Environment) > 0 { + fmtc.Printf("{s-}│{!}\n") + + for i, env := range a.Environment { + if len(a.Environment) != i+1 { + fmtc.Printf("{s-}├ %s: %s{!}\n", env.Name, env.Version) + } else { + fmtc.Printf("{s-}└ %s: %s{!}\n", env.Name, env.Version) + } + } + } + + fmtc.NewLine() + if a.Owner != "" { if a.Year == 0 { fmtc.Printf( diff --git a/usage/usage_test.go b/usage/usage_test.go index 2a200dfe..9ec544c1 100644 --- a/usage/usage_test.go +++ b/usage/usage_test.go @@ -28,14 +28,15 @@ var _ = Suite(&UsageSuite{}) func (s *UsageSuite) TestAbout(c *C) { about := &About{ - App: "Application", - Version: "1.0.0", - Release: ".A45", - Build: "37163", - Desc: "Test application", - Year: 2010, - Owner: "Some company", - License: "MIT", + App: "Application", + Version: "1.0.0", + Release: ".A45", + Build: "37163", + Desc: "Test application", + Year: 2010, + Owner: "Some company", + License: "MIT", + Environment: Environment{{"A", "1"}, {"B", "2"}}, } about.Render()