summaryrefslogtreecommitdiffabout
path: root/scheme/idest/format/lyrics.scm
blob: 1ab5f9680e86854e1e03152b16265e46ba3b7f1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
;; This file is part of Idest
;; Copyright (C) 2011, 2017 Sergey Poznyakoff
;;
;; Idest is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; Idest is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Idest.  If not, see <http://www.gnu.org/licenses/>.

(define-module (idest format lyrics))

(use-modules (ice-9 getopt-long)
	     (ice-9 popen))

(define-public description
  "display lyrics (the USLT content), if present in the tags")

(define-public idest-main #f)

(define-public (idest-init)
  (let* ((cmd (command-line))
	 (progname (car cmd))
	 (lang #f)
	 (condesc #f)
	 (grammar `((lang (single-char #\l) (value #t))
		    (content (single-char #\c) (value #t))
		    (help (single-char #\h)))))
    (catch 'misc-error
	   (lambda ()
	     (for-each
	      (lambda (x)
		(case (car x)
		  ((file)
		   (set! file (cdr x)))
		  ((lang)
		   (set! lang (cdr x)))
		  ((content)
		   (set! condesc (cdr x)))
		  ((help)
		   (format #t "usage: idest --format=~A [OPTIONS] FILE...\n"
			   progname)
		   (format #t "displays the USLT (unsynchronised lyric text) frame\n")
		   (format #t "OPTIONS are:\n")
		   (format #t "  -l, --lang NAME    set language in which the lyrics is writen (default: eng)\n")
		   (format #t "  -c, --content TEXT set content description\n")
		   (format #t "  -h, --help         show this help summary\n")
		   (exit 0))
		  (else
		   (set-program-arguments (cons progname (cdr x))))))
	      (getopt-long cmd grammar)))
	   (lambda (key . args)
	     (with-output-to-port
		 (current-error-port)
	       (lambda ()
		 (format #t "~A: " progname)
		 (apply format #t (list-ref args 1) (list-ref args 2))
		 (newline)
		 (exit 1)))))
    (set! idest-main
	  (lambda (name frames)
	    (force-output (current-output-port))
	    (force-output (current-error-port))
	    (letrec ((title #f)
		     (print-lyrics (lambda (text)
				     (display (or title name))
				     (newline)
				     (newline)
				     (display text)
				     (newline))))
	      (call-with-current-continuation
	       (lambda (return)
		 (for-each
		  (lambda (elt)
		    (let ((frame-name (car elt))
			  (frame-attr (cdr elt)))
		      (cond
		       ((string=? frame-name "TIT2")
			(set! title (assoc-ref frame-attr 'text)))
		       ((and (string=? frame-name "USLT")
			     (or (not lang)
				 (string=? lang (assoc-ref frame-attr 'lang)))
			     (or (not condesc)
				 (string=? condesc
					   (assoc-ref frame-attr 'condesc))))
			(let ((text (assoc-ref frame-attr 'text)))
			  (cond
			   ((getenv "PAGER") =>
			    (lambda (pag)
			      (let ((port (open-output-pipe pag)))
				(with-output-to-port
				    port
				  (lambda ()
				    (print-lyrics text)))
				(close-pipe port))))
			   (else
			    (print-lyrics text)))
			  (return))))))
		  frames)
		 (format (current-error-port)  "~A: no lyrics~%" name))))))))

Return to:

Send suggestions and report system problems to the System administrator.