Skip to content

Commit

Permalink
perf: ♻️ Optimize terminal performance
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Nov 14, 2023
1 parent 5b6b8d1 commit d3afc4b
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 43 deletions.
66 changes: 63 additions & 3 deletions electron/exposes/adbkit/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import util from 'node:util'
import child_process from 'node:child_process'
import { exec as _exec, spawn } from 'node:child_process'
import path from 'node:path'
import fs from 'node:fs'
import dayjs from 'dayjs'
Expand All @@ -8,7 +8,7 @@ import appStore from '@electron/helpers/store.js'
import { adbPath } from '@electron/configs/index.js'
import { uniq } from 'lodash-es'

const exec = util.promisify(child_process.exec)
const exec = util.promisify(_exec)

let client = null

Expand Down Expand Up @@ -37,7 +37,66 @@ appStore.onDidChange('common.adbPath', async (value, oldValue) => {
client = Adb.createClient({ bin: value || adbPath })
})

const shell = async command => exec(`${adbPath} ${command}`)
const shell = async (command) => {
const execPath = appStore.get('common.adbPath') || adbPath
return exec(`${execPath} ${command}`, {
env: { ...process.env },
shell: true,
})
}

const spawnShell = async (command, { stdout, stderr } = {}) => {
const spawnPath = appStore.get('common.adbPath') || adbPath
const args = command.split(' ')

console.log('adb.spawnShell.spawnPath', spawnPath)
console.log('adb.spawnShell.args', args)

const spawnProcess = spawn(`"${spawnPath}"`, args, {
env: { ...process.env },
shell: true,
encoding: 'utf8',
})

spawnProcess.stdout.on('data', (data) => {
const stringData = data.toString()

console.log('spawnProcess.stdout.data:', stringData)

if (stdout) {
stdout(stringData, spawnProcess)
}
})

let lastStderr = ''
spawnProcess.stderr.on('data', (data) => {
const stringData = data.toString()

lastStderr = stringData

console.error('spawnProcess.stderr.data:', stringData)

if (stderr) {
stderr(stringData, spawnProcess)
}
})

return new Promise((resolve, reject) => {
spawnProcess.on('close', (code) => {
if (code === 0) {
resolve()
}
else {
reject(new Error(lastStderr || `Command failed with code ${code}`))
}
})

spawnProcess.on('error', (err) => {
reject(err)
})
})
}

const getDevices = async () => client.listDevicesWithPaths()
const deviceShell = async (id, command) => {
const res = await client.getDevice(id).shell(command).then(Adb.util.readAll)
Expand Down Expand Up @@ -193,6 +252,7 @@ export default () => {

return {
shell,
spawnShell,
getDevices,
deviceShell,
kill,
Expand Down
3 changes: 3 additions & 0 deletions electron/exposes/gnirehtet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ const shell = async (command, { debug = false, stdout, stderr } = {}) => {
}
})

let lastStderr = ''
gnirehtetProcess.stderr.on('data', (data) => {
const stringData = data.toString()

lastStderr = stringData

if (debug) {
console.error(`${command}.gnirehtet.process.stderr.data:`, stringData)
}
Expand Down
5 changes: 4 additions & 1 deletion electron/exposes/scrcpy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ const shell = async (command, { stdout, stderr } = {}) => {
}
})

let lastStderr = ''
scrcpyProcess.stderr.on('data', (data) => {
const stringData = data.toString()

lastStderr = stringData

console.error('scrcpyProcess.stderr.data:', stringData)

if (stderr) {
Expand All @@ -49,7 +52,7 @@ const shell = async (command, { stdout, stderr } = {}) => {
resolve()
}
else {
reject(new Error(`Command failed with code ${code}`))
reject(new Error(lastStderr || `Command failed with code ${code}`))
}
})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { debounce } from 'lodash-es'
import { createStderr, createStdout, textFormatter } from 'vue-command'
import { useFixCursor } from './helper.js'

const $adb = window.adbkit

export function useAdb({ vShell, history, loading } = {}) {
const adb = async (args) => {
loading.value = true

const command = args.slice(1).join(' ')

const appendToHistory = debounce(vShell.value.appendToHistory, 500)

let stdoutText = ''
let stderrText = ''
$adb.spawnShell(command, {
stdout(text) {
loading.value = false

stdoutText += text

useFixCursor(history)

appendToHistory(createStdout(stdoutText))
},
stderr(text) {
loading.value = false

stderrText += text

useFixCursor(history)

appendToHistory(createStderr(stderrText))
},
})

return textFormatter('Waiting...')
}

return {
adb,
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { debounce } from 'lodash-es'
import { createStderr, createStdout, textFormatter } from 'vue-command'
import { useFixCursor } from './helper.js'

const $gnirehtet = window.gnirehtet

const fixCursor = (history) => {
const length = history.value.length
if (history.value[length - 1]?.__name === 'VueCommandQuery') {
history.value.splice(length - 1, 1, textFormatter('Waiting...'))
}
}

export function useGnirehtet({ vShell, history, loading } = {}) {
const gnirehtet = async (args) => {
loading.value = true
Expand All @@ -26,7 +20,7 @@ export function useGnirehtet({ vShell, history, loading } = {}) {

stdoutText += text

fixCursor(history)
useFixCursor(history)

appendToHistory(createStdout(stdoutText))
},
Expand All @@ -35,13 +29,13 @@ export function useGnirehtet({ vShell, history, loading } = {}) {

stderrText += text

fixCursor(history)
useFixCursor(history)

appendToHistory(createStderr(stderrText))
},
})

return textFormatter('Loading...')
return textFormatter('Waiting...')
}

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { textFormatter } from 'vue-command'

export function useFixCursor(history) {
const length = history.value.length
if (history.value[length - 1]?.__name === 'VueCommandQuery') {
history.value.splice(length - 1, 1, textFormatter('Waiting...'))
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { debounce } from 'lodash-es'
import { createStderr, createStdout, textFormatter } from 'vue-command'
import { useFixCursor } from './helper.js'

const $scrcpy = window.scrcpy

const fixCursor = (history) => {
const length = history.value.length
if (history.value[length - 1]?.__name === 'VueCommandQuery') {
history.value.splice(length - 1, 1, textFormatter('Waiting...'))
}
}

export function useScrcpy({ vShell, history, loading } = {}) {
const scrcpy = async (args) => {
loading.value = true
Expand All @@ -22,11 +16,12 @@ export function useScrcpy({ vShell, history, loading } = {}) {
let stderrText = ''
$scrcpy.shell(command, {
stdout(text) {
console.log('history', history)
loading.value = false

stdoutText += text

fixCursor(history)
useFixCursor(history)

appendToHistory(createStdout(stdoutText))
},
Expand All @@ -35,13 +30,13 @@ export function useScrcpy({ vShell, history, loading } = {}) {

stderrText += text

fixCursor(history)
useFixCursor(history)

appendToHistory(createStderr(stderrText))
},
})

return textFormatter('Loading...')
return textFormatter('Waiting...')
}

return {
Expand Down
Loading

0 comments on commit d3afc4b

Please sign in to comment.