-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
page_result_formatter.go
67 lines (48 loc) · 1.3 KB
/
page_result_formatter.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
package main
import (
"fmt"
"sort"
"strings"
"github.com/logrusorgru/aurora/v3"
)
type pageResultFormatter struct {
verbose bool
aurora aurora.Aurora
}
func newPageResultFormatter(verbose bool, color bool) *pageResultFormatter {
return &pageResultFormatter{verbose, aurora.NewAurora(color)}
}
func (f *pageResultFormatter) Format(r *pageResult) string {
ss := []string(nil)
if f.verbose {
ss = append(ss, f.formatSuccessLinkResults(r.SuccessLinkResults)...)
}
ss = append(ss, f.formatErrorLinkResults(r.ErrorLinkResults)...)
return strings.Join(
append([]string{fmt.Sprint(f.aurora.Yellow(r.URL))}, formatMessages(ss)...),
"\n",
)
}
func (f *pageResultFormatter) formatSuccessLinkResults(rs []*successLinkResult) []string {
ss := make([]string, 0, len(rs))
for _, r := range rs {
ss = append(ss, fmt.Sprintf("%v", f.aurora.Green(r.StatusCode))+"\t"+r.URL)
}
sort.Strings(ss)
return ss
}
func (f *pageResultFormatter) formatErrorLinkResults(rs []*errorLinkResult) []string {
ss := make([]string, 0, len(rs))
for _, r := range rs {
ss = append(ss, fmt.Sprintf("%v", f.aurora.Red(r.Error))+"\t"+r.URL)
}
sort.Strings(ss)
return ss
}
func formatMessages(ss []string) []string {
ts := make([]string, 0, len(ss))
for _, s := range ss {
ts = append(ts, "\t"+s)
}
return ts
}