Skip to content

Instantly share code, notes, and snippets.

@calebsmith
Created January 30, 2014 05:02
Show Gist options
  • Save calebsmith/8702912 to your computer and use it in GitHub Desktop.
Save calebsmith/8702912 to your computer and use it in GitHub Desktop.
First pass at Weechat plugin to control pianobar (CLI Pandora/Last.fm client)
(use-modules (ice-9 popen))
(weechat:register "piano" "MCConsALot" "0.1" "GPL3" "Control piano bar client" "" "")
(weechat:hook_command
"piano" "Control pianobar"
"[command]" "command to send to pianobar. Use /piano start, to begin"
"start next quit station send"
"main-command-handler" "")
(define pianobar-running #f)
(define pianobar-pipe #f)
(define (main-command-handler . args)
(let* (
(command (list-ref (car args) 2))
(cmd-l (string-tokenize command)))
(if (string=? command "start")
(piano-start))
(if (string=? command "quit")
(piano-quit))
(if (string=? command "next")
(piano-send "n"))
(if (and (string=? (car cmd-l) "station") (= (length cmd-l) 2))
(piano-send
(string-append "s" (list-ref cmd-l 1))))
(if (and (string=? (car cmd-l) "send") (= (length cmd-l) 2))
(piano-send (list-ref cmd-l 1))))
weechat:WEECHAT_RC_OK)
(define (piano-start)
(set! pianobar-running #t)
(set! pianobar-pipe (open-output-pipe "pianobar"))
weechat:WEECHAT_RC_OK)
(define (piano-quit)
(if (eq? pianobar-running #t)
(begin
(set! pianobar-running #f)
(display "q\n" pianobar-pipe)
(display "Piano - Quitting pianobar")
(newline)))
weechat:WEECHAT_RC_OK)
(define (piano-send cmd)
(if (eq? pianobar-running #t)
(let ((pianobar-command (newlinify cmd)))
(display pianobar-command pianobar-pipe))
(begin
(display "Pianobar is not running. use `/piano start` to start")
(newline)))
weechat:WEECHAT_RC_OK)
;Take a space separated string and return a string with no spaces, and a
;trailing newline
(define (newlinify input-str)
(string-append (apply string-append (string-tokenize input-str)) "\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment