diff --git a/index.js b/index.js index 80038aa..3ddec1f 100755 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ import WebSocket from 'ws' import cli from './utils/cli.js' import { sceneHelp, sceneList, sceneSwitch, sceneCurrent } from './utils/scene.js' -import { audioHelp, audioMute, audioUnmute, audioToggle, audioStatus } from './utils/audio.js' +import { audioHelp, audioList, audioMute, audioUnmute, audioToggle, audioStatus } from './utils/audio.js' import { streamHelp, streamStart, streamStop, streamStatus } from './utils/stream.js' import { recordHelp, recordStart, recordStop, recordStatus } from './utils/record.js' @@ -34,8 +34,8 @@ socket.onopen = function () { case 'list': channel = new QWebChannel(socket, function (channel) { sceneList(channel, flags.id) - .then((scenes) => { - console.log(scenes) + .then((table) => { + console.log(table.toString()) socket.close() process.exit(0) }) @@ -96,6 +96,21 @@ socket.onopen = function () { const audioCommand = input[1] const audioName = input[2] switch (audioCommand) { + case 'list': + channel = new QWebChannel(socket, function (channel) { + audioList(channel, flags.id) + .then((table) => { + console.log(table.toString()) + socket.close() + process.exit(0) + }) + .catch((err) => { + console.error(`${err}`) + socket.close() + process.exit(1) + }) + }) + break case 'mute': if (!audioName) { console.error('Error: Audio name is required for the mute command.') diff --git a/utils/audio.js b/utils/audio.js index 5ab75a4..b900ff0 100644 --- a/utils/audio.js +++ b/utils/audio.js @@ -1,8 +1,12 @@ import meowHelp from 'cli-meow-help' +import Table from 'cli-table3' import { highlight, error } from './style.js' const commands = { + list: { + desc: 'List all audio devices' + }, mute: { desc: 'Mute the audio' }, @@ -33,6 +37,40 @@ const audioHelp = meowHelp({ defaults: false }) +function audioList (channel, showId) { + if (!channel.objects || !channel.objects.meld) { + return Promise.reject(new Error('Meld object not found in channel.')) + } + + const headers = [{ content: 'Audio Name', hAlign: 'center' }, { content: 'Muted', hAlign: 'center' }] + if (showId) { + headers.push({ content: 'ID', hAlign: 'center' }) + } + const table = new Table({ + style: { + head: ['none'], + compact: true + }, + head: headers + }) + + const meld = channel.objects.meld + for (const [key, value] of Object.entries(meld.session.items)) { + if (value.type === 'track') { + if (showId) { + table.push([highlight(value.name), { content: value.muted ? highlight('✓') : '✗', hAlign: 'center' }, highlight(key)]) + } else { + table.push([highlight(value.name), { content: value.muted ? highlight('✓') : '✗', hAlign: 'center' }]) + } + } + } + + if (table.length === 0) { + return Promise.resolve('No audio devices found.') + } + return Promise.resolve(table) +} + function audioMute (channel, audioName) { if (!channel.objects || !channel.objects.meld) { return Promise.reject(new Error(error('Meld object not found in channel.'))) @@ -151,6 +189,7 @@ function audioStatus (channel, audioName) { export { audioHelp, + audioList, audioMute, audioUnmute, audioToggle, diff --git a/utils/scene.js b/utils/scene.js index 248a8c7..ef56d40 100644 --- a/utils/scene.js +++ b/utils/scene.js @@ -65,7 +65,7 @@ function sceneList (channel, showId) { if (table.length === 0) { return Promise.reject(new Error('No scenes found.')) } - return Promise.resolve(table.toString()) + return Promise.resolve(table) } function sceneSwitch (channel, sceneName) {