add clifmt from 'git.xdrm.io/go/aicra'

This commit is contained in:
xdrm-brackets 2018-11-08 14:09:19 +01:00
parent f763dd50b3
commit 34cdb761e4
2 changed files with 73 additions and 0 deletions

16
internal/clifmt/colors.go Normal file
View File

@ -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)
}

View File

@ -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(" ")
}
}