-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
77 lines (65 loc) · 1.7 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
package main
import (
"flag"
"fmt"
"os"
"stockprice/finance"
)
var format = flag.String("f", "term", "formatting: term or vim")
func init() {
flag.Parse()
if len(flag.Args()) != 1 {
fmt.Println("Usage: stockprice [symbol], e.g. stockprice grpn")
os.Exit(1)
}
}
func main() {
price, delta, percentage, err := finance.FindStockPrice(flag.Args()[0])
if err != nil {
fmt.Println(err.Error())
return
}
var result string
if *format == "vim" {
result = formatForVim(price, delta, percentage)
} else if *format == "term" {
result = formatForTerminal(price, delta, percentage)
}
fmt.Println(result)
}
func formatForTerminal(price float64, delta float64, percentage string) string {
var deltaFormatted string
var percentageFormatted = percentage
if delta > 0 {
deltaFormatted = fmt.Sprintf("\x1b[32m+%.2f\x1b[0m", delta)
percentageFormatted = fmt.Sprintf("\x1b[32m%s\x1b[0m", percentage)
} else if delta < 0 {
deltaFormatted = fmt.Sprintf("\x1b[31m%.2f\x1b[0m", delta)
percentageFormatted = fmt.Sprintf("\x1b[31m%s\x1b[0m", percentage)
} else {
deltaFormatted = fmt.Sprintf("%.2f", delta)
}
return fmt.Sprintf("%.2f %s %s", price, deltaFormatted, percentageFormatted)
}
func formatForVim(price float64, delta float64, percentage string) string {
var result string
if delta > 0 {
result = fmt.Sprintf(`echohl Normal
echo "%.2f"
echohl MoreMsg
echon " +%.2f %s"
echohl Normal`, price, delta, percentage)
} else if delta < 0 {
result = fmt.Sprintf(`echohl Normal
echo "%.2f"
echohl WarningMsg
echon " %.2f %s"
echohl Normal`, price, delta, percentage)
} else {
result = fmt.Sprintf(`echohl Normal
echo "%.2f"
echon " %.2f %s"
echohl Normal`, price, delta, percentage)
}
return result
}