Skip to content

Instantly share code, notes, and snippets.

@ar-nelson
Created September 14, 2013 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ar-nelson/6564305 to your computer and use it in GitHub Desktop.
Save ar-nelson/6564305 to your computer and use it in GitHub Desktop.
Initial skeleton version of the core wmii config node.js module.
// wmii core Module
// Adam R. Nelson
// August 2013
var wmiir = require('./wmiir.js');
var events = require('./wmii_events.js');
var keys = require('./wmii_keys.js');
var dialog = require('./dialog.js');
var spawn = require('child_process').spawn;
var actions = {};
var programs = [];
function indexPrograms() {
try {
var proglist = "";
var child = spawn('wmiir', ['proglist'].concat(process.env.PATH.split(':')));
child.stdout.on('data', function(chunk) {
proglist += chunk;
});
child.on('exit', function() {
programs = proglist.split('\n');
});
} catch (e) {
dialog.error('Failed to index programs: ' + e.message);
}
}
// Executes a shell command string.
function execString(cmdStr) {
wmiir.setsid('sh', '-c', cmdStr);
}
exports.events = events;
exports.keys = keys;
// Trigger preconfigured actions with the "Action" event.
events.on('Action', function(action) {
var handler = actions[action];
if (handler) handler.apply(this, Array.prototype.slice.call(arguments, 1));
else events.emit('Error', 'No such action: ' + action);
});
// This flag is used to prevent infinite loops from spawning too many error
// messages. Only one error message may be on screen at a time.
var errorMessageVisible = false;
// Display an error message when the "Error" event occurs.
events.on('Error', function(/* varargs */) {
var message = Array.prototype.slice.apply(arguments).join(' ');
console.log('Error occurred!');
console.log(message);
if (!errorMessageVisible) {
errorMessageVisible = true;
dialog.error(message, {title: 'wmii error'}, function() {
errorMessageVisible = false;
});
}
});
// Displays a wimenu, containing the items in the array `items` and displaying
// the prompt `prompt`, and, once the user selects an item, calls `callback`
// with the selected item as the first argument.
function menu(items, prompt, callback) {
var child = spawn('wimenu', ['-p', prompt]);
child.on('error', function(err) {
events.emit('Error', err.message || err);
});
var stdout = "";
child.stdout.on('data', function(chunk) {
stdout += chunk.toString();
});
child.on('exit', function(code) {
if (code === 0 && callback)
callback(stdout);
});
child.stdin.end(items.sort().join('\n'));
return child;
}
exports.menu = menu;
// Defines a new action, with the name `name` and the callback function
// `action`. This action will be listed in the actions menu.
exports.defineAction = function(name, action) {
actions[name] = action;
};
// Displays the actions menu.
exports.actionsMenu = function() {
menu(Object.keys(actions), 'Action>', function(action) {
events.emit('Action', action);
});
};
// Displays a menu of all executable programs in the user's PATH, and executes
// whatever command is entered into the menu.
exports.programsMenu = function() {
menu(programs, 'Run>', function(command) {
execString(command);
});
};
// Index the programs menu before starting wmii.
indexPrograms();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment