1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

chore: use wakatime colors for editors and os (resolve #100)

This commit is contained in:
Ferdinand Mütsch
2021-01-30 09:51:36 +01:00
parent b732eea9b7
commit a6aff07b21
9 changed files with 366 additions and 1508 deletions

View File

@@ -40,7 +40,7 @@ type appConfig struct {
AggregationTime string `yaml:"aggregation_time" default:"02:15" env:"WAKAPI_AGGREGATION_TIME"` AggregationTime string `yaml:"aggregation_time" default:"02:15" env:"WAKAPI_AGGREGATION_TIME"`
CountingTime string `yaml:"counting_time" default:"05:15" env:"WAKAPI_COUNTING_TIME"` CountingTime string `yaml:"counting_time" default:"05:15" env:"WAKAPI_COUNTING_TIME"`
CustomLanguages map[string]string `yaml:"custom_languages"` CustomLanguages map[string]string `yaml:"custom_languages"`
LanguageColors map[string]string `yaml:"-"` Colors map[string]map[string]string `yaml:"-"`
} }
type securityConfig struct { type securityConfig struct {
@@ -193,11 +193,19 @@ func sqliteConnectionString(config *dbConfig) string {
} }
func (c *appConfig) GetCustomLanguages() map[string]string { func (c *appConfig) GetCustomLanguages() map[string]string {
return cloneStringMap(c.CustomLanguages) return cloneStringMap(c.CustomLanguages, false)
} }
func (c *appConfig) GetLanguageColors() map[string]string { func (c *appConfig) GetLanguageColors() map[string]string {
return cloneStringMap(c.LanguageColors) return cloneStringMap(c.Colors["languages"], true)
}
func (c *appConfig) GetEditorColors() map[string]string {
return cloneStringMap(c.Colors["editors"], true)
}
func (c *appConfig) GetOSColors() map[string]string {
return cloneStringMap(c.Colors["operating_systems"], true)
} }
func IsDev(env string) bool { func IsDev(env string) bool {
@@ -219,14 +227,16 @@ func readVersion() string {
return strings.TrimSpace(string(bytes)) return strings.TrimSpace(string(bytes))
} }
func readLanguageColors() map[string]string { func readColors() map[string]map[string]string {
// Read language colors // Read language colors
// Source: https://raw.githubusercontent.com/ozh/github-colors/master/colors.json // Source:
var colors = make(map[string]string) // https://raw.githubusercontent.com/ozh/github-colors/master/colors.json
var rawColors map[string]struct { // https://wakatime.com/colors/operating_systems
Color string `json:"color"` // - https://wakatime.com/colors/editors
Url string `json:"url"` // Extracted from Wakatime website with XPath (see below) and did a bit of regex magic after.
} // $x('//span[@class="editor-icon tip"]/@data-original-title').map(e => e.nodeValue)
// $x('//span[@class="editor-icon tip"]/div[1]/text()').map(e => e.nodeValue)
var colors = make(map[string]map[string]string)
file, err := pkger.Open("/data/colors.json") file, err := pkger.Open("/data/colors.json")
if err != nil { if err != nil {
@@ -238,14 +248,10 @@ func readLanguageColors() map[string]string {
log.Fatal(err) log.Fatal(err)
} }
if err := json.Unmarshal(bytes, &rawColors); err != nil { if err := json.Unmarshal(bytes, &colors); err != nil {
log.Fatal(err) log.Fatal(err)
} }
for k, v := range rawColors {
colors[strings.ToLower(k)] = v.Color
}
return colors return colors
} }
@@ -284,7 +290,7 @@ func Load() *Config {
} }
config.Version = readVersion() config.Version = readVersion()
config.App.LanguageColors = readLanguageColors() config.App.Colors = readColors()
config.Db.Dialect = resolveDbDialect(config.Db.Type) config.Db.Dialect = resolveDbDialect(config.Db.Type)
config.Security.SecureCookie = securecookie.New( config.Security.SecureCookie = securecookie.New(
securecookie.GenerateRandomKey(64), securecookie.GenerateRandomKey(64),

View File

@@ -1,8 +1,13 @@
package config package config
func cloneStringMap(m map[string]string) map[string]string { import "strings"
func cloneStringMap(m map[string]string, keysToLower bool) map[string]string {
m2 := make(map[string]string) m2 := make(map[string]string)
for k, v := range m { for k, v := range m {
if keysToLower {
k = strings.ToLower(k)
}
m2[k] = v m2[k] = v
} }
return m2 return m2

File diff suppressed because it is too large Load Diff

View File

@@ -66,6 +66,8 @@ type SummaryItemContainer struct {
type SummaryViewModel struct { type SummaryViewModel struct {
*Summary *Summary
LanguageColors map[string]string LanguageColors map[string]string
EditorColors map[string]string
OSColors map[string]string
Error string Error string
Success string Success string
ApiKey string ApiKey string

View File

@@ -59,7 +59,9 @@ func (h *SummaryHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
vm := models.SummaryViewModel{ vm := models.SummaryViewModel{
Summary: summary, Summary: summary,
LanguageColors: utils.FilterLanguageColors(h.config.App.GetLanguageColors(), summary), LanguageColors: utils.FilterColors(h.config.App.GetLanguageColors(), summary.Languages),
EditorColors: utils.FilterColors(h.config.App.GetEditorColors(), summary.Editors),
OSColors: utils.FilterColors(h.config.App.GetOSColors(), summary.OperatingSystems),
ApiKey: user.ApiKey, ApiKey: user.ApiKey,
} }

View File

@@ -110,7 +110,7 @@ function draw(subselection) {
data: wakapiData.operatingSystems data: wakapiData.operatingSystems
.slice(0, Math.min(showTopN[1], wakapiData.operatingSystems.length)) .slice(0, Math.min(showTopN[1], wakapiData.operatingSystems.length))
.map(p => parseInt(p.total)), .map(p => parseInt(p.total)),
backgroundColor: wakapiData.operatingSystems.map(p => getRandomColor(p.key)) backgroundColor: wakapiData.operatingSystems.map(p => osColors[p.key.toLowerCase()] || getRandomColor(p.key))
}], }],
labels: wakapiData.operatingSystems labels: wakapiData.operatingSystems
.slice(0, Math.min(showTopN[1], wakapiData.operatingSystems.length)) .slice(0, Math.min(showTopN[1], wakapiData.operatingSystems.length))
@@ -132,7 +132,7 @@ function draw(subselection) {
data: wakapiData.editors data: wakapiData.editors
.slice(0, Math.min(showTopN[2], wakapiData.editors.length)) .slice(0, Math.min(showTopN[2], wakapiData.editors.length))
.map(p => parseInt(p.total)), .map(p => parseInt(p.total)),
backgroundColor: wakapiData.editors.map(p => getRandomColor(p.key)) backgroundColor: wakapiData.editors.map(p => editorColors[p.key.toLowerCase()] || getRandomColor(p.key))
}], }],
labels: wakapiData.editors labels: wakapiData.editors
.slice(0, Math.min(showTopN[2], wakapiData.editors.length)) .slice(0, Math.min(showTopN[2], wakapiData.editors.length))

View File

@@ -5,9 +5,9 @@ import (
"strings" "strings"
) )
func FilterLanguageColors(all map[string]string, summary *models.Summary) map[string]string { func FilterColors(all map[string]string, haystack models.SummaryItems) map[string]string {
subset := make(map[string]string) subset := make(map[string]string)
for _, item := range summary.Languages { for _, item := range haystack {
if c, ok := all[strings.ToLower(item.Key)]; ok { if c, ok := all[strings.ToLower(item.Key)]; ok {
subset[strings.ToLower(item.Key)] = c subset[strings.ToLower(item.Key)] = c
} }

View File

@@ -1 +1 @@
1.20.2 1.20.3

View File

@@ -158,6 +158,8 @@
<script> <script>
const languageColors = {{ .LanguageColors | json }} const languageColors = {{ .LanguageColors | json }}
const editorColors = {{ .EditorColors | json }}
const osColors = {{ .OSColors | json }}
const wakapiData = {} const wakapiData = {}
wakapiData.projects = {{ .Projects | json }} wakapiData.projects = {{ .Projects | json }}