From 74df0e70223de8beb838cddbd6736fd70bc63960 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Tue, 1 Jul 2025 09:08:15 +0100 Subject: [PATCH] dry up the help logic clip/screenshot are now command groups. --- index.js | 114 ++++++++++++++++++++------------------------ utils/clip.js | 37 ++++++++++---- utils/screenshot.js | 37 ++++++++++---- 3 files changed, 108 insertions(+), 80 deletions(-) diff --git a/index.js b/index.js index 6934a38..a18a4df 100755 --- a/index.js +++ b/index.js @@ -8,8 +8,8 @@ import { sceneHelp, sceneList, sceneSwitch, sceneCurrent } from './utils/scene.j import { audioHelp, audioList, audioMute, audioUnmute, audioToggle, audioStatus } from './utils/audio.js' import { streamHelp, streamStart, streamStop, streamToggle, streamStatus } from './utils/stream.js' import { recordHelp, recordStart, recordStop, recordToggle, recordStatus } from './utils/record.js' -import { recordClip } from './utils/clip.js' -import { recordScreenshot } from './utils/screenshot.js' +import { clipHelp, saveClip } from './utils/clip.js' +import { screenshotHelp, takeScreenshot } from './utils/screenshot.js' const input = cli.input const flags = cli.flags @@ -19,6 +19,15 @@ const port = flags.port ?? process.env.MELD_CLI_PORT ?? 13376 const socket = new WebSocket(`ws://${address}:${port}`) +/** + * Print help information. + * @param {string} helpText + */ +function printHelp(helpText) { + console.log(helpText) + process.exit(0) +} + /** * Helper to wrap QWebChannel usage and handle promise-based command execution. * @param {WebSocket} socket - The websocket instance. @@ -49,13 +58,21 @@ function withChannel(socket, fn) { socket.onopen = function () { (() => { try { - if (input[0] === 'scene') { - if (flags.help) { - console.log(sceneHelp) - socket.close() - process.exit(0) - } + const command = input[0] + const helpMap = { + scene: sceneHelp, + audio: audioHelp, + stream: streamHelp, + record: recordHelp, + clip: clipHelp, + screenshot: screenshotHelp + } + if (flags.help && helpMap[command]) { + printHelp(helpMap[command]) + } + + if (command === 'scene') { const [sceneCommand, ...sceneArguments] = input.slice(1) switch (sceneCommand) { case 'list': @@ -72,19 +89,10 @@ socket.onopen = function () { withChannel(socket, (channel) => sceneCurrent(channel, flags.id)) break default: - console.log(sceneHelp) - socket.close() - process.exit(0) + printHelp(sceneHelp) } - } else if (input[0] === 'audio') { - if (flags.help) { - console.log(audioHelp) - socket.close() - process.exit(0) - } - - const audioCommand = input[1] - const audioName = input[2] + } else if (command === 'audio') { + const [audioCommand, ...audioArguments] = input.slice(1) switch (audioCommand) { case 'list': withChannel(socket, (channel) => audioList(channel, flags.id)) @@ -94,33 +102,25 @@ socket.onopen = function () { console.error('Error: Audio name is required for the mute command.') process.exit(1) } - withChannel(socket, (channel) => audioMute(channel, audioName)) + withChannel(socket, (channel) => audioMute(channel, audioArguments[0])) break case 'unmute': - if (!audioName) { + if (!audioArguments[0]) { console.error('Error: Audio name is required for the unmute command.') process.exit(1) } - withChannel(socket, (channel) => audioUnmute(channel, audioName)) + withChannel(socket, (channel) => audioUnmute(channel, audioArguments[0])) break case 'toggle': - withChannel(socket, (channel) => audioToggle(channel, audioName)) + withChannel(socket, (channel) => audioToggle(channel, audioArguments[0])) break case 'status': - withChannel(socket, (channel) => audioStatus(channel, audioName)) + withChannel(socket, (channel) => audioStatus(channel, audioArguments[0])) break default: - console.log(audioHelp) - socket.close() - process.exit(0) + printHelp(audioHelp) } - } else if (input[0] === 'stream') { - if (flags.help) { - console.log(streamHelp) - socket.close() - process.exit(0) - } - + } else if (command === 'stream') { const streamCommand = input[1] switch (streamCommand) { case 'start': @@ -140,13 +140,7 @@ socket.onopen = function () { socket.close() process.exit(0) } - } else if (input[0] === 'record') { - if (flags.help) { - console.log(recordHelp) - socket.close() - process.exit(0) - } - + } else if (command === 'record') { const recordCommand = input[1] switch (recordCommand) { case 'start': @@ -162,33 +156,29 @@ socket.onopen = function () { withChannel(socket, (channel) => recordStatus(channel)) break default: - console.log(recordHelp) - socket.close() - process.exit(0) + printHelp(recordHelp) } - } else if (input[0] === 'clip') { - if (flags.help) { - console.log(`usage: meld-cli clip`) - socket.close() - process.exit(0) + } else if (command === 'clip') { + const clipCommand = input[1] + if (clipCommand === 'save') { + withChannel(socket, (channel) => saveClip(channel)) + } else { + printHelp(clipHelp) } - - withChannel(socket, (channel) => recordClip(channel)) - } else if (input[0] === 'screenshot') { - if (flags.help) { - console.log(`usage: meld-cli screenshot`) - socket.close() - process.exit(0) + } else if (command === 'screenshot') { + const screenshotCommand = input[1] + if (screenshotCommand === 'take') { + withChannel(socket, (channel) => takeScreenshot(channel)) + } else { + printHelp(screenshotHelp) } - - withChannel(socket, (channel) => recordScreenshot(channel)) } else { - console.log('Unknown command. Use meld-cli --help for available commands.') + printHelp(cli.help) socket.close() - process.exit(0) + process.exit(1) } } catch (error) { - console.error('Error handling CLI flags:', error) + console.error(`Error: ${error.message}`) } })() } diff --git a/utils/clip.js b/utils/clip.js index c6e5ea4..9205e91 100644 --- a/utils/clip.js +++ b/utils/clip.js @@ -1,11 +1,30 @@ -function recordClip(channel) { - return new Promise((resolve, reject) => { - channel.objects.meld.sendCommand('meld.recordClip').then(() => { - resolve('Clip command sent successfully.') - }).catch((err) => { - reject(err) - }) - }) +import meowHelp from 'cli-meow-help' + +const clipHelp = meowHelp({ + name: 'meld-cli clip', + desc: 'Manage clips in Meld', + commands: { + save: { + desc: 'Save a clip' + } + }, + flags: { + help: { + type: 'boolean', + shortFlag: 'h', + desc: 'Display help information' + } + }, + defaults: false +}) + +async function saveClip(channel) { + try { + await channel.objects.meld.sendCommand('meld.recordClip'); + return 'Clip command sent successfully.'; + } catch (err) { + throw err; + } } -export { recordClip } +export { clipHelp, saveClip } diff --git a/utils/screenshot.js b/utils/screenshot.js index 254ef7c..d8b0b7e 100644 --- a/utils/screenshot.js +++ b/utils/screenshot.js @@ -1,11 +1,30 @@ -function recordScreenshot(channel) { - return new Promise((resolve, reject) => { - channel.objects.meld.sendCommand('meld.screenshot').then(() => { - resolve('Screenshot command sent successfully.') - }).catch((err) => { - reject(err) - }) - }) +import meowHelp from 'cli-meow-help' + +const screenshotHelp = meowHelp({ + name: 'meld-cli screenshot', + desc: 'Manage screenshots in Meld', + commands: { + take: { + desc: 'Take a screenshot' + } + }, + flags: { + help: { + type: 'boolean', + shortFlag: 'h', + desc: 'Display help information' + } + }, + defaults: false +}) + +async function takeScreenshot(channel) { + try { + await channel.objects.meld.sendCommand('meld.screenshot'); + return 'Screenshot command sent successfully.'; + } catch (err) { + throw err; + } } -export { recordScreenshot } \ No newline at end of file +export { screenshotHelp, takeScreenshot } \ No newline at end of file