diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fd17678..63237135 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ ## Changelog +### 12.80.0 + +* `[system]` Added ANSI color info to `OSInfo` +* `[system]` Added methods `OSInfo.ColoredPrettyName` and `OSInfo.ColoredName` +* `[strutil]` Improved usage examples + ### 12.79.0 * `[panel]` Added indent customization diff --git a/ek.go b/ek.go index 6f94e1e2..71e3c8e3 100644 --- a/ek.go +++ b/ek.go @@ -20,7 +20,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "12.79.0" +const VERSION = "12.80.0" // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/go.mod b/go.mod index e257c18a..b3fd5d9b 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.18 require ( github.com/essentialkaos/check v1.4.0 github.com/essentialkaos/go-linenoise/v3 v3.4.0 - golang.org/x/crypto v0.13.0 - golang.org/x/sys v0.12.0 + golang.org/x/crypto v0.14.0 + golang.org/x/sys v0.13.0 ) require ( diff --git a/go.sum b/go.sum index 0025c076..e3fa1fc3 100644 --- a/go.sum +++ b/go.sum @@ -11,7 +11,7 @@ github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsK github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= -golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck= -golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/strutil/example_test.go b/strutil/example_test.go index 7a5e3777..3f0464cd 100644 --- a/strutil/example_test.go +++ b/strutil/example_test.go @@ -80,9 +80,11 @@ func ExampleExclude() { func ExampleLen() { fmt.Println(Len("Пример 例子 例 მაგალითად")) + fmt.Println(Len("😚😘🥰")) // Output: // 21 + // 3 } func ExampleHead() { diff --git a/system/info.go b/system/info.go index c3d4ea6c..69cd648a 100644 --- a/system/info.go +++ b/system/info.go @@ -12,6 +12,7 @@ import ( "bufio" "os" "strconv" + "strings" ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -149,6 +150,7 @@ type OSInfo struct { BugReportURL string `json:"bugreport_url"` DocumentationURL string `json:"documentation_url"` Logo string `json:"logo"` + ANSIColor string `json:"ansi_color"` SupportURL string `json:"support_url"` SupportProduct string `json:"support_product"` SupportProductVersion string `json:"support_product_version"` @@ -156,6 +158,26 @@ type OSInfo struct { // ////////////////////////////////////////////////////////////////////////////////// // +// ColoredPrettyName returns pretty name with applied color +func (i *OSInfo) ColoredPrettyName() string { + if !isValidANSIColor(i.ANSIColor) { + return i.PrettyName + } + + return "\033[" + i.ANSIColor + "m" + i.PrettyName + "\033[0m" +} + +// ColoredName returns name with applied color +func (i *OSInfo) ColoredName() string { + if !isValidANSIColor(i.ANSIColor) { + return i.Name + } + + return "\033[" + i.ANSIColor + "m" + i.Name + "\033[0m" +} + +// ////////////////////////////////////////////////////////////////////////////////// // + // getFileScanner opens file and creates scanner for reading text files line by line func getFileScanner(file string) (*bufio.Scanner, func() error, error) { fd, err := os.OpenFile(file, os.O_RDONLY, 0) @@ -180,3 +202,8 @@ func parseSize(v string) (uint64, error) { return size * 1024, nil } + +// isValidANSIColor validates ansi color code +func isValidANSIColor(color string) bool { + return color != "" && strings.Trim(color, "0123456789;") == "" +} diff --git a/system/info_linux.go b/system/info_linux.go index b65e58de..c64ecd4c 100644 --- a/system/info_linux.go +++ b/system/info_linux.go @@ -113,6 +113,8 @@ func applyOSInfo(info *OSInfo, name, value string) { info.DocumentationURL = value case "LOGO": info.Logo = value + case "ANSI_COLOR": + info.ANSIColor = value } switch { diff --git a/system/info_linux_test.go b/system/info_linux_test.go index 8040a082..8d381274 100644 --- a/system/info_linux_test.go +++ b/system/info_linux_test.go @@ -637,6 +637,7 @@ HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" VERSION_CODENAME=groovy +ANSI_COLOR="0;34" VARIANT="Server" VARIANT_ID="server" PLATFORM_ID="platform:el8" @@ -668,6 +669,13 @@ REDHAT_SUPPORT_PRODUCT_VERSION="7" c.Assert(osInfo.Logo, Equals, "fedora-logo-icon") c.Assert(osInfo.SupportProduct, Equals, "centos") c.Assert(osInfo.SupportProductVersion, Equals, "7") + c.Assert(osInfo.ANSIColor, Equals, "0;34") + + c.Assert(osInfo.ColoredPrettyName(), Equals, "\x1b[0;34mUbuntu 20.10\x1b[0m") + c.Assert(osInfo.ColoredName(), Equals, "\x1b[0;34mUbuntu\x1b[0m") + osInfo.ANSIColor = "ABCD" + c.Assert(osInfo.ColoredPrettyName(), Equals, "Ubuntu 20.10") + c.Assert(osInfo.ColoredName(), Equals, "Ubuntu") c.Assert(getArchName("i386"), Equals, "386") c.Assert(getArchName("i586"), Equals, "586")