当前位置:首页 > JavaScript

js 实现终端

2026-02-02 11:44:46JavaScript

使用 Node.js 实现终端功能

Node.js 提供了 readline 模块和 process 标准输入输出流,可以轻松实现终端交互功能。

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('请输入命令: ', (command) => {
  console.log(`执行命令: ${command}`);
  rl.close();
});

添加命令历史记录

通过维护一个数组来存储命令历史,并实现上下箭头浏览历史功能。

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const history = [];
let historyIndex = -1;

rl.on('line', (input) => {
  history.push(input);
  console.log(`执行: ${input}`);
});

process.stdin.on('keypress', (str, key) => {
  if (key.name === 'up' && historyIndex < history.length - 1) {
    historyIndex++;
    rl.write(null, {ctrl: true, name: 'u'});
    rl.write(history[history.length - 1 - historyIndex]);
  }
  if (key.name === 'down' && historyIndex > 0) {
    historyIndex--;
    rl.write(null, {ctrl: true, name: 'u'});
    rl.write(history[history.length - 1 - historyIndex]);
  }
});

实现自动补全功能

利用 readline 的 completer 选项可以添加命令自动补全功能。

const commands = ['help', 'exit', 'clear', 'list'];

const completer = (line) => {
  const hits = commands.filter(c => c.startsWith(line));
  return [hits.length ? hits : commands, line];
};

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  completer
});

添加彩色输出

使用 chalk 库可以为终端输出添加颜色。

const chalk = require('chalk');

console.log(chalk.blue('蓝色信息'));
console.log(chalk.red.bold('红色加粗错误'));
console.log(chalk.green('成功信息'));

实现多行输入

通过检测特定字符(如分号)来判断命令是否结束。

let buffer = '';

rl.on('line', (input) => {
  buffer += input;
  if (input.endsWith(';')) {
    console.log(`执行多行命令: ${buffer}`);
    buffer = '';
  } else {
    console.log('继续输入...');
  }
});

创建自定义提示符

可以动态修改提示符来显示当前工作目录或其他信息。

function setPrompt() {
  rl.setPrompt(`[${process.cwd()}] $ `);
  rl.prompt();
}

rl.on('line', (input) => {
  if (input === 'cd') {
    // 改变目录逻辑
    setPrompt();
  }
});

setPrompt();

处理 SIGINT 信号

优雅地处理 Ctrl+C 中断信号。

rl.on('SIGINT', () => {
  rl.question('确定要退出吗? (y/n) ', (answer) => {
    if (answer.match(/^y(es)?$/i)) rl.close();
    else rl.prompt();
  });
});

实现命令路由

建立一个命令路由系统来处理不同命令。

const commands = {
  help: () => console.log('可用命令: help, exit'),
  exit: () => process.exit(0),
  greet: (args) => console.log(`Hello, ${args[0] || 'stranger'}!`)
};

rl.on('line', (input) => {
  const [cmd, ...args] = input.trim().split(/\s+/);
  if (commands[cmd]) commands[cmd](args);
  else console.log(`未知命令: ${cmd}`);
});

添加异步命令支持

处理需要异步操作的命令。

js 实现终端

const commands = {
  fetch: async (url) => {
    const response = await fetch(url[0]);
    console.log(await response.text());
  }
};

rl.on('line', async (input) => {
  const [cmd, ...args] = input.trim().split(/\s+/);
  if (commands[cmd]) await commands[cmd](args);
});

标签: 终端js
分享给朋友:

相关文章

js实现分页

js实现分页

分页的基本实现思路 在JavaScript中实现分页功能通常需要结合前端和后端逻辑。前端负责渲染分页控件和处理用户交互,后端负责提供分页数据。 前端分页实现 纯前端分页适用于数据量较小的情况,可以直…

js实现图片放大缩小

js实现图片放大缩小

实现图片放大缩小的基础方法 使用CSS的transform: scale()属性可以快速实现图片的放大缩小效果。通过JavaScript动态修改该属性值: const img = document.…

js防抖和节流实现

js防抖和节流实现

防抖(Debounce)的实现 防抖的核心思想是在事件被触发后,延迟执行回调函数。如果在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口大小调整等场景。 function debounce…

js实现变形

js实现变形

使用 CSS 和 JavaScript 实现元素变形 在 JavaScript 中实现变形效果通常结合 CSS 的 transform 属性,通过动态修改该属性实现元素的旋转、缩放、倾斜或平移。 基…

js实现交换

js实现交换

交换变量的方法 在JavaScript中,交换两个变量的值有多种方法。以下是常见的几种实现方式: 使用临时变量 通过引入一个临时变量来存储其中一个变量的值,实现交换: let a = 1; le…

js实现密码

js实现密码

密码强度验证 使用正则表达式验证密码强度是一种常见方法。以下代码检查密码是否包含大小写字母、数字和特殊字符,且长度至少为8位: function checkPasswordStrength(pass…