Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/cards/gist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import {
parseEmojis,
wrapTextMultiline,
encodeHTML,
measureText,
flexLayout,
Expand All @@ -11,7 +10,7 @@ import {
} from "../common/utils.js";
import Card from "../common/Card.js";
import { getCardColors } from "../common/color.js";
import { kFormatter } from "../common/fmt.js";
import { kFormatter, wrapTextMultiline } from "../common/fmt.js";
import { icons } from "../common/icons.js";

/** Import language colors.
Expand Down
3 changes: 1 addition & 2 deletions src/cards/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import { Card } from "../common/Card.js";
import { getCardColors } from "../common/color.js";
import { kFormatter } from "../common/fmt.js";
import { kFormatter, wrapTextMultiline } from "../common/fmt.js";
import { I18n } from "../common/I18n.js";
import { icons } from "../common/icons.js";
import {
encodeHTML,
flexLayout,
measureText,
parseEmojis,
wrapTextMultiline,
iconWithLabel,
createLanguageNode,
clampValue,
Expand Down
56 changes: 32 additions & 24 deletions src/cards/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,36 @@ const RANK_CARD_DEFAULT_WIDTH = 450;
const RANK_ONLY_CARD_MIN_WIDTH = 290;
const RANK_ONLY_CARD_DEFAULT_WIDTH = 290;

/**
* Long locales that need more space for text. Keep sorted alphabetically.
*
* @type {(keyof typeof wakatimeCardLocales["wakatimecard.title"])[]}
*/
const LONG_LOCALES = [
"az",
"bg",
"cs",
"de",
"es",
"fil",
"fr",
"id",
"ml",
"my",
"nl",
"pl",
"pt-br",
"pt-pt",
"ru",
"sr",
"sr-latn",
"sw",
"ta",
"uk-ua",
"uz",
"zh-tw",
];

/**
* Create a stats card text item.
*
Expand Down Expand Up @@ -374,30 +404,8 @@ const renderStatsCard = (stats, options = {}) => {
id: "contribs",
};

const longLocales = [
"az",
"bg",
"de",
"es",
"fil",
"fr",
"id",
"ml",
"my",
"nl",
"pl",
"pt-br",
"pt-pt",
"ru",
"sr",
"sr-latn",
"sw",
"ta",
"uk-ua",
"uz",
"zh-tw",
];
const isLongLocale = locale ? longLocales.includes(locale) : false;
// @ts-ignore
const isLongLocale = locale ? LONG_LOCALES.includes(locale) : false;

// filter out hidden stats defined by user & create the text nodes
const statItems = Object.keys(STATS)
Expand Down
42 changes: 41 additions & 1 deletion src/common/fmt.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// @ts-check

import wrap from "word-wrap";
import { encodeHTML } from "./utils.js";

/**
* Retrieves num with suffix k(thousands) precise to given decimal places.
*
Expand Down Expand Up @@ -47,4 +52,39 @@ const formatBytes = (bytes) => {
return `${(bytes / Math.pow(base, i)).toFixed(1)} ${sizes[i]}`;
};

export { kFormatter, formatBytes };
/**
* Split text over multiple lines based on the card width.
*
* @param {string} text Text to split.
* @param {number} width Line width in number of characters.
* @param {number} maxLines Maximum number of lines.
* @returns {string[]} Array of lines.
*/
const wrapTextMultiline = (text, width = 59, maxLines = 3) => {
const fullWidthComma = ",";
const encoded = encodeHTML(text);
const isChinese = encoded.includes(fullWidthComma);

let wrapped = [];

if (isChinese) {
wrapped = encoded.split(fullWidthComma); // Chinese full punctuation
} else {
wrapped = wrap(encoded, {
width,
}).split("\n"); // Split wrapped lines to get an array of lines
}

const lines = wrapped.map((line) => line.trim()).slice(0, maxLines); // Only consider maxLines lines

// Add "..." to the last line if the text exceeds maxLines
if (wrapped.length > maxLines) {
lines[maxLines - 1] += "...";
}

// Remove empty lines if text fits in less than maxLines lines
const multiLineText = lines.filter(Boolean);
return multiLineText;
};

export { kFormatter, formatBytes, wrapTextMultiline };
1 change: 0 additions & 1 deletion src/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export {
clampValue,
request,
flexLayout,
wrapTextMultiline,
logger,
measureText,
lowercaseTrim,
Expand Down
37 changes: 0 additions & 37 deletions src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import axios from "axios";
import toEmoji from "emoji-name-map";
import wrap from "word-wrap";
import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from "./error.js";
import { getCardColors } from "./color.js";

Expand Down Expand Up @@ -234,41 +233,6 @@ const renderError = ({
`;
};

/**
* Split text over multiple lines based on the card width.
*
* @param {string} text Text to split.
* @param {number} width Line width in number of characters.
* @param {number} maxLines Maximum number of lines.
* @returns {string[]} Array of lines.
*/
const wrapTextMultiline = (text, width = 59, maxLines = 3) => {
const fullWidthComma = ",";
const encoded = encodeHTML(text);
const isChinese = encoded.includes(fullWidthComma);

let wrapped = [];

if (isChinese) {
wrapped = encoded.split(fullWidthComma); // Chinese full punctuation
} else {
wrapped = wrap(encoded, {
width,
}).split("\n"); // Split wrapped lines to get an array of lines
}

const lines = wrapped.map((line) => line.trim()).slice(0, maxLines); // Only consider maxLines lines

// Add "..." to the last line if the text exceeds maxLines
if (wrapped.length > maxLines) {
lines[maxLines - 1] += "...";
}

// Remove empty lines if text fits in less than maxLines lines
const multiLineText = lines.filter(Boolean);
return multiLineText;
};

const noop = () => {};
// return console instance based on the environment
const logger =
Expand Down Expand Up @@ -388,7 +352,6 @@ export {
clampValue,
request,
flexLayout,
wrapTextMultiline,
logger,
measureText,
lowercaseTrim,
Expand Down
3 changes: 2 additions & 1 deletion src/fetchers/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import * as dotenv from "dotenv";
import githubUsernameRegex from "github-username-regex";
import { calculateRank } from "../calculateRank.js";
import { retryer } from "../common/retryer.js";
import { logger, request, wrapTextMultiline } from "../common/utils.js";
import { logger, request } from "../common/utils.js";
import { excludeRepositories } from "../common/envs.js";
import { CustomError, MissingParamError } from "../common/error.js";
import { wrapTextMultiline } from "../common/fmt.js";

dotenv.config();

Expand Down
3 changes: 2 additions & 1 deletion src/fetchers/top-languages.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// @ts-check

import { retryer } from "../common/retryer.js";
import { logger, request, wrapTextMultiline } from "../common/utils.js";
import { logger, request } from "../common/utils.js";
import { excludeRepositories } from "../common/envs.js";
import { CustomError, MissingParamError } from "../common/error.js";
import { wrapTextMultiline } from "../common/fmt.js";

/**
* @typedef {import("axios").AxiosRequestHeaders} AxiosRequestHeaders Axios request headers.
Expand Down
Loading