-- Copyright (C) 2019 the mpv developers
--
-- Permission to use, copy, modify, and/or distribute this software for any
-- purpose with or without fee is hereby granted, provided that the above
-- copyright notice and this permission notice appear in all copies.
--
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
-- SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
-- 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'
-- Default options
local opts = {
-- All drawing is scaled by this value, including the text borders and the
-- cursor. Change it if you have a high-DPI display.
scale = 1,
-- Set the font used for the REPL and the console.
-- This has to be a monospaced font.
font = "",
-- Set the font size used for the REPL and the console. This will be
-- multiplied by "scale".
font_size = 16,
border_size = 1,
case_sensitive = true,
-- Remove duplicate entries in history as to only keep the latest one.
history_dedup = true,
-- The ratio of font height to font width.
-- Adjusts table width of completion suggestions.
-- Values in the range 1.8..2.5 make sense for common monospace fonts.
font_hw_ratio = 'auto',
}
function detect_platform()
local platform = mp.get_property_native('platform')
if platform == 'darwin' or platform == 'windows' then
return platform
elseif os.getenv('WAYLAND_DISPLAY') then
return 'wayland'
end
return 'x11'
end
-- Pick a better default font for Windows and macOS
local platform = detect_platform()
if platform == 'windows' then
opts.font = 'Consolas'
opts.case_sensitive = false
elseif platform == 'darwin' then
opts.font = 'Menlo'
else
opts.font = 'monospace'
end
-- Apply user-set options
require 'mp.options'.read_options(opts)
local styles = {
-- Colors are stolen from base16 Eighties by Chris Kempson
-- and converted to BGR as is required by ASS.
-- 2d2d2d 393939 515151 697374
-- 939fa0 c8d0d3 dfe6e8 ecf0f2
-- 7a77f2 5791f9 66ccff 99cc99
-- cccc66 cc9966 cc99cc 537bd2
debug = '{\\1c&Ha09f93&}',
v = '{\\1c&H99cc99&}',
warn = '{\\1c&H66ccff&}',
error = '{\\1c&H7a77f2&}',
fatal = '{\\1c&H5791f9&\\b1}',
suggestion = '{\\1c&Hcc99cc&}',
selected_suggestion = '{\\1c&H2fbdfa&\\b1}',
}
local terminal_styles = {
debug = '\027[1;30m',
v = '\027[32m',
warn = '\027[33m',
error = '\027[31m',
fatal = '\027[1;31m',
selected_suggestion = '\027[7m',
}
local repl_active = false
local insert_mode = false
local pending_update = false
local line = ''
local cursor = 1
local default_prompt = '>'
local prompt = default_prompt
local default_id = 'default'
local id = default_id
local histories = {[id] = {}}
local history = histories[id]
local history_pos = 1
local log_buffers = {[id] = {}}
local key_bindings = {}
local global_margins = { t = 0, b = 0 }
local input_caller
local suggestion_buffer = {}
local selected_suggestion_index
local completion_start_position
local completion_append
local file_commands = {}
local path_separator = platform == 'windows' and '\\' or '/'
local completion_old_line
local completion_old_cursor
local update_timer = nil
update_timer = mp.add_periodic_timer(0.05, function()
if pending_update then
update()
else
update_timer:kill()
end
end)
update_timer:kill()
mp.observe_property("user-data/osc/margins", "native", function(_, val)
if val then
global_margins = val
else
global_margins = { t = 0, b = 0 }
end
update()
end)
do
local width_length_ratio = 0.5
local osd_width, osd_height = 100, 100
---Update osd resolution if valid
local function update_osd_resolution()
local dim = mp.get_property_native('osd-dimensions')
if not dim or dim.w == 0 or dim.h == 0 then
return
end
osd_width = dim.w
osd_height = dim.h
end
local text_osd = mp.create_osd_overlay('ass-events')
text_osd.compute_bounds, text_osd.hidden = true, true
local function measure_bounds(ass_text)
update_osd_resolution()
text_osd.res_x, text_osd.res_y = osd_width, osd_height
text_osd.data = ass_text
local res = text_osd:update()
return res.x0, res.y0, res.x1, res.y1
end
---Measure text width and normalize to a font size of 1
---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 x1, y1 = nil, nil
size = size / 0.8
-- prevent endless loop
local repetitions_left = 5
repeat
size = size * 0.8
local ass = assdraw.ass_new()
ass.text = template:format(align, size, opts.font, rotation, text)
_, _, x1, y1 = measure_bounds(ass.text)
repetitions_left = repetitions_left - 1
-- make sure nothing got clipped
until (x1 and x1 < osd_width and y1 < osd_height) or repetitions_left == 0
local width = (repetitions_left == 0 and not x1) and 0 or (horizontal and x1 or y1)
return width / size, horizontal and osd_width or osd_height
end
local function fit_on_osd(text)
local estimated_width = #text * width_length_ratio
if osd_width >= osd_height then
-- Fill the osd as much as possible, bigger is more accurate.
return math.min(osd_width / estimated_width, osd_height), true
else
return math.min(osd_height / estimated_width, osd_width), false
end
end
local measured_font_hw_ratio = nil
function get_font_hw_ratio()
local font_hw_ratio = tonumber(opts.font_hw_ratio)
if font_hw_ratio then
return font_hw_ratio
end
if not measured_font_hw_ratio then
local alphabet = 'abcdefghijklmnopqrstuvwxyz'
local text = alphabet:rep(3)
update_osd_resolution()
local size, horizontal = fit_on_osd(text)
local normalized_width = normalized_text_width(text, size * 0.9, horizontal)
measured_font_hw_ratio = #text / normalized_width * 0.95
end
return measured_font_hw_ratio
end
end
-- Add a line to the log buffer (which is limited to 100 lines)
function log_add(text, style, terminal_style)
local log_buffer = log_buffers[id]
log_buffer[#log_buffer + 1] = {
text = text,
style = style or '',
terminal_style = terminal_style or '',
}
if #log_buffer > 100 then
table.remove(log_buffer, 1)
end
if repl_active then
if not update_timer:is_enabled() then
update()
update_timer:resume()
else
pending_update = true
end
end
end
-- Escape a string for verbatim display on the OSD
function ass_escape(str)
return mp.command_native({'escape-ass', str})
end
-- Takes a list of strings, a max width in characters and
-- optionally a max row count.
-- The result contains at least one column.
-- Rows are cut off from the top if rows_max is specified.
-- returns a string containing the formatted table and the row count
function format_table(list, width_max, rows_max)
if #list == 0 then
return '', 0
end
local spaces_min = 2
local spaces_max = 8
local list_size = #list
local column_count = 1
local row_count = list_size
local column_widths
-- total width without spacing
local width_total = 0
local list_widths = {}
for i, item in ipairs(list) do
list_widths[i] = len_utf8(item)
end
-- use as many columns as possible
|