/
terminal.go
116 lines (94 loc) · 2.99 KB
/
terminal.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Package terminal provides methods for printing messages to terminal
package terminal
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"fmt"
"os"
"strings"
"github.com/essentialkaos/ek/v12/fmtc"
"github.com/essentialkaos/ek/v12/strutil"
)
// ////////////////////////////////////////////////////////////////////////////////// //
var (
// ErrorColorTag is fmtc color tag used for error messages
ErrorColorTag = "{r}"
// WarnColorTag is fmtc color tag used for warning messages
WarnColorTag = "{y}"
// InfoColorTag is fmtc color tag used for info messages
InfoColorTag = "{c-}"
)
var (
// ErrorPrefix is prefix for error messages
ErrorPrefix = ""
// WarnPrefix is prefix for warning messages
WarnPrefix = ""
// InfoPrefix is prefix for info messages
InfoPrefix = ""
)
// ////////////////////////////////////////////////////////////////////////////////// //
// PrintActionMessage prints message about action currently in progress
func PrintActionMessage(message string) {
fmtc.Printf("{*}%s:{!} ", message)
}
// PrintActionStatus prints message with action execution status
func PrintActionStatus(status int) {
switch status {
case 0:
fmtc.Println("{g}OK{!}")
case 1:
fmtc.Println("{r}ERROR{!}")
case 2:
fmtc.Println("{y}WARNING{!}")
default:
fmtc.Println("{s}UNKNOWN{!}")
}
}
// Error prints error message
func Error(message any, args ...any) {
fmtc.Fprintf(
os.Stdout, ErrorColorTag+ErrorPrefix+"%s{!}\n",
formatMessage(message, ErrorPrefix, args),
)
}
// Warn prints warning message
func Warn(message any, args ...any) {
fmtc.Fprintf(
os.Stdout, WarnColorTag+WarnPrefix+"%s{!}\n",
formatMessage(message, WarnPrefix, args),
)
}
// Info prints info message
func Info(message any, args ...any) {
fmtc.Fprintf(
os.Stdout, InfoColorTag+InfoPrefix+"%s{!}\n",
formatMessage(message, InfoPrefix, args),
)
}
// ////////////////////////////////////////////////////////////////////////////////// //
// formatMessage formats message based on it type
func formatMessage(message any, prefix string, args []any) string {
var msg string
switch m := message.(type) {
case string:
msg = m
case error:
msg = m.Error()
default:
msg = fmt.Sprint(message)
}
if len(args) > 0 {
msg = fmt.Sprintf(msg, args...)
}
if prefix != "" && strings.Contains(msg, "\n") {
prefixOffset := strings.Repeat(" ", strutil.Len(prefix))
msgText := strings.TrimRight(msg, "\n")
tail := strutil.Substr(msg, len(msgText), 999999)
msg = strings.ReplaceAll(msgText, "\n", "\n"+prefixOffset) + tail
}
return msg
}