2018-11-08 13:09:19 +00:00
|
|
|
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)
|
2018-11-10 17:35:00 +00:00
|
|
|
// also crops the content to add '...' at the end if too long (must have a space
|
|
|
|
// at the end)
|
2018-11-08 13:09:19 +00:00
|
|
|
func Align(s string) {
|
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
// 1. replace tabs with 4 spaces
|
|
|
|
tabs := strings.Split(s, "\t")
|
|
|
|
s = strings.Join(tabs, " ")
|
2018-11-08 13:09:19 +00:00
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
// 1. get real size
|
|
|
|
size := getRealSize(s)
|
2018-11-08 13:09:19 +00:00
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
if size > alignOffset-6 {
|
2018-11-08 13:09:19 +00:00
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
for i, l := 0, len(s); i < l; i++ { // find when real size is right under
|
|
|
|
next := fmt.Sprintf("%s\033[0m… ", s[0:i+1])
|
|
|
|
if getRealSize(next) >= alignOffset-5 {
|
|
|
|
s = next
|
|
|
|
break
|
|
|
|
}
|
2018-11-08 13:09:19 +00:00
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
}
|
2018-11-11 14:12:31 +00:00
|
|
|
size = getRealSize(s)
|
2018-11-08 13:09:19 +00:00
|
|
|
}
|
2018-11-10 12:20:10 +00:00
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
// 2. print string
|
|
|
|
fmt.Printf("%s", s)
|
2018-11-10 12:20:10 +00:00
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
// 3. print trailing spaces
|
|
|
|
for i := size; i < alignOffset; i++ {
|
|
|
|
fmt.Printf(" ")
|
2018-11-10 12:20:10 +00:00
|
|
|
}
|
2018-11-10 17:35:00 +00:00
|
|
|
}
|
2018-11-10 12:20:10 +00:00
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
// getRealSize returns the real size escaping special characters
|
|
|
|
func getRealSize(s string) int {
|
|
|
|
// 1. get actual size
|
2018-11-10 12:20:10 +00:00
|
|
|
size := len(s)
|
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
// 2. remove \033[XYm format characters
|
|
|
|
size -= 7 * len(strings.Split(s, "\033"))
|
2018-11-10 12:20:10 +00:00
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
// 3. compensate 1 char for \033[0m
|
|
|
|
size += len(strings.Split(s, "\033[0m"))
|
2018-11-10 12:20:10 +00:00
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
return size
|
2018-11-10 12:20:10 +00:00
|
|
|
}
|