oh-my-posh/src/segment_wakatime.go

70 lines
1.3 KiB
Go
Raw Normal View History

2021-12-11 08:31:58 -08:00
package main
import (
"encoding/json"
"oh-my-posh/environment"
2021-12-11 08:31:58 -08:00
)
type wakatime struct {
2022-01-01 11:08:08 -08:00
props Properties
env environment.Environment
2021-12-11 08:31:58 -08:00
wtData
}
type wtTotals struct {
Seconds float64 `json:"seconds"`
Text string `json:"text"`
}
type wtData struct {
CummulativeTotal wtTotals `json:"cummulative_total"`
Start string `json:"start"`
End string `json:"end"`
}
func (w *wakatime) template() string {
return "{{ secondsRound .CummulativeTotal.Seconds }}"
}
2021-12-11 08:31:58 -08:00
func (w *wakatime) enabled() bool {
err := w.setAPIData()
return err == nil
}
func (w *wakatime) setAPIData() error {
url := w.props.getString(URL, "")
cacheTimeout := w.props.getInt(CacheTimeout, DefaultCacheTimeout)
if cacheTimeout > 0 {
// check if data stored in cache
if val, found := w.env.Cache().Get(url); found {
2021-12-11 08:31:58 -08:00
err := json.Unmarshal([]byte(val), &w.wtData)
if err != nil {
return err
}
return nil
}
}
httpTimeout := w.props.getInt(HTTPTimeout, DefaultHTTPTimeout)
2022-01-07 10:41:58 -08:00
body, err := w.env.HTTPRequest(url, httpTimeout)
2021-12-11 08:31:58 -08:00
if err != nil {
return err
}
err = json.Unmarshal(body, &w.wtData)
if err != nil {
return err
}
if cacheTimeout > 0 {
w.env.Cache().Set(url, string(body), cacheTimeout)
2021-12-11 08:31:58 -08:00
}
return nil
}
func (w *wakatime) init(props Properties, env environment.Environment) {
2021-12-11 08:31:58 -08:00
w.props = props
w.env = env
}