Skip to content

Commit

Permalink
Support hide_progress for top-langs feature (#2514)
Browse files Browse the repository at this point in the history
* Add support for hide_progress in top languages feature

* Fix mistake

* Add documents for all languages

* Remove unnecessary value check

* Update top-languages-card.js

* Revert document for all languages except English

* Update documentation

* Update documentation

---------

Co-authored-by: Zohan Subhash <[email protected]>
  • Loading branch information
amirhakimnejad and Zo-Bro-23 authored Feb 16, 2023
1 parent 888663a commit ba7c2f8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
2 changes: 2 additions & 0 deletions api/top-langs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default async (req, res) => {
border_radius,
border_color,
disable_animations,
hide_progress,
} = req.query;
res.setHeader("Content-Type", "image/svg+xml");

Expand Down Expand Up @@ -77,6 +78,7 @@ export default async (req, res) => {
border_color,
locale: locale ? locale.toLowerCase() : null,
disable_animations: parseBoolean(disable_animations),
hide_progress: parseBoolean(hide_progress),
}),
);
} catch (err) {
Expand Down
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ You can provide multiple comma-separated values in the bg_color option to render
- `exclude_repo` - Exclude specified repositories _(Comma-separated values)_. Default: `[] (blank array)`.
- `custom_title` - Sets a custom title for the card _(string)_. Default `Most Used Languages`.
- `disable_animations` - Disables all animations in the card _(boolean)_. Default: `false`.
- `hide_progress` - It uses the compact layout option, hides percentages, and removes the bars. Default: `false`.

> **Warning**
> Language names should be URI-escaped, as specified in [Percent Encoding](https://en.wikipedia.org/wiki/Percent-encoding)
Expand Down Expand Up @@ -398,6 +399,14 @@ You can use the `&layout=compact` option to change the card design.
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&layout=compact)](https://github.com/anuraghazra/github-readme-stats)
```

### Hide Progress Bars

You can use the `&hide_progress=true` option to hide the percentages and the progress bars (layout will be automatically set to `compact`).

```md
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&hide_progress=true)](https://github.com/anuraghazra/github-readme-stats)
```

### Demo

[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra)](https://github.com/anuraghazra/github-readme-stats)
Expand All @@ -406,6 +415,10 @@ You can use the `&layout=compact` option to change the card design.

[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&layout=compact)](https://github.com/anuraghazra/github-readme-stats)

- Hidden progress bars

[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&hide_progress=true)](https://github.com/anuraghazra/github-readme-stats)

# Wakatime Week Stats

Change the `?username=` value to your [Wakatime](https://wakatime.com) username.
Expand Down
39 changes: 28 additions & 11 deletions src/cards/top-languages-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ const createProgressTextNode = ({ width, color, name, progress, index }) => {
* @param {object} props Function properties.
* @param {Lang} props.lang Programming language object.
* @param {number} props.totalSize Total size of all languages.
* @param {boolean} props.hideProgress Whether to hide percentage.
* @param {number} props.index Index of the programming language.
* @returns {string} Compact layout programming language SVG node.
*/
const createCompactLangNode = ({ lang, totalSize, index }) => {
const createCompactLangNode = ({ lang, totalSize, hideProgress, index }) => {
const percentage = ((lang.size / totalSize) * 100).toFixed(2);
const staggerDelay = (index + 3) * 150;
const color = lang.color || "#858585";
Expand All @@ -88,7 +89,7 @@ const createCompactLangNode = ({ lang, totalSize, index }) => {
<g class="stagger" style="animation-delay: ${staggerDelay}ms">
<circle cx="5" cy="6" r="5" fill="${color}" />
<text data-testid="lang-name" x="15" y="10" class='lang-name'>
${lang.name} ${percentage}%
${lang.name} ${hideProgress ? "" : percentage + "%"}
</text>
</g>
`;
Expand All @@ -100,9 +101,10 @@ const createCompactLangNode = ({ lang, totalSize, index }) => {
* @param {object[]} props Function properties.
* @param {Lang[]} props.langs Array of programming languages.
* @param {number} props.totalSize Total size of all languages.
* @param {boolean} props.hideProgress Whether to hide percentage.
* @returns {string} Programming languages SVG node.
*/
const createLanguageTextNode = ({ langs, totalSize }) => {
const createLanguageTextNode = ({ langs, totalSize, hideProgress }) => {
const longestLang = getLongestLang(langs);
const chunked = chunkArray(langs, langs.length / 2);
const layouts = chunked.map((array) => {
Expand All @@ -111,6 +113,7 @@ const createLanguageTextNode = ({ langs, totalSize }) => {
createCompactLangNode({
lang,
totalSize,
hideProgress,
index,
}),
);
Expand Down Expand Up @@ -160,9 +163,10 @@ const renderNormalLayout = (langs, width, totalLanguageSize) => {
* @param {Lang[]} langs Array of programming languages.
* @param {number} width Card width.
* @param {number} totalLanguageSize Total size of all languages.
* @param {boolean} hideProgress Whether to hide progress bar.
* @returns {string} Compact layout card SVG object.
*/
const renderCompactLayout = (langs, width, totalLanguageSize) => {
const renderCompactLayout = (langs, width, totalLanguageSize, hideProgress) => {
const paddingRight = 50;
const offsetWidth = width - paddingRight;
// progressOffset holds the previous language's width and used to offset the next language
Expand Down Expand Up @@ -193,15 +197,21 @@ const renderCompactLayout = (langs, width, totalLanguageSize) => {
.join("");

return `
<mask id="rect-mask">
${
!hideProgress
? `
<mask id="rect-mask">
<rect x="0" y="0" width="${offsetWidth}" height="8" fill="white" rx="5"/>
</mask>
${compactProgressBar}
<g transform="translate(0, 25)">
`
: ""
}
<g transform="translate(0, ${hideProgress ? "0" : "25"})">
${createLanguageTextNode({
langs,
totalSize: totalLanguageSize,
hideProgress: hideProgress,
})}
</g>
`;
Expand Down Expand Up @@ -276,6 +286,7 @@ const renderTopLanguages = (topLangs, options = {}) => {
text_color,
bg_color,
hide,
hide_progress,
theme,
layout,
custom_title,
Expand Down Expand Up @@ -305,11 +316,17 @@ const renderTopLanguages = (topLangs, options = {}) => {
let height = calculateNormalLayoutHeight(langs.length);

let finalLayout = "";
if (layout === "compact") {
if (layout === "compact" || hide_progress == true) {
width = width + 50; // padding
height = calculateCompactLayoutHeight(langs.length);

finalLayout = renderCompactLayout(langs, width, totalLanguageSize);
height =
calculateCompactLayoutHeight(langs.length) + (hide_progress ? -25 : 0);

finalLayout = renderCompactLayout(
langs,
width,
totalLanguageSize,
hide_progress,
);
} else {
finalLayout = renderNormalLayout(langs, width, totalLanguageSize);
}
Expand Down
1 change: 1 addition & 0 deletions src/cards/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type TopLangOptions = CommonOptions & {
custom_title: string;
langs_count: number;
disable_animations: boolean;
hide_progress: boolean;
};

type WakaTimeOptions = CommonOptions & {
Expand Down

0 comments on commit ba7c2f8

Please sign in to comment.