先制作离开应用程序的功能
在流程控制 class 的 mainMenu 追加个判断
然後加个离开程序的函式
controller\flowController.js
/**
* @description 主要选单
*
* @memberof FlowController
*/
async mainMenu() {
let selectOption = await viewPrinter.mainMenu();
switch (selectOption) {
case "登入":
break;
case "离开":
this.leaveApp();
break;
default:
break;
}
}
/**
* @description 离开应用程序
*
* @memberof FlowController
*/
async leaveApp() {
viewPrinter.clearView();
process.exit();
}
然後制作清空画面的函式
view\viewPrinter.js
/**
* @description 清空画面
*
* @memberof ViewPrinter
*/
async clearView() {
process.stdout.write(
process.platform === "win32" ? "\x1Bc" : "\x1B[2J\x1B[3J\x1B[H"
);
}
然後为了增加仪式感
再加一个按下任意键後在继续进行下一个动作的缓冲感
/**
* @description 按下去任何按键来继续程序
*
* @return {*}
* @memberof InterfaceClass
*/
async pressToContinue() {
return new Promise((resolve) => {
const handler = () => {
process.stdin.setRawMode(false);
process.stdin.pause();
process.stdout.write("\n");
resolve();
};
process.stdin.resume();
process.stdin.setRawMode(true);
process.stdin.once("data", handler);
});
}
然後稍微修改一下流程控制的 leaveApp 函式
async leaveApp() {
viewPrinter.clearView();
console.log("按 任意键 离开程序");
await viewPrinter.pressToContinue();
process.exit();
}
离开功能的仪式感 upup
欲善其事 先利其器 目录 前言 Vim 配置 Vim 插件 终端机指令 前言 工程师要学习 Vim ...
一、当笔记愈来愈多时,会发生什麽问题? 多数人使用笔记软件或是文件愈来愈多时一定都会遭遇相同问题:想...
Python 中的字串和串列有很多相似的性质,可以想成字串是元只能是字元的串列 所以可以用索引和 S...
生命周期介绍 在一开始建立Vue的实体,然後挂载到#app就算是已经在使用元件,而这一连串的建立到被...
此用React的最大目的就是将UI分区模组化成独立的、可复用的元件。 该如何运用这些元件? 就是将资...