Skip to content

Commit 359ff46

Browse files
authored
Load all language colours (lowlighter#467)
1 parent 1a52074 commit 359ff46

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

package-lock.json

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"faker": "^5.5.3",
4242
"jimp": "^0.16.1",
4343
"js-yaml": "^4.1.0",
44-
"linguist-js": "^1.6.1",
44+
"linguist-js": "^1.7.0",
4545
"marked": "^3.0.0",
4646
"memory-cache": "^0.2.0",
4747
"minimatch": "^3.0.4",

source/plugins/languages/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ For better results, it's advised to add either your surnames and eventually no-r
6060
plugin_languages_sections: most-used, recently-used # Display most used and recently used languages stats
6161
plugin_languages_indepth: no # Get indepth stats (see documentation before enabling)
6262
plugin_languages_categories: programming # Display only languages that match these categories in most-used section
63-
plugin_languages_categories: markup, programming, data # Display only languages that match these categories in recently-used section
63+
plugin_languages_recent_categories: markup, programming, data # Display only languages that match these categories in recently-used section
6464
plugin_languages_recent_load: 500 # Load up to 500 events to compute recently used stats
6565
plugin_languages_recent_days: 7 # Limit recently used stats to last week
6666
commits_authoring: lowlighter@users.noreply.github.com # Surnames or email addresses used to identify your commits

source/plugins/languages/analyzers.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import linguist from "linguist-js"
44
export async function indepth({login, data, imports, repositories}, {skipped, categories}) {
55

66
//Compute repositories stats from fetched repositories
7-
const results = {total:0, lines:{}, stats:{}, commits:0, files:0, missed:0}
7+
const results = {total:0, lines:{}, stats:{}, colors:{}, commits:0, files:0, missed:0}
88
for (const repository of repositories) {
99
//Skip repository if asked
1010
if ((skipped.includes(repository.name.toLocaleLowerCase())) || (skipped.includes(`${repository.owner.login}/${repository.name}`.toLocaleLowerCase()))) {
@@ -48,7 +48,7 @@ export async function recent({login, data, imports, rest, account}, {skipped = [
4848

4949
//Get user recent activity
5050
console.debug(`metrics/compute/${login}/plugins > languages > querying api`)
51-
const commits = [], pages = Math.ceil(load/100), results = {total:0, lines:{}, stats:{}, commits:0, files:0, missed:0, days}
51+
const commits = [], pages = Math.ceil(load/100), results = {total:0, lines:{}, stats:{}, colors:{}, commits:0, files:0, missed:0, days}
5252
try {
5353
for (let page = 1; page <= pages; page++) {
5454
console.debug(`metrics/compute/${login}/plugins > languages > loading page ${page}`)
@@ -140,6 +140,7 @@ async function analyze({login, imports, data}, {results, path, categories = ["pr
140140
//Gather language data
141141
console.debug(`metrics/compute/${login}/plugins > languages > indepth > running linguist`)
142142
const {results:files, languages:languageResults} = await linguist(path)
143+
Object.assign(results.colors, Object.fromEntries(Object.entries(languageResults.all).map(([lang, {color}]) => [lang, color])))
143144

144145
//Processing diff
145146
const per_page = 1
@@ -212,7 +213,7 @@ if (/languages.analyzers.mjs$/.test(process.argv[1])) {
212213

213214
//Prepare call
214215
const imports = await import("../../app/metrics/utils.mjs")
215-
const results = {total:0, lines:{}, stats:{}, missed:0}
216+
const results = {total:0, lines:{}, colors:{}, stats:{}, missed:0}
216217
console.debug = log => /exited with code null/.test(log) ? console.error(log.replace(/^.*--max-count=(?<step>\d+) --skip=(?<start>\d+).*$/, (_, step, start) => `error: skipped commits ${start} from ${Number(start)+Number(step)}`)) : null
217218

218219
//Analyze repository

source/plugins/languages/index.mjs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default async function({login, data, imports, q, rest, account}, {enabled
3838
//Iterate through user's repositories and retrieve languages data
3939
console.debug(`metrics/compute/${login}/plugins > languages > processing ${data.user.repositories.nodes.length} repositories`)
4040
const languages = {unique, sections, details, indepth, colors:{}, total:0, stats:{}, "stats.recent":{}}
41+
const customColors = {}
4142
for (const repository of data.user.repositories.nodes) {
4243
//Skip repository if asked
4344
if ((skipped.includes(repository.name.toLocaleLowerCase())) || (skipped.includes(`${repository.owner.login}/${repository.name}`.toLocaleLowerCase()))) {
@@ -47,7 +48,10 @@ export default async function({login, data, imports, q, rest, account}, {enabled
4748
//Process repository languages
4849
for (const {size, node:{color, name}} of Object.values(repository.languages.edges)) {
4950
languages.stats[name] = (languages.stats[name] ?? 0) + size
50-
languages.colors[name] = colors[name.toLocaleLowerCase()] ?? color ?? "#ededed"
51+
if (colors[name.toLocaleLowerCase()])
52+
customColors[name] = colors[name.toLocaleLowerCase()]
53+
if (!languages.colors[name])
54+
languages.colors[name] = color
5155
languages.total += size
5256
}
5357
}
@@ -58,12 +62,15 @@ export default async function({login, data, imports, q, rest, account}, {enabled
5862
if ((sections.includes("recently-used"))&&(context.mode === "user")) {
5963
console.debug(`metrics/compute/${login}/plugins > languages > using recent analyzer`)
6064
languages["stats.recent"] = await recent_analyzer({login, data, imports, rest, account}, {skipped, categories:_recent_categories ?? categories, days:_recent_days, load:_recent_load})
65+
Object.assign(languages.colors, languages["stats.recent"].colors)
6166
}
6267

6368
//Indepth mode
6469
if (indepth) {
6570
console.debug(`metrics/compute/${login}/plugins > languages > switching to indepth mode (this may take some time)`)
71+
const existingColors = languages.colors
6672
Object.assign(languages, await indepth_analyzer({login, data, imports, repositories}, {skipped, categories}))
73+
Object.assign(languages.colors, existingColors)
6774
console.debug(`metrics/compute/${login}/plugins > languages > indepth analysis missed ${languages.missed} commits`)
6875
}
6976
}
@@ -74,11 +81,14 @@ export default async function({login, data, imports, q, rest, account}, {enabled
7481
languages[section] = Object.entries(stats).filter(([name]) => !ignored.includes(name.toLocaleLowerCase())).sort(([_an, a], [_bn, b]) => b - a).slice(0, limit).map(([name, value]) => ({name, value, size:value, color:languages.colors[name], x:0})).filter(({value}) => value / total > threshold)
7582
const visible = {total:Object.values(languages[section]).map(({size}) => size).reduce((a, b) => a + b, 0)}
7683
for (let i = 0; i < languages[section].length; i++) {
84+
const {name} = languages[section][i]
7785
languages[section][i].value /= visible.total
7886
languages[section][i].x = (languages[section][i - 1]?.x ?? 0) + (languages[section][i - 1]?.value ?? 0)
79-
languages[section][i].lines = lines[languages[section][i].name] ?? 0
80-
if ((colors[i]) && (!colors[languages[section][i].name.toLocaleLowerCase()]))
87+
languages[section][i].lines = lines[name] ?? 0
88+
if ((colors[i]) && (!colors[name.toLocaleLowerCase()]))
8189
languages[section][i].color = colors[i]
90+
else
91+
languages[section][i].color = customColors[name] ?? languages.colors[name] ?? "#ededed"
8292
}
8393
}
8494

0 commit comments

Comments
 (0)