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);

  • 相关阅读:
    巧用数据流让 Word 文档在线阅读
    《开源公开课分享》:Java开源框架案例分享
    存储过程
    opencv求取RGB分量
    opencv中的矩阵操作
    三维高斯模型 opencv实现
    线性均值滤波和中值滤波的比较
    opencv 画延长线
    MATLAB曲线绘制
    如何在博客园发博客时插入优酷视频
  • 原文地址:https://www.cnblogs.com/lexus/p/2486925.html
Copyright © 2011-2022 走看看