【问题标题】:How to print box around characters in terminal - Node.js如何在终端中的字符周围打印框 - Node.js
【发布时间】:2021-02-16 20:27:09
【问题描述】:

我目前有这些代码行可以在终端的框中打印信息:

  `
  \t\t   ╭───────────────────────────────────────╮
  \t\t   │                                       │
  \t\t   │   Server @ ${version}                      │
  \t\t   │                                       │
  \t\t   │   ▸ Name      :   ${serverName}             │
  \t\t   │   ▸ Network   :   ${isNetwork ? "Yes" : "No"}                 │
  \t\t   │   ▸ Internet  :   ${isInternet ? "Yes" : "No"}                 │
  \t\t   │   ▸ Device IP :   ${ip.address()}       │
  \t\t   │                                       │
  \t\t   │                                       │
  \t\t   │   ${`Listening: http://localhost:${port}/`)}   |
  \t\t   │                                       │
  \t\t   ╰───────────────────────────────────────╯
  `

上面的行给出了下面的结果,我手动移动了这些行。

           ╭───────────────────────────────────────╮
           │                                       │
           │   Server @ 1.1.0                      │
           │                                       │
           │   ▸ Name      :   Equinox             │
           │   ▸ Network   :   Yes                 │
           │   ▸ Internet  :   Yes                 │
           │   ▸ Device IP :   x.x.x.x             │
           │                                       │
           │                                       │
           │   Listening: http://localhost:6500/   |
           │                                       │
           ╰───────────────────────────────────────╯

有没有办法以编程方式插入框,这样我就不必计算字符并手动放置行?

【问题讨论】:

    标签: node.js terminal


    【解决方案1】:

    如果您可以添加依赖项,node-cli-box 之类的东西可以解决问题:

    var Box = require("cli-box");
    
    const version = 'v1.0.0';
    const serverName = 'ServerName';
    const isNetwork = true;
    const isInternet = true;
    const ip = {
      address: function() { return '127.0.0.1' }
    };
    const port = 8080;
    
    const myBox = new Box({
      w: 50,
      h: 10,
      stringify: false,
      marks: {
        nw: '╭',
        n: '─',
        ne: '╮',
        e: '│',
        se: '╯',
        s: '─',
        sw: '╰',
        w: '│'
      },
      hAlign: 'left',
    }, `Server @ ${version}
    ▸ Name      :   ${serverName}
    ▸ Network   :   ${isNetwork ? "Yes" : "No"}
    ▸ Internet  :   ${isInternet ? "Yes" : "No"}
    ▸ Device IP :   ${ip.address()}
    
    Listening: http://localhost:${port}/`);
    console.log(myBox.stringify());
    

    Repl.it

    【讨论】:

      【解决方案2】:

      从算法上讲,这样做的方法是确定将出现在框中的最长文本行的长度,然后将边框的长度设置为围绕它填充。但是,似乎确实有一个名为 node-cli-box 的 Node 模块可以为您执行此操作。

      【讨论】:

        猜你喜欢
        • 2013-11-27
        • 2013-02-24
        • 2015-05-23
        • 1970-01-01
        • 1970-01-01
        • 2021-05-23
        • 1970-01-01
        • 2021-09-10
        • 2012-06-27
        相关资源
        最近更新 更多