Skip to content

Commit

Permalink
脚本更新
Browse files Browse the repository at this point in the history
  • Loading branch information
dyphire committed Aug 18, 2024
1 parent 846ef32 commit 8470f16
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 159 deletions.
2 changes: 0 additions & 2 deletions script-opts/adevice_list.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ indent=\h\h\h\h
num_entries=16
##是否循环滚动
wrap=yes
##打开菜单时重置光标导航
reset_cursor_on_close=yes
##菜单打开时绑定的按键
key_move_begin=HOME
key_move_end=END
Expand Down
2 changes: 0 additions & 2 deletions script-opts/chapter_list.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ num_entries=16
max_title_length=100
##是否循环滚动
wrap=yes
##打开菜单时重置光标导航
reset_cursor_on_close=yes
##菜单打开时绑定的按键
key_move_begin=HOME
key_move_end=END
Expand Down
2 changes: 0 additions & 2 deletions script-opts/edition_list.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ num_entries=16
max_title_length=100
##是否循环滚动
wrap=yes
##打开菜单时重置光标导航
reset_cursor_on_close=yes
##菜单打开时绑定的按键
key_move_begin=HOME
key_move_end=END
Expand Down
83 changes: 33 additions & 50 deletions scripts/adevice-list.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--[[
* adevice-list.lua v.2024-01-07
* adevice-list.lua v.2024-08-18
*
* AUTHORS: dyphire
* License: MIT
Expand Down Expand Up @@ -36,8 +36,6 @@ local o = {
num_entries = 16,
-- wrap the cursor around the top and bottom of the list
wrap = true,
-- reset cursor navigation when open the list
reset_cursor_on_close = true,
-- set dynamic keybinds to bind when the list is open
key_move_begin = "HOME",
key_move_end = "END",
Expand All @@ -56,7 +54,6 @@ package.path = mp.command_native({"expand-path", "~~/script-modules/?.lua;"}) ..
local list = require "scroll-list"

--modifying the list settings
local original_open = list.open
list.header = o.header
list.cursor = o.cursor
list.indent = o.indent
Expand All @@ -80,53 +77,45 @@ end

--update the list when the current audio-device changes
local function adevice_list()
list.list = {}
local adeviceList = mp.get_property_native('audio-device-list', {})
for i = 1, #adeviceList do
local item = {}
if (i == list.selected) then
current_name = adeviceList[i].name
item.style = o.active_style
item.ass = "" .. list.ass_escape(adeviceList[i].description)
else
item.ass = "" .. list.ass_escape(adeviceList[i].description)
end
list.list[i] = item
end
list:update()
mp.observe_property('audio-device-list', 'native', function(_, adevice_list)
mp.observe_property('audio-device', 'string', function(_, current_device)
list.list = {}
if adevice_list == nil then
list:update()
return
end
if list.selected > 0 and string.find(adevice_list[list.selected].name, current_device) == nil then
list.selected = 0
end
for i = 1, #adevice_list do
local item = {}
if current_device ~= nil then
if adevice_list[i].name == current_device then
list.selected = i
end
end
if (i == list.selected) then
item.style = o.active_style
item.ass = "" .. list.ass_escape(adevice_list[i].description)
else
item.ass = "" .. list.ass_escape(adevice_list[i].description)
end
list.list[i] = item
end
list:update()
end)
end)
list:toggle()
end

--open to the selected audio-device
local function open_adevice()
local adeviceList = mp.get_property_native('audio-device-list', {})
local adevice_list = mp.get_property_native('audio-device-list', {})
if list.list[list.selected] then
mp.set_property("audio-device", adeviceList[list.selected].name)
mp.set_property("audio-device", adevice_list[list.selected].name)
end
end

--reset cursor navigation when open the list
local function reset_cursor()
local adeviceList = mp.get_property_native('audio-device-list', {})
if current_name ~= nil and list.selected > 0 then
if string.find(adeviceList[list.selected].name, current_name) == nil then
list.selected = 0
end
end
for s = 1, #adeviceList do
if current_name ~= nil then
if adeviceList[s].name == current_name then list.selected = s end
end
end
if o.reset_cursor_on_close and list.selected > 0 then
list:update()
end
end

function list:open()
reset_cursor()
original_open(self)
end

--dynamic keybinds to bind when the list is open
list.keybinds = {}

Expand All @@ -147,13 +136,7 @@ add_keys(o.key_move_end, 'move_end', function() list:move_end() end, {})
add_keys(o.key_open_adevice, 'open_adevice', open_adevice, {})
add_keys(o.key_close_browser, 'close_browser', function() list:close() end, {})

mp.observe_property('audio-device', 'string', adevice_list)
mp.observe_property('audio-device-list', 'string', function()
reset_cursor()
adevice_list()
end)

mp.register_script_message("toggle-adevice-browser", function() list:toggle() end)
mp.register_script_message("toggle-adevice-browser", adevice_list)

mp.register_event('end-file', function()
list:close()
Expand Down
89 changes: 35 additions & 54 deletions scripts/chapter-list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ local o = {
max_title_length = 100,
-- wrap the cursor around the top and bottom of the list
wrap = true,
-- reset cursor navigation when open the list
reset_cursor_on_close = true,
-- set dynamic keybinds to bind when the list is open
key_move_begin = "HOME",
key_move_end = "END",
Expand Down Expand Up @@ -65,7 +63,6 @@ if not success then
end

--modifying the list settings
local original_open = list.open
list.header = o.header
list.cursor = o.cursor
list.indent = o.indent
Expand Down Expand Up @@ -110,32 +107,40 @@ local function utf8_sub(s, i, j)
end

--update the list when the current chapter changes
function chapter_list(curr_chapter)
list.list = {}
local chapter_list = mp.get_property_native('chapter-list', {})
for i = 1, #chapter_list do
local item = {}
if (i - 1 == curr_chapter) then
if reset_curr then list.selected = curr_chapter + 1 end
item.style = o.active_style
end

local time = chapter_list[i].time
local title = chapter_list[i].title
if not title or title == '(unnamed)' or title == '' then
title = "Chapter " .. string.format("%02.f", i)
end
local title_clip = utf8_sub(title, 1, o.max_title_length)
if title ~= title_clip then
title = title_clip .. "..."
end
if time < 0 then time = 0
else time = math.floor(time) end
item.ass = string.format("[%02d:%02d:%02d]", math.floor(time / 60 / 60), math.floor(time / 60) % 60, time % 60)
item.ass = item.ass .. '\\h\\h\\h' .. list.ass_escape(title)
list.list[i] = item
end
list:update()
local function chapter_list()
mp.observe_property('chapter-list', 'native', function(_, chapter_list)
mp.observe_property('chapter', 'number', function(_, curr_chapter)
list.list = {}
if chapter_list == nil then
list:update()
return
end
for i = 1, #chapter_list do
local item = {}
if (i - 1 == curr_chapter) then
if reset_curr then list.selected = curr_chapter + 1 end
item.style = o.active_style
end

local time = chapter_list[i].time
local title = chapter_list[i].title
if not title or title == '(unnamed)' or title == '' then
title = "Chapter " .. string.format("%02.f", i)
end
local title_clip = utf8_sub(title, 1, o.max_title_length)
if title ~= title_clip then
title = title_clip .. "..."
end
if time < 0 then time = 0
else time = math.floor(time) end
item.ass = string.format("[%02d:%02d:%02d]", math.floor(time / 60 / 60), math.floor(time / 60) % 60, time % 60)
item.ass = item.ass .. '\\h\\h\\h' .. list.ass_escape(title)
list.list[i] = item
end
list:update()
end)
end)
list:toggle()
end

local function change_title_callback(user_input, err, chapter_index)
Expand Down Expand Up @@ -238,21 +243,6 @@ local function open_chapter()
end
end

--reset cursor navigation when open the list
local function reset_cursor()
if o.reset_cursor_on_close then
if mp.get_property('chapter') then
list.selected = mp.get_property_number('chapter') + 1
list:update()
end
end
end

function list:open()
reset_cursor()
original_open(self)
end

--dynamic keybinds to bind when the list is open
list.keybinds = {}

Expand All @@ -275,16 +265,7 @@ add_keys(o.key_close_browser, 'close_browser', function() list:close() end, {})
add_keys(o.key_remove_chapter, 'remove_chapter', remove_chapter, {})
add_keys(o.key_edit_chapter, 'edit_chapter', edit_chapter, {})

mp.register_script_message("toggle-chapter-browser", function() list:toggle() end)

mp.observe_property('chapter', 'number', function(_, curr_chapter)
chapter_list(curr_chapter)
end)

mp.observe_property('chapter-list', 'native', function()
local curr_chapter = mp.get_property_number("chapter")
if curr_chapter then chapter_list(curr_chapter) end
end)
mp.register_script_message("toggle-chapter-browser", chapter_list)

if user_input_module then
mp.add_hook("on_unload", 50, function() input.cancel_user_input() end)
Expand Down
78 changes: 29 additions & 49 deletions scripts/edition-list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ local o = {
max_title_length = 100,
-- wrap the cursor around the top and bottom of the list
wrap = true,
-- reset cursor navigation when open the list
reset_cursor_on_close = true,
-- set dynamic keybinds to bind when the list is open
key_move_begin = "HOME",
key_move_end = "END",
Expand Down Expand Up @@ -116,7 +114,6 @@ function changedFile()
end

--modifying the list settings
local original_open = list.open
list.header = o.header
list.cursor = o.cursor
list.indent = o.indent
Expand All @@ -139,26 +136,34 @@ function list:format_header_string(str)
end

--update the list when the current edition changes
function edition_list(curr_edition)
list.list = {}
local edition_list = mp.get_property_native('edition-list', {})
for i = 1, #edition_list do
local item = {}
local title = edition_list[i].title
if not title then title = "Edition " .. string.format("%02.f", i) end
if o.max_title_length > 0 and title:len() > o.max_title_length + 5 then
title = title:sub(1, o.max_title_length) .. " ..."
end
if (i - 1 == curr_edition) then
list.selected = curr_edition + 1
item.style = o.active_style
item.ass = "" .. list.ass_escape(title)
else
item.ass = "" .. list.ass_escape(title)
end
list.list[i] = item
end
list:update()
local function edition_list()
mp.observe_property('edition-list', 'native', function(_, edition_list)
mp.observe_property('current-edition', 'number', function(_, curr_edition)
list.list = {}
if edition_list == nil then
list:update()
return
end
for i = 1, #edition_list do
local item = {}
local title = edition_list[i].title
if not title then title = "Edition " .. string.format("%02.f", i) end
if o.max_title_length > 0 and title:len() > o.max_title_length + 5 then
title = title:sub(1, o.max_title_length) .. " ..."
end
if (i - 1 == curr_edition) then
list.selected = curr_edition + 1
item.style = o.active_style
item.ass = "" .. list.ass_escape(title)
else
item.ass = "" .. list.ass_escape(title)
end
list.list[i] = item
end
list:update()
end)
end)
list:toggle()
end

--jump to the selected edition
Expand All @@ -168,21 +173,6 @@ local function select_edition()
end
end

--reset cursor navigation when open the list
local function reset_cursor()
if o.reset_cursor_on_close then
if mp.get_property('editions') then
list.selected = mp.get_property_number('current-edition') + 1
list:update()
end
end
end

function list:open()
reset_cursor()
original_open(self)
end

--dynamic keybinds to bind when the list is open
list.keybinds = {}

Expand All @@ -203,17 +193,7 @@ add_keys(o.key_move_end, 'move_end', function() list:move_end() end, {})
add_keys(o.key_select_edition, 'select_edition', select_edition, {})
add_keys(o.key_close_browser, 'close_browser', function() list:close() end, {})

mp.register_script_message("toggle-edition-browser", function() list:toggle() end)

mp.observe_property('current-edition', 'number', function(_, curr_edition)
editionChanged()
edition_list(curr_edition)
end)

mp.observe_property('edition-list', 'native', function()
local curr_edition = mp.get_property_number("current-edition")
if curr_edition then edition_list(curr_edition) end
end)
mp.register_script_message("toggle-edition-browser", edition_list)

mp.register_event('file-loaded', main)
mp.register_event('end-file', function()
Expand Down

0 comments on commit 8470f16

Please sign in to comment.