aicra/internal/clifmt/symbols.go

58 lines
1.2 KiB
Go
Raw Normal View History

package clifmt
import (
"fmt"
2018-07-07 12:27:48 +00:00
"strings"
)
2018-09-13 08:21:35 +00:00
var titleIndex = 0
var alignOffset = 30
2018-07-07 10:56:03 +00:00
2018-07-08 23:34:21 +00:00
// 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])
}
2018-07-08 23:34:21 +00:00
// 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])
}
2018-07-07 10:56:03 +00:00
2018-07-08 23:34:21 +00:00
// Title prints a formatted title (auto-indexed from local counted)
2018-07-07 10:56:03 +00:00
func Title(s string) {
2018-09-13 08:21:35 +00:00
titleIndex++
fmt.Printf("\n%s |%d| %s %s\n", Color(33, ">>", false), titleIndex, s, Color(33, "<<", false))
2018-07-07 10:56:03 +00:00
}
2018-07-08 23:34:21 +00:00
// Align prints strings with space padding to align line ends (fixed width)
2018-07-07 10:56:03 +00:00
func Align(s string) {
2018-07-07 12:27:48 +00:00
// 1. print string
2018-07-07 10:56:03 +00:00
fmt.Printf("%s", s)
2018-07-07 12:27:48 +00:00
// 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
2018-09-13 08:21:35 +00:00
for i := size; i < alignOffset; i++ {
2018-07-07 10:56:03 +00:00
fmt.Printf(" ")
}
}