GithubHelp home page GithubHelp logo

Comments (2)

Elius94 avatar Elius94 commented on May 27, 2024

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.

Elius94 avatar Elius94 commented on May 27, 2024

Now colors are managed with the new drawing algorytm

from console-gui-tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.