-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
99 lines (78 loc) · 1.83 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
package main
import (
"bufio"
"fmt"
"os"
"runtime"
"strings"
"time"
"github.com/technowar/crypto/lib"
"github.com/technowar/crypto/utils"
)
func main() {
utils.Clear(runtime.GOOS)
fmt.Println("Fetching data...")
coinList, now := lib.FetchCoins()
reader := bufio.NewReader(os.Stdin)
tickerDone := make(chan struct{})
utils.Clear(runtime.GOOS)
for {
fmt.Print("> ")
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
texts := strings.Split(text, " ")
select {
case tickerDone <- struct{}{}:
default:
}
if texts[0] == "coin" {
coin := "BTC"
if len(texts) >= 2 {
coin = strings.ToUpper(texts[1])
}
lib.CoinDetails(coin, coinList, now)
go timeTick("coin", coin, "", 5, tickerDone)
} else if texts[0] == "price" {
from := "BTC"
to := "BTC"
if len(texts) == 2 {
from = texts[1]
}
if len(texts) >= 3 {
from = texts[1]
to = texts[2]
}
lib.FetchPrice(strings.ToUpper(from), strings.ToUpper(to))
go timeTick("price", strings.ToUpper(from), strings.ToUpper(to), 1, tickerDone)
} else if text == "watch" {
lib.CoinDetails("", coinList, now)
go timeTick("watch", "", "", 5, tickerDone)
} else if text == "clear" || text == "cls" {
utils.Clear(runtime.GOOS)
} else if text == "help" {
utils.Usage()
} else {
fmt.Printf("Bad option: %v\n\n", text)
}
}
}
func timeTick(format, from, to string, tick int, done <-chan struct{}) {
tickChan := time.NewTicker(time.Minute * time.Duration(tick))
for {
select {
case <-tickChan.C:
switch format {
case "watch":
coinList, now := lib.FetchCoins()
lib.CoinDetails("", coinList, now)
case "coin":
coinList, now := lib.FetchCoins()
lib.CoinDetails(from, coinList, now)
case "price":
lib.FetchPrice(from, to)
}
case <-done:
return
}
}
}