add some more out/err colouring

This commit is contained in:
onyx-and-iris 2025-07-01 19:06:57 +01:00
parent 98aeada23a
commit 49df910763
6 changed files with 56 additions and 53 deletions

View File

@ -4,6 +4,7 @@ import { QWebChannel } from 'qwebchannel'
import WebSocket from 'ws' import WebSocket from 'ws'
import cli from './utils/cli.js' import cli from './utils/cli.js'
import style from './utils/style.js'
import { sceneHelp, sceneList, sceneSwitch, sceneCurrent } from './utils/scene.js' import { sceneHelp, sceneList, sceneSwitch, sceneCurrent } from './utils/scene.js'
import { audioHelp, audioList, audioMute, audioUnmute, audioToggle, audioStatus } from './utils/audio.js' import { audioHelp, audioList, audioMute, audioUnmute, audioToggle, audioStatus } from './utils/audio.js'
import { streamHelp, streamStart, streamStop, streamToggle, streamStatus } from './utils/stream.js' import { streamHelp, streamStart, streamStop, streamToggle, streamStatus } from './utils/stream.js'
@ -29,6 +30,14 @@ function printHelp (helpText) {
process.exit(0) process.exit(0)
} }
/** * Print an error message and exit the process.
* @param {string} message - The error message to print.
*/
function printError (message) {
console.error(style.err(message))
process.exit(1)
}
/** /**
* Helper to wrap QWebChannel usage and handle promise-based command execution. * Helper to wrap QWebChannel usage and handle promise-based command execution.
* @param {WebSocket} socket - The websocket instance. * @param {WebSocket} socket - The websocket instance.
@ -41,16 +50,14 @@ function withChannel (socket, fn) {
.then((result) => { .then((result) => {
if (typeof result === 'object' && result !== null && typeof result.toString === 'function') { if (typeof result === 'object' && result !== null && typeof result.toString === 'function') {
console.log(result.toString()) console.log(result.toString())
} else { } else if (result !== undefined) {
if (result !== undefined) {
console.log(result) console.log(result)
} }
}
socket.close() socket.close()
process.exit(0) process.exit(0)
}) })
.catch((err) => { .catch((err) => {
console.error(`${err}`) console.error(style.err(err))
socket.close() socket.close()
process.exit(1) process.exit(1)
}) })
@ -83,8 +90,7 @@ socket.onopen = function () {
break break
case 'switch': case 'switch':
if (!sceneArguments[0]) { if (!sceneArguments[0]) {
console.error('Error: Scene name is required for the switch command.') printError('Error: Scene name is required for the switch command.')
process.exit(1)
} }
withChannel(socket, (channel) => sceneSwitch(channel, sceneArguments[0])) withChannel(socket, (channel) => sceneSwitch(channel, sceneArguments[0]))
break break
@ -102,29 +108,25 @@ socket.onopen = function () {
break break
case 'mute': case 'mute':
if (!audioArguments[0]) { if (!audioArguments[0]) {
console.error('Error: Audio name is required for the mute command.') printError('Error: Audio name is required for the mute command.')
process.exit(1)
} }
withChannel(socket, (channel) => audioMute(channel, audioArguments[0])) withChannel(socket, (channel) => audioMute(channel, audioArguments[0]))
break break
case 'unmute': case 'unmute':
if (!audioArguments[0]) { if (!audioArguments[0]) {
console.error('Error: Audio name is required for the unmute command.') printError('Error: Audio name is required for the unmute command.')
process.exit(1)
} }
withChannel(socket, (channel) => audioUnmute(channel, audioArguments[0])) withChannel(socket, (channel) => audioUnmute(channel, audioArguments[0]))
break break
case 'toggle': case 'toggle':
if (!audioArguments[0]) { if (!audioArguments[0]) {
console.error('Error: Audio name is required for the toggle command.') printError('Error: Audio name is required for the toggle command.')
process.exit(1)
} }
withChannel(socket, (channel) => audioToggle(channel, audioArguments[0])) withChannel(socket, (channel) => audioToggle(channel, audioArguments[0]))
break break
case 'status': case 'status':
if (!audioArguments[0]) { if (!audioArguments[0]) {
console.error('Error: Audio name is required for the status command.') printError('Error: Audio name is required for the status command.')
process.exit(1)
} }
withChannel(socket, (channel) => audioStatus(channel, audioArguments[0])) withChannel(socket, (channel) => audioStatus(channel, audioArguments[0]))
break break

View File

@ -1,7 +1,7 @@
import meowHelp from 'cli-meow-help' import meowHelp from 'cli-meow-help'
import Table from 'cli-table3' import Table from 'cli-table3'
import { highlight, error } from './style.js' import style from './style.js'
const commands = { const commands = {
list: { list: {
@ -58,9 +58,9 @@ function audioList (channel, showId) {
for (const [key, value] of Object.entries(meld.session.items)) { for (const [key, value] of Object.entries(meld.session.items)) {
if (value.type === 'track') { if (value.type === 'track') {
if (showId) { if (showId) {
table.push([highlight(value.name), { content: value.muted ? highlight('✓') : '✗', hAlign: 'center' }, highlight(key)]) table.push([style.highlight(value.name), { content: value.muted ? style.highlight('✓') : '✗', hAlign: 'center' }, style.highlight(key)])
} else { } else {
table.push([highlight(value.name), { content: value.muted ? highlight('✓') : '✗', hAlign: 'center' }]) table.push([style.highlight(value.name), { content: value.muted ? style.highlight('✓') : '✗', hAlign: 'center' }])
} }
} }
} }
@ -73,7 +73,7 @@ function audioList (channel, showId) {
function audioMute (channel, audioName) { function audioMute (channel, audioName) {
if (!channel.objects || !channel.objects.meld) { if (!channel.objects || !channel.objects.meld) {
return Promise.reject(new Error(error('Meld object not found in channel.'))) return Promise.reject(new Error('Meld object not found in channel.'))
} }
const meld = channel.objects.meld const meld = channel.objects.meld
let itemId let itemId
@ -87,19 +87,19 @@ function audioMute (channel, audioName) {
} }
if (!itemId) { if (!itemId) {
return Promise.reject(new Error(`No audio device with name ${audioName} found.`)) return Promise.reject(new Error(`No audio device with name ${style.errHighlight(audioName)} found.`))
} }
if (isMuted) { if (isMuted) {
return Promise.resolve(`Audio track ${highlight(audioName)} is already muted.`) return Promise.reject(new Error(`Audio track ${style.errHighlight(audioName)} is already muted.`))
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
meld.toggleMute(itemId) meld.toggleMute(itemId)
.then(() => { .then(() => {
resolve(`Audio track ${highlight(audioName)} has been muted.`) resolve(`Audio track ${style.highlight(audioName)} has been muted.`)
}) })
.catch((err) => { .catch((err) => {
reject(new Error(`Error muting audio track: ${err}`)) reject(new Error(`Error muting audio track: ${err.message}`))
}) })
}) })
} }
@ -122,16 +122,16 @@ function audioUnmute (channel, audioName) {
return Promise.reject(new Error('No audio track found.')) return Promise.reject(new Error('No audio track found.'))
} }
if (!isMuted) { if (!isMuted) {
return Promise.resolve(`Audio track ${audioName} is already unmuted.`) return Promise.reject(new Error(`Audio track ${style.errHighlight(audioName)} is already unmuted.`))
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
meld.toggleMute(itemId) meld.toggleMute(itemId)
.then(() => { .then(() => {
resolve(`Audio track ${audioName} has been unmuted.`) resolve(`Audio track ${style.highlight(audioName)} has been unmuted.`)
}) })
.catch((err) => { .catch((err) => {
reject(new Error(`Error unmuting audio track: ${err}`)) reject(new Error(`Error unmuting audio track: ${err.message}`))
}) })
}) })
} }
@ -151,16 +151,17 @@ function audioToggle (channel, audioName) {
} }
} }
if (!itemId) { if (!itemId) {
return Promise.reject(new Error(`No audio device with name ${audioName} found.`)) return Promise.reject(new Error(`No audio device with name ${style.errHighlight(audioName)} found.`))
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
meld.toggleMute(itemId) meld.toggleMute(itemId)
.then(() => { .then(() => {
const status = isMuted ? 'unmuted' : 'muted' const status = isMuted ? 'unmuted' : 'muted'
resolve(`Audio track ${audioName} has been ${status}.`) resolve(`Audio track ${style.highlight(audioName)} has been ${status}.`)
}) })
.catch((err) => { .catch((err) => {
reject(new Error(`Error toggling audio track: ${err}`)) reject(new Error(`Error toggling audio track: ${err.message}`))
}) })
}) })
} }
@ -180,10 +181,11 @@ function audioStatus (channel, audioName) {
} }
} }
if (!itemId) { if (!itemId) {
return Promise.reject(new Error(`No audio device with name ${audioName} found.`)) return Promise.reject(new Error(`No audio device with name ${style.errHighlight(audioName)} found.`))
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
resolve(`${highlight(audioName)} is ${isMuted ? 'muted' : 'unmuted'}`) resolve(`${style.highlight(audioName)} is ${isMuted ? 'muted' : 'unmuted'}`)
}) })
} }

View File

@ -47,7 +47,7 @@ function recordStart (channel) {
resolve('Recording started successfully.') resolve('Recording started successfully.')
}) })
.catch((err) => { .catch((err) => {
reject(err) reject(new Error(`Failed to start recording: ${err.message}`))
}) })
}) })
} }
@ -68,7 +68,7 @@ function recordStop (channel) {
resolve('Recording stopped successfully.') resolve('Recording stopped successfully.')
}) })
.catch((err) => { .catch((err) => {
reject(err) reject(new Error(`Failed to stop recording: ${err.message}`))
}) })
}) })
} }
@ -85,7 +85,7 @@ function recordToggle (channel) {
resolve(`Recording ${meld.isRecording ? 'stopped' : 'started'} successfully.`) resolve(`Recording ${meld.isRecording ? 'stopped' : 'started'} successfully.`)
}) })
.catch((err) => { .catch((err) => {
reject(err) reject(new Error(`Failed to toggle recording: ${err.message}`))
}) })
}) })
} }

View File

@ -1,7 +1,7 @@
import meowHelp from 'cli-meow-help' import meowHelp from 'cli-meow-help'
import Table from 'cli-table3' import Table from 'cli-table3'
import { highlight, error, errorHighlight } from './style.js' import style from './style.js'
const commands = { const commands = {
list: { list: {
@ -56,14 +56,15 @@ function sceneList (channel, showId) {
for (const [key, value] of Object.entries(meld.session.items)) { for (const [key, value] of Object.entries(meld.session.items)) {
if (value.type === 'scene') { if (value.type === 'scene') {
if (showId) { if (showId) {
table.push([highlight(value.name), { content: value.current ? highlight('✓') : '✗', hAlign: 'center' }, highlight(key)]) table.push([style.highlight(value.name), { content: value.current ? style.highlight('✓') : '✗', hAlign: 'center' }, style.highlight(key)])
} else { } else {
table.push([highlight(value.name), { content: value.current ? highlight('✓') : '✗', hAlign: 'center' }]) table.push([style.highlight(value.name), { content: value.current ? style.highlight('✓') : '✗', hAlign: 'center' }])
} }
} }
} }
if (table.length === 0) { if (table.length === 0) {
return Promise.reject(new Error('No scenes found.')) return Promise.resolve('No scenes found.')
} }
return Promise.resolve(table) return Promise.resolve(table)
} }
@ -82,12 +83,12 @@ function sceneSwitch (channel, sceneName) {
} }
} }
if (!itemId) { if (!itemId) {
return Promise.reject(new Error(error(`No scene with name ${errorHighlight(sceneName)} found.`))) return Promise.reject(new Error(`No scene with name ${style.errHighlight(sceneName)} found.`))
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
meld.showScene(itemId).then(() => { meld.showScene(itemId).then(() => {
resolve(`Switched to scene: ${highlight(sceneName)}`) resolve(`Switched to scene: ${style.highlight(sceneName)}`)
}).catch(err => { }).catch(err => {
reject(err) reject(err)
}) })
@ -103,9 +104,9 @@ function sceneCurrent (channel, showId) {
for (const [key, value] of Object.entries(meld.session.items)) { for (const [key, value] of Object.entries(meld.session.items)) {
if (value.type === 'scene' && value.current) { if (value.type === 'scene' && value.current) {
if (showId) { if (showId) {
return Promise.resolve(`Current scene: ${highlight(value.name)} (ID: ${highlight(key)})`) return Promise.resolve(`Current scene: ${style.highlight(value.name)} (ID: ${style.highlight(key)})`)
} }
return Promise.resolve(`Current scene: ${highlight(value.name)}`) return Promise.resolve(`Current scene: ${style.highlight(value.name)}`)
} }
} }
return Promise.reject(new Error('No current scene found.')) return Promise.reject(new Error('No current scene found.'))

View File

@ -47,7 +47,7 @@ function streamStart (channel) {
resolve('Streaming started successfully.') resolve('Streaming started successfully.')
}) })
.catch((err) => { .catch((err) => {
reject(err) reject(new Error(`Failed to start streaming: ${err.message}`))
}) })
}) })
} }
@ -68,7 +68,7 @@ function streamStop (channel) {
resolve('Streaming stopped successfully.') resolve('Streaming stopped successfully.')
}) })
.catch((err) => { .catch((err) => {
reject(err) reject(new Error(`Failed to stop streaming: ${err.message}`))
}) })
}) })
} }
@ -85,7 +85,7 @@ function streamToggle (channel) {
resolve(`Streaming ${meld.isStreaming ? 'stopped' : 'started'} successfully.`) resolve(`Streaming ${meld.isStreaming ? 'stopped' : 'started'} successfully.`)
}) })
.catch((err) => { .catch((err) => {
reject(err) reject(new Error(`Failed to toggle streaming: ${err.message}`))
}) })
}) })
} }

View File

@ -1,11 +1,9 @@
import clc from 'cli-color' import clc from 'cli-color'
const highlight = clc.cyan const style = {
const error = clc.red.bold highlight: clc.cyan,
const errorHighlight = clc.yellow.bold err: clc.red.bold,
errHighlight: clc.yellow.bold
export {
highlight,
error,
errorHighlight
} }
export default style