-
Notifications
You must be signed in to change notification settings - Fork 3
/
dionysos-fs-mode.el
288 lines (231 loc) · 8.74 KB
/
dionysos-fs-mode.el
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
;;; dionysos-fs-mode.el --- Dionysos Filesystem mode
;; Copyright (C) 2015-2016 Nicolas Lamirault <nicolas.lamirault@gmail.com>
;; This program 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 2
;; of the License, or (at your option) any later version.
;; This program 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 this program; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301, USA.
;;; Commentary:
;;; Code:
(require 'widget)
(require 'cl-lib)
(require 'f)
(require 'dionysos-io)
(require 'dionysos-custom)
(require 'dionysos-backend-vlc)
(require 'dionysos-mode)
(require 'dionysos-volume)
;; ------------------
;; Customization
;; ------------------
(defgroup dionysos-fs-mode nil
"Customization group for `dionysos-fs-mode'."
:prefix "dionysos-fs-mode-"
:tag "Dionysos Filesystem Mode"
:group 'dionysos)
(defcustom dionysos-fs-mode-buffer "*dionysos-fs*"
"The Dionysos buffer name."
:type 'string
:group 'dionysos-fs-mode)
(defcustom dionysos-fs-mode-padding 2
"The number of columns used for padding on the left side of the buffer."
:type 'integer
:group 'dionysos-fs-mode)
;; ;; ------------------
;; ;; Faces
;; ;; ------------------
(defgroup dionysos-fs-mode-faces '((dionysos-fs-mode custom-group))
"Customization group for the faces of `dionysos-fs-mode'."
:prefix "dionysos-fs-mode-"
:tag "Dionysos filesystem-mode faces"
:group 'dionysos-fs-mode)
(defface dionysos-fs-mode-song-file
'((t :weight bold :inherit font-lock-warning-name-face))
"Face used on the song render in the Dionysos buffer."
:group 'dionysos-fs-mode-faces)
(defface dionysos-fs-mode-song-title
'((t :weight bold :inherit font-lock-warning-name-face))
"Face used on the song render in the Dionysos buffer."
:group 'dionysos-fs-mode-faces)
(defface dionysos-fs-mode-song-artist
'((t :inherit font-lock-comment-face))
"Face used on the song render in the Dionysos buffer"
:group 'dionysos-fs-mode-faces)
(defface dionysos-fs-mode-song-album
'((t :inherit font-lock-string-face))
"Face used on the song render in the Dionysos buffer"
:group 'dionysos-fs-mode-faces)
(defface dionysos-fs-mode-song-track
'((t :inherit font-lock-function-name-face))
"Face used on the song render in the Dionysos buffer"
:group 'dionysos-fs-mode-faces)
(defface dionysos-fs-mode-song-type
'((t :inherit font-lock-comment-face))
"Face used on the song render in the Dionysos buffer"
:group 'dionysos-fs-mode-faces)
;; ;; ------------------
;; ;; Backend I/O
;; ;; ------------------
(defun dionysos--fs-mode-start ()
"Start playing song."
(interactive)
(let ((song (dionysos--mode-current-media)))
(if song
(dionysos--with-backend
(funcall (dionysos--backend-start dionysos-backend)
(s-trim song)
'dionysos--fs-mode-next-action))
(message "[dionysos-fs] No song available"))))
(defun dionysos--fs-mode-stop ()
"Stop playing song."
(interactive)
(dionysos--with-backend
(funcall (dionysos--backend-stop dionysos-backend))))
(defun dionysos--fs-mode-next ()
"Play next song."
(interactive)
(dionysos--with-backend
(dionysos--mode-next-media)
(dionysos--fs-mode-stop)
(dionysos--fs-mode-start)))
(defun dionysos--fs-mode-previous ()
"Play previous song."
(interactive)
(dionysos--with-backend
(dionysos--mode-prev-media)
(dionysos--fs-mode-stop)
(dionysos--fs-mode-start)))
(defun dionysos--fs-mode-pause ()
"Pause playing song."
(interactive)
(dionysos--with-backend
(funcall (dionysos--backend-pause dionysos-backend))))
(defun dionysos--fs-mode-quit ()
"Stop player and exit."
(interactive)
(dionysos--with-backend
(dionysos--fs-mode-stop))
(kill-buffer dionysos-fs-mode-buffer))
(defun dionysos--fs-mode-next-action ()
"Next action after process end."
(dionysos--fs-mode-next)
(dionysos--fs-mode-start))
;; ;; ------------------
;; ;; UI
;; ;; ------------------
(defun dionysos--fs-mode-width ()
"Return the width of the renderable content."
(- (/ (frame-width) 2) (* dionysos-fs-mode-padding 2)))
(defun dionysos--fs-mode-horizontal-rule ()
"Insert a horizontal rule into the buffer."
(widget-insert
(concat (make-string dionysos-fs-mode-padding ?\s)
(make-string (- (dionysos--fs-mode-width) dionysos-fs-mode-padding) ?-)
(make-string dionysos-fs-mode-padding ?\s)
"\n")))
(defun dionysos--fs-mode-render-row (left right &optional width-right)
"Render a row with a `LEFT' and a `RIGHT' part.
Optional argument `WIDTH-RIGHT' is the width of the right argument."
(widget-insert (format "[%s] %s\n" right left)))
(defun dionysos--fs-mode-render-multilines-row (left right left-2 right-2 &optional width-right)
"Render a row with a `LEFT' and a `RIGHT' part.
Optional argument `WIDTH-RIGHT' is the width of the right argument."
(widget-insert (format "> %s - %s\n%s / %s\n" left right left-2 right-2)))
(defun dionysos--fs-mode-render-song (song)
"Render a `SONG' to the Dionysos buffer."
(message "Song: %s" song)
(if (executable-find "id3")
(let* ((tags (dionysos--id3-tag-info song))
(track (gethash "Track" tags))
(title (gethash "Title" tags))
(artist (gethash "Artist" tags))
(album (gethash "Album" tags)))
(dionysos--fs-mode-render-multilines-row
(if track
(format "%s" (propertize track 'face 'dionysos-fs-mode-song-track))
"")
(if title
(format "%s" (propertize title 'face 'dionysos-fs-mode-song-title))
(format "%s" (propertize (file-name-base song) 'face 'dionysos-fs-mode-song-type)))
(if artist
(format "%s" (propertize artist 'face 'dionysos-fs-mode-song-artist))
"")
(if album
(format "%s" (propertize album 'face 'dionysos-fs-mode-song-album))
"")))
(dionysos--fs-mode-render-row
(format "%s" (propertize (file-name-base song) 'face 'dionysos-fs-mode-song-file))
(format "%s" (propertize (file-name-extension song) 'face 'dionysos-fs-mode-song-type)))))
(defun dionysos--fs-mode-render (songs)
"Render `SONGS'."
(let ((start (point)))
;;(dionysos--horizontal-rule)
(cl-loop
for n from 1 to (length songs)
do (let ((song (elt songs (- n 1)))
(start (point)))
(dionysos--fs-mode-render-song song)
(put-text-property start (point) :dionysos-media song)))
(widget-insert "\n")))
;; ;; ------------------
;; ;; Mode
;; ;; ------------------
(defmacro dionysos--fs-mode-with-widget (title &rest body)
`(progn
(set-buffer (get-buffer-create dionysos-fs-mode-buffer))
(switch-to-buffer-other-window dionysos-fs-mode-buffer)
(kill-all-local-variables)
(let ((inhibit-read-only t))
(erase-buffer)
(remove-overlays)
(widget-insert (format "\n[%s]\n\n" ,title))
,@body)
(use-local-map widget-keymap)
(widget-setup)
(dionysos--fs-mode)
(widget-minor-mode)
(goto-char 0)))
(defvar dionysos--fs-mode-hook nil)
(defvar dionysos--fs-mode-map
(let ((map (make-keymap)))
(define-key map (kbd "p") 'dionysos--fs-mode-previous)
(define-key map (kbd "n") 'dionysos--fs-mode-next)
(define-key map (kbd "s") 'dionysos--fs-mode-start)
(define-key map (kbd "SPC") 'dionysos--fs-mode-stop)
(define-key map (kbd "P") 'dionysos--fs-mode-pause)
(define-key map (kbd "q") 'dionysos--fs-mode-quit)
(define-key map (kbd "+") 'dionysos-volume-raise)
(define-key map (kbd "-") 'dionysos-volume-decrease)
map)
"Keymap for `dionysos--fs-mode' major mode.")
(define-derived-mode dionysos--fs-mode tabulated-list-mode
"Dionysos Filesysteme mode"
"Major mode for Dionysos."
:group 'dionysos
)
;; ;; ------------------
;; ;; API
;; ;; ------------------
(defvar dionysos--fs-mode-history nil)
;;;###autoload
(defun dionysos-fs-list (directory)
"Show music files in `DIRECTORY'."
(interactive
(list (read-from-minibuffer "Music directory : "
(car dionysos--fs-mode-history)
nil
nil
'dionysos--fs-mode-history)))
(dionysos--fs-mode-with-widget
(propertize "Playlist")
(dionysos--fs-mode-render
(dionysos--list-directory directory '("ogg" "mp3" "wav" "flac")))))
(provide 'dionysos-fs-mode)
;;; dionysos-fs-mode.el ends here