Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1a3edca

Browse files
MorphclueClashsoftanuraghazra
authored
feat: hide wakatime languages (anuraghazra#1212)
* feat: added option to hide languages* feat: recalculate percentages for hidden languages* refactor: reverted langs_count and did some formatting* doc: added hide to readme* feat: make languages var mutable and improve filter logicCo-authored-by: Adrian Kunz <clashsoft@hotmail.com>* refactor: improve code and added testsCo-authored-by: Adrian Kunz <clashsoft@hotmail.com>Co-authored-by: Anurag <hazru.anurag@gmail.com>
1 parent8f31634 commit1a3edca

File tree

8 files changed

+82
-37
lines changed

8 files changed

+82
-37
lines changed

‎api/wakatime.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {
33
renderError,
44
parseBoolean,
55
clampValue,
6+
parseArray,
67
CONSTANTS,
78
isLocaleAvailable,
89
}=require("../src/common/utils");
@@ -26,6 +27,7 @@ module.exports = async (req, res) => {
2627
locale,
2728
layout,
2829
langs_count,
30+
hide,
2931
api_domain,
3032
range,
3133
border_radius,
@@ -58,6 +60,7 @@ module.exports = async (req, res) => {
5860
custom_title,
5961
hide_title:parseBoolean(hide_title),
6062
hide_border:parseBoolean(hide_border),
63+
hide:parseArray(hide),
6164
line_height,
6265
title_color,
6366
icon_color,

‎readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ You can provide multiple comma-separated values in bg_color option to render a g
212212
213213
####Wakatime Card Exclusive Options:
214214

215+
-`hide` - Hide the languages specified from the card_(Comma-separated values)_
215216
-`hide_title` -_(boolean)_
216217
-`line_height` - Sets the line-height between text_(number)_
217218
-`hide_progress` - Hides the progress bar and percentage_(boolean)_

‎src/cards/top-languages-card.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ const Card = require("../common/Card");
22
constI18n=require("../common/I18n");
33
const{ langCardLocales}=require("../translations");
44
const{ createProgressNode}=require("../common/createProgressNode");
5-
const{ clampValue, getCardColors, flexLayout}=require("../common/utils");
5+
const{
6+
clampValue,
7+
getCardColors,
8+
flexLayout,
9+
lowercaseTrim,
10+
}=require("../common/utils");
611

712
constDEFAULT_CARD_WIDTH=300;
813
constDEFAULT_LANGS_COUNT=5;
914
constDEFAULT_LANG_COLOR="#858585";
1015
constCARD_PADDING=25;
1116

12-
constlowercaseTrim=(name)=>name.toLowerCase().trim();
13-
1417
constcreateProgressTextNode=({ width, color, name, progress})=>{
1518
constpaddingRight=95;
1619
constprogressTextX=width-paddingRight+10;

‎src/cards/wakatime-card.js

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ const { getStyles } = require("../getStyles");
44
const{ wakatimeCardLocales}=require("../translations");
55
constlanguageColors=require("../common/languageColors.json");
66
const{ createProgressNode}=require("../common/createProgressNode");
7-
const{ clampValue, getCardColors, flexLayout}=require("../common/utils");
7+
const{
8+
clampValue,
9+
getCardColors,
10+
flexLayout,
11+
lowercaseTrim,
12+
}=require("../common/utils");
813

914
constnoCodingActivityNode=({ color, text})=>{
1015
return`
@@ -61,34 +66,47 @@ const createTextNode = ({
6166
constcardProgress=hideProgress
6267
?null
6368
:createProgressNode({
64-
x:110,
65-
y:4,
66-
progress:percent,
67-
color:progressBarColor,
68-
width:220,
69-
name:label,
70-
progressBarBackgroundColor,
71-
});
69+
x:110,
70+
y:4,
71+
progress:percent,
72+
color:progressBarColor,
73+
width:220,
74+
name:label,
75+
progressBarBackgroundColor,
76+
});
7277

7378
return`
7479
<g class="stagger" style="animation-delay:${staggerDelay}ms" transform="translate(25, 0)">
75-
<text y="12.5">${label}:</text>
80+
<text y="12.5" data-testid="${id}">${label}:</text>
7681
<text
7782
class="stat"
7883
x="${hideProgress ?170 :350}"
7984
y="12.5"
80-
data-testid="${id}"
8185
>${value}</text>
8286
${cardProgress}
8387
</g>
8488
`;
8589
};
8690

91+
constrecalculatePercentages=(languages)=>{
92+
// recalculating percentages so that,
93+
// compact layout's progress bar does not break when hiding languages
94+
consttotalSum=languages.reduce(
95+
(totalSum,language)=>totalSum+language.percent,
96+
0,
97+
);
98+
constweight=(100/totalSum).toFixed(2);
99+
languages.forEach((language)=>{
100+
language.percent=(language.percent*weight).toFixed(2);
101+
});
102+
};
103+
87104
constrenderWakatimeCard=(stats={},options={hide:[]})=>{
88-
const{ languages}=stats;
105+
let{ languages}=stats;
89106
const{
90107
hide_title=false,
91108
hide_border=false,
109+
hide,
92110
line_height=25,
93111
title_color,
94112
icon_color,
@@ -104,6 +122,15 @@ const renderWakatimeCard = (stats = {}, options = { hide: [] }) => {
104122
border_color,
105123
}=options;
106124

125+
constshouldHideLangs=Array.isArray(hide)&&hide.length>0;
126+
if(shouldHideLangs){
127+
constlanguagesToHide=newSet(hide.map((lang)=>lowercaseTrim(lang)));
128+
languages=languages.filter(
129+
(lang)=>!languagesToHide.has(lowercaseTrim(lang.name)),
130+
);
131+
recalculatePercentages(languages);
132+
}
133+
107134
consti18n=newI18n({
108135
locale,
109136
translations:wakatimeCardLocales,
@@ -131,8 +158,8 @@ const renderWakatimeCard = (stats = {}, options = { hide: [] }) => {
131158

132159
constfilteredLanguages=languages
133160
?languages
134-
.filter((language)=>language.hours||language.minutes)
135-
.slice(0,langsCount)
161+
.filter((language)=>language.hours||language.minutes)
162+
.slice(0,langsCount)
136163
:[];
137164

138165
// Calculate the card height depending on how many items there are
@@ -186,17 +213,16 @@ const renderWakatimeCard = (stats = {}, options = { hide: [] }) => {
186213
</mask>
187214
${compactProgressBar}
188215
${createLanguageTextNode({
189-
x:0,
190-
y:25,
191-
langs:filteredLanguages,
192-
totalSize:100,
193-
}).join("")}
216+
x:0,
217+
y:25,
218+
langs:filteredLanguages,
219+
totalSize:100,
220+
}).join("")}
194221
`;
195222
}else{
196223
finalLayout=flexLayout({
197224
items:filteredLanguages.length
198-
?filteredLanguages
199-
.map((language)=>{
225+
?filteredLanguages.map((language)=>{
200226
returncreateTextNode({
201227
id:language.name,
202228
label:language.name,
@@ -208,11 +234,11 @@ const renderWakatimeCard = (stats = {}, options = { hide: [] }) => {
208234
});
209235
})
210236
:[
211-
noCodingActivityNode({
212-
color:textColor,
213-
text:i18n.t("wakatimecard.nocodingactivity"),
214-
}),
215-
],
237+
noCodingActivityNode({
238+
color:textColor,
239+
text:i18n.t("wakatimecard.nocodingactivity"),
240+
}),
241+
],
216242
gap:lheight,
217243
direction:"column",
218244
}).join("");

‎src/common/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ function measureText(str, fontSize = 10) {
230230
.reduce((cur,acc)=>acc+cur)*fontSize
231231
);
232232
}
233+
constlowercaseTrim=(name)=>name.toLowerCase().trim();
233234

234235
module.exports={
235236
renderError,
@@ -248,4 +249,5 @@ module.exports = {
248249
logger,
249250
CONSTANTS,
250251
CustomError,
252+
lowercaseTrim,
251253
};

‎tests/__snapshots__/renderWakatimeCard.test.js.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,11 @@ exports[`Test Render Wakatime Card should render correctly 1`] = `
9999
<svgx=\\"0\\"y=\\"0\\"width=\\"100%\\">
100100
<gtransform=\\"translate(0,0)\\">
101101
<gclass=\\"stagger\\"style=\\"animation-delay:NaNms\\"transform=\\"translate(25,0)\\">
102-
<textclass=\\"statbold\\"y=\\"12.5\\">Other:</text>
102+
<textclass=\\"statbold\\"y=\\"12.5\\"data-testid=\\"Other\\">Other:</text>
103103
<text
104104
class=\\"stat\\"
105105
x=\\"350\\"
106106
y=\\"12.5\\"
107-
data-testid=\\"Other\\"
108107
>19 mins</text>
109108
110109
<svgwidth=\\"220\\"x=\\"110\\"y=\\"4\\">
@@ -122,12 +121,11 @@ exports[`Test Render Wakatime Card should render correctly 1`] = `
122121
</g>
123122
</g><gtransform=\\"translate(0,25)\\">
124123
<gclass=\\"stagger\\"style=\\"animation-delay:NaNms\\"transform=\\"translate(25,0)\\">
125-
<textclass=\\"statbold\\"y=\\"12.5\\">TypeScript:</text>
124+
<textclass=\\"statbold\\"y=\\"12.5\\"data-testid=\\"TypeScript\\">TypeScript:</text>
126125
<text
127126
class=\\"stat\\"
128127
x=\\"350\\"
129128
y=\\"12.5\\"
130-
data-testid=\\"TypeScript\\"
131129
>1 min</text>
132130
133131
<svgwidth=\\"220\\"x=\\"110\\"y=\\"4\\">

‎tests/renderTopLanguages.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require("@testing-library/jest-dom");
22
constcssToObject=require("css-to-object");
3-
constfetchTopLanguages=require("../src/fetchers/top-languages-fetcher");
43
constrenderTopLanguages=require("../src/cards/top-languages-card");
54

65
const{ queryByTestId, queryAllByTestId}=require("@testing-library/dom");

‎tests/renderWakatimeCard.test.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require("@testing-library/jest-dom");
2-
constrenderWakatimeCard=require("../src/cards/wakatime-card");
2+
const{ queryByTestId}=require("@testing-library/dom");
33

4+
constrenderWakatimeCard=require("../src/cards/wakatime-card");
45
const{ wakaTimeData}=require("./fetchWakatime.test");
56

67
describe("Test Render Wakatime Card",()=>{
@@ -16,6 +17,16 @@ describe("Test Render Wakatime Card", () => {
1617
expect(card).toMatchSnapshot();
1718
});
1819

20+
it("should hide languages when hide is passed",()=>{
21+
document.body.innerHTML=renderWakatimeCard(wakaTimeData.data,{
22+
hide:["YAML","Other"],
23+
});
24+
25+
expect(queryByTestId(document.body,/YAML/i)).toBeNull();
26+
expect(queryByTestId(document.body,/Other/i)).toBeNull();
27+
expect(queryByTestId(document.body,/TypeScript/i)).not.toBeNull();
28+
});
29+
1930
it("should render translations",()=>{
2031
document.body.innerHTML=renderWakatimeCard({},{locale:"cn"});
2132
expect(document.getElementsByClassName("header")[0].textContent).toBe(
@@ -28,9 +39,11 @@ describe("Test Render Wakatime Card", () => {
2839
});
2940

3041
it("should render without rounding",()=>{
31-
document.body.innerHTML=renderWakatimeCard(wakaTimeData.data,{border_radius:"0"});
42+
document.body.innerHTML=renderWakatimeCard(wakaTimeData.data,{
43+
border_radius:"0",
44+
});
3245
expect(document.querySelector("rect")).toHaveAttribute("rx","0");
33-
document.body.innerHTML=renderWakatimeCard(wakaTimeData.data,{});
46+
document.body.innerHTML=renderWakatimeCard(wakaTimeData.data,{});
3447
expect(document.querySelector("rect")).toHaveAttribute("rx","4.5");
3548
});
3649
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp