stumpwm-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[STUMP] [PATCH] mpd.lisp: collapse current-song


From: Vitaly Mayatskikh
Subject: [STUMP] [PATCH] mpd.lisp: collapse current-song
Date: Mon, 18 Feb 2008 14:25:08 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux)

This patch allows to set length limits for the album and overall song
name while building song name for mode-line via two variables:

(setq *mpd-collapse-album-length* 20) ; for album
(setq *mpd-collapse-all-length* 60) ; overall song name length

--- /home/vitaly/src/stumpwm/contrib-/mpd.lisp  2008-02-16 10:51:52.000000000 
+0100
+++ mpd.lisp    2008-02-18 14:16:07.000000000 +0100
@@ -45,6 +45,9 @@
   )
 (defparameter *mpd-port* 6600)
 
+(defvar *mpd-collapse-album-length* nil)
+(defvar *mpd-collapse-all-length* nil)
+
 (defmacro with-mpd-connection (&body body)
   `(if *mpd-socket*
        (handler-case (progn ,@body)
@@ -137,19 +140,24 @@
 (defun mpd-update-status ()
   (setf *mpd-status* (mpd-send-command "status")))
 
-(defun format-mpd-current-song (current-song)
-  (let ((artist (assoc-value :artist current-song))
+(defun format-mpd-current-song (current-song &optional (collapse-album nil) 
(collapse-all nil))
+  (let* ((artist (assoc-value :artist current-song))
         (album (assoc-value :album current-song))
         (title (assoc-value :title current-song))
-        (file (assoc-value :file current-song)))
-    (if (or (null artist)
+        (file (assoc-value :file current-song))
+        (song (if (or (null artist)
             (null album)
             (null title))
         (format nil "~a" file)
-      (format nil "~a (~a): ~a"
+                  (format nil "~a \"~a\" - ~a"
               artist
-              album
+                          (if (and collapse-album *mpd-collapse-album-length* 
(> (length album) *mpd-collapse-album-length*))
+                              (concatenate 'string (subseq album 0 
*mpd-collapse-album-length*) "...")
+                              album)
               title))))
+    (if (and collapse-all *mpd-collapse-all-length* (> (length song) 
*mpd-collapse-all-length*))
+       (concatenate 'string (subseq song 0 *mpd-collapse-all-length*) "...")
+       song)))
 
 (defun format-mpd-status (status)
   (let ((mpd-state (assoc-value :state status)))
@@ -199,7 +207,7 @@
          (progn (mpd-update-current-song)
                 (format nil "~a: ~a"
                         (format-mpd-status *mpd-status*)
-                        (format-mpd-current-song *mpd-current-song*)))))
+                        (format-mpd-current-song *mpd-current-song* t t)))))
     "Not connected"))
 
 ;;mpd commands
--- mpd.lisp.bak        2008-02-18 14:13:52.000000000 +0100
+++ mpd.lisp    2008-02-18 14:16:07.000000000 +0100
@@ -31,17 +31,38 @@
 ;;;
 ;;; NOTES:
 ;;; see http://mpd.wikia.com/wiki/Protocol_Reference for full protocol
+#-(or sbcl clisp) (error "unimplemented")
+
 (in-package :stumpwm)
 
 ;;mpd client
 (defparameter *mpd-socket* nil)
-(defparameter *mpd-server* "localhost")
+(defparameter *mpd-server* 
+  #+cmu
+  "localhost"
+  #+sbcl
+  #(127 0 0 1)
+  )
 (defparameter *mpd-port* 6600)
 
+(defvar *mpd-collapse-album-length* nil)
+(defvar *mpd-collapse-all-length* nil)
+
+(defmacro with-mpd-connection (&body body)
+  `(if *mpd-socket*
+       (handler-case (progn ,@body)
+                     (error (c) (progn
+                                  (message "Error with mpd connection: ~a" c)
+                                  (setf *mpd-socket* nil))))
+     (message "Error: not connected to mpd~%")))
+
 (defun mpd-send (command)
   "Send command to stream ending with newline"
   (with-mpd-connection
-   (ext:write-char-sequence
+   (#+cmu   
+    ext:write-char-sequence
+    #+sbcl  
+    write-sequence
     (concatenate  'string command (string #\Newline))
     *mpd-socket*)))
 
@@ -78,11 +99,19 @@
 
 (defun mpd-connect ()
   "Connect to mpd server"
-  (setf *mpd-socket*
+    (setq *mpd-socket*
+         #+cmu
         (handler-case (socket:socket-connect *mpd-port* *mpd-server*
                                              :element-type 'character)
                       ((or system::simple-os-error error)
                        (err)
+                        (format t  "Error connecting to mpd: ~a~%" err)))
+         #+sbcl
+         (handler-case (let ((s (make-instance 'sb-bsd-sockets:inet-socket 
:type :stream :protocol :tcp)))
+                         (sb-bsd-sockets:socket-connect s *mpd-server* 
*mpd-port*)
+                         (sb-bsd-sockets:socket-make-stream s :input t :output 
t :buffering :none))
+                       ((or simple-error error) 
+                        (err)
                        (format t  "Error connecting to mpd: ~a~%" err))))
   (when *mpd-socket*
     (read-line *mpd-socket*)))
@@ -98,13 +127,6 @@
   (mpd-send cmd)
   (mpd-receive))
 
-(defmacro with-mpd-connection (&body body)
-  `(if *mpd-socket*
-       (handler-case (progn ,@body)
-                     (error (c) (progn
-                                  (message "Error with mpd connection: ~a" c)
-                                  (setf *mpd-socket* nil))))
-     (message "Error: not connected to mpd~%")))
 
 ;;mpd formatter
 (dolist (a '((#\m fmt-mpd-status)))
@@ -118,19 +140,24 @@
 (defun mpd-update-status ()
   (setf *mpd-status* (mpd-send-command "status")))
 
-(defun format-mpd-current-song (current-song)
-  (let ((artist (assoc-value :artist current-song))
+(defun format-mpd-current-song (current-song &optional (collapse-album nil) 
(collapse-all nil))
+  (let* ((artist (assoc-value :artist current-song))
         (album (assoc-value :album current-song))
         (title (assoc-value :title current-song))
-        (file (assoc-value :file current-song)))
-    (if (or (null artist)
+        (file (assoc-value :file current-song))
+        (song (if (or (null artist)
             (null album)
             (null title))
         (format nil "~a" file)
-      (format nil "~a (~a): ~a"
+                  (format nil "~a \"~a\" - ~a"
               artist
-              album
+                          (if (and collapse-album *mpd-collapse-album-length* 
(> (length album) *mpd-collapse-album-length*))
+                              (concatenate 'string (subseq album 0 
*mpd-collapse-album-length*) "...")
+                              album)
               title))))
+    (if (and collapse-all *mpd-collapse-all-length* (> (length song) 
*mpd-collapse-all-length*))
+       (concatenate 'string (subseq song 0 *mpd-collapse-all-length*) "...")
+       song)))
 
 (defun format-mpd-status (status)
   (let ((mpd-state (assoc-value :state status)))
@@ -180,7 +207,7 @@
          (progn (mpd-update-current-song)
                 (format nil "~a: ~a"
                         (format-mpd-status *mpd-status*)
-                        (format-mpd-current-song *mpd-current-song*)))))
+                        (format-mpd-current-song *mpd-current-song* t t)))))
     "Not connected"))
 
 ;;mpd commands
-- 
wbr, Vitaly

reply via email to

[Prev in Thread] Current Thread [Next in Thread]