-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (110 loc) · 3.21 KB
/
main.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
156
157
158
159
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"strings"
"github.com/fatih/color"
"github.com/rodaine/table"
"github.com/root27/go-crypto/CoinAPI"
)
type Color string
const (
ColorBlack Color = "\u001b[30m"
ColorRed = "\u001b[31m"
ColorGreen = "\u001b[32m"
ColorYellow = "\u001b[33m"
ColorBlue = "\u001b[34m"
ColorReset = "\u001b[0m"
)
var script = `
GREEN="\e[32m"
ENDCOLOR="\e[0m"
# Simulate a loading effect
chars="
################## Welcome to Coin App ##################
Usage with arguments:
"
greenChars="
--help, -help: show this help message and exit
--coin, -coin [ARG]: show coin info
Exampe: -coin Bitcoin
--all, -all: show first 500 coins
--number, -number [ARG]: Number of coins to display. Use with -all flag
Example: -number 50 -all or -all -number 50"
for (( j=0; j<${#chars}; j++ )); do
printf "${chars:$j:1}"
sleep 0.005
done
for (( j=0; j<${#greenChars}; j++ )); do
printf "${GREEN}${greenChars:$j:1}${ENDCOLOR}"
sleep 0.005
done
printf "\n"
`
func WelcomeMessage() {
cmd := exec.Command("bash")
cmd.Stdin = strings.NewReader(script)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println("Error running the welcome script:", err)
// Proceed with the program even if the script fails
}
}
func Colorize(s string, color Color) string {
return fmt.Sprintf("%s%s%s", color, s, ColorReset)
}
func main() {
if len(os.Args) == 1 {
WelcomeMessage()
os.Exit(0)
}
// Flags
help := flag.Bool("help", false, "Show this help message")
coin := flag.String("coin", "", "Show the details of a coin.")
all := flag.Bool("all", false, "Show the details of first 10 coins")
numberOfCoin := flag.String("number", "", "Number of coins to display. Use with -all flag")
flag.Parse()
if *help {
fmt.Println("You can use the following flags:")
fmt.Println(" -help: Show this help message")
fmt.Println(" -coin [ARG]: Show the details of a coin.")
fmt.Println(" -all: Show the details of first 10 coins")
fmt.Println(" -number [ARG]: Number of coins to display. Use with -all flag ")
os.Exit(0)
}
if *all {
if *numberOfCoin != "" {
data := CoinAPI.FilterGetAll(*numberOfCoin)
Table(data)
} else {
data := CoinAPI.GetAll()
Table(data)
}
}
if *numberOfCoin != "" {
if !*all {
fmt.Println("You must use with -all flag !!!")
}
}
if *coin != "" {
data, err := CoinAPI.GetCoin(*coin)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("Name", "Price $", "Change in 1H", "Last Updated")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
if data.Quote.USD.PercentChange1H > 0 {
tbl.AddRow(data.Name, data.Quote.USD.Price, Colorize(fmt.Sprintf("+%f", data.Quote.USD.PercentChange1H), ColorGreen), data.Quote.USD.LastUpdated)
} else {
tbl.AddRow(data.Name, data.Quote.USD.Price, Colorize(fmt.Sprintf("%f", data.Quote.USD.PercentChange1H), ColorRed), data.Quote.USD.LastUpdated)
}
tbl.Print()
}
}