fix symbols with a *consistent* method (regexp) | add debug method escape(string) string
This commit is contained in:
parent
8a08f429d2
commit
ff458d83da
|
@ -2,11 +2,12 @@ package clifmt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var titleIndex = 0
|
var titleIndex = 0
|
||||||
var alignOffset = 30
|
var alignOffset = 40
|
||||||
|
|
||||||
// Warn returns a red warning ASCII sign. If a string is given
|
// Warn returns a red warning ASCII sign. If a string is given
|
||||||
// as argument, it will print it after the warning sign
|
// as argument, it will print it after the warning sign
|
||||||
|
@ -41,44 +42,74 @@ func Title(s string) {
|
||||||
func Align(s string) {
|
func Align(s string) {
|
||||||
|
|
||||||
// 1. replace tabs with 4 spaces
|
// 1. replace tabs with 4 spaces
|
||||||
tabs := strings.Split(s, "\t")
|
tabs := strings.Split(strings.Trim(s, " \t"), "\t")
|
||||||
s = strings.Join(tabs, " ")
|
s = strings.Join(tabs, " ")
|
||||||
|
|
||||||
// 1. get real size
|
// 2. get real size
|
||||||
size := getRealSize(s)
|
size := displaySize(s)
|
||||||
|
|
||||||
|
offset := alignOffset
|
||||||
|
|
||||||
if size > alignOffset-6 {
|
if size > alignOffset-6 {
|
||||||
|
|
||||||
for i, l := 0, len(s); i < l; i++ { // find when real size is right under
|
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])
|
next := fmt.Sprintf("%s\033[0m… ", s[0:i+1])
|
||||||
if getRealSize(next) >= alignOffset-5 {
|
if displaySize(next) >= alignOffset-5 {
|
||||||
s = next
|
s = next
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
size = getRealSize(s)
|
size = displaySize(s)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// fix
|
||||||
|
offset -= 2
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. print string
|
// 3. print string
|
||||||
fmt.Printf("%s", s)
|
fmt.Printf("%s", s)
|
||||||
|
|
||||||
// 3. print trailing spaces
|
// 4. print trailing spaces
|
||||||
for i := size; i < alignOffset; i++ {
|
for i := size; i < offset; i++ {
|
||||||
fmt.Printf(" ")
|
fmt.Printf(" ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// getRealSize returns the real size escaping special characters
|
var re = regexp.MustCompile(`(?m)\[(?:\d+;)*\d+m`)
|
||||||
func getRealSize(s string) int {
|
|
||||||
|
// displaySize returns the real size escaping special characters
|
||||||
|
func displaySize(s string) int {
|
||||||
|
|
||||||
// 1. get actual size
|
// 1. get actual size
|
||||||
size := len(s)
|
size := len(s)
|
||||||
|
|
||||||
// 2. remove \033[XYm format characters
|
// 2. get all terminal coloring matches
|
||||||
size -= 7 * len(strings.Split(s, "\033"))
|
matches := re.FindAllString(s, -1)
|
||||||
|
for _, m := range matches {
|
||||||
// 3. compensate 1 char for \033[0m
|
size -= len(m)
|
||||||
size += len(strings.Split(s, "\033[0m"))
|
}
|
||||||
|
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue