Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
dyphire committed Oct 24, 2023
1 parent b8d2fae commit adc3f92
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 69 deletions.
3 changes: 2 additions & 1 deletion script-opts/uosc.conf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ speed_persistency=
# 控制所有菜单,如上下文菜单,字幕加载器/选择器等
menu_item_height=36
menu_min_width=220
menu_padding=1
# 确定激活搜索需要`/`或`ctrl+f`或者是否键入任何文本就足够了
# 启用后,如果菜单是 unicode 字符,则无法再使用打开菜单的相同键关闭菜单
menu_type_to_search=no
Expand Down Expand Up @@ -146,7 +147,7 @@ font=Noto Sans Mono CJK SC
font_scale=1
# 指定直接在视频顶部绘制时文本和图标的边框。默认值:1.2
text_border=1.2
# 指定按钮、菜单和所有其他矩形的边框半径。默认值:2
# 指定按钮、菜单和所有其他矩形的边框半径。默认值:4
border_radius=2
# 设置以逗号分隔的颜色覆盖列表,采用 RGB 十六进制格式:`rrggbb`
# 默认值: foreground=ffffff,foreground_text=000000,background=000000,background_text=ffffff,curtain=111111
Expand Down
15 changes: 12 additions & 3 deletions script-opts/youtube-download.conf
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,18 @@ youtube_dl_exe=yt-dlp
# 这允许监控下载进度
# 目前仅适用于 Windows 和新的 wt.exe 终端
open_new_terminal=no
# 如果 open_new_terminal_autoclose 为 yes,则终端窗口下载后将关闭,即使存在错误
open_new_terminal_autoclose=no
#open_new_terminal_timeout=3
#open_new_terminal_timeout=

# 设置打开新终端的命令(JSON 数组)
# 使用"$cwd"作为工作目录的占位符
# 使用"$cmd"作为下载命令的占位符
# 打开选项卡 `--tab` 的 xfce4-terminal 示例
# 下载完成后保持选项卡打开`-H`
# open_new_terminal_command=["xfce4-terminal", "--tab", "-H", "-x", "$cmd"]
# 在新窗口中打开选项卡 `-new-tab` 的 Windows 终端示例
# 下载完成后调用 `ytdlp` 并保持选项卡打开 `/K`。
# 将 `/K` 替换为 `/C` 以在下载完成后关闭选项卡。
#open_new_terminal_command=["wt", "-w", "ytdlp", "new-tab", "-d", "$cwd", "cmd", "/K", "$cmd"]

# 用于本地化 uosc 脚本子菜单的显示内容,默认为空
locale_content=[{"Download": "下载","Audio": "音频","Subtitles": "字幕","Select range": "选择范围","Video (Current quality)": "视频(当前质量)","Video (Default quality)": "视频(默认质量)","Video with subtitles": "视频和字幕","Current file is not a youtube video": "当前文件不受支持","Video (Config file)": "视频(使用配置文件)","Audio (Config file)": "音频(使用配置文件)","Subtitle (Config file)": "字幕(使用配置文件)","Video with subtitles (Config file)": "视频和字幕(使用配置文件)","Download whole mpv playlist": "下载整个播放列表"}]
63 changes: 38 additions & 25 deletions scripts/uosc/elements/Menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ function Menu:update_content_dimensions()
self.item_height = round(options.menu_item_height * state.scale)
self.min_width = round(options.menu_min_width * state.scale)
self.separator_size = round(1 * state.scale)
self.padding = round(2 * state.scale)
self.padding = round(options.menu_padding * state.scale)
self.gap = round(2 * state.scale)
self.font_size = round(self.item_height * 0.48 * options.font_scale)
self.font_size_hint = self.font_size - 1
Expand Down Expand Up @@ -322,7 +322,7 @@ function Menu:update_dimensions()
for _, menu in ipairs(self.all) do
local width = math.max(menu.search and menu.search.max_width or 0, menu.max_width)
menu.width = round(clamp(min_width, width, width_available))
local title_height = (menu.is_root and menu.title or menu.search) and self.scroll_step or 0
local title_height = (menu.is_root and menu.title or menu.search) and self.scroll_step + self.padding or 0
local max_height = height_available - title_height
local content_height = self.scroll_step * #menu.items
menu.height = math.min(content_height - self.item_spacing, max_height)
Expand Down Expand Up @@ -1105,15 +1105,17 @@ function Menu:render()
local end_index = math.ceil((menu.scroll_y + menu.height) / self.scroll_step)
local menu_rect = {
ax = ax,
ay = ay - (draw_title and self.scroll_step or 0) - self.padding,
ay = ay - (draw_title and self.scroll_step + self.padding or 0) - self.padding,
bx = bx,
by = by + self.padding,
}
local blur_selected_index = is_current and self.mouse_nav

-- Background
ass:rect(menu_rect.ax, menu_rect.ay, menu_rect.bx, menu_rect.by, {
color = bg, opacity = menu_opacity * config.opacity.menu, radius = state.radius + self.padding,
color = bg,
opacity = menu_opacity * config.opacity.menu,
radius = state.radius > 0 and state.radius + self.padding or 0,
})

if is_parent then
Expand All @@ -1139,8 +1141,8 @@ function Menu:render()
local item_by = item_ay + self.item_height
local item_center_y = item_ay + (self.item_height / 2)
local item_clip = (item_ay < ay or item_by > by) and scroll_clip or nil
local content_ax, content_bx = ax + spacing, bx - spacing
local is_selected = menu.selected_index == index or item.active
local content_ax, content_bx = ax + self.padding + spacing, bx - self.padding - spacing
local is_selected = menu.selected_index == index

-- Select hovered item
if is_current and self.mouse_nav and item.selectable ~= false then
Expand All @@ -1156,28 +1158,32 @@ function Menu:render()
if submenu_is_hovered or get_point_to_rectangle_proximity(cursor, item_rect_hitbox) == 0 then
blur_selected_index = false
menu.selected_index = index
if not is_selected then request_render() end
end
end
end

local has_background = is_selected or item.active
local next_item = menu.items[index + 1]
local next_is_active = next_item and next_item.active
local next_is_highlighted = menu.selected_index == index + 1 or next_is_active
local next_has_background = menu.selected_index == index + 1 or next_is_active
local font_color = item.active and fgt or bgt

