Colors in JavaScript console

前端 未结 27 2121
说谎
说谎 2020-11-22 11:42

Can Chrome\'s built-in JavaScript console display colors?

I want errors in red, warnings in orange and console.log\'s in green. Is that possible?

27条回答
  •  有刺的猬
    2020-11-22 12:16

    I doubt that anyone will actually see it but I have a simple solution for those who want to mix several colors in the same line:

    export enum Colors {
        Black = '\033[30m',
        Red = '\x1b[31m',
        Green = '\x1b[32m',
        Yellow = '\x1b[33m',
        Blue = '\033[34m',
        Magenta = '\033[35m',
        Cyan = '\033[36m',
        White = '\033[37m'
    }
    
    
    function color(text: string, color: color: Colors) {
        return `${color}${text}\x1b[0m`;
    }
    
    
    console.log(`This is ${color('green text', Colors.Green)} but this is black. This is red ${color('red', Colors.Red)} etc`);
    

提交回复
热议问题