2021-12-11 08:31:58 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-01-26 01:23:18 -08:00
|
|
|
"oh-my-posh/environment"
|
2022-01-26 04:53:35 -08:00
|
|
|
"oh-my-posh/properties"
|
2021-12-11 08:31:58 -08:00
|
|
|
)
|
|
|
|
|
2022-01-26 05:10:18 -08:00
|
|
|
type Wakatime struct {
|
2022-01-26 04:53:35 -08:00
|
|
|
props properties.Properties
|
2022-01-26 01:23:18 -08:00
|
|
|
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 {
|
2022-01-23 12:37:51 -08:00
|
|
|
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
|
2022-01-23 12:37:51 -08:00
|
|
|
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 {
|
2022-01-23 12:37:51 -08:00
|
|
|
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
|
|
|
|
}
|