-- Separator
local separator_ay = item.separator and item_by - 1 or item_by
local separator_by = item_by + (item.separator and 2 or 1)
if is_selected then separator_ay = item_by + 1 end
if next_is_highlighted then separator_by = item_by end
if separator_by - separator_ay > 0 and item_by < by then
ass:rect(ax + spacing / 2, separator_ay, bx - spacing / 2, separator_by, {
color = fg, opacity = menu_opacity * (item.separator and 0.08 or 0.06),
if item_by < by and ((not has_background and not next_has_background) or item.separator) then
local separator_ay, separator_by = item_by, item_by + 1
if has_background then
separator_ay, separator_by = separator_ay + 1, separator_by + 1
elseif next_has_background then
separator_ay, separator_by = separator_ay - 1, separator_by - 1
end
ass:rect(ax + spacing, separator_ay, bx - spacing, separator_by, {
color = fg, opacity = menu_opacity * (item.separator and 0.13 or 0.04),
})
end

-- Highlight
local highlight_opacity = 0 + (item.active and 0.8 or 0) + (menu.selected_index == index and 0.15 or 0)
-- Background
local highlight_opacity = 0 + (item.active and 0.8 or 0) + (is_selected and 0.15 or 0)
if not is_submenu and highlight_opacity > 0 then
ass:rect(ax + self.padding, item_ay, bx - self.padding, item_by, {
radius = state.radius,
Expand Down Expand Up @@ -1246,9 +1252,14 @@ function Menu:render()

-- Menu title
if draw_title then
local title_height = self.item_height - 3
local title_height = self.item_height + self.padding - 3
local requires_submit = menu.search_debounce == 'submit'
local rect = {ax = ax + spacing, ay = ay - self.scroll_step, bx = bx - spacing, by = math.min(by, ay - 2)}
local rect = {
ax = ax + spacing / 2 + self.padding,
ay = ay - self.scroll_step - self.padding * 2,
bx = bx - spacing / 2 - self.padding,
by = math.min(by, ay - self.padding),
}
rect.cx, rect.cy = rect.ax + (rect.bx - rect.ax) / 2, rect.ay + (rect.by - rect.ay) / 2 -- centers

if menu.title and not menu.ass_safe_title then
Expand All @@ -1257,18 +1268,20 @@ function Menu:render()

-- Background
if menu.search then
ass:rect(ax + 3, rect.ay, bx - 3, rect.ay + title_height - 2, {
color = fg .. '\\1a&HFF', opacity = menu_opacity * 0.1, radius = state.radius,
ass:rect(ax + 3, rect.ay + 3, bx - 3, rect.ay + title_height - 1, {
color = fg .. '\\1a&HFF', opacity = menu_opacity * 0.1,
radius = state.radius > 0 and state.radius + self.padding or 0,
border = 1, border_color = fg, border_opacity = menu_opacity * 0.8
})
ass:texture(ax + 3, rect.ay, bx - 3, rect.ay + title_height - 2, 'n', {
size = 80, color = bg, opacity = menu_opacity * 0.1, anchor_x = ax + 2, anchor_y = rect.ay,
ass:texture(ax + 3, rect.ay + 3, bx - 3, rect.ay + title_height - 1, 'n', {
size = 80, color = bg, opacity = menu_opacity * 0.1, anchor_x = ax + 2, anchor_y = rect.ay + 2,
})
else
ass:rect(ax + 2, rect.ay - 1, bx - 2, rect.ay + title_height - 1, {
color = fg, opacity = menu_opacity * 0.8, radius = state.radius,
ass:rect(ax + 2, rect.ay + 2, bx - 2, rect.ay + title_height, {
color = fg, opacity = menu_opacity * 0.8,
radius = state.radius > 0 and state.radius + self.padding or 0,
})
ass:texture(ax + 2, rect.ay - 1, bx - 2, rect.ay + title_height - 1, 'n', {
ass:texture(ax + 2, rect.ay + 2, bx - 2, rect.ay + title_height, 'n', {
size = 80, color = bg, opacity = menu_opacity * 0.1,
})
end
Expand Down
3 changes: 2 additions & 1 deletion scripts/uosc/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ defaults = {

menu_item_height = 36,
menu_min_width = 260,
menu_padding = 4,
menu_type_to_search = true,

top_bar = 'no-border',
Expand All @@ -67,7 +68,7 @@ defaults = {
font = '',
font_scale = 1,
text_border = 1.2,
border_radius = 2,
border_radius = 4,
color = '',
opacity = '',
animation_duration = 100,
Expand Down
103 changes: 64 additions & 39 deletions scripts/youtube-download.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,21 @@ local opts = {
download_subtitle_config_file = "",
download_video_embed_subtitle_config_file= "",

-- Open a new "Windows Terminal" window/tab for download
-- Open a new terminal window/tab for download
-- This allows you to monitor the download progress
-- Currently only works on Windows with the new wt terminal
-- If open_new_terminal_autoclose is true, then the terminal window
-- will close after the download, even if there were errors
-- If mpv_playlist is true and the whole mpv playlist should be
-- downloaded, then all the downloads are scheduled immediately.
-- Before each download is started, the script waits the given
-- timeout in seconds
open_new_terminal = false,
open_new_terminal_autoclose = false,
open_new_terminal_timeout = 3,
-- Set the command that opens a new terminal (JSON array)
-- Use "$cwd" as a placeholder for the working directory
-- Use "$cmd" as a placeholder for the download command
-- See .conf file for Windows and xfce examples.
open_new_terminal_command = [[
["wt", "-w", "ytdlp", "new-tab", "-d", "$cwd", "cmd", "/K", "$cmd"]
]],

-- Used to localize uosc-submenu content
-- Must use json format, example for Chinese: [{"Download": "下载","Audio": "音频"}]
Expand Down Expand Up @@ -161,13 +164,14 @@ local function exec(args, capture_stdout, capture_stderr)
return ret.status, ret.stdout, ret.stderr, ret
end

local function exec_async(args, capture_stdout, capture_stderr, callback)
local function exec_async(args, capture_stdout, capture_stderr, detach, callback)
return mp.command_native_async({
name = "subprocess",
playback_only = false,
capture_stdout = capture_stdout,
capture_stderr = capture_stderr,
args = args,
detach = detach,
}, callback)
end

Expand Down Expand Up @@ -209,6 +213,9 @@ end

--Read text string
local locale_content = utils.parse_json(opts.locale_content)
local open_new_terminal_command = utils.parse_json(opts.open_new_terminal_command)

local is_windows = package.config:sub(1, 1) == "\\"

local function locale(str)
if str and locale_content then
Expand Down Expand Up @@ -241,7 +248,7 @@ local function detect_executable()
opts.youtube_dl_exe = table.remove(executables, 1)
if opts.youtube_dl_exe ~= nil then
msg.debug("Trying executable '" .. opts.youtube_dl_exe .. "' ...")
exec_async({opts.youtube_dl_exe, "--version"}, false, false, detect_executable_callback)
exec_async({opts.youtube_dl_exe, "--version"}, false, false, false, detect_executable_callback)
else
msg.error("No working executable found, using fallback 'youtube-dl'")
opts.youtube_dl_exe = "youtube-dl"
Expand All @@ -266,7 +273,6 @@ end

--create opts.download_path if it doesn't exist
if not_empty(opts.download_path) and utils.readdir(opts.download_path) == nil then
local is_windows = package.config:sub(1, 1) == "\\"
local windows_args = { 'powershell', '-NoProfile', '-Command', 'mkdir', string.format("\"%s\"", opts.download_path) }
local unix_args = { 'mkdir', '-p', opts.download_path }
local args = is_windows and windows_args or unix_args
Expand Down Expand Up @@ -943,42 +949,61 @@ local function download(download_type, config_file, overwrite_opts)

-- Check working directory is writable (in case the filename does not specify a directory)
local cwd = utils.getcwd()
local win_programs = "C:\\Program Files"
local win_win = "C:\\Windows"
if cwd:lower():sub(1, #win_programs) == win_programs:lower() or cwd:lower():sub(1, #win_win) == win_win:lower() then
msg.debug("The mpv working directory ('" ..cwd .."') is probably not writable. Trying %USERPROFILE%...")
local user_profile = os.getenv("USERPROFILE")
if user_profile ~= nil then
cwd = user_profile
else
msg.warn("open_new_terminal is enabled, but %USERPROFILE% is not defined")
mp.osd_message("open_new_terminal is enabled, but %USERPROFILE% is not defined", 3)
end
local has_cwd = false
for _, value in pairs(open_new_terminal_command) do
if value == "$cwd" then
has_cwd = true
break
end
end
if has_cwd then
local win_programs = "C:\\Program Files"
local win_win = "C:\\Windows"
if cwd:lower():sub(1, #win_programs) == win_programs:lower() or cwd:lower():sub(1, #win_win) == win_win:lower() then
msg.debug("The mpv working directory ('" ..cwd .."') is probably not writable. Trying %USERPROFILE%...")
local user_profile = os.getenv("USERPROFILE")
if user_profile ~= nil then
cwd = user_profile
else
msg.warn("open_new_terminal is enabled, but %USERPROFILE% is not defined")
mp.osd_message("open_new_terminal is enabled, but %USERPROFILE% is not defined", 3)
end
end
end

-- Escape restricted characters on Windows
local restricted = "&<>|"
for key, value in ipairs(command) do
command[key] = value:gsub("[".. restricted .. "]", "^%0")
if is_windows then
local restricted = "&<>|"
for key, value in ipairs(command) do
command[key] = value:gsub("[".. restricted .. "]", "^%0")
end
end

-- Prepend command with wt.exe
table.insert(command, 1, "wt")
table.insert(command, 2, "-w")
table.insert(command, 3, "ytdlp")
table.insert(command, 4, "new-tab")
table.insert(command, 5, "-d")
table.insert(command, 6, cwd)
table.insert(command, 7, "cmd")
if opts.open_new_terminal_autoclose then
table.insert(command, 8, "/C")
else
table.insert(command, 8, "/K")
-- Prepend command with open_new_terminal_command
local i = 1
local inserted_cmd = false
for _, value in pairs(open_new_terminal_command) do
if value == "$cwd" then
table.insert(command, i, cwd)
elseif value == "$cmd" then
inserted_cmd = true
elseif inserted_cmd then
table.insert(command, value) -- append after command
else
table.insert(command, i, value) -- prepend before command
end
i = i + 1
end
msg.debug("exec (async): " .. table.concat(command, " "))
end

process_id = exec_async(command, true, true, download_ended)
process_id = exec_async(
command,
not opts.open_new_terminal,
not opts.open_new_terminal,
opts.open_new_terminal,
download_ended
)

end

Expand Down Expand Up @@ -1071,10 +1096,10 @@ local function select_range()
else
select_range_mode = 1
-- Add keybinds for arrow keys
mp.add_key_binding("up", "select-range-set-up", select_range_set_end)
mp.add_key_binding("down", "select-range-set-down", select_range_set_start)
mp.add_key_binding("left", "select-range-set-left", select_range_set_left)
mp.add_key_binding("right", "select-range-set-right", select_range_set_right)
mp.add_forced_key_binding("up", "select-range-set-up", select_range_set_end)
mp.add_forced_key_binding("down", "select-range-set-down", select_range_set_start)
mp.add_forced_key_binding("left", "select-range-set-left", select_range_set_left)
mp.add_forced_key_binding("right", "select-range-set-right", select_range_set_right)

-- Defaults
if start_time_seconds == nil then
Expand Down

0 comments on commit adc3f92

Please sign in to comment.