/
output.go
155 lines (139 loc) · 3.71 KB
/
output.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package output
import (
"fmt"
"github.com/fatih/color"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
type Flags struct {
Debug bool
Verbose bool
}
type Formated struct{
Text string
Attributes []color.Attribute
}
var flags *Flags
var attributeList map[string]color.Attribute
func OuputInit(flagsImp *Flags){
flags = flagsImp
attributeList = map[string]color.Attribute{
"R" : color.Reset,
"B" : color.Bold,
"F" : color.Faint,
"I" : color.Italic,
"U" : color.Underline,
"BS" : color.BlinkSlow,
"BP" : color.BlinkRapid,
"RV" : color.ReverseVideo,
"C" : color.Concealed,
"CO" : color.CrossedOut,
"FK" : color.FgBlack,
"FR" : color.FgRed,
"FG" : color.FgGreen,
"FY" : color.FgYellow,
"FB" : color.FgBlue,
"FM" : color.FgMagenta,
"FC" : color.FgCyan,
"FW" : color.FgWhite,
"FHK" : color.FgHiBlack,
"FHR" : color.FgHiRed,
"FHG" : color.FgHiGreen,
"FHY" : color.FgHiYellow,
"FHB" : color.FgHiBlue,
"FHM" : color.FgHiMagenta,
"FHC" : color.FgHiCyan,
"FHW" : color.FgHiWhite,
"BK" : color.BgBlack,
"BR" : color.BgRed,
"BG" : color.BgGreen,
"BY" : color.BgYellow,
"BB" : color.BgBlue,
"BM" : color.BgMagenta,
"BC" : color.BgCyan,
"BW" : color.BgWhite,
"BHK" : color.BgHiBlack,
"BHR" : color.BgHiRed,
"BHG" : color.BgHiGreen,
"BHY" : color.BgHiYellow,
"BHB" : color.BgHiBlue,
"BHM" : color.BgHiMagenta,
"BHC" : color.BgHiCyan,
"BHW" : color.BgHiWhite,
}
}
func NewLogFile(fDir, fRunFile, fExt string) (logFile *os.File, err error) {
err = os.MkdirAll(fDir, os.ModeDir | 0777)
logFileName := filepath.Base(fRunFile)
logFileName = strings.Replace(logFileName, filepath.Ext(logFileName), "", -1)
logFileName = fDir + logFileName + fExt
logFile, err = os.Create(logFileName)
if err != nil { panic(err) }
return
}
func checkFormat(args []interface{}) (format string, hasFormat bool, attributes []color.Attribute) {
if len(args) < 1 { return }
switch arg := args[0].(type){
case string:
format = arg
hasFormat = true
}
if !hasFormat { return }
if len(format) < 1 || strings.Index(format, "?:") < 0 {
hasFormat = false
return
}
re := regexp.MustCompile("^(?:([^.?:]+)[.]?)*[?]:")
attributesStr := re.FindString(format)
format = re.ReplaceAllString(format, "")
if len(attributesStr) < 2 { return }
attributesArr := strings.Split(attributesStr[:len(attributesStr)-2], ".")
if len(attributesArr) < 1 { return }
for _, attr := range attributesArr {
attributes = append(attributes, attributeList[attr])
}
return
}
func Format(args ...interface{}) (result Formated) {
var (format string; hasFormat bool)
format, hasFormat, result.Attributes = checkFormat(args)
if hasFormat {
result.Text = fmt.Sprintf(format, args[1:]...)
} else {
result.Text = fmt.Sprint(args...)
}
return
}
func Print(args ...interface{}) {
formated := Format(args...)
if len(formated.Attributes) > 0 {
color.Set(formated.Attributes...)
}
fmt.Print(formated.Text)
if len(formated.Attributes) > 0 {
color.Unset()
}
}
func Log(args ...interface{}) {
formated := Format(args...)
log.Print(formated.Text)
}
func Out(args ...interface{}) {
formated := Format(args...)
log.Print(formated.Text)
if len(formated.Attributes) > 0 {
color.Set(formated.Attributes...)
}
fmt.Print(formated.Text)
if len(formated.Attributes) > 0 {
color.Unset()
}
}
func Debug(args ...interface{}) {
if flags.Debug {
Out(args...)
}
}