« get me outta code hell

basic command log implementation - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-07-15 21:20:39 -0300
committerFlorrie <towerofnix@gmail.com>2020-07-15 21:20:39 -0300
commita2437eafb1a2d7f39c7e816a693ec4f03654b636 (patch)
tree415b9efbf821bf88970caf1a83849d88aae80ef3
parent1230a2cfac4b0d907631eddb4d0dc798f5bb3564 (diff)
basic command log implementation
-rw-r--r--backend.js4
-rwxr-xr-xindex.js7
-rw-r--r--socket.js28
-rw-r--r--ui.js44
4 files changed, 81 insertions, 2 deletions
diff --git a/backend.js b/backend.js
index 20aec7d..67c6335 100644
--- a/backend.js
+++ b/backend.js
@@ -815,6 +815,10 @@ class Backend extends EventEmitter {
   async download(item) {
     return download(item, this.getRecordFor(item))
   }
+
+  showLogMessage(text) {
+    this.emit('log message', text)
+  }
 }
 
 module.exports = Backend
diff --git a/index.js b/index.js
index 6e83e8a..d628a20 100755
--- a/index.js
+++ b/index.js
@@ -75,6 +75,7 @@ async function main() {
     'player-options': {type: 'series'},
     'stress-test': {type: 'flag'},
     'socket-client': {type: 'value'},
+    'socket-name': {type: 'value'},
     'socket-server': {type: 'value'},
     'telnet-server': {type: 'flag'},
     [parseOptions.handleDashless](option) {
@@ -170,6 +171,12 @@ async function main() {
     attachBackendToSocketClient(backend, socketClient, {
       getPlaylistSources: () => appElement.playlistSources
     })
+
+    let nickname = process.env.USER
+    if (options['socket-name']) {
+      nickname = options['socket-name']
+    }
+    socketClient.setNickname(nickname)
   }
 
   if (options['stress-test']) {
diff --git a/socket.js b/socket.js
index ab23a28..b418853 100644
--- a/socket.js
+++ b/socket.js
@@ -145,6 +145,12 @@ function validateCommand(command) {
             typeof command.queuePlayer === 'string' &&
             typeof command.time === 'number'
           )
+        case 'set-nickname':
+          return (
+            typeof command.nickname === 'string' &&
+            command.nickname.length >= 1 &&
+            command.nickname.length <= 12
+          )
         case 'set-pause':
           return (
             typeof command.queuePlayer === 'string' &&
@@ -219,6 +225,8 @@ function makeSocketServer() {
   server.on('connection', socket => {
     sockets.push(socket)
 
+    let nickname = '(Unnamed)'
+
     socket.on('close', () => {
       if (sockets.includes(socket)) {
         sockets.splice(sockets.indexOf(socket), 1)
@@ -236,6 +244,7 @@ function makeSocketServer() {
       }
 
       command.sender = 'client'
+      command.senderNickname = nickname
 
       if (!validateCommand(command)) {
         return
@@ -294,6 +303,12 @@ function makeSocketServer() {
         readyToResume[command.queuePlayer] = []
       }
 
+      // If it's a 'set-nickname' command, save the nickname.
+
+      if (command.code === 'set-nickname') {
+        nickname = command.nickname
+      }
+
       // Relay the command to client sockets besides the sender.
 
       const otherSockets = sockets.filter(s => s !== socket)
@@ -332,6 +347,11 @@ function makeSocketClient() {
   client.sendCommand = function(command) {
     const data = serializeCommandToData(command)
     client.socket.write(data + '\n')
+    client.emit('sent-command', command)
+  }
+
+  client.setNickname = function(nickname) {
+    client.sendCommand({code: 'set-nickname', nickname})
   }
 
   client.socket.on('data', perLine(line => {
@@ -363,7 +383,15 @@ function attachBackendToSocketClient(backend, client, {
 
   backend.setAlwaysStartPaused(true)
 
+  function logCommand(command) {
+    const nickname = command.sender === 'server' ? 'the server' : command.nickname
+    backend.showLogMessage(`${nickname} sent ${command.code}!`)
+  }
+
+  client.on('sent-command', logCommand)
+
   client.on('command', async command => {
+    logCommand(command)
     switch (command.sender) {
       case 'server':
         switch (command.code) {
diff --git a/ui.js b/ui.js
index c9e30e5..2777326 100644
--- a/ui.js
+++ b/ui.js
@@ -59,7 +59,7 @@ const {
 } = require('tui-lib')
 
 /* text editor features disabled because theyre very much incomplete and havent
- * gotten much use from me or anyonea afaik!
+ * gotten much use from me or anyone afaik!
 const TuiTextEditor = require('tui-text-editor')
 */
 
@@ -242,6 +242,12 @@ class AppElement extends FocusElement {
     })
     */
 
+    this.logPane = new Pane()
+    this.addChild(this.logPane)
+
+    this.log = new Log()
+    this.logPane.addChild(this.log)
+
     if (!this.config.showTabberPane) {
       this.tabberPane.visible = false
     }
@@ -430,7 +436,8 @@ class AppElement extends FocusElement {
       'handleProcessMetadataProgress',
       'handleQueueUpdated',
       'handleAddedQueuePlayer',
-      'handleRemovedQueuePlayer'
+      'handleRemovedQueuePlayer',
+      'handleLogMessage'
     ]) {
       this[key] = this[key].bind(this)
     }
@@ -501,12 +508,14 @@ class AppElement extends FocusElement {
     this.backend.on('processMetadata progress', this.handleProcessMetadataProgress)
     this.backend.on('added queue player', this.handleAddedQueuePlayer)
     this.backend.on('removed queue player', this.handleRemovedQueuePlayer)
+    this.backend.on('log message', this.handleLogMessage)
   }
 
   removeBackendListeners() {
     this.backend.removeListener('processMetadata progress', this.handleProcessMetadataProgress)
     this.backend.removeListener('added queue player', this.handleAddedQueuePlayer)
     this.backend.removeListener('removed queue player', this.handleRemovedQueuePlayer)
+    this.backend.removeListener('log message', this.handleLogMessage)
   }
 
   handleAddedQueuePlayer(queuePlayer) {
@@ -520,6 +529,10 @@ class AppElement extends FocusElement {
     }
   }
 
+  handleLogMessage(text) {
+    this.log.newLogMessage(text)
+  }
+
   async handlePlayingDetails(track, oldTrack, queuePlayer) {
     const PIE = this.getPlaybackInfoElementForQueuePlayer(queuePlayer)
     if (PIE) {
@@ -1247,10 +1260,21 @@ class AppElement extends FocusElement {
     }
     */
 
+    if (this.logPane.visible) {
+      this.logPane.w = leftWidth
+      this.logPane.h = 6
+      this.log.fillParent()
+      this.log.fixLayout()
+    }
+
     if (this.tabberPane.visible) {
       this.tabberPane.w = leftWidth
       this.tabberPane.y = bottomY
       this.tabberPane.h = topY - this.tabberPane.y
+      if (this.logPane.visible) {
+        this.tabberPane.h -= this.logPane.h
+        this.logPane.y = this.tabberPane.bottom
+      }
       /*
       if (this.textInfoPane.visible) {
         this.tabberPane.h -= this.textInfoPane.h
@@ -4366,4 +4390,20 @@ class NotesTextEditor extends TuiTextEditor {
 }
 */
 
+class Log extends ListScrollForm {
+  constructor() {
+    super('vertical')
+  }
+
+  newLogMessage(text) {
+    const logMessage = new LogMessage(text)
+    this.addInput(logMessage)
+    this.fixLayout()
+    this.scrollToEnd()
+    return logMessage
+  }
+}
+
+class LogMessage extends Button {}
+
 module.exports = AppElement