Skip to content

Commit aa6c0a4

Browse files
committed
Migrate log to store, and pubsub for widgets
1 parent fd0cbbf commit aa6c0a4

File tree

6 files changed

+93
-36
lines changed

6 files changed

+93
-36
lines changed

src/jack_client.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async function connectPorts(src, dst, disconnect = false, quiet = true) {
100100
{ encoding: "utf8" }
101101
);
102102
} catch (error) {
103-
Layout.wlogError(error);
103+
store.wlogError(error);
104104
}
105105
}
106106

@@ -122,7 +122,7 @@ function connectPlugins(src, dst, disconnect = false) {
122122

123123
// Direct Monitor not supported right now (It may conflict on multi instance mode.)
124124
if (src === "input" && dst === "output") {
125-
Layout.wlogError(
125+
store.wlogError(
126126
"Direct Monitor not supported right now (It may conflict on multi instance mode.)"
127127
);
128128
return;
@@ -131,7 +131,7 @@ function connectPlugins(src, dst, disconnect = false) {
131131
// Input => Plugin
132132
if (src === "input") {
133133
if (!dst.ports.audio.input || dst.ports.audio.input.length === 0) {
134-
Layout.wlog(
134+
store.wlog(
135135
"Dst plugin does not have correct number of ports or does not exist."
136136
);
137137
return;
@@ -161,7 +161,7 @@ function connectPlugins(src, dst, disconnect = false) {
161161
// Plugin => Output
162162
else if (dst === "output") {
163163
if (!src.ports.audio.output || src.ports.audio.output.length === 0) {
164-
Layout.wlog(
164+
store.wlog(
165165
"src plugin does not have correct number of ports or does not exist."
166166
);
167167
return;
@@ -189,14 +189,14 @@ function connectPlugins(src, dst, disconnect = false) {
189189
// Plugin => Plugin
190190
else {
191191
if (!src.ports.audio.output || src.ports.audio.output.length === 0) {
192-
Layout.wlog(
192+
store.wlog(
193193
"src plugin does not have correct number of ports or does not exist."
194194
);
195195
return;
196196
}
197197

198198
if (!dst.ports.audio.input || dst.ports.audio.input.length === 0) {
199-
Layout.wlog(
199+
store.wlog(
200200
"dst plugin does not have correct number of ports or does not exist."
201201
);
202202
return;
@@ -268,15 +268,15 @@ function clearPluginPorts(plugin, direction = "all") {
268268
`${instanceName}_${plugin.info.instanceNumber}_${plugin.info.safeName}:${port.symbol}`
269269
)
270270
.join(",");
271-
// Layout.wlog(ports);
271+
// store.wlog(ports);
272272

273273
try {
274274
const status = cp.execSync(
275275
`python3 ./py/calvo_cli_tools/jack_tools/port_tools.py clear ${ports}`,
276276
{ encoding: "utf8" }
277277
);
278278
} catch (error) {
279-
Layout.wlogError(error.toString());
279+
store.wlogError(error.toString());
280280
}
281281
}
282282

src/jalv.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ async function spawn_plugin(plugin, rackIndex) {
4040
processSpawned = true;
4141
});
4242
// process.stdout.on("data", function (msg) {
43-
// Layout.wlog(`[#${rackIndex}] ${msg}`);
43+
// store.wlog(`[#${rackIndex}] ${msg}`);
4444
// });
4545
// process.stderr.on("data", function (msg) {
46-
// Layout.wlogError(`[#${rackIndex}] ${msg}`);
46+
// store.wlogError(`[#${rackIndex}] ${msg}`);
4747
// });
4848

4949
// process.on("message", function (msg) {
50-
// Layout.wlog(`[#${rackIndex}] ${msg}`);
50+
// store.wlog(`[#${rackIndex}] ${msg}`);
5151
// });
5252

5353
let retries = 0;
@@ -56,7 +56,7 @@ async function spawn_plugin(plugin, rackIndex) {
5656
}
5757

5858
if (!processSpawned) {
59-
Layout.wlogError("Could not load plugin");
59+
store.wlogError("Could not load plugin");
6060
return null;
6161
}
6262
return process;
@@ -91,7 +91,7 @@ async function getControls(plugin, type) {
9191
}
9292

9393
if (!done) {
94-
Layout.wlogError("Could not load plugin");
94+
store.wlogError("Could not load plugin");
9595
return null;
9696
}
9797

@@ -119,7 +119,7 @@ async function setControl(plugin, control, value) {
119119
}
120120

121121
if (!done) {
122-
Layout.wlogError(`Could set control ${control.symbol}`);
122+
store.wlogError(`Could set control ${control.symbol}`);
123123
return;
124124
}
125125
resultJSON = jalvStdoutToJSON(result, "set");
@@ -155,7 +155,7 @@ function kill_plugin(process, rackIndex) {
155155
try {
156156
process.kill();
157157
} catch (error) {
158-
Layout.wlogError(`[#${rackIndex}] ${error}`);
158+
store.wlogError(`[#${rackIndex}] ${error}`);
159159
}
160160
}
161161

src/keyboard.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ const App = require("./app");
1010
* Register keyboard shorcuts for a screen
1111
*
1212
* @param {*} screen The blessed screen which will receive the shortcuts.
13-
* @param {*} mainGrid (not used)
1413
*/
15-
function registerKeyboardShortcuts(screen, mainGrid) {
14+
function registerKeyboardShortcuts(screen) {
1615
screen.key(["escape", "q", "C-c"], function (ch, key) {
1716
App.exit();
1817
});

src/lv2.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ function init() {
3131
* Scans the system for installed plugins, and save a database in memory and disk.
3232
*/
3333
function buildPluginCache() {
34-
Layout.wlog(
35-
"Building plugins cache, this might take a while, please wait..."
36-
);
34+
store.wlog("Building plugins cache, this might take a while, please wait...");
3735
try {
3836
// const names = lv2ls(true);
3937
const plugins = grep();
@@ -48,7 +46,7 @@ function buildPluginCache() {
4846
});
4947
store.saveCache();
5048
} catch (error) {
51-
Layout.wlog(error);
49+
store.wlog(error);
5250
}
5351
}
5452

@@ -69,7 +67,7 @@ function grep(regex = ".") {
6967
);
7068
return JSON.parse(result);
7169
} catch (error) {
72-
Layout.wlog(error);
70+
store.wlog(error);
7371
return error;
7472
}
7573
}
@@ -105,7 +103,7 @@ function pluginInfo(uri) {
105103
);
106104
return JSON.parse(result);
107105
} catch (error) {
108-
Layout.wlogError(`Plugin ${uri} could not be loaded.`);
106+
store.wlogError(`Plugin ${uri} could not be loaded.`);
109107
throw error;
110108
}
111109
}

src/store.js

+51-11
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ function connectAll() {
171171
}
172172

173173
rack.forEach((plugin, index, arr) => {
174-
Layout.wlogDebug(`${index} => ${rack.length}`);
174+
wlogDebug(`${index} => ${rack.length}`);
175175

176176
if (index === 0) {
177177
Jack.connectPlugins("input", plugin);
178178
}
179179

180180
if (index === rack.length - 1) {
181-
Layout.wlogDebug("Connect to output");
181+
wlogDebug("Connect to output");
182182
Jack.connectPlugins(plugin, "output");
183183
} else {
184184
Jack.connectPlugins(plugin, rack[index + 1]);
@@ -205,8 +205,8 @@ function setAudioSource(mode, channel, name) {
205205
if (channel === "right") jack.CONNECTIONS.outputRight = name;
206206
}
207207
notifySubscribers("jack", jack);
208-
Layout.wlog(jack.CONNECTIONS.inputLeft);
209-
Layout.wlog(jack.CONNECTIONS.inputRight);
208+
wlog(jack.CONNECTIONS.inputLeft);
209+
wlog(jack.CONNECTIONS.inputRight);
210210
}
211211

212212
/**
@@ -280,9 +280,9 @@ async function addPluginToRack(pluginName) {
280280
Jack.connectPlugins(plugin, "output");
281281
}
282282
notifySubscribers("rack", rack);
283-
Layout.wlog(`Added ${plugin.name} to rack. (#${rack.length - 1})`);
283+
wlog(`Added ${plugin.name} to rack. (#${rack.length - 1})`);
284284
} catch (error) {
285-
Layout.wlogError(`Error adding plugin to rack: ${error}`);
285+
wlogError(`Error adding plugin to rack: ${error}`);
286286
console.trace(error);
287287
}
288288
}
@@ -305,7 +305,7 @@ function clearRack() {
305305
function removePluginAt(index) {
306306
const plugin = rack[index];
307307
rack.splice(index, 1);
308-
Layout.wlog(`Remove plugin #${index} - ${plugin.name}`);
308+
wlog(`Remove plugin #${index} - ${plugin.name}`);
309309

310310
Jalv.kill_plugin(plugin.process, index);
311311

@@ -355,7 +355,7 @@ function moveRackItem(rackIndex, direction, max = false) {
355355
// if (prev_plugin) Jack.disconnectPlugins(prev_plugin, plugin, true);
356356
// if (next_plugin) Jack.disconnectPlugins(plugin, next_plugin, true);
357357

358-
// Layout.wlogError(`${rackIndex} => ${direction} => ${max}`);
358+
// store.wlogError(`${rackIndex} => ${direction} => ${max}`);
359359
// TODO: This could be better but for now we will disconnect and reconnect everything.
360360

361361
if (settings.AUTO_RECONNECT) reconectAll();
@@ -402,7 +402,7 @@ function setCategoryFilter(filter = "") {
402402
}
403403

404404
function saveCache(directoryPath = __dirname) {
405-
Layout.wlog("Saving cache...");
405+
wlog("Saving cache...");
406406
try {
407407
if (fs.existsSync(path.join(directoryPath, `pluginCatalog.json`))) {
408408
fs.unlinkSync(path.join(directoryPath, `pluginCatalog.json`));
@@ -434,9 +434,9 @@ function saveCache(directoryPath = __dirname) {
434434
JSON.stringify(categories.sort())
435435
);
436436

437-
Layout.wlog("Cache Saved.");
437+
wlog("Cache Saved.");
438438
} catch (error) {
439-
Layout.wlog(error);
439+
wlog(error);
440440
}
441441
}
442442

@@ -457,6 +457,43 @@ function loadCache(directoryPath = __dirname) {
457457
}
458458
}
459459

460+
/**
461+
* Logs a message in the log-widget
462+
* @todo Move to the widget module.
463+
* @param {*} msg message to log.
464+
*/
465+
function wlog(msg) {
466+
if (app.INITIALIZED) {
467+
notifySubscribers("wlog", msg);
468+
} else {
469+
console.log(msg);
470+
}
471+
}
472+
473+
/**
474+
* Logs an error message in the log Widget
475+
* * @param {*} msg
476+
*/
477+
function wlogError(msg) {
478+
if (app.INITIALIZED) {
479+
notifySubscribers("wlogError", msg);
480+
} else {
481+
console.trace(msg);
482+
}
483+
}
484+
485+
function wlogDebug(msg) {
486+
if (!settings.SHOW_DEBUG_MSG) {
487+
return;
488+
}
489+
if (app.INITIALIZED) {
490+
notifySubscribers("wlogDebug", msg);
491+
// logWidget.log(`{green-fg}${msg}{/}`);
492+
} else {
493+
console.log(msg);
494+
}
495+
}
496+
460497
exports.pluginCatalog = pluginCatalog;
461498
exports.pluginCategories = pluginCategories;
462499
exports.setJackStatus = setJackStatus;
@@ -478,3 +515,6 @@ exports.setAudioSourceMode = setAudioSourceMode;
478515
exports.reconectAll = reconectAll;
479516
exports.clearRack = clearRack;
480517
exports.moveRackItem = moveRackItem;
518+
exports.wlog = wlog;
519+
exports.wlogError = wlogError;
520+
exports.wlogDebug = wlogDebug;

src/widgets/log.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const blessed = require("blessed");
2-
2+
const PubSub = require("pubsub-js");
33
var log = {};
44

55
function make(grid, x, y, xSpan, ySpan) {
@@ -22,11 +22,31 @@ function make(grid, x, y, xSpan, ySpan) {
2222
// screen.copyToClipboard("ssss");
2323
// wlog("Log copied to clipboard.");
2424
// });
25+
var token = PubSub.subscribe("wlog", update);
26+
var token = PubSub.subscribe("wlogError", update);
27+
var token = PubSub.subscribe("wlogDebug", update);
2528

2629
return log;
2730
}
2831

29-
function update() {}
32+
/**
33+
* Fired whenever a new log message arrives
34+
*
35+
* @param {string} type Type of messsage (wlog, wlogError, wlogDebug)
36+
* @param {string} content Message to loh
37+
*/
38+
function update(type, content) {
39+
let color = "";
40+
if (type === "wlogError") {
41+
color = "{red-fg}";
42+
} else if (type === "wlogDebug") {
43+
color = "{green-fg}";
44+
} else {
45+
color = "{white-fg}";
46+
}
47+
48+
log.log(`${color} ${content}`);
49+
}
3050

3151
exports.make = make;
3252
exports.update = update;

0 commit comments

Comments
 (0)