diff --git a/player/lua/console.lua b/player/lua/console.lua index 9df5adccb1..d3dcb6bb96 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -12,17 +12,17 @@ -- OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN -- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -local utils = require 'mp.utils' -local assdraw = require 'mp.assdraw' +local utils = require "mp.utils" +local assdraw = require "mp.assdraw" local function detect_platform() - local platform = mp.get_property_native('platform') - if platform == 'darwin' or platform == 'windows' then + local platform = mp.get_property_native("platform") + if platform == "darwin" or platform == "windows" then return platform - elseif os.getenv('WAYLAND_DISPLAY') or os.getenv('WAYLAND_SOCKET') then - return 'wayland' + elseif os.getenv("WAYLAND_DISPLAY") or os.getenv("WAYLAND_SOCKET") then + return "wayland" end - return 'x11' + return "x11" end local platform = detect_platform() @@ -35,34 +35,34 @@ local opts = { background_alpha = 80, padding = 10, menu_outline_size = 0, - menu_outline_color = '#FFFFFF', + menu_outline_color = "#FFFFFF", corner_radius = 8, margin_x = -1, margin_y = -1, scale_with_window = "auto", - selected_color = '#222222', - selected_back_color = '#FFFFFF', - match_color = '#0088FF', - case_sensitive = platform ~= 'windows' and true or false, + selected_color = "#222222", + selected_back_color = "#FFFFFF", + match_color = "#0088FF", + case_sensitive = platform ~= "windows" and true or false, history_dedup = true, - font_hw_ratio = 'auto', + font_hw_ratio = "auto", } local styles = { - error = '{\\1c&H7a77f2&}', - completion = '{\\1c&Hcc99cc&}', + error = "{\\1c&H7a77f2&}", + completion = "{\\1c&Hcc99cc&}", } for key, style in pairs(styles) do - styles[key] = style .. '{\\3c&H111111&}' + styles[key] = style .. "{\\3c&H111111&}" end local terminal_styles = { - error = '\027[31m', - selected_completion = '\027[7m', - default_item = '\027[1m', - disabled = '\027[38;5;8m', - matched_position = '\027[34m', - match_end = '\027[39m', + error = "\027[31m", + selected_completion = "\027[7m", + default_item = "\027[1m", + disabled = "\027[38;5;8m", + matched_position = "\027[34m", + match_end = "\027[39m", } local open = false @@ -70,7 +70,7 @@ local osd_msg_active = false local insert_mode = false local pending_update = false local ime_active = false -local line = '' +local line = "" local cursor = 1 local prompt local id @@ -85,8 +85,8 @@ local histories_to_save = {} local log_buffers = {} local key_bindings = {} local dont_bind_up_down = false -local overlay = mp.create_osd_overlay('ass-events') -local width_overlay = mp.create_osd_overlay('ass-events') +local overlay = mp.create_osd_overlay("ass-events") +local width_overlay = mp.create_osd_overlay("ass-events") local global_margins = { t = 0, b = 0 } local input_caller local keep_open = false @@ -123,7 +123,7 @@ local function get_property_cached(name, def) end local function get_font() - if opts.font ~= '' then + if opts.font ~= "" then return opts.font end @@ -132,28 +132,28 @@ local function get_font() end -- Pick a better default font for Windows and macOS - if platform == 'windows' then - return 'Consolas' + if platform == "windows" then + return "Consolas" end - if platform == 'darwin' then - return 'Menlo' + if platform == "darwin" then + return "Menlo" end - return 'monospace' + return "monospace" end local function get_margin_x() - return opts.margin_x > -1 and opts.margin_x or mp.get_property_native('osd-margin-x') + return opts.margin_x > -1 and opts.margin_x or mp.get_property_native("osd-margin-x") end local function get_margin_y() - return opts.margin_y > -1 and opts.margin_y or mp.get_property_native('osd-margin-y') + return opts.margin_y > -1 and opts.margin_y or mp.get_property_native("osd-margin-y") end --- Naive helper function to find the next UTF-8 character in 'str' after 'pos' --- by skipping continuation bytes. Assumes 'str' contains valid UTF-8. +-- Naive helper function to find the next UTF-8 character in "str" after "pos" +-- by skipping continuation bytes. Assumes "str" contains valid UTF-8. local function next_utf8(str, pos) if pos > str:len() then return pos end repeat @@ -162,7 +162,7 @@ local function next_utf8(str, pos) return pos end --- As above, but finds the previous UTF-8 character in 'str' before 'pos' +-- As above, but finds the previous UTF-8 character in "str" before "pos" local function prev_utf8(str, pos) if pos <= 1 then return pos end repeat @@ -197,7 +197,7 @@ end -- Functions to calculate the font width. local width_length_ratio = 0.5 local osd_width, osd_height = 100, 100 -local text_osd = mp.create_osd_overlay('ass-events') +local text_osd = mp.create_osd_overlay("ass-events") text_osd.compute_bounds, text_osd.hidden = true, true local function measure_bounds(ass_text) @@ -211,7 +211,7 @@ end ---text has to be ass safe local function normalized_text_width(text, size, horizontal) local align, rotation = horizontal and 7 or 1, horizontal and 0 or -90 - local template = '{\\pos(0,0)\\rDefault\\blur0\\bord0\\shad0\\q2\\an%s\\fs%s\\fn%s\\frz%s}%s' + local template = "{\\pos(0,0)\\rDefault\\blur0\\bord0\\shad0\\q2\\an%s\\fs%s\\fn%s\\frz%s}%s" size = size / 0.8 local width -- Limit to 5 iterations @@ -250,7 +250,7 @@ local function get_font_hw_ratio() return font_hw_ratio end if not measured_font_hw_ratio then - local alphabet = 'abcdefghijklmnopqrstuvwxyz' + local alphabet = "abcdefghijklmnopqrstuvwxyz" local text = alphabet:rep(3) local size, horizontal = fit_on_osd(text) local normalized_width = normalized_text_width(text, size * 0.9, horizontal) @@ -262,7 +262,7 @@ end -- Escape a string for verbatim display on the OSD local function ass_escape(str) - return mp.command_native({'escape-ass', str}) + return mp.command_native({"escape-ass", str}) end local function should_scale() @@ -275,13 +275,13 @@ local function scale_factor() return osd_height / 720 end - return get_property_cached('display-hidpi-scale', 1) + return get_property_cached("display-hidpi-scale", 1) end local function terminal_output() -- Unlike vo-configured, current-vo doesn't become falsy while switching VO, -- which would print the log to the OSD. - return not mp.get_property('current-vo') or not mp.get_property_native('video-osd') + return not mp.get_property("current-vo") or not mp.get_property_native("video-osd") end local function get_scaled_osd_dimensions() @@ -298,8 +298,8 @@ local function calculate_max_lines() if terminal_output() then -- Subtract 1 for the input line and for each line in the status line. -- This does not detect wrapped lines. - return mp.get_property_native('term-size/h', 24) - 2 - - select(2, mp.get_property('term-status-msg'):gsub('\\n', '')) + return mp.get_property_native("term-size/h", 24) - 2 - + select(2, mp.get_property("term-status-msg"):gsub("\\n", "")) end return math.floor((select(2, get_scaled_osd_dimensions()) @@ -316,7 +316,7 @@ local function calculate_max_item_width() return end - local longest_item = prompt .. ('a'):rep(9) + local longest_item = prompt .. ("a"):rep(9) for _, item in pairs(selectable_items) do if #item > #longest_item then longest_item = item @@ -329,8 +329,8 @@ local function calculate_max_item_width() width_overlay.hidden = true width_overlay.res_x = osd_w width_overlay.res_y = osd_h - width_overlay.data = '{\\fs' .. opts.font_size .. - (font and '\\fn' .. font or '') .. '\\q2}' .. + width_overlay.data = "{\\fs" .. opts.font_size .. + (font and "\\fn" .. font or "") .. "\\q2}" .. ass_escape(longest_item) local result = width_overlay:update() max_item_width = math.min(result.x1 - result.x0, @@ -344,7 +344,7 @@ end local function mpv_color_to_ass(color) return color:sub(8,9) .. color:sub(6,7) .. color:sub(4,5), - string.format('%x', 255 - tonumber('0x' .. color:sub(2,3))) + string.format("%x", 255 - tonumber("0x" .. color:sub(2,3))) end local function option_color_to_ass(color) @@ -352,11 +352,11 @@ local function option_color_to_ass(color) end local function get_selected_ass() - local color, alpha = mpv_color_to_ass(mp.get_property('osd-selected-color')) + local color, alpha = mpv_color_to_ass(mp.get_property("osd-selected-color")) local outline_color, outline_alpha = - mpv_color_to_ass(mp.get_property('osd-selected-outline-color')) - return '{\\b1\\1c&H' .. color .. '&\\1a&H' .. alpha .. - '&\\3c&H' .. outline_color .. '&\\3a&H' .. outline_alpha .. '&}' + mpv_color_to_ass(mp.get_property("osd-selected-outline-color")) + return "{\\b1\\1c&H" .. color .. "&\\1a&H" .. alpha .. + "&\\3c&H" .. outline_color .. "&\\3a&H" .. outline_alpha .. "&}" end -- Takes a list of strings, a max width in characters and @@ -366,7 +366,7 @@ end -- returns a string containing the formatted table and the row count local function format_grid(list, width_max, rows_max) if #list == 0 then - return '', 0 + return "", 0 end local spaces_min = 2 @@ -426,8 +426,8 @@ local function format_grid(list, width_max, rows_max) local spaces = math.floor((width_max - width_total) / (column_count - 1)) spaces = math.max(spaces_min, math.min(spaces_max, spaces)) local spacing = column_count > 1 - and ass_escape(string.format('%' .. spaces .. 's', ' ')) - or '' + and ass_escape(string.format("%" .. spaces .. "s", " ")) + or "" local rows = {} for row = 1, row_count do @@ -435,24 +435,24 @@ local function format_grid(list, width_max, rows_max) for column = 1, column_count do local i = row + (column - 1) * row_count if i > #list then break end - -- more then 99 leads to 'invalid format (width or precision too long)' - local format_string = column == column_count and '%s' - or '%-' .. math.min(column_widths[column], 99) .. 's' + -- more then 99 leads to "invalid format (width or precision too long)" + local format_string = column == column_count and "%s" + or "%-" .. math.min(column_widths[column], 99) .. "s" columns[column] = ass_escape(string.format(format_string, list[i])) if should_highlight_completion(i) then columns[column] = get_selected_ass() .. columns[column] .. - '{\\b\\1a&\\3a&}' .. styles.completion + "{\\b\\1a&\\3a&}" .. styles.completion end end -- first row is at the bottom rows[row_count - row + 1] = table.concat(columns, spacing) end - return table.concat(rows, ass_escape('\n')), row_count + return table.concat(rows, ass_escape("\n")), row_count end local function fuzzy_find(needle, haystacks, case_sensitive) - local result = require 'mp.fzy'.filter(needle, haystacks, case_sensitive) + local result = require "mp.fzy".filter(needle, haystacks, case_sensitive) table.sort(result, function (i, j) if i[3] ~= j[3] then return i[3] > j[3] @@ -473,7 +473,7 @@ local function get_matches_to_print(terminal) local max_lines = calculate_max_lines() local escape = terminal and function (str) return str end or ass_escape local highlight = terminal and terminal_styles.matched_position or - '{\\1c&H' .. option_color_to_ass(opts.match_color) .. '}' + "{\\1c&H" .. option_color_to_ass(opts.match_color) .. "}" if selected_match < first_match_to_print then first_match_to_print = selected_match @@ -485,8 +485,8 @@ local function get_matches_to_print(terminal) #matches) for i = first_match_to_print, last_match_to_print do - local item = '' - local end_highlight = terminal and terminal_styles.match_end or '{\\1c}' + local item = "" + local end_highlight = terminal and terminal_styles.match_end or "{\\1c}" if terminal then if matches[i].index == default_item then @@ -498,10 +498,10 @@ local function get_matches_to_print(terminal) else if i == selected_match then if searching_history and - mp.get_property('osd-border-style') == 'outline-and-shadow' then + mp.get_property("osd-border-style") == "outline-and-shadow" then item = get_selected_ass() else - item = '{\\1c&H' .. option_color_to_ass(opts.selected_color) .. '&}' + item = "{\\1c&H" .. option_color_to_ass(opts.selected_color) .. "&}" end end_highlight = item end @@ -571,52 +571,52 @@ local function print_to_terminal() -- Clear the log after closing the console. if not open then if osd_msg_active then - mp.osd_message('') + mp.osd_message("") end osd_msg_active = false return end - local log = '' - local counter = '' + local log = "" + local counter = "" if selectable_items then if #selectable_items > calculate_max_lines() then local digits = math.ceil(math.log(#selectable_items, 10)) counter = terminal_styles.disabled .. - '[' .. string.format('%0' .. digits .. 'd', selected_match) .. - '/' .. string.format('%0' .. digits .. 'd', #matches) .. - ']\027[0m ' + "[" .. string.format("%0" .. digits .. "d", selected_match) .. + "/" .. string.format("%0" .. digits .. "d", #matches) .. + "]\027[0m " end - local clip = mp.get_property('term-clip-cc') + local clip = mp.get_property("term-clip-cc") for _, item in ipairs(get_matches_to_print(true)) do - log = log .. clip .. item .. '\027[0m\n' + log = log .. clip .. item .. "\027[0m\n" end else for _, log_line in ipairs(log_buffers[id]) do - log = log .. log_line.terminal_style .. log_line.text .. '\027[0m\n' + log = log .. log_line.terminal_style .. log_line.text .. "\027[0m\n" end for i, completion in ipairs(completion_buffer) do if should_highlight_completion(i) then log = log .. terminal_styles.selected_completion .. - completion .. '\027[0m' + completion .. "\027[0m" else log = log .. completion end - log = log .. (i < #completion_buffer and '\t' or '\n') + log = log .. (i < #completion_buffer and "\t" or "\n") end end local before_cur = line:sub(1, cursor - 1) local after_cur = line:sub(cursor) -- Ensure there is a character with inverted colors to print. - if after_cur == '' then - after_cur = ' ' + if after_cur == "" then + after_cur = " " end - mp.osd_message(log .. counter .. prompt .. ' ' .. before_cur .. '\027[7m' .. - after_cur:sub(1, 1) .. '\027[0m' .. after_cur:sub(2), 999) + mp.osd_message(log .. counter .. prompt .. " " .. before_cur .. "\027[7m" .. + after_cur:sub(1, 1) .. "\027[0m" .. after_cur:sub(2), 999) osd_msg_active = true end @@ -630,13 +630,13 @@ local function render() -- Clear the OSD if the console was being printed to the terminal if osd_msg_active then - mp.osd_message('') + mp.osd_message("") osd_msg_active = false end -- Clear the OSD after closing the console if not open then - update_overlay('', 0, 0, 0) + update_overlay("", 0, 0, 0) return end @@ -650,55 +650,55 @@ local function render() x = (osd_w - max_item_width) / 2 y = osd_h / 2 - (math.min(#selectable_items, max_lines) + 1.5) * line_height / 2 alignment = 7 - clipping_coordinates = '0,0,' .. x + max_item_width .. ',' .. osd_h + clipping_coordinates = "0,0," .. x + max_item_width .. "," .. osd_h else x = get_margin_x() y = osd_h * (1 - global_margins.b) - get_margin_y() alignment = 1 -- Avoid drawing below topbar OSC when there are wrapped lines. local coordinate_top = math.floor(global_margins.t * osd_h + 0.5) - clipping_coordinates = '0,' .. coordinate_top .. ',' .. osd_w .. ',' .. osd_h + clipping_coordinates = "0," .. coordinate_top .. "," .. osd_w .. "," .. osd_h end local font = get_font() -- Use the same blur value as the rest of the OSD. 288 is the OSD's -- PlayResY. - local blur = mp.get_property_native('osd-blur') * osd_h / 288 - local border_style = mp.get_property('osd-border-style') + local blur = mp.get_property_native("osd-blur") * osd_h / 288 + local border_style = mp.get_property("osd-border-style") - local style = '{\\r' .. - (font and '\\fn' .. font or '') .. - '\\fs' .. opts.font_size .. - '\\bord' .. opts.border_size .. '\\fsp0' .. - '\\blur' .. blur .. - (selectable_items and '\\q2' or '\\q1') .. - '\\clip(' .. clipping_coordinates .. ')}' + local style = "{\\r" .. + (font and "\\fn" .. font or "") .. + "\\fs" .. opts.font_size .. + "\\bord" .. opts.border_size .. "\\fsp0" .. + "\\blur" .. blur .. + (selectable_items and "\\q2" or "\\q1") .. + "\\clip(" .. clipping_coordinates .. ")}" -- Create the cursor glyph as an ASS drawing. ASS will draw the cursor -- inline with the surrounding text, but it sets the advance to the width -- of the drawing. So the cursor doesn't affect layout too much, make it as -- thin as possible and make it appear to be 1px wide by giving it 0.5px -- horizontal borders. - local color, alpha = mpv_color_to_ass(mp.get_property('osd-color')) + local color, alpha = mpv_color_to_ass(mp.get_property("osd-color")) local cheight = opts.font_size * 8 - local cglyph = '{\\r\\blur0' .. - (get_property_cached('focused') == false - and '\\alpha&HFF&' or '\\3a&H' .. alpha .. '&') .. - '\\3c&H' .. color .. '&' .. - '\\xbord0.5\\ybord0\\xshad0\\yshad1\\p4\\pbo24}' .. - 'm 0 0 l 1 0 l 1 ' .. cheight .. ' l 0 ' .. cheight .. - '{\\p0}' + local cglyph = "{\\r\\blur0" .. + (get_property_cached("focused") == false + and "\\alpha&HFF&" or "\\3a&H" .. alpha .. "&") .. + "\\3c&H" .. color .. "&" .. + "\\xbord0.5\\ybord0\\xshad0\\yshad1\\p4\\pbo24}" .. + "m 0 0 l 1 0 l 1 " .. cheight .. " l 0 " .. cheight .. + "{\\p0}" local before_cur = ass_escape(line:sub(1, cursor - 1)) local after_cur = ass_escape(line:sub(cursor)) - local log_ass = '' + local log_ass = "" local log_buffer = log_buffers[id] or {} for i = #log_buffer - math.min(max_lines, #log_buffer) + 1, #log_buffer do log_ass = log_ass .. style .. log_buffer[i].style .. - ass_escape(log_buffer[i].text) .. '\\N' + ass_escape(log_buffer[i].text) .. "\\N" end - local completion_ass = '' + local completion_ass = "" if completion_buffer[1] and not selectable_items then -- Estimate how many characters fit in one line -- Even with bottom-left anchoring, @@ -707,32 +707,32 @@ local function render() -- TODO: --osd-margin-x should scale with osd-width and PlayResX to make -- the calculation accurate. local width_max = math.floor( - (osd_w - x - mp.get_property_native('osd-margin-x') * 2) + (osd_w - x - mp.get_property_native("osd-margin-x") * 2) / opts.font_size * get_font_hw_ratio()) local completions, rows = format_grid(completion_buffer, width_max, max_lines) max_lines = max_lines - rows - completion_ass = style .. styles.completion .. completions .. '\\N' + completion_ass = style .. styles.completion .. completions .. "\\N" end -- Background if selectable_items and - (not searching_history or border_style == 'background-box') then - style = style .. '{\\bord0\\blur0\\4a&Hff&}' + (not searching_history or border_style == "background-box") then + style = style .. "{\\bord0\\blur0\\4a&Hff&}" local back_color, back_alpha = mpv_color_to_ass(mp.get_property( - border_style == 'background-box' and 'osd-back-color' or 'osd-outline-color')) + border_style == "background-box" and "osd-back-color" or "osd-outline-color")) if not searching_history then - back_alpha = string.format('%x', opts.background_alpha) + back_alpha = string.format("%x", opts.background_alpha) end ass:new_event() ass:an(alignment) ass:pos(x, y) - ass:append('{\\1c&H' .. back_color .. '&\\1a&H' .. back_alpha .. - '&\\bord' .. opts.menu_outline_size .. '\\3c&H' .. - option_color_to_ass(opts.menu_outline_color) .. '&}') - if border_style == 'background-box' then - ass:append('{\\4a&Hff&}') + ass:append("{\\1c&H" .. back_color .. "&\\1a&H" .. back_alpha .. + "&\\bord" .. opts.menu_outline_size .. "\\3c&H" .. + option_color_to_ass(opts.menu_outline_color) .. "&}") + if border_style == "background-box" then + ass:append("{\\4a&Hff&}") end ass:draw_start() ass:round_rect_cw(-opts.padding, @@ -753,14 +753,14 @@ local function render() if (first_match_to_print - 1 + i == selected_match or matches[first_match_to_print - 1 + i].index == default_item) - and (not searching_history or border_style == 'background-box') then + and (not searching_history or border_style == "background-box") then ass:new_event() ass:an(4) ass:pos(x, item_y) - ass:append('{\\blur0\\bord0\\4aH&ff&\\1c&H' .. - option_color_to_ass(opts.selected_back_color) .. '&}') + ass:append("{\\blur0\\bord0\\4aH&ff&\\1c&H" .. + option_color_to_ass(opts.selected_back_color) .. "&}") if first_match_to_print - 1 + i ~= selected_match then - ass:append('{\\1aH&cc&}') + ass:append("{\\1aH&cc&}") end ass:draw_start() ass:rect_cw(-opts.padding, 0, max_item_width + opts.padding, line_height) @@ -782,10 +782,10 @@ local function render() ass:an(alignment + 2) ass:pos(x + max_item_width, y) ass:append(style) - if not searching_history or border_style == 'background-box' then - ass:append('{\\bord0\\4a&Hff&\\blur0}') + if not searching_history or border_style == "background-box" then + ass:append("{\\bord0\\4a&Hff&\\blur0}") end - ass:append(selected_match .. '/' .. #matches) + ass:append(selected_match .. "/" .. #matches) local start_percentage = (first_match_to_print - 1) / #matches local end_percentage = (first_match_to_print - 1 + max_lines) / #matches @@ -803,7 +803,7 @@ local function render() ass:new_event() ass:an(alignment) - ass:append('{\\blur0\\4a&Hff&\\bord1}') + ass:append("{\\blur0\\4a&Hff&\\bord1}") ass:pos(x + max_item_width + opts.padding - 1, bar_y) ass:draw_start() ass:rect_cw(0, 0, -opts.padding / 2, height) @@ -814,9 +814,9 @@ local function render() ass:an(alignment) ass:pos(x, y) if not selectable_items then - ass:append(log_ass .. '\\N' .. completion_ass) + ass:append(log_ass .. "\\N" .. completion_ass) end - ass:append(style .. ass_escape(prompt) .. ' ' .. before_cur) + ass:append(style .. ass_escape(prompt) .. " " .. before_cur) ass:append(cglyph) ass:append(style .. after_cur) @@ -825,9 +825,9 @@ local function render() ass:new_event() ass:an(alignment) ass:pos(x, y) - ass:append(style .. '{\\alpha&HFF&}' .. ass_escape(prompt) .. ' ' .. before_cur) + ass:append(style .. "{\\alpha&HFF&}" .. ass_escape(prompt) .. " " .. before_cur) ass:append(cglyph) - ass:append(style .. '{\\alpha&HFF&}' .. after_cur) + ass:append(style .. "{\\alpha&HFF&}" .. after_cur) -- z with selectable_items needs to be greater than the OSC's. update_overlay(ass.text, osd_w, osd_h, selectable_items and 2000 or 0) @@ -845,7 +845,7 @@ update_timer:kill() -- Add a line to the history and deduplicate local function history_add(text) - if history[#history] == text or text == '' then + if history[#history] == text or text == "" then return end @@ -862,14 +862,14 @@ local function history_add(text) history[#history + 1] = text if history_paths[id] then - histories_to_save[id] = histories_to_save[id] .. text .. '\n' + histories_to_save[id] = histories_to_save[id] .. text .. "\n" end end local function handle_cursor_move() -- Don't show completions after a command is entered because they move its -- output up, and allow clearing completions by emptying the line. - if line == '' then + if line == "" then completion_buffer = {} render() else @@ -880,7 +880,7 @@ end local function handle_edit() if not selectable_items then handle_cursor_move() - mp.commandv('script-message-to', input_caller, 'input-event', 'edited', + mp.commandv("script-message-to", input_caller, "input-event", "edited", utils.format_json({line})) return end @@ -894,7 +894,7 @@ local function handle_edit() } end - if line == '' and default_item then + if line == "" and default_item then selected_match = default_item local max_lines = calculate_max_lines() @@ -955,7 +955,7 @@ end -- Clear the current line (Ctrl+C) local function clear() - line = '' + line = "" cursor = 1 insert_mode = false history_pos = #history + 1 @@ -965,7 +965,7 @@ end -- Close the console if the current line is empty, otherwise delete the next -- character (Ctrl+D) local function maybe_exit() - if line == '' then + if line == "" then set_active(false) else handle_del() @@ -973,8 +973,8 @@ local function maybe_exit() end local function unbind_mouse() - mp.remove_key_binding('_console_mouse_move') - mp.remove_key_binding('_console_mbtn_left') + mp.remove_key_binding("_console_mouse_move") + mp.remove_key_binding("_console_mbtn_left") end -- Run the current command or select the current item @@ -982,7 +982,7 @@ local function submit() if searching_history then searching_history = false selectable_items = nil - line = #matches > 0 and matches[selected_match].text or '' + line = #matches > 0 and matches[selected_match].text or "" cursor = #line + 1 handle_edit() unbind_mouse() @@ -991,7 +991,7 @@ local function submit() if selectable_items then if #matches > 0 then - mp.commandv('script-message-to', input_caller, 'input-event', 'submit', + mp.commandv("script-message-to", input_caller, "input-event", "submit", utils.format_json({matches[selected_match].index})) end else @@ -999,7 +999,7 @@ local function submit() cycle_through_completions() end - mp.commandv('script-message-to', input_caller, 'input-event', 'submit', + mp.commandv("script-message-to", input_caller, "input-event", "submit", utils.format_json({line})) history_add(line) @@ -1015,7 +1015,7 @@ end local function determine_hovered_item() local osd_w, _ = get_scaled_osd_dimensions() local scale = scale_factor() - local mouse_pos = mp.get_property_native('mouse-pos') + local mouse_pos = mp.get_property_native("mouse-pos") local mouse_x = mouse_pos.x / scale local mouse_y = mouse_pos.y / scale local item_x0 = (searching_history and get_margin_x() or (osd_w - max_item_width) / 2) @@ -1033,7 +1033,7 @@ local function determine_hovered_item() end local function bind_mouse() - mp.add_forced_key_binding('MOUSE_MOVE', '_console_mouse_move', function() + mp.add_forced_key_binding("MOUSE_MOVE", "_console_mouse_move", function() local item = determine_hovered_item() if item and item ~= selected_match then selected_match = item @@ -1041,7 +1041,7 @@ local function bind_mouse() end end) - mp.add_forced_key_binding('MBTN_LEFT', '_console_mbtn_left', function() + mp.add_forced_key_binding("MBTN_LEFT", "_console_mbtn_left", function() local item = determine_hovered_item() if item then selected_match = item @@ -1080,7 +1080,7 @@ local function go_history(new_pos) if history_pos <= #history then line = history[history_pos] else - line = '' + line = "" end cursor = line:len() + 1 insert_mode = false @@ -1193,14 +1193,14 @@ local function prev_word() -- This is basically the same as next_word() but backwards, so reverse the -- string in order to do a "backwards" find. This wouldn't be as annoying -- to do if Lua didn't insist on 1-based indexing. - cursor = line:len() - select(2, line:reverse():find('%s*[^%s]*', line:len() - cursor + 2)) + 1 + cursor = line:len() - select(2, line:reverse():find("%s*[^%s]*", line:len() - cursor + 2)) + 1 handle_cursor_move() end -- Move to the end of the current word, or if already at the end, the end of -- the next word. (Ctrl+Right) local function next_word() - cursor = select(2, line:find('%s*[^%s]*', cursor)) + 1 + cursor = select(2, line:find("%s*[^%s]*", cursor)) + 1 handle_cursor_move() end @@ -1221,7 +1221,7 @@ local function del_word() local before_cur = line:sub(1, cursor - 1) local after_cur = line:sub(cursor) - before_cur = before_cur:gsub('[^%s]+%s*$', '', 1) + before_cur = before_cur:gsub("[^%s]+%s*$", "", 1) line = before_cur .. after_cur cursor = before_cur:len() + 1 handle_edit() @@ -1234,7 +1234,7 @@ local function del_next_word() local before_cur = line:sub(1, cursor - 1) local after_cur = line:sub(cursor) - after_cur = after_cur:gsub('^%s*[^%s]+', '', 1) + after_cur = after_cur:gsub("^%s*[^%s]+", "", 1) line = before_cur .. after_cur handle_edit() end @@ -1260,37 +1260,37 @@ end -- Returns a string of UTF-8 text from the clipboard (or the primary selection) local function get_clipboard(clip) - if platform == 'x11' then + if platform == "x11" then local res = utils.subprocess({ - args = { 'xclip', '-selection', clip and 'clipboard' or 'primary', '-out' }, + args = { "xclip", "-selection", clip and "clipboard" or "primary", "-out" }, playback_only = false, }) if not res.error then return res.stdout end - elseif platform == 'wayland' then - if mp.get_property('current-clipboard-backend') == 'wayland' then - local property = clip and 'clipboard/text' or 'clipboard/text-primary' - return mp.get_property(property, '') + elseif platform == "wayland" then + if mp.get_property("current-clipboard-backend") == "wayland" then + local property = clip and "clipboard/text" or "clipboard/text-primary" + return mp.get_property(property, "") end -- Wayland VO clipboard is only updated on window focus - if clip and get_property_cached('focused') then - return mp.get_property('clipboard/text', '') + if clip and get_property_cached("focused") then + return mp.get_property("clipboard/text", "") end local res = utils.subprocess({ - args = { 'wl-paste', clip and '-n' or '-np' }, + args = { "wl-paste", clip and "-n" or "-np" }, playback_only = false, }) if not res.error then return res.stdout end - elseif platform == 'windows' or platform == 'darwin' then - return mp.get_property('clipboard/text', '') + elseif platform == "windows" or platform == "darwin" then + return mp.get_property("clipboard/text", "") end - return '' + return "" end --- Paste text from the window-system's clipboard. 'clip' determines whether the +-- Paste text from the window-system's clipboard. "clip" determines whether the -- clipboard or the primary selection buffer is used (on X11 and Wayland only.) local function paste(clip) local text = get_clipboard(clip) @@ -1347,7 +1347,7 @@ end cycle_through_completions = function (backwards) if #completion_buffer == 0 then -- Allow Tab completion of commands before typing anything. - if line == '' then + if line == "" then complete() end @@ -1374,8 +1374,8 @@ end complete = function () completion_old_line = line completion_old_cursor = cursor - mp.commandv('script-message-to', input_caller, 'input-event', - 'complete', utils.format_json({line:sub(1, cursor - 1)})) + mp.commandv("script-message-to", input_caller, "input-event", + "complete", utils.format_json({line:sub(1, cursor - 1)})) render() end @@ -1383,68 +1383,68 @@ end -- bindings and readline bindings. local function get_bindings() local bindings = { - { 'esc', function() set_active(false) end }, - { 'ctrl+[', function() set_active(false) end }, - { 'enter', submit }, - { 'kp_enter', submit }, - { 'shift+enter', function() handle_char_input('\n') end }, - { 'ctrl+j', submit }, - { 'ctrl+m', submit }, - { 'bs', handle_backspace }, - { 'shift+bs', handle_backspace }, - { 'ctrl+h', handle_backspace }, - { 'del', handle_del }, - { 'shift+del', handle_del }, - { 'ins', handle_ins }, - { 'shift+ins', function() paste(false) end }, - { 'mbtn_mid', function() paste(false) end }, - { 'left', function() prev_char() end }, - { 'ctrl+b', function() page_up_or_prev_char() end }, - { 'right', function() next_char() end }, - { 'ctrl+f', function() page_down_or_next_char() end}, - { 'up', function() move_history(-1) end }, - { 'ctrl+p', function() move_history(-1) end }, - { 'wheel_up', function() move_history(-1, true) end }, - { 'down', function() move_history(1) end }, - { 'ctrl+n', function() move_history(1) end }, - { 'wheel_down', function() move_history(1, true) end }, - { 'wheel_left', function() end }, - { 'wheel_right', function() end }, - { 'ctrl+left', prev_word }, - { 'alt+b', prev_word }, - { 'ctrl+right', next_word }, - { 'alt+f', next_word }, - { 'tab', cycle_through_completions }, - { 'ctrl+i', cycle_through_completions }, - { 'shift+tab', function() cycle_through_completions(true) end }, - { 'ctrl+a', go_home }, - { 'home', go_home }, - { 'ctrl+e', go_end }, - { 'end', go_end }, - { 'pgup', handle_pgup }, - { 'pgdwn', handle_pgdown }, - { 'ctrl+r', search_history }, - { 'ctrl+c', clear }, - { 'ctrl+d', maybe_exit }, - { 'ctrl+k', del_to_eol }, - { 'ctrl+l', clear_log_buffer }, - { 'ctrl+u', del_to_start }, - { 'ctrl+v', function() paste(true) end }, - { 'meta+v', function() paste(true) end }, - { 'ctrl+bs', del_word }, - { 'ctrl+w', del_word }, - { 'ctrl+del', del_next_word }, - { 'alt+d', del_next_word }, - { 'kp_dec', function() handle_char_input('.') end }, - { 'kp_add', function() handle_char_input('+') end }, - { 'kp_subtract', function() handle_char_input('-') end }, - { 'kp_multiply', function() handle_char_input('*') end }, - { 'kp_divide', function() handle_char_input('/') end }, + { "esc", function() set_active(false) end }, + { "ctrl+[", function() set_active(false) end }, + { "enter", submit }, + { "kp_enter", submit }, + { "shift+enter", function() handle_char_input("\n") end }, + { "ctrl+j", submit }, + { "ctrl+m", submit }, + { "bs", handle_backspace }, + { "shift+bs", handle_backspace }, + { "ctrl+h", handle_backspace }, + { "del", handle_del }, + { "shift+del", handle_del }, + { "ins", handle_ins }, + { "shift+ins", function() paste(false) end }, + { "mbtn_mid", function() paste(false) end }, + { "left", function() prev_char() end }, + { "ctrl+b", function() page_up_or_prev_char() end }, + { "right", function() next_char() end }, + { "ctrl+f", function() page_down_or_next_char() end}, + { "up", function() move_history(-1) end }, + { "ctrl+p", function() move_history(-1) end }, + { "wheel_up", function() move_history(-1, true) end }, + { "down", function() move_history(1) end }, + { "ctrl+n", function() move_history(1) end }, + { "wheel_down", function() move_history(1, true) end }, + { "wheel_left", function() end }, + { "wheel_right", function() end }, + { "ctrl+left", prev_word }, + { "alt+b", prev_word }, + { "ctrl+right", next_word }, + { "alt+f", next_word }, + { "tab", cycle_through_completions }, + { "ctrl+i", cycle_through_completions }, + { "shift+tab", function() cycle_through_completions(true) end }, + { "ctrl+a", go_home }, + { "home", go_home }, + { "ctrl+e", go_end }, + { "end", go_end }, + { "pgup", handle_pgup }, + { "pgdwn", handle_pgdown }, + { "ctrl+r", search_history }, + { "ctrl+c", clear }, + { "ctrl+d", maybe_exit }, + { "ctrl+k", del_to_eol }, + { "ctrl+l", clear_log_buffer }, + { "ctrl+u", del_to_start }, + { "ctrl+v", function() paste(true) end }, + { "meta+v", function() paste(true) end }, + { "ctrl+bs", del_word }, + { "ctrl+w", del_word }, + { "ctrl+del", del_next_word }, + { "alt+d", del_next_word }, + { "kp_dec", function() handle_char_input(".") end }, + { "kp_add", function() handle_char_input("+") end }, + { "kp_subtract", function() handle_char_input("-") end }, + { "kp_multiply", function() handle_char_input("*") end }, + { "kp_divide", function() handle_char_input("/") end }, } for i = 0, 9 do bindings[#bindings + 1] = - {'kp' .. i, function() handle_char_input('' .. i) end} + {"kp" .. i, function() handle_char_input("" .. i) end} end return bindings @@ -1455,7 +1455,7 @@ local function define_key_bindings() return end for _, bind in ipairs(get_bindings()) do - if not (dont_bind_up_down and (bind[1] == 'up' or bind[1] == 'down')) then + if not (dont_bind_up_down and (bind[1] == "up" or bind[1] == "down")) then -- Generate arbitrary name for removing the bindings later. local name = "_console_" .. (#key_bindings + 1) key_bindings[#key_bindings + 1] = name @@ -1479,7 +1479,7 @@ local function read_history() return end - local history_file = io.open(mp.command_native({'expand-path', history_paths[id]})) + local history_file = io.open(mp.command_native({"expand-path", history_paths[id]})) if history_file == nil then return @@ -1522,9 +1522,9 @@ set_active = function (active) open = true insert_mode = false define_key_bindings() - mp.set_property_bool('user-data/mpv/console/open', true) - ime_active = mp.get_property_bool('input-ime') - mp.set_property_bool('input-ime', true) + mp.set_property_bool("user-data/mpv/console/open", true) + ime_active = mp.get_property_bool("input-ime") + mp.set_property_bool("input-ime", true) elseif searching_history then searching_history = false selectable_items = nil @@ -1533,29 +1533,29 @@ set_active = function (active) open = false undefine_key_bindings() unbind_mouse() - mp.set_property_bool('user-data/mpv/console/open', false) - mp.set_property_bool('input-ime', ime_active) - mp.commandv('script-message-to', input_caller, 'input-event', - 'closed', utils.format_json({line, cursor})) + mp.set_property_bool("user-data/mpv/console/open", false) + mp.set_property_bool("input-ime", ime_active) + mp.commandv("script-message-to", input_caller, "input-event", + "closed", utils.format_json({line, cursor})) collectgarbage() end render() end -mp.register_script_message('disable', function() +mp.register_script_message("disable", function() set_active(false) end) -mp.register_script_message('get-input', function (script_name, args) +mp.register_script_message("get-input", function (script_name, args) if open and script_name ~= input_caller then - mp.commandv('script-message-to', input_caller, 'input-event', - 'closed', utils.format_json({line, cursor})) + mp.commandv("script-message-to", input_caller, "input-event", + "closed", utils.format_json({line, cursor})) end input_caller = script_name args = utils.parse_json(args) prompt = args.prompt - line = args.default_text or '' + line = args.default_text or "" cursor = tonumber(args.cursor_position) or line:len() + 1 keep_open = args.keep_open default_item = args.default_item @@ -1587,32 +1587,32 @@ mp.register_script_message('get-input', function (script_name, args) if histories[id] == nil then histories[id] = {} log_buffers[id] = {} - histories_to_save[id] = '' + histories_to_save[id] = "" end history = histories[id] history_paths[id] = args.history_path read_history() history_pos = #history + 1 - if line ~= '' then + if line ~= "" then complete() end end set_active(true) - mp.commandv('script-message-to', input_caller, 'input-event', 'opened') + mp.commandv("script-message-to", input_caller, "input-event", "opened") end) -- Add a line to the log buffer (which is limited to 100 lines) -mp.register_script_message('log', function (message) +mp.register_script_message("log", function (message) local log_buffer = log_buffers[id] message = utils.parse_json(message) log_buffer[#log_buffer + 1] = { text = message.text, - style = message.error and styles.error or message.style or '', + style = message.error and styles.error or message.style or "", terminal_style = message.error and terminal_styles.error or - message.terminal_style or '', + message.terminal_style or "", } if #log_buffer > 100 then @@ -1631,21 +1631,21 @@ mp.register_script_message('log', function (message) end end) -mp.register_script_message('set-log', function (log) +mp.register_script_message("set-log", function (log) log = utils.parse_json(log) log_buffers[id] = {} for i = 1, #log do - if type(log[i]) == 'table' then + if type(log[i]) == "table" then log[i].text = log[i].text - log[i].style = log[i].style or '' - log[i].terminal_style = log[i].terminal_style or '' + log[i].style = log[i].style or "" + log[i].terminal_style = log[i].terminal_style or "" log_buffers[id][i] = log[i] else log_buffers[id][i] = { text = log[i], - style = '', - terminal_style = '', + style = "", + terminal_style = "", } end end @@ -1653,7 +1653,7 @@ mp.register_script_message('set-log', function (log) render() end) -mp.register_script_message('complete', function (list, start_pos, append) +mp.register_script_message("complete", function (list, start_pos, append) if line ~= completion_old_line or cursor ~= completion_old_cursor then return end @@ -1677,7 +1677,7 @@ local function resize() render() end -mp.observe_property('osd-dimensions', 'native', function (_, value) +mp.observe_property("osd-dimensions", "native", function (_, value) if value.w == osd_width and value.h == osd_height then return end @@ -1685,12 +1685,12 @@ mp.observe_property('osd-dimensions', 'native', function (_, value) resize() end) -mp.observe_property('display-hidpi-scale', 'native', function (name, value) +mp.observe_property("display-hidpi-scale", "native", function (name, value) property_cache[name] = value resize() end) -mp.observe_property('focused', 'native', function (name, value) +mp.observe_property("focused", "native", function (name, value) property_cache[name] = value render() end) @@ -1704,31 +1704,31 @@ mp.observe_property("user-data/osc/margins", "native", function(_, val) render() end) -mp.register_event('shutdown', function () - mp.del_property('user-data/mpv/console') +mp.register_event("shutdown", function () + mp.del_property("user-data/mpv/console") for history_id, history_path in pairs(history_paths) do - history_path = mp.command_native({'expand-path', history_path}) - local history_file, error_message = io.open(history_path, 'ab') + history_path = mp.command_native({"expand-path", history_path}) + local history_file, error_message = io.open(history_path, "ab") if history_file then history_file:write(histories_to_save[history_id]) history_file:close() else - mp.msg.error('Failed to write history: ' .. error_message) + mp.msg.error("Failed to write history: " .. error_message) end end end) -- These are for backwards compatibility only. -mp.add_key_binding(nil, 'enable', function () - mp.command('script-message-to commands open') +mp.add_key_binding(nil, "enable", function () + mp.command("script-message-to commands open") end) -mp.register_script_message('type', function (text, cursor_pos) - mp.commandv('script-message-to', 'commands', 'type', unpack({text, cursor_pos})) +mp.register_script_message("type", function (text, cursor_pos) + mp.commandv("script-message-to", "commands", "type", unpack({text, cursor_pos})) end) -require 'mp.options'.read_options(opts, nil, render) +require "mp.options".read_options(opts, nil, render) collectgarbage()