« 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
committer(quasar) nebula <qznebula@protonmail.com>2024-05-16 19:06:05 -0300
commitafeda69313e0250d654986ba0203974c56b30670 (patch)
tree74ceab4ffce0f7c722374596ddeb7b6ca45d46d2
parent1be05b974e5ff09fbab1cda89b2f4223cd97bedb (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 3b2343e..fe7014b 100644
--- a/backend.js
+++ b/backend.js
@@ -866,4 +866,8 @@ export default class Backend extends EventEmitter {
   async download(item) {
     return download(item, this.getRecordFor(item))
   }
+
+  showLogMessage(text) {
+    this.emit('log message', text)
+  }
 }
diff --git a/index.js b/index.js
index 2c352cd..efee187 100755
--- a/index.js
+++ b/index.js
@@ -64,6 +64,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'},
     'skip-config-file': {type: 'flag'},
@@ -200,6 +201,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 aae1504..12d08af 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 @@ export 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 @@ export function makeSocketServer() {
       }
 
       command.sender = 'client'
+      command.senderNickname = nickname
 
       if (!validateCommand(command)) {
         return
@@ -294,6 +303,12 @@ export 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 @@ export 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 @@ export 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 f9f8e5f..05e359e 100644
--- a/ui.js
+++ b/ui.js
@@ -55,7 +55,7 @@ import {
 } from './serialized-backend.js'
 
 /* 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')
 */
 
@@ -239,6 +239,12 @@ export default 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
     }
@@ -465,7 +471,8 @@ export default class AppElement extends FocusElement {
       'handleQueueUpdated',
       'handleAddedQueuePlayer',
       'handleRemovedQueuePlayer',
-      'handleSetLoopQueueAtEnd'
+      'handleSetLoopQueueAtEnd',
+      'handleLogMessage'
     ]) {
       this[key] = this[key].bind(this)
     }
@@ -537,6 +544,7 @@ export default class AppElement extends FocusElement {
     this.backend.on('added queue player', this.handleAddedQueuePlayer)
     this.backend.on('removed queue player', this.handleRemovedQueuePlayer)
     this.backend.on('set-loop-queue-at-end', this.handleSetLoopQueueAtEnd)
+    this.backend.on('log message', this.handleLogMessage)
   }
 
   removeBackendListeners() {
@@ -544,6 +552,7 @@ export default class AppElement extends FocusElement {
     this.backend.removeListener('added queue player', this.handleAddedQueuePlayer)
     this.backend.removeListener('removed queue player', this.handleRemovedQueuePlayer)
     this.backend.removeListener('set-loop-queue-at-end', this.handleSetLoopQueueAtEnd)
+    this.backend.removeListener('log message', this.handleLogMessage)
   }
 
   handleAddedQueuePlayer(queuePlayer) {
@@ -561,6 +570,10 @@ export default class AppElement extends FocusElement {
     this.updateQueueLengthLabel()
   }
 
+  handleLogMessage(text) {
+    this.log.newLogMessage(text)
+  }
+
   async handlePlayingDetails(track, oldTrack, startTime, queuePlayer) {
     const PIE = this.getPlaybackInfoElementForQueuePlayer(queuePlayer)
     if (PIE) {
@@ -1711,10 +1724,21 @@ export default 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
@@ -5499,3 +5523,19 @@ 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 {}