Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit ad4870d

Browse files
committed
1.15
1 parent 3daa155 commit ad4870d

4 files changed

Lines changed: 37 additions & 42 deletions

File tree

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@
55
</div>
66

77
# 🍏 Quickstart
8-
*Use [Replit](https://replit.com/@zmh-program/code-stats) Server **[stats.deeptrain.net](https://stats.deeptrain.net/)***
8+
##### Rapid generation 👉 **[stats.deeptrain.net](https://stats.deeptrain.net/)** 👈
99

1010

11-
Example:
12-
1311
### 🍊 Repo
12+
*/repo/**username**/**repo**/*
1413
```markdown
1514
[![code statistic](https://stats.deeptrain.net/repo/zmh-program/Zh-Website/)](https://github.com/zmh-program/code-statistic)
1615
```
1716
[![code statistic](https://stats.deeptrain.net/repo/zmh-program/Zh-Website/)](https://github.com/zmh-program/code-statistic)
1817

1918
### 🍉 User
19+
*/user/**username**/*
2020
```markdown
2121
[![code statistic](https://stats.deeptrain.net/user/zmh-program/)](https://github.com/zmh-program/code-statistic)
2222
```
2323
[![code statistic](https://stats.deeptrain.net/user/zmh-program/)](https://github.com/zmh-program/code-statistic)
2424

2525
### 🥝 Dark Mode
26+
*.../**?theme=dark***
2627
```markdown
2728
[![code statistic](https://stats.deeptrain.net/repo/zmh-program/Zh-Website/?theme=dark)](https://github.com/zmh-program/code-statistic)
2829
```
@@ -47,6 +48,9 @@ npm install
4748
### 🍬 Configuration
4849
https://github.com/zmh-program/code-statistic/blob/f9b182767796fc5aef57d241fb5910a1dd293dbd/config.js#L1-L6
4950

51+
### 🎄 Environment Variable
52+
`CODE_STATISTIC`: your GitHub Access Token
53+
5054
### 🍇 Run
5155
```shell
5256
node index

stats.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,6 @@ function storeConvert(size, idx=0) {
562562
if (size <= 0) {
563563
return "0";
564564
}
565-
566565
while (idx < (store_units.length - 1) && size > 1024) {
567566
size /= 1024;
568567
idx ++;
@@ -577,6 +576,17 @@ function decConvert(n, allowed_pre=true) {
577576
return idx === 0 ? n : n.toFixed(1) + dec_units[idx];
578577
}
579578

579+
function sum(arr) {
580+
switch (arr.length) {
581+
case 0 :
582+
return 0;
583+
case 1:
584+
return arr[0];
585+
default:
586+
return arr.reduce((a, b) => a + b);
587+
}
588+
}
589+
580590
async function getLanguage(user, repo) {
581591
return await cache.requestWithCache(`/repos/${user}/${repo}/languages`);
582592
}
@@ -598,16 +608,15 @@ async function langStatistics(queue) {
598608
}
599609

600610
function langHandler(langs) {
601-
langs = {...langs}; // 这里有一个很神奇的关于指针的Bug的回忆
602-
// (缓存与操作的对象指向一个Array, 导致了奇妙的事情发生, 有意者可以亲自拉下代码复刻这一[特性])
603-
const val_arr = sort(Object.values(langs)).reverse();
604-
const total = val_arr.length ? val_arr.reduce((before, after) => before + after) : 0;
611+
langs = {...langs}; // deep copy
612+
const arr = sort(Object.values(langs)).reverse();
613+
const total = sum(arr);
605614
for (let k in langs) {
606615
langs[langs[k]] = k;
607616
delete langs[k];
608617
}
609618
let cursor = 0;
610-
return val_arr.map(key => {
619+
return arr.map(key => {
611620
const lang = langs[key];
612621
const ratio = key / total;
613622
cursor += ratio;
@@ -626,10 +635,10 @@ async function getAccount(username, dark=false) {
626635
return {
627636
dark : dark,
628637
locs: response['location'],
629-
stars: repos.map(repo => repo['stargazers_count']).reduce((a, b) => a + b),
630-
forks: repos.map(repo => repo['forks_count']).reduce((a, b) => a + b),
631-
issues: repos.map(repo => repo['open_issues_count']).reduce((a, b) => a + b),
632-
watchers: repos.map(repo => repo['watchers_count']).reduce((a, b) => a + b),
638+
stars: decConvert(sum(repos.map(repo => repo['stargazers_count']))),
639+
forks: decConvert(sum(repos.map(repo => repo['forks_count']))),
640+
issues: decConvert(sum(repos.map(repo => repo['open_issues_count']))),
641+
watchers: decConvert(sum(repos.map(repo => repo['watchers_count']))),
633642
username: username,
634643
followers: decConvert(response['followers']),
635644
repos: response['public_repos'],
@@ -641,18 +650,18 @@ async function getAccount(username, dark=false) {
641650
}
642651

643652
async function getRepository(username, repo, dark=false) {
644-
// get releases (700ms): (await cache.requestWithCache(`/repos/${username}/${repo}/releases`)).length
645-
const info = await cache.requestWithCache(`/repos/${username}/${repo}`);
653+
const res = await cache.requestWithCache(`/repos/${username}/${repo}`);
654+
const license = res['license'];
646655
return {
647656
dark : dark,
648657
username: username,
649658
repo: repo,
650-
size: storeConvert(info['size'], 1),
651-
forks: decConvert(info['forks']),
652-
stars: decConvert(info['stargazers_count']),
653-
watchers: decConvert(info['watchers_count']),
654-
license: info['license']['spdx_id'],
655-
color: lang_colors[info['language']],
659+
size: storeConvert(res['size'], 1),
660+
forks: decConvert(res['forks']),
661+
stars: decConvert(res['stargazers_count']),
662+
watchers: decConvert(res['watchers_count']),
663+
license: license ? license['spdx_id'] : "Empty",
664+
color: lang_colors[res['language']],
656665
langs: langHandler(await getLanguage(username, repo)),
657666
};
658667
}

utils.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

views/repo.ejs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<svg width="450" height="195" viewBox="0 0 660 195" fill="none" xmlns="http://www.w3.org/2000/svg" role="img" aria-labelledby="descId">
1+
<% const left = Math.ceil(langs.length / 2); const height = 215 + (left > 4 ? (left - 4) * 20 : 0) %>
2+
<svg width="450" height="195" viewBox="0 0 660 <%- height + 1 %>" fill="none" xmlns="http://www.w3.org/2000/svg" role="img" aria-labelledby="descId">
23
<title id="titleId"><%- repo %>'s Code Stats</title>
34
<desc id="descId">Repository Code Statistics</desc>
45
<style>
@@ -133,7 +134,7 @@
133134
</g>
134135
</g>
135136
</svg></g>
136-
<line x1="290" y1="40" x2="290" y2="186" stroke="#eeecec"></line>
137+
<line x1="290" y1="40" x2="290" y2="<%- height - 40 %>" stroke="#eeecec"></line>
137138
<g xmlns="http://www.w3.org/2000/svg" transform="translate(300, 55)" data-testid="main-card-progress">
138139

139140
<svg data-testid="lang-items" x="25">
@@ -145,7 +146,6 @@
145146
<rect mask="url(#rect-mask)" data-testid="lang-progress" x="<%- lang.cursor * 300 %>" y="0" width="<%- lang.ratio * 300 %>" height="8" fill="<%- lang.color %>"/>
146147
<% }); %>
147148
<g transform="translate(0, 25)">
148-
<% const left = Math.ceil(langs.length / 2) %>
149149
<g transform="translate(0, 0)">
150150
<% langs.splice(0, left).forEach(function (lang, idx) { %>
151151
<g transform="translate(0, <%- idx * 25 %>)">

0 commit comments

Comments
 (0)