« get me outta code hell

Let users import keybindings from files - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-09-03 14:01:10 -0300
committerFlorrie <towerofnix@gmail.com>2017-09-03 14:01:57 -0300
commit58deb461d42846a05fe3407a8c96e1ae7efabf64 (patch)
tree457065472f64d484b060a6a650da0a7ef85ec3ec /src
parent1f8b3e84d64e13cb83ead166a0df9938e876a4f8 (diff)
Let users import keybindings from files
Example: https://gist.github.com/towerofnix/381c813f4b231ded5ae416f040134261
Diffstat (limited to 'src')
-rwxr-xr-xsrc/play.js70
1 files changed, 53 insertions, 17 deletions
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[')