Skip to content

Commit

Permalink
remove setRawMode
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Sep 20, 2023
1 parent 93d179f commit b696265
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/node/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import readline from 'readline'
import type { ViteDevServer } from 'vite'
import c from 'picocolors'
import { clearCache } from './markdownToVue'
Expand All @@ -24,19 +25,13 @@ export function bindShortcuts(
server.config.logger.info(
c.dim(c.green(' ➜')) +
c.dim(' press ') +
c.bold('h') +
c.bold('h + enter') +
c.dim(' to show help')
)

let actionRunning = false

const onInput = async (input: string) => {
// ctrl+c or ctrl+d
if (input === '\x03' || input === '\x04') {
await server.close().finally(() => process.exit(1))
return
}

if (actionRunning) return

if (input === 'h') {
Expand All @@ -47,7 +42,7 @@ export function bindShortcuts(
...SHORTCUTS.map(
(shortcut) =>
c.dim(' press ') +
c.bold(shortcut.key) +
c.bold(`${shortcut.key} + enter`) +
c.dim(` to ${shortcut.description}`)
)
].join('\n')
Expand All @@ -62,14 +57,9 @@ export function bindShortcuts(
actionRunning = false
}

process.stdin.setRawMode(true)

process.stdin.on('data', onInput).setEncoding('utf8').resume()

server.httpServer.on('close', () => {
process.stdin.off('data', onInput).pause()
process.stdin.setRawMode(false)
})
const rl = readline.createInterface({ input: process.stdin })
rl.on('line', onInput)
server.httpServer.on('close', () => rl.close())
}

const SHORTCUTS: CLIShortcut[] = [
Expand Down

0 comments on commit b696265

Please sign in to comment.