Comments (2)
It's difficult to replace all color strings from chalk without wrapping it.
I was thinking to use HTML like tags to set colors and style in the text and make the library replacing that substring with the relative chalk function.
The trouble is that the regex method I've tried is too slower and it is blocking.
That's the color class I suppose to do:
class ColorsManager {
constructor() {
}
replaceColorTagsWithChalk(text) {
const colorTags = text.match(/<([a-zA-Z1-6]+)([^<]+)*(?:>(.*)<\/\1>| *\/>)/gmu)
colorTags.forEach(tag => {
const tagName = tag.match(/<([a-zA-Z1-6]+)/)[1]
const tagContent = tag.match(/<([a-zA-Z1-6]+)([^<]+)*(?:>(.*)<\/\1>| *\/>)/)[0]
const tagContentWithoutTag = tag.replace(/<([a-zA-Z1-6]+)([^<]+)*(?:>(.*)<\/\1>| *\/>)/, '$3')
const tagContentWithoutTagWithChalk = chalk[tagName](tagContentWithoutTag)
text = text.replace(tagContent, tagContentWithoutTagWithChalk)
})
return text
}
red(text) {
return `<red>${text}</red>`;
}
green(text) {
return `<green>${text}</green>`;
}
yellow(text) {
return `<yellow>${text}</yellow>`;
}
blue(text) {
return `<blue>${text}</blue>`;
}
magenta(text) {
return `<magenta>${text}</magenta>`;
}
cyan(text) {
return `<cyan>${text}</cyan>`;
}
white(text) {
return `<white>${text}</white>`;
}
gray(text) {
return `<gray>${text}</gray>`;
}
black(text) {
return `<black>${text}</black>`;
}
bgRed(text) {
return `<bgRed>${text}</bgRed>`;
}
bgGreen(text) {
return `<bgGreen>${text}</bgGreen>`;
}
bgYellow(text) {
return `<bgYellow>${text}</bgYellow>`;
}
bgBlue(text) {
return `<bgBlue>${text}</bgBlue>`;
}
bgMagenta(text) {
return `<bgMagenta>${text}</bgMagenta>`;
}
bgCyan(text) {
return `<bgCyan>${text}</bgCyan>`;
}
bgWhite(text) {
return `<bgWhite>${text}</bgWhite>`;
}
bgGray(text) {
return `<bgGray>${text}</bgGray>`;
}
bgBlack(text) {
return `<bgBlack>${text}</bgBlack>`;
}
bold(text) {
return `<bold>${text}</bold>`;
}
italic(text) {
return `<italic>${text}</italic>`;
}
underline(text) {
return `<underline>${text}</underline>`;
}
blink(text) {
return `<blink>${text}</blink>`;
}
isLastClosed(text) {
const lastEndTag = this.findLastEndTag(text)
const lastStartTag = this.findLastStartTag(text)
if (lastEndTag && lastStartTag) {
return lastEndTag.index > -1 && lastEndTag.index > lastStartTag.index
}
return false
}
isFirstOpened(text) {
const firstEndTag = this.findFirstEndTag(text)
const firstStartTag = this.findFirstStartTag(text)
if (firstEndTag && firstStartTag) {
return firstStartTag.index > -1 && firstStartTag.index < firstEndTag.index
}
return false
}
findLastEndTag(text) {
// Find one of the end tags within the text using regex
// return an object with the tag and the index
let lastEndTag = text.match(/<\/[a-zA-Z]+>/g);
if (lastEndTag) {
lastEndTag = lastEndTag[lastEndTag.length - 1];
let lastEndTagIndex = text.lastIndexOf(lastEndTag);
return {
tag: lastEndTag,
index: lastEndTagIndex
}
}
}
findLastStartTag(text) {
// Find one of the start tags within the text using regex
// return an object with the tag and the index
let lastStartTag = text.match(/<[a-zA-Z]+>/g);
if (lastStartTag) {
lastStartTag = lastStartTag[lastStartTag.length - 1];
let lastStartTagIndex = text.lastIndexOf(lastStartTag);
return {
tag: lastStartTag,
index: lastStartTagIndex
}
}
}
findFirstEndTag(text) {
// Find one of the end tags within the text using regex
// return an object with the tag and the index
let firstEndTag = text.match(/<\/[a-zA-Z]+>/g);
if (firstEndTag) {
firstEndTag = firstEndTag[0];
let firstEndTagIndex = text.indexOf(firstEndTag);
return {
tag: firstEndTag,
index: firstEndTagIndex
}
}
}
findFirstStartTag(text) {
// Find one of the start tags within the text using regex
// return an object with the tag and the index
let firstStartTag = text.match(/<[a-zA-Z]+>/g);
if (firstStartTag) {
firstStartTag = firstStartTag[0];
let firstStartTagIndex = text.indexOf(firstStartTag);
return {
tag: firstStartTag,
index: firstStartTagIndex
}
}
}
}
from console-gui-tools.
Now colors are managed with the new drawing algorytm
from console-gui-tools.
Related Issues (20)
- New type of widgets: In page widget HOT 1
- Right border anticipation in some case of ButtonPopup
- In PageBuilder Class: getPageHeight returns the exact number of rows but not the visible rows. Need to fix
- InputPopup numeric should be able to manage decimal numbers
- GUI.log(), GUI.warn(), GUI.info(), GUI.error() functions have issues when the input is a number
- Convert all library to Typescript HOT 1
- Set up Typescript environment to build project for both es2022 and commonjs HOT 3
- Add SingleLayout, TripleLayout and QuadLayout. Each layout should be responsive (also the existing Double Layout)
- Will be enabled the mouse in the future? HOT 4
- Add the possibility to include a Control Widget inside a PageBuilder instance
- Add mouse feature to layout
- Add tab index to Controls to focus them with the keyboard and use keyboard only for them HOT 1
- Make a draggable progressbar to use like "analog" selector HOT 1
- When "enableMouse" is false, any key press cause a crash
- Add "increment" parameter to ProgressBar to define how much increase the value at every input change HOT 6
- Adding ASCII ART Text builder (For inPageBuilder) HOT 5
- Add keyboard key to push button
- [Feature]: Change all class constructors parameters with one single object with optional props
- [Bug]: Input popup doesn't handle special characters properly HOT 1
- [Bug]: OptionPopup - selected prop = "" is considered undefined HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from console-gui-tools.