Skip to content

Commit a9021bd

Browse files
authored
Merge pull request #80 from aykhans/refactor/color-utils
🔨 Replace color utils with 'github.com/fatih/color'
2 parents a01bf19 + 48c2dc7 commit a9021bd

File tree

7 files changed

+27
-53
lines changed

7 files changed

+27
-53
lines changed

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
const (
15-
VERSION string = "0.5.6"
15+
VERSION string = "0.5.7"
1616
DefaultUserAgent string = "Dodo/" + VERSION
1717
ProxyCheckURL string = "https://www.google.com"
1818
DefaultMethod string = "GET"

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ require (
99
golang.org/x/net v0.35.0
1010
)
1111

12+
require (
13+
github.com/mattn/go-colorable v0.1.13 // indirect
14+
github.com/mattn/go-isatty v0.0.20 // indirect
15+
)
16+
1217
require (
1318
github.com/andybalholm/brotli v1.1.1 // indirect
19+
github.com/fatih/color v1.18.0
1420
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
1521
github.com/go-playground/locales v0.14.1 // indirect
1622
github.com/go-playground/universal-translator v0.18.1 // indirect

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7X
22
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
6+
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
57
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
68
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
79
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
@@ -18,6 +20,11 @@ github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IX
1820
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
1921
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
2022
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
23+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
24+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
25+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
26+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
27+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
2128
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
2229
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
2330
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -37,6 +44,8 @@ golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
3744
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
3845
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
3946
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
47+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
48+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4049
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
4150
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
4251
golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/aykhans/dodo/requests"
1717
"github.com/aykhans/dodo/utils"
1818
"github.com/aykhans/dodo/validation"
19+
"github.com/fatih/color"
1920
goValidator "github.com/go-playground/validator/v10"
2021
)
2122

@@ -109,13 +110,13 @@ func main() {
109110
responses, err := requests.Run(ctx, requestConf)
110111
if err != nil {
111112
if customerrors.Is(err, customerrors.ErrInterrupt) {
112-
utils.PrintlnC(utils.Colors.Yellow, err.Error())
113+
color.Yellow(err.Error())
113114
return
114115
} else if customerrors.Is(err, customerrors.ErrNoInternet) {
115116
utils.PrintAndExit("No internet connection")
116117
return
117118
}
118-
panic(err)
119+
utils.PrintErrAndExit(err)
119120
}
120121

121122
responses.Print()

readers/cli.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/aykhans/dodo/config"
99
. "github.com/aykhans/dodo/types"
10-
"github.com/aykhans/dodo/utils"
10+
"github.com/fatih/color"
1111
)
1212

1313
const usageText = `Usage:
@@ -114,7 +114,7 @@ func CLIConfigReader() (*config.CLIConfig, error) {
114114
case "timeout", "t":
115115
var maxUint32 uint = 4294967295
116116
if timeout > maxUint32 {
117-
utils.PrintfC(utils.Colors.Yellow, "timeout value is too large, setting to %d\n", maxUint32)
117+
color.Yellow("timeout value is too large, setting to %d", maxUint32)
118118
timeout = maxUint32
119119
}
120120
cliConfig.Timeout = uint32(timeout)

requests/client.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/aykhans/dodo/config"
1212
"github.com/aykhans/dodo/readers"
1313
"github.com/aykhans/dodo/utils"
14+
"github.com/fatih/color"
1415
"github.com/valyala/fasthttp"
1516
"github.com/valyala/fasthttp/fasthttpproxy"
1617
)
@@ -73,18 +74,9 @@ func getClients(
7374
var yesOrNoDefault bool
7475
if activeProxyClientsCount == 0 {
7576
yesOrNoDefault = false
76-
yesOrNoMessage = utils.Colored(
77-
utils.Colors.Yellow,
78-
"No active proxies found. Do you want to continue?",
79-
)
77+
yesOrNoMessage = color.YellowString("No active proxies found. Do you want to continue?")
8078
} else {
81-
yesOrNoMessage = utils.Colored(
82-
utils.Colors.Yellow,
83-
fmt.Sprintf(
84-
"Found %d active proxies. Do you want to continue?",
85-
activeProxyClientsCount,
86-
),
87-
)
79+
yesOrNoMessage = color.YellowString("Found %d active proxies. Do you want to continue?", activeProxyClientsCount)
8880
}
8981
if !yes {
9082
response := readers.CLIYesOrNoReader("\n"+yesOrNoMessage, yesOrNoDefault)

utils/print.go

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,12 @@ package utils
33
import (
44
"fmt"
55
"os"
6-
)
7-
8-
var Colors = struct {
9-
reset string
10-
Red string
11-
Green string
12-
Yellow string
13-
Orange string
14-
Blue string
15-
Magenta string
16-
Cyan string
17-
Gray string
18-
White string
19-
}{
20-
reset: "\033[0m",
21-
Red: "\033[31m",
22-
Green: "\033[32m",
23-
Yellow: "\033[33m",
24-
Orange: "\033[38;5;208m",
25-
Blue: "\033[34m",
26-
Magenta: "\033[35m",
27-
Cyan: "\033[36m",
28-
Gray: "\033[37m",
29-
White: "\033[97m",
30-
}
31-
32-
func Colored(color string, a ...any) string {
33-
return color + fmt.Sprint(a...) + Colors.reset
34-
}
356

36-
func PrintfC(color string, format string, a ...any) {
37-
fmt.Printf(Colored(color, format), a...)
38-
}
39-
40-
func PrintlnC(color string, a ...any) {
41-
fmt.Println(Colored(color, a...))
42-
}
7+
"github.com/fatih/color"
8+
)
439

4410
func PrintErr(err error) {
45-
PrintlnC(Colors.Red, err.Error())
11+
color.New(color.FgRed).Fprintln(os.Stderr, err.Error())
4612
}
4713

4814
func PrintErrAndExit(err error) {

0 commit comments

Comments
 (0)