在 Node.js 中获取终端的宽度 [英] Get width of terminal in Node.js

查看:42
本文介绍了在 Node.js 中获取终端的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

console.log(process.env.COLUMNS)

产量 undefined,虽然,

$ echo $COLUMNS

按预期输出:

78

我一直在尝试像这样 env node myprog.js 一样运行 Node,但仍然 undefined.我有什么问题或以任何其他方式知道终端的宽度?(为了一些输出数据的良好格式).

I have been trying running Node like this env node myprog.js, still undefined. What's wrong with me or any other way to know the width of terminal? (For nice formatting of some output data).

推荐答案

console.log('Terminal size: ' + process.stdout.columns + 'x' + process.stdout.rows);

输出看起来像例如终端尺寸:80x24".如果控制台大小发生变化,也会有一个事件.

And the output looks like e.g. "Terminal size: 80x24". There is also an event if the console size changes.

它在文档中的 tty 下进行了解释.

It's explained under tty in the docs.

这篇关于在 Node.js 中获取终端的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