Skip to content

Commit

Permalink
feat: perform terminal shortcuts on keypress (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
wrongsahil committed Nov 7, 2021
1 parent 28262f7 commit e46e325
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions packages/slidev/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path'
import net from 'net'
import os from 'os'
import { exec } from 'child_process'
import * as readline from 'readline'
import fs from 'fs-extra'
import openBrowser from 'open'
import yargs, { Argv } from 'yargs'
Expand Down Expand Up @@ -150,15 +151,23 @@ cli.command(
function bindShortcut() {
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', (data) => {
const str = data.toString().trim().toLowerCase()
const [sh] = SHORTCUTS.filter(item => item.name === str)
if (sh) {
try {
sh.action()
}
catch (err) {
console.error(`Failed to execute shortcut ${sh.fullname}`, err)
readline.emitKeypressEvents(process.stdin)
if (process.stdin.isTTY)
process.stdin.setRawMode(true)

process.stdin.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') {
process.exit()
}
else {
const [sh] = SHORTCUTS.filter(item => item.name === str)
if (sh) {
try {
sh.action()
}
catch (err) {
console.error(`Failed to execute shortcut ${sh.fullname}`, err)
}
}
}
})
Expand Down

0 comments on commit e46e325

Please sign in to comment.