-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
219 lines (202 loc) · 5.28 KB
/
main.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
-- Immersive is licensed under the terms of the GNU GPL v3: https://www.gnu.org/licenses/; © 2021 Ben Kerman
require "utility.compat"
script_name = mp.get_script_name()
-- always log verbose messages unless the user overrides msg-level
mp.set_property("msg-level", (function()
local msg_lvl = mp.get_property("msg-level")
local new_msg_lvl = script_name .. "=v"
if msg_lvl and #msg_lvl ~= 0 then
new_msg_lvl = new_msg_lvl .. "," .. msg_lvl
end
return new_msg_lvl
end)())
local bus = require "systems.bus"
local ActiveSubLookup = require "interface.active_sub_lookup"
local anki = require "systems.anki"
local cfg = require "systems.config"
local DictTargetMenu = require "interface.dict_target_menu"
local export = require "systems.export"
local ExportMenu = require "interface.export_menu"
local helper = require "utility.helper"
local kbds = require "systems.key_bindings"
local Menu = require "interface.menu"
local menu_stack = require "interface.menu_stack"
local series_id = require "utility.series_id"
local msg = require "systems.message"
local SubSelect = require "interface.sub_select"
local Subtitle = require "systems.subtitle"
local sys = require "systems.system"
local TargetSelect = require "interface.target_select"
-- forward declaration
local menu
local autocopy = cfg.values.enable_autocopy
local function get_id_title(fn_name)
local value, custom = series_id[fn_name]()
if custom then return value
else
return {
style = {"menu_info", "unset"},
value and value or "unknown"
}
end
end
local infos = {
{
name = "Series ID",
display = function() return get_id_title("id") end
},
{
name = "Title",
display = function() return get_id_title("title") end
},
{
name = "Sub auto-copy",
display = function() return helper.display_bool(autocopy) end
},
{
name = "Screenshots",
display = function() return helper.display_bool(cfg.take_scrot) end
},
{
name = "Immersive version",
value = "1.4"
}
}
mp.register_event("file-loaded", function()
menu:redraw()
end)
local function copy_active_line(_, sub_text)
if not sub_text then sub_text = mp.get_property("sub-text") end
if sub_text and sub_text ~= "" then
sys.clipboard_write(sub_text)
end
end
local function toggle_autocopy()
if autocopy then
mp.osd_message("Subtitle auto-copy disabled")
mp.unobserve_property(copy_active_line)
else
mp.osd_message("Subtitle auto-copy enabled")
mp.observe_property("sub-text", "string", copy_active_line)
end
autocopy = not autocopy
menu:redraw()
end
if autocopy then
mp.observe_property("sub-text", "string", copy_active_line)
end
local function export_active_line(instant, use_export_menu)
local sub_text = helper.check_active_sub()
if sub_text then
local menu_type = use_export_menu and ExportMenu or TargetSelect
local export_fn = instant and export.execute or function(data)
menu_stack.push(menu_type:new(data))
end
export_fn{
subtitles = {
Subtitle:new(sub_text,
mp.get_property_number("sub-start"),
mp.get_property_number("sub-end"),
mp.get_property_number("sub-delay"))
}
}
end
end
local bindings = {
group = "global_menu",
{
id = "begin_sub_select",
default = "a",
desc = "Start line selection",
action = function() menu_stack.push(SubSelect:new()) end,
global = true
},
{
id = "open_global_menu",
default = "Ctrl+a",
desc = "Open the global menu",
action = function() menu_stack.push(menu) end,
global = true
},
{
id = "show_dict_target",
default = "Ctrl+A",
desc = "Show dictionary/target menu",
action = function() menu_stack.push(DictTargetMenu:new()) end,
global = true
},
{
id = "copy_active_line",
default = "c",
desc = "Copy current subtitle to clipboard",
action = copy_active_line,
global = true
},
{
id = "toggle_autocopy",
default = "C",
desc = "Toggle subtitle auto-copy",
action = toggle_autocopy,
global = true
},
{
id = "toggle_scrot",
default = "s",
desc = "Toggle screenshots on/off",
action = function() cfg.take_scrot = not cfg.take_scrot; menu:redraw() end
},
{
id = "lookup_word",
default = "k",
desc = "Select and look up word from active subtitle",
action = function() menu_stack.push(ActiveSubLookup:new()) end,
global = true
},
{
id = "export_active_line",
default = "K",
desc = "Create card from active subtitle, skip line selection",
action = export_active_line,
global = true
},
{
id = "export_active_line_instant",
default = "Ctrl+k",
desc = "Create card from active subtitle, export immediately",
action = function() export_active_line(true) end,
global = true
},
{
id = "export_active_line_menu",
default = "Ctrl+K",
desc = "Create card from active subtitle, open export menu",
action = function() export_active_line(false, true) end,
global = true
},
{
id = "close_menu",
default = "ESC",
desc = "Go back to previous menu",
action = menu_stack.pop,
global = true
},
{
id = "clear_menus",
default = "Shift+ESC",
desc = "Close all active menus",
action = menu_stack.clear,
global = true
},
{
id = "reload_config",
default = "r",
desc = "Reload all non-dictionary config files",
action = function()
menu_stack.clear()
bus.fire("reload_config")
end
}
}
menu = Menu:new{infos = infos, bindings = bindings}
kbds.create_global(bindings)
msg.end_startup()