From 30ddd005eb5ed20e2547c1b131092d48954afad0 Mon Sep 17 00:00:00 2001 From: BrookJeynes Date: Tue, 3 Jun 2025 00:27:20 +1000 Subject: [PATCH] appview/knotserver: repo: include langauge colour and calculate percentage This commit updates the types related to fetching the languages used within a repo to be more structured and include additional information such as the language colour. Additional logic to calculate the language percentage was added to the repo index. Signed-off-by: BrookJeynes --- appview/pages/pages.go | 2 +- appview/repo/repo.go | 44 +++++++++++++++++++++++++++++++++++++++++- knotserver/routes.go | 18 +++++++++++++---- types/repo.go | 14 +++++++++++++- 4 files changed, 71 insertions(+), 7 deletions(-) diff --git a/appview/pages/pages.go b/appview/pages/pages.go index 5b342d9..9a71016 100644 --- a/appview/pages/pages.go +++ b/appview/pages/pages.go @@ -416,7 +416,7 @@ type RepoIndexParams struct { Raw bool EmailToDidOrHandle map[string]string VerifiedCommits commitverify.VerifiedCommits - Languages *types.RepoLanguageResponse + Languages []types.RepoLanguageDetails types.RepoIndexResponse } diff --git a/appview/repo/repo.go b/appview/repo/repo.go index e652bbe..c1bfa44 100644 --- a/appview/repo/repo.go +++ b/appview/repo/repo.go @@ -179,6 +179,48 @@ func (rp *Repo) RepoIndex(w http.ResponseWriter, r *http.Request) { log.Printf("failed to compute language percentages: %s", err) // non-fatal } + if repoLanguages == nil { + repoLanguages = &types.RepoLanguageResponse{Languages: make(map[string]types.RepoLanguageFile)} + } + + languageFileCount := 0 + for _, details := range repoLanguages.Languages { + languageFileCount += details.Count + } + + var languageStats []types.RepoLanguageDetails + var otherLanguageStat *types.RepoLanguageDetails + var otherPercentage float32 = 0 + + for fileType, details := range repoLanguages.Languages { + percentage := (float32(details.Count) / float32(languageFileCount)) * 100 + + if percentage <= 0.1 { + otherPercentage += percentage + continue + } + + // Exclude languages + if fileType == "Text" { + otherPercentage += percentage + continue + } + + if fileType == "Other" { + otherLanguageStat = &types.RepoLanguageDetails{Name: fileType, Percentage: percentage, Color: details.Color} + } else { + languageStats = append(languageStats, types.RepoLanguageDetails{Name: fileType, Percentage: percentage, Color: details.Color}) + } + } + + sort.Slice(languageStats, func(i, j int) bool { + return languageStats[i].Percentage > languageStats[j].Percentage + }) + + if otherLanguageStat != nil { + otherLanguageStat.Percentage += otherPercentage + languageStats = append(languageStats, *otherLanguageStat) + } rp.pages.RepoIndexPage(w, pages.RepoIndexParams{ LoggedInUser: user, @@ -191,7 +233,7 @@ func (rp *Repo) RepoIndex(w http.ResponseWriter, r *http.Request) { BranchesTrunc: branchesTrunc, EmailToDidOrHandle: emailToDidOrHandle(rp, emailToDidMap), VerifiedCommits: vc, - Languages: repoLanguages, + Languages: languageStats, }) return } diff --git a/knotserver/routes.go b/knotserver/routes.go index 7424c99..c822c96 100644 --- a/knotserver/routes.go +++ b/knotserver/routes.go @@ -767,7 +767,7 @@ func (h *Handle) RepoLanguages(w http.ResponseWriter, r *http.Request) { return } - languageFileCount := make(map[string]int) + languageFileCount := make(map[string]types.RepoLanguageFile) err = recurseEntireTree(r.Context(), gr, func(absPath string) { lang, safe := enry.GetLanguageByExtension(absPath) @@ -775,19 +775,29 @@ func (h *Handle) RepoLanguages(w http.ResponseWriter, r *http.Request) { content, _ := gr.FileContentN(absPath, 1024) if !safe { lang = enry.GetLanguage(absPath, content) + if len(lang) == 0 { + lang = "Other" + } } else { lang, _ = enry.GetLanguageByContent(absPath, content) if len(lang) == 0 { - return + lang = "Other" } } } + color := enry.GetColor(lang) + v, ok := languageFileCount[lang] if ok { - languageFileCount[lang] = v + 1 + v.Count += 1 + languageFileCount[lang] = v } else { - languageFileCount[lang] = 1 + languageFileCount[lang] = types.RepoLanguageFile{ + Name: lang, + Color: color, + Count: 1, + } } }, "") if err != nil { diff --git a/types/repo.go b/types/repo.go index 985da9e..75ee97d 100644 --- a/types/repo.go +++ b/types/repo.go @@ -109,7 +109,19 @@ type AncestorCheckResponse struct { Status ForkStatus `json:"status"` } +type RepoLanguageFile struct { + Name string + Count int + Color string +} + +type RepoLanguageDetails struct { + Name string + Percentage float32 + Color string +} + type RepoLanguageResponse struct { // Language: Percentage - Languages map[string]int `json:"languages"` + Languages map[string]RepoLanguageFile `json:"languages"` } -- 2.43.0 From 1136b561acf3d28312d6370f384cbdb994d4f871 Mon Sep 17 00:00:00 2001 From: BrookJeynes Date: Tue, 3 Jun 2025 00:33:03 +1000 Subject: [PATCH] appview: repo(base): add languages to repo index and show name and percentage on hover This commit adds languages with their associated colour just below the tabs on the repo index page. Hovering over the colours will display the language name and file percentage within the repo. Signed-off-by: BrookJeynes --- appview/pages/templates/layouts/repobase.html | 56 +++++++++++-------- appview/pages/templates/repo/index.html | 11 ++++ 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/appview/pages/templates/layouts/repobase.html b/appview/pages/templates/layouts/repobase.html index ef6f4b4..33eb307 100644 --- a/appview/pages/templates/layouts/repobase.html +++ b/appview/pages/templates/layouts/repobase.html @@ -1,29 +1,32 @@ {{ define "title" }}{{ .RepoInfo.FullName }}{{ end }} {{ define "content" }} -
- {{ if .RepoInfo.Source }} -

-

- {{ i "git-fork" "w-3 h-3 mr-1"}} - forked from - {{ $sourceOwner := didOrHandle .RepoInfo.Source.Did .RepoInfo.SourceHandle }} - {{ $sourceOwner }}/{{ .RepoInfo.Source.Name }} -
-

- {{ end }} -
- +
+ {{ if .RepoInfo.Source }} +

+

+ {{ i "git-fork" "w-3 h-3 mr-1"}} + forked from + {{ $sourceOwner := didOrHandle .RepoInfo.Source.Did .RepoInfo.SourceHandle }} + {{ $sourceOwner }}/{{ .RepoInfo.Source.Name }} +
+

+ {{ end }} +
+ - {{ template "repo/fragments/repoActions" .RepoInfo }} -
- {{ template "repo/fragments/repoDescription" . }} -
-
+ {{ template "repo/fragments/repoActions" .RepoInfo }} +
+ {{ template "repo/fragments/repoDescription" . }} +
+ +
+ {{ if .Languages }} +
+ {{ block "repoLanguages" . }}{{ end }} +
+ {{ end }}
{{ block "repoContent" . }}{{ end }}
diff --git a/appview/pages/templates/repo/index.html b/appview/pages/templates/repo/index.html index a06e3f6..1cb5a41 100644 --- a/appview/pages/templates/repo/index.html +++ b/appview/pages/templates/repo/index.html @@ -7,6 +7,17 @@ {{ template "repo/fragments/og" (dict "RepoInfo" .RepoInfo) }} {{ end }} +{{ define "repoLanguages" }} +
+ {{ range $value := .Languages }} +
+ {{ end }} +
+{{ end }} {{ define "repoContent" }}
-- 2.43.0