zoukankan      html  css  js  c++  java
  • a colorful termial output for phantomjs porting from casperjs


    var fs = require('fs');

        var options    = { bold: 1, underscore: 4, blink: 5, reverse: 7, conceal: 8 };
        var foreground = { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37 };
        var background = { black: 40, red: 41, green: 42, yellow: 43, blue: 44, magenta: 45, cyan: 46, white: 47 };
        var styles     = {
            'ERROR':     { bg: 'red', fg: 'white', bold: true },
            'INFO':      { fg: 'green', bold: true },
            'TRACE':     { fg: 'green', bold: true },
            'PARAMETER': { fg: 'cyan' },
            'COMMENT':   { fg: 'yellow' },
            'WARN':   { fg: 'red', bold: true },
            'GREEN_BAR': { fg: 'white', bg: 'green', bold: true },
            'RED_BAR':   { fg: 'white', bg: 'red', bold: true },
            'INFO_BAR':  { bg: 'cyan', fg: 'white', bold: true }
        };

        /**
         * Adds a style to provided text.
         *
         * @param   String  text
         * @param   String  styleName
         * @return  String
         */
        function colorize(text, styleName, pad) {

            return format(text, styles[styleName], pad);
        };

        /**
         * Formats a text using a style declaration object.
         *
         * @param  String  text
         * @param  Object  style
         * @return String
         */
        function format(text, style, pad) {
            //if (fs.isWindows() || !isObject(style)) {
            //    return text;
            //}
            var codes = [];
            if (style.fg && foreground[style.fg]) {
                codes.push(foreground[style.fg]);
            }
            if (style.bg && background[style.bg]) {
                codes.push(background[style.bg]);
            }
            for (var option in options) {
                if (style[option] === true) {
                    codes.push(options[option]);
                }
            }
            // pad
            if (typeof pad === "number" && text.length < pad) {
                text += new Array(pad - text.length + 1).join(' ');
            }
            return "\033[" + codes.join(';') + 'm' + text + "\033[0m";
        };






    var pad=0;
    console.log(colorize("aaa", "ERROR"));
    console.log("zzz");
    phantom.exit(0);

  • 相关阅读:
    C语言与汇编的嵌入式编程:统计字符串中各字符出现的次数
    一个汇编小demo
    C语言与汇编的嵌入式编程:求100以内素数
    TCP网络调试助手上提示错误:“1035 未知错误”的有效解决方法,本人实测确实可行
    一个支持国密SM2/SM3/SM4/SM9/ZUC/SSL的密码工具箱
    DRM(device resource management)介绍
    TODO
    pinctrl(1)——pinctrl子系统的使用
    GPIO使用总结
    Gerrit使用技巧
  • 原文地址:https://www.cnblogs.com/lexus/p/2486925.html
Copyright © 2011-2022 走看看