yay/pkg/text/print.go

147 lines
3.3 KiB
Go
Raw Normal View History

package text
import (
"fmt"
"os"
"strconv"
"strings"
"syscall"
"unicode"
2020-05-08 18:13:51 +02:00
"github.com/leonelquinteros/gotext"
"golang.org/x/sys/unix"
)
const (
arrow = "==>"
smallArrow = " ->"
opSymbol = "::"
)
2022-09-20 00:11:10 +02:00
var (
cachedColumnCount = -1
DebugMode = false
)
func Debugln(a ...interface{}) {
if !DebugMode {
return
}
2022-10-28 00:38:11 +02:00
fmt.Fprintln(os.Stdout, append([]interface{}{Bold(yellow("[DEBUG] "))}, a...)...)
fmt.Fprint(os.Stdout, ResetCode)
2022-09-20 00:11:10 +02:00
}
func OperationInfoln(a ...interface{}) {
2020-07-10 02:36:45 +02:00
fmt.Fprint(os.Stdout, append([]interface{}{Bold(Cyan(opSymbol + " ")), boldCode}, a...)...)
fmt.Fprintln(os.Stdout, ResetCode)
}
func OperationInfo(a ...interface{}) {
2020-07-10 02:36:45 +02:00
fmt.Fprint(os.Stdout, append([]interface{}{Bold(Cyan(opSymbol + " ")), boldCode}, a...)...)
fmt.Fprint(os.Stdout, ResetCode)
}
func SprintOperationInfo(a ...interface{}) string {
return fmt.Sprint(append([]interface{}{Bold(Cyan(opSymbol + " ")), boldCode}, a...)...) + ResetCode
}
func Info(a ...interface{}) {
2020-08-01 01:20:00 +02:00
fmt.Fprint(os.Stdout, append([]interface{}{Bold(Green(arrow + " "))}, a...)...)
}
func Infoln(a ...interface{}) {
2020-08-01 01:20:00 +02:00
fmt.Fprintln(os.Stdout, append([]interface{}{Bold(Green(arrow))}, a...)...)
}
2020-07-11 00:48:30 +02:00
func SprintWarn(a ...interface{}) string {
return fmt.Sprint(append([]interface{}{Bold(yellow(smallArrow + " "))}, a...)...)
}
func Warn(a ...interface{}) {
fmt.Fprint(os.Stdout, append([]interface{}{Bold(yellow(smallArrow + " "))}, a...)...)
}
func Warnln(a ...interface{}) {
fmt.Fprintln(os.Stdout, append([]interface{}{Bold(yellow(smallArrow))}, a...)...)
}
2020-07-10 02:36:45 +02:00
func SprintError(a ...interface{}) string {
return fmt.Sprint(append([]interface{}{Bold(Red(smallArrow + " "))}, a...)...)
}
func Error(a ...interface{}) {
2020-07-10 02:36:45 +02:00
fmt.Fprint(os.Stderr, append([]interface{}{Bold(Red(smallArrow + " "))}, a...)...)
}
func Errorln(a ...interface{}) {
2020-07-10 02:36:45 +02:00
fmt.Fprintln(os.Stderr, append([]interface{}{Bold(Red(smallArrow))}, a...)...)
}
func getColumnCount() int {
if cachedColumnCount > 0 {
return cachedColumnCount
}
2021-08-11 20:13:28 +02:00
if count, err := strconv.Atoi(os.Getenv("COLUMNS")); err == nil {
cachedColumnCount = count
return cachedColumnCount
}
2021-08-11 20:13:28 +02:00
if ws, err := unix.IoctlGetWinsize(syscall.Stdout, unix.TIOCGWINSZ); err == nil {
cachedColumnCount = int(ws.Col)
return cachedColumnCount
}
2021-08-11 20:13:28 +02:00
return 80
}
func PrintInfoValue(key string, values ...string) {
const (
keyLength = 32
delimCount = 2
)
specialWordsCount := 0
for _, runeValue := range key {
// CJK handling: the character 'ー' is Katakana
// but if use unicode.Katakana, it will return false
if unicode.IsOneOf([]*unicode.RangeTable{
unicode.Han,
unicode.Hiragana,
unicode.Katakana,
unicode.Hangul,
}, runeValue) || runeValue == 'ー' {
specialWordsCount++
}
}
keyTextCount := specialWordsCount - keyLength + delimCount
str := fmt.Sprintf(Bold("%-*s: "), keyTextCount, key)
if len(values) == 0 || (len(values) == 1 && values[0] == "") {
fmt.Fprintf(os.Stdout, "%s%s\n", str, gotext.Get("None"))
return
}
maxCols := getColumnCount()
cols := keyLength + len(values[0])
str += values[0]
2021-08-11 20:13:28 +02:00
for _, value := range values[1:] {
if maxCols > keyLength && cols+len(value)+delimCount >= maxCols {
cols = keyLength
str += "\n" + strings.Repeat(" ", keyLength)
} else if cols != keyLength {
str += strings.Repeat(" ", delimCount)
cols += delimCount
}
2021-08-11 20:13:28 +02:00
str += value
cols += len(value)
}
2021-08-11 20:13:28 +02:00
fmt.Println(str)
}