Skip to content

Commit

Permalink
[system] Added ANSI color info to 'OSInfo'
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 12, 2023
1 parent ab6195b commit 9066216
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
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.79.1"
const VERSION = "12.80.0"

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

Expand Down
27 changes: 27 additions & 0 deletions system/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"bufio"
"os"
"strconv"
"strings"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -149,13 +150,34 @@ 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"`
}

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

// 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)
Expand All @@ -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;") == ""
}
2 changes: 2 additions & 0 deletions system/info_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions system/info_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 9066216

Please sign in to comment.