Last active
March 20, 2021 06:30
-
-
Save ArtemGr/1e668405bb07bb330cc4717f6b8f891e to your computer and use it in GitHub Desktop.
termkit-pause
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/node_modules | |
/package-lock.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "termkit-pause", | |
"version": "1.0.3", | |
"description": "log a pause message for a while, with terminal-kit", | |
"main": "termkit-pause.js", | |
"scripts": { | |
"test": "node -e \"require('./termkit-pause.js').pause()\"" | |
}, | |
"contributors": [ | |
"Artemciy" | |
], | |
"repository": { | |
"type": "git", | |
"url": "https://gist.github.com/1e668405bb07bb330cc4717f6b8f891e.git" | |
}, | |
"license": "MIT", | |
"dependencies": { | |
"terminal-kit": "^2.0.3" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@ts-check | |
const termkit = require ('terminal-kit'); | |
/** | |
* Sleep for the given number of milliseconds | |
* @param {number} ms | |
*/ | |
exports.snooze = function (ms) {return new Promise (resolve => setTimeout (resolve, ms))} | |
/** | |
* @callback statusCallback | |
* @param {number} remaining - Second remaining before continuing automatically | |
* @param {termkit.Terminal} term | |
*/ | |
/** | |
* @typedef {Object} pauseOptions | |
* @property {number} [sec] - How long to wait for user input before continuing | |
* @property {statusCallback} [status] - Function to update the status line | |
*/ | |
/** | |
* Pause for a given number of seconds or until user presses a button | |
* @param {pauseOptions} [options] | |
* @returns Promise<any> Information about the unpause event, if any | |
*/ | |
exports.pause = async function (options) { | |
options = options ?? {} | |
const start = Date.now() | |
const term = termkit.terminal | |
// https://github.com/cronvel/terminal-kit/blob/master/doc/high-level.md#ref.grabInput | |
term.grabInput (true); | |
let input = null | |
term.on ('key', | |
function (name, matches, data) { | |
if (name == 'CTRL_C') { | |
console.log ('^C') | |
process.kill (process.pid, 'SIGINT')} | |
//console.log ('termkit-pause, key]', name, matches, data) | |
input = {tag: 'key', name: name, matches: matches, data: data}}, | |
// @ts-ignore | |
{id: 'termkit-pause-key'}) | |
term.on ('terminal', | |
function (name, data) { | |
console.log ('termkit-pause, terminal]', name, data) | |
input = {tag: 'terminal', name: name, data: data}}, | |
// @ts-ignore | |
{id: 'termkit-pause-terminal'}) | |
term.on ('mouse', | |
function (name, data) { | |
console.log ('termkit-pause, mouse]', name, data) | |
input = {tag: 'mouse', name: name, data: data}}, | |
// @ts-ignore | |
{id: 'termkit-pause-mouse'}) | |
const sec = options.sec || 3.14 | |
const status = options.status || function (remaining, term) { | |
term.column (1) | |
term (`Will continue in ${remaining} s`) | |
term.eraseLineAfter()} | |
while (!input) { | |
const passed = Date.now() - start | |
let remaining = sec - passed / 1000 | |
if (remaining <= 0) break | |
remaining = remaining > 10 ? Math.floor (remaining) : Math.floor (remaining * 10) / 10 | |
status (remaining, term) | |
await exports.snooze (314)} | |
term.off ('key', 'termkit-pause-key') | |
term.off ('terminal', 'termkit-pause-terminal') | |
term.off ('mouse', 'termkit-pause-mouse') | |
term.grabInput (false) | |
term.column (1) .eraseLine() | |
return input} | |
// When invoked from console, “npm i && node termkit-pause.js” | |
// cf. https://nodejs.org/dist/latest-v15.x/docs/api/modules.html#modules_accessing_the_main_module | |
if (require.main === module) {exports.pause()} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment