2018-11-08 13:09:19 +00:00
|
|
|
|
package clifmt
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2018-11-12 23:20:05 +00:00
|
|
|
|
"regexp"
|
2018-11-08 13:09:19 +00:00
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var titleIndex = 0
|
2018-11-12 23:20:05 +00:00
|
|
|
|
var alignOffset = 40
|
2018-11-08 13:09:19 +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])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
2018-11-12 23:20:05 +00:00
|
|
|
|
tabs := strings.Split(strings.Trim(s, " \t"), "\t")
|
2018-11-10 17:35:00 +00:00
|
|
|
|
s = strings.Join(tabs, " ")
|
2018-11-08 13:09:19 +00:00
|
|
|
|
|
2018-11-12 23:20:05 +00:00
|
|
|
|
// 2. get real size
|
|
|
|
|
size := displaySize(s)
|
|
|
|
|
|
|
|
|
|
offset := alignOffset
|
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])
|
2018-11-12 23:20:05 +00:00
|
|
|
|
if displaySize(next) >= alignOffset-5 {
|
2018-11-10 17:35:00 +00:00
|
|
|
|
s = next
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-11-08 13:09:19 +00:00
|
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
|
}
|
2018-11-12 23:20:05 +00:00
|
|
|
|
size = displaySize(s)
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// fix
|
|
|
|
|
offset -= 2
|
2018-11-08 13:09:19 +00:00
|
|
|
|
}
|
2018-11-10 12:20:10 +00:00
|
|
|
|
|
2018-11-12 23:20:05 +00:00
|
|
|
|
// 3. print string
|
2018-11-10 17:35:00 +00:00
|
|
|
|
fmt.Printf("%s", s)
|
2018-11-10 12:20:10 +00:00
|
|
|
|
|
2018-11-12 23:20:05 +00:00
|
|
|
|
// 4. print trailing spaces
|
|
|
|
|
for i := size; i < offset; i++ {
|
2018-11-10 17:35:00 +00:00
|
|
|
|
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-12 23:20:05 +00:00
|
|
|
|
var re = regexp.MustCompile(`(?m)\[(?:\d+;)*\d+m`)
|
|
|
|
|
|
|
|
|
|
// displaySize returns the real size escaping special characters
|
|
|
|
|
func displaySize(s string) int {
|
|
|
|
|
|
2018-11-10 17:35:00 +00:00
|
|
|
|
// 1. get actual size
|
2018-11-10 12:20:10 +00:00
|
|
|
|
size := len(s)
|
|
|
|
|
|
2018-11-12 23:20:05 +00:00
|
|
|
|
// 2. get all terminal coloring matches
|
|
|
|
|
matches := re.FindAllString(s, -1)
|
|
|
|
|
for _, m := range matches {
|
|
|
|
|
size -= len(m)
|
|
|
|
|
}
|
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
|
|
|
|
}
|
2018-11-12 23:20:05 +00:00
|
|
|
|
|
|
|
|
|
func escape(in string) string {
|
|
|
|
|
|
|
|
|
|
out := make([]rune, 0)
|
|
|
|
|
|
|
|
|
|
for _, char := range in {
|
|
|
|
|
if char == '\n' {
|
|
|
|
|
out = append(out, []rune("\\n")...)
|
|
|
|
|
} else if char == '\r' {
|
|
|
|
|
out = append(out, []rune("\\r")...)
|
|
|
|
|
} else if char == '\t' {
|
|
|
|
|
out = append(out, []rune("\\t")...)
|
|
|
|
|
} else if char == '\033' {
|
|
|
|
|
out = append(out, []rune("\\033")...)
|
|
|
|
|
} else {
|
|
|
|
|
out = append(out, char)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
|
}
|