From 143f54a6e9114bdbfdb321303b5cfa9b3f52fe15 Mon Sep 17 00:00:00 2001 From: Rahul Yadav Date: Thu, 5 Sep 2024 17:13:28 +0530 Subject: [PATCH] fix: convert HTML logs to ANSI format (#102) --- .../workspace/BuildProject/BuildProject.tsx | 9 +++++-- src/utility/utils.ts | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/components/workspace/BuildProject/BuildProject.tsx b/src/components/workspace/BuildProject/BuildProject.tsx index 7af0c51..0936201 100644 --- a/src/components/workspace/BuildProject/BuildProject.tsx +++ b/src/components/workspace/BuildProject/BuildProject.tsx @@ -15,6 +15,7 @@ import { buildTs } from '@/utility/typescriptHelper'; import { delay, getFileExtension, + htmlToAnsi, isIncludesTypeCellOrSlice, tonHttpEndpoint, } from '@/utility/utils'; @@ -290,7 +291,9 @@ const BuildProject: FC = ({ projectId, contract, updateContract }) => { const wallet = await blockchain.treasury('user'); globalWorkspace.sandboxWallet = wallet; createLog( - `Sandbox account created. Address: ${wallet.address.toString()}`, + htmlToAnsi( + `Sandbox account created. Address: ${wallet.address.toString()}`, + ), 'info', false, ); @@ -312,7 +315,9 @@ const BuildProject: FC = ({ projectId, contract, updateContract }) => { environment: environment.toLowerCase(), }); createLog( - `Contract deployed on ${environment}
Contract address: ${_contractAddress}}`, + htmlToAnsi( + `Contract deployed on ${environment}
Contract address: ${_contractAddress}`, + ), 'success', ); diff --git a/src/utility/utils.ts b/src/utility/utils.ts index bd765ef..fff7834 100644 --- a/src/utility/utils.ts +++ b/src/utility/utils.ts @@ -88,6 +88,32 @@ export const getContractURL = ( }tonviewer.com/${contractAddress}`; }; +export const htmlToAnsi = (html: string) => { + // Replace and tags with ANSI escape codes for bold and italic + html = html.replace(/(.*?)<\/b>/g, '\x1b[1m$1\x1b[22m'); // Bold + html = html.replace(/(.*?)<\/i>/g, '\x1b[3m$1\x1b[23m'); // Italic + + // Replace elements with ANSI escape codes for color + html = html.replace( + /(.*?)<\/span>/g, + (_, color, content) => { + const colorMap: Record = { + red: '\x1b[31m', + green: '\x1b[32m', + yellow: '\x1b[33m', + blue: '\x1b[34m', + reset: '\x1b[0m', + }; + return `${colorMap[color]}${content}${colorMap.reset}`; + }, + ); + + // Remove other HTML tags + html = html.replace(/<\/?[^>]+(>|$)/g, ''); + + return html; +}; + export const getFileNameFromPath = (filePath: string): string => { const pathArray = filePath.split('/');