Skip to content

Commit

Permalink
Merge pull request #359 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 12.65.0
  • Loading branch information
andyone authored Apr 27, 2023
2 parents a215ee0 + 435c92c commit 0de3914
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

### 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

* `[processes]` `ProcessInfo.Childs` renamed to `ProcessInfo.Children`
* Fixed typos

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.64.1"
const VERSION = "12.65.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
16 changes: 8 additions & 8 deletions fmtc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 34 additions & 0 deletions fmtutil/fmtutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ import (

// ////////////////////////////////////////////////////////////////////////////////// //

type Alignment uint8

const (
LEFT Alignment = 0
CENTER Alignment = 1
RIGHT Alignment = 2
)

// ////////////////////////////////////////////////////////////////////////////////// //

const (
_KILO = 1024
_MEGA = 1048576
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions fmtutil/fmtutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
6 changes: 6 additions & 0 deletions usage/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func ExampleAbout_Render() {
License: "MIT",
Owner: "John Dow <john@domain.com>",

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
}
Expand Down
27 changes: 25 additions & 2 deletions usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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(
Expand Down
17 changes: 9 additions & 8 deletions usage/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0de3914

Please sign in to comment.