-
Notifications
You must be signed in to change notification settings - Fork 0
/
polybar.go
156 lines (128 loc) · 3.78 KB
/
polybar.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
package format
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/dustin/go-humanize"
"github.com/u3mur4/crypto-price/exchange"
)
type PolybarConfig struct {
Sort string
Icon bool
}
type polybarFormat struct {
markets map[string]exchange.Market
showPrice map[string]bool
config PolybarConfig
keys []string
}
func NewPolybar(config PolybarConfig) Formatter {
return &polybarFormat{
markets: make(map[string]exchange.Market),
config: config,
showPrice: make(map[string]bool),
}
}
func (p *polybarFormat) Open() {
process := func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
market := r.FormValue("market")
if showPrice, ok := p.showPrice[market]; ok {
p.showPrice[market] = !showPrice
}
}
}
http.HandleFunc("/", process)
go http.ListenAndServe(":60253", nil)
}
func (p *polybarFormat) formatQuote(market exchange.Market) string {
if strings.EqualFold(market.Quote, "btc") {
// return "Ƀ"
return ""
} else if strings.EqualFold(market.Quote, "usd") || strings.EqualFold(market.Quote, "usdt") {
return "$"
} else if strings.EqualFold(market.Quote, "eur") {
return "€"
}
return ""
}
func (i *polybarFormat) formatPrice(market exchange.Market) string {
if strings.EqualFold(market.Quote, "btc") {
if market.Candle.Close < 1 {
return fmt.Sprintf("%.8f", market.Candle.Close)
}
return humanize.Comma(int64(market.Candle.Close))
}
return fmt.Sprintf("%.0f", market.Candle.Close)
}
func (i *polybarFormat) openTradingViewCmd(market exchange.Market) string {
b := strings.Builder{}
b.WriteString("chromium --newtab ")
b.WriteString("https://www.tradingview.com/chart/?symbol=")
b.WriteString(market.Exchange)
b.WriteString(":")
b.WriteString(market.Base)
b.WriteString(market.Quote)
b.WriteString(" --profile-directory=\"Profile 2\"")
return "%{A1:" + strings.Replace(b.String(), ":", "\\:", -1) + ":}"
}
func (i *polybarFormat) tooglePrice(market, data string) string {
b := strings.Builder{}
b.WriteString("%{A1:")
b.WriteString("curl -d 'market=" + market + "' -X POST http\\://localhost\\:60253")
b.WriteString(":}")
b.WriteString(data)
b.WriteString("%{A}")
return b.String()
}
func (i *polybarFormat) Show(market exchange.Market) {
key := market.Exchange + market.Base + market.Quote
// keep output consistent
if _, ok := i.markets[key]; !ok {
i.keys = append(i.keys, key)
}
i.markets[key] = market
if _, ok := i.showPrice[key]; !ok {
i.showPrice[key] = true
}
// format all market
builder := strings.Builder{}
for _, k := range i.keys {
market := i.markets[k]
price := i.formatPrice(market)
quote := i.formatQuote(market)
// icon := i.getIcon(market)
// // use icon or the base
// tmp := fmt.Sprintf("<span foreground='%s'>%s: </span>", color(market).Hex(), strings.ToUpper(market.Base()))
// builder.WriteString(tmp)
// }
// // print price
// tmp := fmt.Sprintf("<span foreground='%s'>%s%s (%+.1f%%)</span> ", color(market).Hex(), price, quote, percent(market))
// builder.WriteString(tmp)
builder.WriteString("%{F")
builder.WriteString(color(market.Candle).Hex())
builder.WriteString("}")
// builder.WriteString(i.openTradingViewCmd(chart))
builder.WriteString(i.tooglePrice(k, strings.ToUpper(market.Base)))
if showPrice, ok := i.showPrice[k]; ok && showPrice {
builder.WriteString(": ")
builder.WriteString(quote)
builder.WriteString(price)
builder.WriteString(fmt.Sprintf(" (%+.1f%%) ", market.Candle.Percent()))
} else {
builder.WriteString(" ")
}
// builder.WriteString("%{A}")
builder.WriteString("%{F-}")
}
fmt.Fprintln(os.Stdout, builder.String())
// logrus.Debug(builder.String())
}
func (p polybarFormat) Close() {
}