-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinit.lua
231 lines (206 loc) · 6.17 KB
/
init.lua
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
local M = {}
function GetPath(str)
local sep = '/'
if ya.target_family() == "windows" then
sep = '\\'
end
return str:match("(.*"..sep..")")
end
function Exiftool(...)
local child = Command("exiftool")
:args({
"-q", "-q", "-S", "-Title", "-SortName",
"-TitleSort", "-TitleSortOrder", "-Artist",
"-SortArtist", "-ArtistSort", "-PerformerSortOrder",
"-Album", "-SortAlbum", "-AlbumSort", "-AlbumSortOrder",
"-AlbumArtist", "-SortAlbumArtist", "-AlbumArtistSort",
"-AlbumArtistSortOrder", "-Genre", "-TrackNumber",
"-Year", "-Duration", "-SampleRate",
"-AudioSampleRate", "-AudioBitrate", "-AvgBitrate",
"-Channels", "-AudioChannels", tostring(...),
})
:stdout(Command.PIPED)
:stderr(Command.NULL)
:spawn()
return child
end
function Mediainfo(...)
local file, cache_dir = ...
local template = cache_dir.."mediainfo.txt"
local child = Command("mediainfo")
:args({
"--Output=file://"..template, tostring(file)
})
:stdout(Command.PIPED)
:stderr(Command.NULL)
:spawn()
return child
end
function M:peek(job)
local cache = ya.file_cache(job)
if not cache then
return
end
-- Get cache dir to find the mediainfo template file
local cache_dir = GetPath(tostring(cache))
-- Try mediainfo, otherwise use exiftool
local status, child = pcall(Mediainfo, job.file.url, cache_dir)
if not status or child == nil then
status, child = pcall(Exiftool, job.file.url)
if not status or child == nil then
local error = ui.Line { ui.Span("Make sure exiftool is installed and in your PATH") }
-- TODO)) Remove legacy method when v0.4 gets released
local function display_error_legacy()
local p = ui.Paragraph(job.area, { error }):wrap(ui.Paragraph.WRAP)
ya.preview_widgets(job, { p })
end
local function display_error()
local p = ui.Text(error):area(job.area):wrap(ui.Text.WRAP)
ya.preview_widgets(job, { p })
end
if pcall(display_error) then else pcall(display_error_legacy) end
return
end
end
local limit = job.area.h
local i, metadata = 0, {}
repeat
local next, event = child:read_line()
if event == 1 then
return self:fallback_to_builtin()
elseif event ~= 0 then
break
end
i = i + 1
if i > job.skip then
local m_title, m_tag = Prettify(next)
if m_title ~= "" and m_tag ~= "" then
local ti = ui.Span(m_title):bold()
local ta = ui.Span(m_tag)
table.insert(metadata, ui.Line{ti, ta})
table.insert(metadata, ui.Line{})
end
end
until i >= job.skip + limit
-- TODO)) Remove legacy method when v0.4 gets released
local function display_metadata_legacy()
local p = ui.Paragraph(job.area, metadata):wrap(ui.Paragraph.WRAP)
ya.preview_widgets(job, { p })
end
local function display_metadata()
local p = ui.Text(metadata):area(job.area):wrap(ui.Text.WRAP)
ya.preview_widgets(job, { p })
end
if pcall(display_metadata) then else pcall(display_metadata_legacy) end
local cover_width = job.area.w / 2 - 5
local cover_height = (job.area.h / 4) + 3
local bottom_right = ui.Rect {
x = job.area.right - cover_width,
y = job.area.bottom - cover_height,
w = cover_width,
h = cover_height,
}
if self:preload(job) == 1 then
ya.image_show(cache, bottom_right)
end
end
function Prettify(metadata)
local substitutions = {
Sortname = "Sort Title:",
SortName = "Sort Title:",
TitleSort = "Sort Title:",
TitleSortOrder = "Sort Title:",
ArtistSort = "Sort Artist:",
SortArtist = "Sort Artist:",
Artist = "Artist:",
ARTIST = "Artist:",
PerformerSortOrder = "Sort Artist:",
SortAlbumArtist = "Sort Album Artist:",
AlbumArtistSortOrder = "Sort Album Artist:",
AlbumArtistSort = "Sort Album Artist:",
AlbumSortOrder = "Sort Album:",
AlbumSort = "Sort Album:",
SortAlbum = "Sort Album:",
Album = "Album:",
ALBUM = "Album:",
AlbumArtist = "Album Artist:",
Genre = "Genre:",
GENRE = "Genre:",
TrackNumber = "Track Number:",
Year = "Year:",
Duration = "Duration:",
AudioBitrate = "Bitrate:",
AvgBitrate = "Average Bitrate:",
AudioSampleRate = "Sample Rate:",
SampleRate = "Sample Rate:",
AudioChannels = "Channels:"
}
for k, v in pairs(substitutions) do
metadata = metadata:gsub(tostring(k)..":", v, 1)
end
-- Separate the tag title from the tag data
local t={}
for str in string.gmatch(metadata , "([^"..":".."]+)") do
if str ~= "\n" then
table.insert(t, str)
else
table.insert(t, nil)
end
end
-- Add back semicolon to title, rejoin tag data if it happened to contain a semicolon
local title, tag_data = "", ""
if t[1] ~= nil then
title, tag_data = t[1]..":", table.concat(t, ":", 2)
end
return title, tag_data
end
function M:seek(job)
local h = cx.active.current.hovered
if h and h.url == job.file.url then
ya.manager_emit("peek", {
tostring(math.max(0, cx.active.preview.skip + job.units)),
only_if = tostring(job.file.url),
})
end
end
function M:preload(job)
local cache = ya.file_cache(job)
if not cache or fs.cha(cache) then
return 1
end
local mediainfo_template = 'General;"\
$if(%Track%,Title: %Track%,)\
$if(%Track/Sort%,Sort Title: %Track/Sort%,)\
$if(%Title/Sort%,Sort Title: %Title/Sort%,)\
$if(%TITLESORT%,Sort Title: %TITLESORT%,)\
$if(%Performer%,Artist: %Performer%,)\
$if(%Performer/Sort%,Sort Artist: %Performer/Sort%,)\
$if(%ARTISTSORT%,Sort Artist: %ARTISTSORT%,)\
$if(%Album%,Album: %Album%,)\
$if(%Album/Sort%,Sort Album: %Album/Sort%)\
$if(%ALBUMSORT%,Sort Album: %ALBUMSORT%)\
$if(%Album/Performer%,Album Artist: %Album/Performer%)\
$if(%Album/Performer/Sort%,Sort Album Artist: %Album/Performer/Sort%)\
$if(%Genre%,Genre: %Genre%)\
$if(%Track/Position%,Track Number: %Track/Position%)\
$if(%Recorded_Date%,Year: %Recorded_Date%)\
$if(%Duration/String%,Duration: %Duration/String%)\
$if(%BitRate/String%,Bitrate: %BitRate/String%)\
"\
Audio;"Sample Rate: %SamplingRate%\
Channels: %Channel(s)%"\
'
-- Write the mediainfo template file into yazi's cache dir
local cache_dir = GetPath(tostring(cache))
fs.write(Url(cache_dir.."mediainfo.txt"), mediainfo_template)
local output = Command("exiftool")
:args({ "-b", "-CoverArt", "-Picture", tostring(job.file.url) })
:stdout(Command.PIPED)
:stderr(Command.PIPED)
:output()
if not output then
return 0
end
return fs.write(cache, output.stdout) and 1 or 2
end
return M