diff --git a/CHANGELOG.md b/CHANGELOG.md index cde9a647..63237135 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ ## Changelog -### 12.79.1 +### 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 diff --git a/ek.go b/ek.go index 5bc54683..71e3c8e3 100644 --- a/ek.go +++ b/ek.go @@ -20,7 +20,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "12.79.1" +const VERSION = "12.80.0" // ////////////////////////////////////////////////////////////////////////////////// // 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")