Fix watermark not displaying correctly for non-english locales

This commit is contained in:
Dev 2024-03-18 21:07:47 +00:00
parent 5d59e4c36e
commit c879e852ed

View File

@ -152,50 +152,45 @@ export class Watermark
{ {
const result = []; const result = [];
// calculate size // Calculate size, add 10% for spacing to the right
const longestLength = this.text.reduce((a, b) => const longestLength = this.text.reduce((a, b) =>
{ {
const a2 = String(a).replace(/[\u0391-\uFFE5]/g, "ab"); const a2 = String(a).replace(/[\u0391-\uFFE5]/g, "ab");
const b2 = String(b).replace(/[\u0391-\uFFE5]/g, "ab"); const b2 = String(b).replace(/[\u0391-\uFFE5]/g, "ab");
return a2.length > b2.length ? a2 : b2; return a.length > b.length ? a : b;
}).length; }).length * 1.1;
// get top-bottom line // Create line of - to add top/bottom of watermark
let line = ""; let line = "";
for (let i = 0; i < longestLength; ++i) for (let i = 0; i < longestLength; ++i)
{ {
line += "─"; line += "─";
} }
// get watermark to draw // Opening line
result.push(`┌─${line}─┐`); result.push(`┌─${line}─┐`);
for (const text of this.text) // Add content of watermark to screen
for (const watermarkText of this.text)
{ {
const spacingSize = longestLength - this.textLength(text); const spacingSize = longestLength - watermarkText.length;
let spacingText = text; let textWithRightPadding = watermarkText;
for (let i = 0; i < spacingSize; ++i) for (let i = 0; i < spacingSize; ++i)
{ {
spacingText += " "; textWithRightPadding += " ";
} }
result.push(`${spacingText}`); result.push(`${textWithRightPadding}`);
} }
// Closing line
result.push(`└─${line}─┘`); result.push(`└─${line}─┘`);
// draw the watermark // Log watermark to screen
for (const text of result) for (const text of result)
{ {
this.logger.logWithColor(text, LogTextColor.YELLOW); this.logger.logWithColor(text, LogTextColor.YELLOW);
} }
} }
/** Caculate text length */
protected textLength(s: string): number
{
return String(s).replace(/[\u0391-\uFFE5]/g, "ab").length;
}
} }