-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (117 loc) · 3.02 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
package forex
import (
"fmt"
"github.com/kamushadenes/swayit/common"
"github.com/kamushadenes/swayit/config"
"sort"
"strconv"
"strings"
)
type Forex struct {
Name string
Description string
Slug string
output *common.WaybarOutput
}
var module = &Forex{
Name: "Forex",
Description: "FIAT Ticker",
Slug: "forex",
output: &common.WaybarOutput{Class: "forex"},
}
func (m *Forex) GetName() string {
return m.Name
}
func (m *Forex) GetDescription() string {
return m.Description
}
func (m *Forex) GetSlug() string {
return m.Slug
}
func (m *Forex) GetWaybarOutput() (*common.WaybarOutput, error) {
m.output.Text = ""
m.output.Tooltip = ""
m.output.Alt = ""
err := run(m.output)
m.output.Text = strings.TrimSpace(m.output.Text)
m.output.Tooltip = strings.TrimSpace(m.output.Tooltip)
m.output.Alt = strings.TrimSpace(m.output.Alt)
return m.output, err
}
func (m *Forex) SaveLastRun() {
common.SaveLastRun(m.Slug)
}
func (m *Forex) GetLastRun() string {
return common.GetLastRun(m.Slug)
}
func (m *Forex) WriteOutput() error {
return common.WriteOutput(m.Slug, m.output.ToJSON())
}
func (m *Forex) Run() error {
m.SaveLastRun()
_, err := m.GetWaybarOutput()
if err != nil {
return err
}
return m.WriteOutput()
}
func (m *Forex) GetRunInterval() int64 {
return config.SwayItConfig.Forex.Interval
}
func (m *Forex) GetRunIntervalOnBattery() int64 {
return config.SwayItConfig.Forex.IntervalOnBattery
}
func (m *Forex) RunCommand(name string, args []string) error {
switch name {
case "convert":
out, _, err := common.WofiInput("Enter the amount to convert", []string{"1.00"}, "--lines", "1")
if err != nil {
return err
}
f, err := strconv.ParseFloat(strings.TrimSpace(out), 64)
if err != nil {
return err
}
initForex()
rates, err := client.Rates.List()
if err != nil {
return err
}
var currencies []string
for coin, name := range names {
currencies = append(currencies, fmt.Sprintf("<b>%s</b>\n%s", name, coin))
}
sort.Slice(currencies, func(i,j int) bool {
return currencies[i] < currencies[j]
})
out, _, err = common.WofiInput("Select the source currency for conversion", currencies)
if err != nil {
return err
}
base := strings.Split(out, "\n")[1]
var conversions []string
for coin, rate := range rates.Rates {
conversions = append(conversions, fmt.Sprintf("<b>%s</b>\n%s\n%.2f", names[coin], coin, 1/(1/rate*rates.Rates[base])*f))
}
sort.Slice(conversions, func(i,j int) bool {
return conversions[i] < conversions[j]
})
out, _, err = common.WofiInput("Select the destination currency for conversion", conversions)
if err != nil {
return err
}
res := strings.Split(out, "\n")[2]
return common.CopyToClipboard(res)
default:
return fmt.Errorf("unknown command %s", name)
}
}
func (m *Forex) IsEnabled() bool {
return config.SwayItConfig.Forex.Enabled
}
func (m *Forex) SuspendOnBattery() bool {
return config.SwayItConfig.Forex.SuspendOnBattery
}
func GetModule() common.Module {
return module
}