From 58deb461d42846a05fe3407a8c96e1ae7efabf64 Mon Sep 17 00:00:00 2001 From: Florrie Date: Sun, 3 Sep 2017 14:01:10 -0300 Subject: Let users import keybindings from files Example: https://gist.github.com/towerofnix/381c813f4b231ded5ae416f040134261 --- src/play.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/play.js b/src/play.js index ff0c0d4..0b0cfcb 100755 --- a/src/play.js +++ b/src/play.js @@ -71,6 +71,21 @@ async function main(args) { let disablePlaybackStatus = false + const keybindings = [ + [['space'], 'togglePause'], + [['left'], 'seek', -5], + [['right'], 'seek', +5], + [['shiftLeft'], 'seek', -30], + [['shiftRight'], 'seek', +30], + [['up'], 'skipBack'], + [['down'], 'skipAhead'], + [['s'], 'skipAhead'], + [['delete'], 'skipUpNext'], + [['i'], 'showTrackInfo'], + [['t'], 'showTrackInfo'], + [['q'], 'quit'] + ] + async function openPlaylist(arg, silent = false) { let playlistText @@ -103,6 +118,30 @@ async function main(args) { processArgv(processedPlaylist.options, optionFunctions) } + async function openKeybindings(arg, add = true) { + console.log("Opening keybindings from: " + arg) + + let keybindingText + + // TODO: Maybe let keybindings be downloaded from a file? We'd probably + // just have to rename the downloadPlaylistFromOptionValue function's + // name. + try { + keybindingText = await readFile(arg) + } catch(err) { + console.error("Failed to open keybinding file: " + arg) + return false + } + + const openedKeybindings = JSON.parse(keybindingText) + + if (!add) { + keybindings.splice(0) + } + + keybindings.push(...openedKeybindings) + } + function requiresOpenPlaylist() { if (activePlaylist === null) { throw new Error( @@ -185,6 +224,19 @@ async function main(args) { '-log-playlist': util => util.alias('-print-playlist'), '-json': util => util.alias('-print-playlist'), + // Add appends the keybindings to the existing keybindings; import replaces + // the current ones with the opened ones. + + '-add-keybindings': async function(util) { + await openKeybindings(util.nextArg()) + }, + + '-open-keybindings': util => util.alias('-add-keybindings'), + + '-import-keybindings': async function(util) { + await openKeybindings(util.nextArg(), false) + }, + '-clear': function(util) { // --clear (alias: -c) // Clears the active playlist. This does not affect the source @@ -438,23 +490,7 @@ async function main(args) { } } - // TODO: Let the user load custom keybindings from a file. - const keybindingHandler = compileKeybindings({ - bindings: [ - [['space'], 'togglePause'], - [['left'], 'seek', -5], - [['right'], 'seek', +5], - [['shiftLeft'], 'seek', -30], - [['shiftRight'], 'seek', +30], - [['up'], 'skipBack'], - [['down'], 'skipAhead'], - [['s'], 'skipAhead'], - [['delete'], 'skipUpNext'], - [['i'], 'showTrackInfo'], - [['t'], 'showTrackInfo'], - [['q'], 'quit'] - ] - }.bindings, commands) + const keybindingHandler = compileKeybindings(keybindings, commands) process.stdin.on('data', data => { const escModifier = Buffer.from('\x1b[') -- cgit 1.3.0-6-gf8a5