-
Notifications
You must be signed in to change notification settings - Fork 151
/
print.go
74 lines (63 loc) · 1.45 KB
/
print.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package interactive
import (
"fmt"
"math"
"regexp"
"strings"
"github.com/scaleway/scaleway-cli/v2/internal/terminal"
)
func Print(a ...interface{}) (int, error) {
if IsInteractive {
return fmt.Fprint(outputWriter, a...)
}
return 0, nil
}
func Println(a ...interface{}) (int, error) {
if IsInteractive {
return fmt.Fprintln(outputWriter, a...)
}
return 0, nil
}
func PrintlnWithoutIndent(a string) (int, error) {
if IsInteractive {
return fmt.Fprintln(outputWriter, RemoveIndent(a))
}
return 0, nil
}
func Printf(format string, a ...interface{}) (int, error) {
if IsInteractive {
return fmt.Fprintf(outputWriter, format, a...)
}
return 0, nil
}
func Line(char string) string {
return makeStr(char, terminal.GetWidth())
}
func Center(str string) string {
longestLine := 0
lines := strings.Split(str, "\n")
for _, line := range lines {
longestLine = int(math.Max(float64(longestLine), float64(len(line))))
}
return Indent(str, (terminal.GetWidth()-longestLine)/2)
}
func Indent(str string, indent int) string {
padding := makeStr(" ", indent)
lines := strings.Split(str, "\n")
for i, line := range lines {
if line != "" {
lines[i] = padding + line
}
}
return strings.Join(lines, "\n")
}
func RemoveIndent(str string) string {
return strings.Trim(regexp.MustCompile("\n[ \t]*").ReplaceAllString(str, "\n"), "\n")
}
func makeStr(char string, len int) string {
str := ""
for i := 0; i < len; i++ {
str += char
}
return str
}