From 34cdb761e4ad5d57c1a6e051a22e8cbbc7d8937c Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 8 Nov 2018 14:09:19 +0100 Subject: [PATCH] add clifmt from 'git.xdrm.io/go/aicra' --- internal/clifmt/colors.go | 16 +++++++++++ internal/clifmt/symbols.go | 57 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 internal/clifmt/colors.go create mode 100644 internal/clifmt/symbols.go diff --git a/internal/clifmt/colors.go b/internal/clifmt/colors.go new file mode 100644 index 0000000..eeb9d50 --- /dev/null +++ b/internal/clifmt/colors.go @@ -0,0 +1,16 @@ +package clifmt + +import ( + "fmt" +) + +// Color returns a bash-formatted string representing +// the string @text with the color code @color and in bold +// if @bold (1 optional argument) is set to true +func Color(color byte, text string, bold ...bool) string { + b := "0" + if len(bold) > 0 && bold[0] { + b = "1" + } + return fmt.Sprintf("\033[%s;%dm%s\033[0m", b, color, text) +} diff --git a/internal/clifmt/symbols.go b/internal/clifmt/symbols.go new file mode 100644 index 0000000..d9e1619 --- /dev/null +++ b/internal/clifmt/symbols.go @@ -0,0 +1,57 @@ +package clifmt + +import ( + "fmt" + "strings" +) + +var titleIndex = 0 +var alignOffset = 30 + +// Warn returns a red warning ASCII sign. If a string is given +// as argument, it will print it after the warning sign +func Warn(s ...string) string { + if len(s) == 0 { + return Color(31, "/!\\") + } + + return fmt.Sprintf("%s %s", Warn(), s[0]) +} + +// Info returns a blue info ASCII sign. If a string is given +// as argument, it will print it after the info sign +func Info(s ...string) string { + if len(s) == 0 { + return Color(34, "(!)") + } + + return fmt.Sprintf("%s %s", Info(), s[0]) +} + +// Title prints a formatted title (auto-indexed from local counted) +func Title(s string) { + titleIndex++ + fmt.Printf("\n%s |%d| %s %s\n", Color(33, ">>", false), titleIndex, s, Color(33, "<<", false)) + +} + +// Align prints strings with space padding to align line ends (fixed width) +func Align(s string) { + + // 1. print string + fmt.Printf("%s", s) + + // 2. get actual size + size := len(s) + + // 3. remove \033[XYm format characters + size -= (len(strings.Split(s, "\033")) - 0) * 6 + + // 3. add 1 char for each \033[0m + size += len(strings.Split(s, "\033[0m")) - 1 + + // 4. print trailing spaces + for i := size; i < alignOffset; i++ { + fmt.Printf(" ") + } +}