Skip to content

Commit

Permalink
refactor: move readline to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiu8081 committed Oct 2, 2022
1 parent 1661344 commit c2c01ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
25 changes: 3 additions & 22 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script setup lang="ts">
import readline from 'readline'
import { onMounted, ref } from 'vue'
import { startListen } from 'blive-message-listener'
import type {
Expand All @@ -9,6 +8,7 @@ import type {
import { TBox } from '@temir/core'
import { getRoomInfo, type RoomInfo } from './utils/getInfo'
import { getInputId } from './utils/cli'
import { listenQuitCommand } from './utils/readline'
import CliHeader from './components/CliHeader.vue'
import CliFooter from './components/CliFooter.vue'
Expand Down Expand Up @@ -40,30 +40,11 @@ const guardBuyList = ref<Message<GuardBuyMsg>[]>([])
const newComerList = ref<Message<NewComerMsg>[]>([])
onMounted(async () => {
let rl: readline.Interface = readline.createInterface({ input: process.stdin, escapeCodeTimeout: 50 })
readline.emitKeypressEvents(process.stdin, rl)
if (process.stdin.isTTY)
process.stdin.setRawMode(true)
function keypressHandler(str: string, key: any) {
// ctrl-c or esc
if (str === '\x03' || str === '\x1B' || (key && key.ctrl && key.name === 'c'))
return process.exit()
const name = key?.name
// quit
if (name === 'q')
return process.exit()
}
process.stdin.on('keypress', keypressHandler)
listenQuitCommand()
const roomInfo = await getRoomInfo(inputRoomId)
if (!roomInfo) {
console.log('房间不存在')
return
return process.exit(1)
}
currentRoomInfo.value = roomInfo
liveStatus.value = {
Expand Down
26 changes: 26 additions & 0 deletions src/utils/readline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import readline from 'readline'

export const listenQuitCommand = () => {
let rl: readline.Interface = readline.createInterface({ input: process.stdin, escapeCodeTimeout: 50 })

readline.emitKeypressEvents(process.stdin, rl)
if (process.stdin.isTTY) {
process.stdin.setRawMode(true)
}

function keypressHandler(str: string, key: any) {
// ctrl-c or esc
if (str === '\x03' || str === '\x1B' || (key && key.ctrl && key.name === 'c')) {
return process.exit()
}

const name = key?.name

// quit
if (name === 'q') {
return process.exit()
}
}

process.stdin.on('keypress', keypressHandler)
}

0 comments on commit c2c01ea

Please sign in to comment.