oh-my-posh/src/segment_wakatime.go

71 lines
1.4 KiB
Go
Raw Normal View History

2021-12-11 08:31:58 -08:00
package main
import (
"encoding/json"
"oh-my-posh/environment"
"oh-my-posh/properties"
2021-12-11 08:31:58 -08:00
)
2022-01-26 05:10:18 -08:00
type Wakatime struct {
props properties.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"`
}
2022-01-26 05:10:18 -08:00
func (w *Wakatime) template() string {
return "{{ secondsRound .CummulativeTotal.Seconds }}"
}
2022-01-26 05:10:18 -08:00
func (w *Wakatime) enabled() bool {
2021-12-11 08:31:58 -08:00
err := w.setAPIData()
return err == nil
}
2022-01-26 05:10:18 -08:00
func (w *Wakatime) setAPIData() error {
2022-01-26 04:09:21 -08:00
url := w.props.GetString(URL, "")
cacheTimeout := w.props.GetInt(CacheTimeout, DefaultCacheTimeout)
2021-12-11 08:31:58 -08:00
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
}
}
2022-01-26 04:09:21 -08:00
httpTimeout := w.props.GetInt(HTTPTimeout, DefaultHTTPTimeout)
2021-12-11 08:31:58 -08:00
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
}
2022-01-26 05:10:18 -08:00
func (w *Wakatime) init(props properties.Properties, env environment.Environment) {
2021-12-11 08:31:58 -08:00
w.props = props
w.env = env
}