最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • Node 系列 - 005 - colors.js

    正文概述 掘金(jsliang)   2021-06-17   329

    接入 commander.jsInquirer.js 之后,本应该直接接上 colors.js,毕竟我们现在是控制台输出,控制台不搞得飘飘亮亮(花里胡哨的)。

    但是上篇 Inquires.js 太给力了,直接上了 1.7w 字,所以相对而言,这篇就简短点呗。

    一 目录

    不折腾的前端,和咸鱼有什么区别

    目录
    一 目录二 前言三 colors.js四 重写 console.log五 参考文献

    二 前言

    colors.js 是 Marak 做的一个 4.1k star(2021-06-16)的仓库。

    接入 colors.js 后可以让你的控制台更爆炸更有美感。

    • 安装:npm i colors
    • 输入代码:
    import program from 'commander';
    import common from './common';
    import colors from 'colors';
    
    program
      .version('0.0.1')
      .description('工具库')
    
    program
      .command('jsliang')
      .description('jsliang 帮助指令')
      .action(() => {
        common();
      });
    
    program
      .command('test')
      .description('测试频道')
      .action(() => {
        const text = `
          _   _____   _       _       ___   __   _   _____  
         | | /  ___/ | |     | |     /   | |  \\ | | /  ___| 
         | | | |___  | |     | |    / /| | |   \\| | | |     
      _  | | \\___  \\ | |     | |   / /—| | | |\\   | | |  _  
     | |_| |  ___| | | |___  | |  / /  | | | | \\  | | |_| |
     \\_____/ /_____/ |_____| |_| /_/   |_| |_|  \\_| \\_____/
        `;
        console.log(colors.rainbow(text));
      });
    
    program.parse(process.argv);
    
    {
      "name": "jsliang",
      "version": "1.0.0",
      "description": "Fe-util, Node 工具库",
      "main": "index.js",
      "scripts": {
        "jsliang": "ts-node ./src/index.ts jsliang",
        "test": "ts-node ./src/index.ts test"
      },
      "keywords": [
        "jsliang",
        "Node 工具库",
        "Node"
      ],
      "author": "jsliang",
      "license": "ISC",
      "devDependencies": {
        "@types/inquirer": "^7.3.1",
        "@types/node": "^15.12.2",
        "@typescript-eslint/eslint-plugin": "^4.26.1",
        "@typescript-eslint/parser": "^4.26.1",
        "eslint": "^7.28.0",
        "ts-node": "^10.0.0",
        "typescript": "^4.3.2"
      },
      "dependencies": {
        "colors": "^1.4.0",
        "commander": "^7.2.0",
        "inquirer": "^8.1.0",
        "rxjs": "^5.5.12"
      }
    }
    
    
    • 执行 npm run test

    发现控制台老漂亮了:

    Node 系列 - 005 - colors.js

    在上面代码中,添加了 test 相关指令(后续我们测试内容就塞到这里,可以不用加,但是 jsliang 会拿来做示例用)

    至于这个好看的字体,就是通过 ASCII 艺术字转换器转换过来的。

    • Kalvin 在线工具
    • ASCII 字形生成器

    这边随意推荐 2 个,更多的小伙伴可以自行挖掘。

    三 colors.js

    工欲善其事,必先利其器。

    在上面我们展示了 colors.js 中的一种彩虹色 colors.rainbow,那么肯定会有其他颜色。

    import colors from 'colors';
    
    console.log(colors.rainbow('rainbow'));
    console.log(colors.black('black'));
    console.log(colors.red('red'));
    console.log(colors.green('green'));
    console.log(colors.yellow('yellow'));
    console.log(colors.blue('blue'));
    console.log(colors.magenta('magenta'));
    console.log(colors.cyan('cyan'));
    console.log(colors.white('white'));
    console.log(colors.gray('gray'));
    console.log(colors.grey('grey'));
    console.log(colors.bgBlack('bgBlack'));
    console.log(colors.bgRed('bgRed'));
    console.log(colors.bgGreen('bgGreen'));
    console.log(colors.bgYellow('bgYellow'));
    console.log(colors.bgBlue('bgBlue'));
    console.log(colors.bgMagenta('bgMagenta'));
    console.log(colors.bgCyan('bgCyan'));
    console.log(colors.bgWhite('bgWhite'));
    console.log(colors.bgGrey('bgGrey'));
    console.log(colors.reset('reset'));
    console.log(colors.bold('bold'));
    console.log(colors.dim('dim'));
    console.log(colors.italic('italic'));
    console.log(colors.underline('underline'));
    console.log(colors.inverse('inverse'));
    console.log(colors.hidden('hidden'));
    console.log(colors.strikethrough('strikethrough'));
    console.log(colors.rainbow('rainbow'));
    console.log(colors.zebra('zebra'));
    console.log(colors.america('america'));
    console.log(colors.trap('trap'));
    console.log(colors.random('random'));
    

    将它们丢到 test 中,执行 npm run test,得到花里花哨的打印:

    Node 系列 - 005 - colors.js

    四 重写 console.log

    OK,在上面我们已经华丽呼哨了,每次打印如果都要引用一遍 colors 那就有点说不过去啦。

    所以咱们重写 console.log,这样只要有地方用到它了我们就有彩虹色了!

    /**
     * @name getType
     * @description 获取类型
     * @param {string|object} param 传入的变量
     */
    export const getType = (param: string): string => {
      return Object.prototype.toString.call(param).slice(8, -1);
    };
    
    import colors from 'colors';
    import { getType } from './getType';
    
    // 打印索引
    let consoleIndex = 1;
    
    // 重写 console.log
    const log = console.log;
    console.log = (...args: any) => {
      log(`\n---${consoleIndex++}---`);
      for (let i = 0; i < args.length; i++) {
        const arg = args[i];
        if (['String', 'Number', 'Boolean'].includes(getType(arg))) {
          log(colors.rainbow(String(arg)));
        } else {
          log(arg);
        }
      }
    };
    
    // 重写 console.error
    const error = console.error;
    console.error = (...args: any) => {
      log(`\n---${consoleIndex++}---`);
      for (let i = 0; i < args.length; i++) {
        const arg = args[i];
        if (['String', 'Number', 'Boolean'].includes(getType(arg))) {
          error(colors.red(String(arg)));
        } else {
          error(arg);
        }
      }
    };
    

    然后在 src/index.ts 中引用这个重写的 console.ts,这样全局就可以使用到了:

    import program from 'commander';
    import common from './common';
    import './base/console';
    
    program
      .version('0.0.1')
      .description('工具库')
    
    program
      .command('jsliang')
      .description('jsliang 帮助指令')
      .action(() => {
        common();
      });
    
    program
      .command('test')
      .description('测试频道')
      .action(() => {
        console.log('There is jsliang?', true);
        console.error('随便报个错,表明它有问题');
      });
    
    program.parse(process.argv);
    

    这时候运行 npm run test 打印效果为:

    Node 系列 - 005 - colors.js

    那么,花里花哨的接入就完毕了,虽然都是 API 复制粘贴工程师,但是做下装饰搞好看一点还是可以有的~

    下一篇见!

    五 参考文献

    • GitHub:Marak/colors.js
    • Kalvin 在线工具
    • ASCII 字形生成器

    不折腾的前端,和咸鱼有什么区别!


    起源地下载网 » Node 系列 - 005 - colors.js

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元