mirror of https://github.com/mpv-player/mpv
console.lua: style log lines in the terminal
When running the console in the terminal, style log lines with the same escape sequences as msg.c. mp.input can also specify terminal escape sequences, e.g. a script to select a playlist entry can invert the color of the selection. Also add a missing newline to help's error message.
This commit is contained in:
parent
f33a4d2fd9
commit
dfecc9f083
|
@ -922,9 +922,10 @@ REPL.
|
|||
``input.terminate()``
|
||||
Close the console.
|
||||
|
||||
``input.log(message, style)``
|
||||
``input.log(message, style, terminal_style)``
|
||||
Add a line to the log buffer. ``style`` can contain additional ASS tags to
|
||||
apply to ``message``.
|
||||
apply to ``message``, and ``terminal_style`` can contain escape sequences
|
||||
that are used when the console is displayed in the terminal.
|
||||
|
||||
``input.log_error(message)``
|
||||
Helper to add a line to the log buffer with the same color as the one the
|
||||
|
@ -933,7 +934,8 @@ REPL.
|
|||
``input.set_log(log)``
|
||||
Replace the entire log buffer.
|
||||
|
||||
``log`` is a table of strings, or tables with ``text`` and ``style`` keys.
|
||||
``log`` is a table of strings, or tables with ``text``, ``style`` and
|
||||
``terminal_style`` keys.
|
||||
|
||||
Example:
|
||||
|
||||
|
@ -941,7 +943,11 @@ REPL.
|
|||
|
||||
input.set_log({
|
||||
"regular text",
|
||||
{ style = "{\\c&H7a77f2&}", text = "error text" }
|
||||
{
|
||||
text = "error text",
|
||||
style = "{\\c&H7a77f2&}",
|
||||
terminal_style = "\027[31m",
|
||||
}
|
||||
})
|
||||
|
||||
Events
|
||||
|
|
|
@ -675,9 +675,12 @@ mp.input = {
|
|||
terminate: function () {
|
||||
mp.commandv("script-message-to", "console", "disable");
|
||||
},
|
||||
log: function (message, style) {
|
||||
mp.commandv("script-message-to", "console", "log",
|
||||
JSON.stringify({ text: message, style: style }));
|
||||
log: function (message, style, terminal_style) {
|
||||
mp.commandv("script-message-to", "console", "log", JSON.stringify({
|
||||
text: message,
|
||||
style: style,
|
||||
terminal_style: terminal_style,
|
||||
}));
|
||||
},
|
||||
log_error: function (message) {
|
||||
mp.commandv("script-message-to", "console", "log",
|
||||
|
|
|
@ -69,7 +69,7 @@ local styles = {
|
|||
-- cccc66 cc9966 cc99cc 537bd2
|
||||
|
||||
debug = '{\\1c&Ha09f93&}',
|
||||
verbose = '{\\1c&H99cc99&}',
|
||||
v = '{\\1c&H99cc99&}',
|
||||
warn = '{\\1c&H66ccff&}',
|
||||
error = '{\\1c&H7a77f2&}',
|
||||
fatal = '{\\1c&H5791f9&\\b1}',
|
||||
|
@ -77,6 +77,14 @@ local styles = {
|
|||
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',
|
||||
}
|
||||
|
||||
local repl_active = false
|
||||
local insert_mode = false
|
||||
local pending_update = false
|
||||
|
@ -197,9 +205,13 @@ do
|
|||
end
|
||||
|
||||
-- Add a line to the log buffer (which is limited to 100 lines)
|
||||
function log_add(style, text)
|
||||
function log_add(text, style, terminal_style)
|
||||
local log_buffer = log_buffers[id]
|
||||
log_buffer[#log_buffer + 1] = { style = style, text = text }
|
||||
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
|
||||
|
@ -331,7 +343,7 @@ local function print_to_terminal()
|
|||
|
||||
local log = ''
|
||||
for _, log_line in ipairs(log_buffers[id]) do
|
||||
log = log .. log_line.text
|
||||
log = log .. log_line.terminal_style .. log_line.text .. '\027[0m'
|
||||
end
|
||||
|
||||
local suggestions = table.concat(suggestion_buffer, '\t')
|
||||
|
@ -640,7 +652,8 @@ function help_command(param)
|
|||
end
|
||||
end
|
||||
if not cmd then
|
||||
log_add(styles.error, 'No command matches "' .. param .. '"!')
|
||||
log_add('No command matches "' .. param .. '"!\n', styles.error,
|
||||
terminal_styles.error)
|
||||
return
|
||||
end
|
||||
output = output .. 'Command "' .. cmd.name .. '"\n'
|
||||
|
@ -655,7 +668,7 @@ function help_command(param)
|
|||
output = output .. 'This command supports variable arguments.\n'
|
||||
end
|
||||
end
|
||||
log_add('', output)
|
||||
log_add(output)
|
||||
end
|
||||
|
||||
-- Add a line to the history and deduplicate
|
||||
|
@ -1399,8 +1412,9 @@ mp.register_script_message('log', function (message)
|
|||
|
||||
message = utils.parse_json(message)
|
||||
|
||||
log_add(message.error and styles.error or message.style or '',
|
||||
message.text .. '\n')
|
||||
log_add(message.text .. '\n',
|
||||
message.error and styles.error or message.style,
|
||||
message.error and terminal_styles.error or message.terminal_style)
|
||||
end)
|
||||
|
||||
mp.register_script_message('set-log', function (log)
|
||||
|
@ -1414,11 +1428,14 @@ mp.register_script_message('set-log', function (log)
|
|||
for i = 1, #log do
|
||||
if type(log[i]) == 'table' then
|
||||
log[i].text = log[i].text .. '\n'
|
||||
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] = {
|
||||
style = '',
|
||||
text = log[i] .. '\n',
|
||||
style = '',
|
||||
terminal_style = '',
|
||||
}
|
||||
end
|
||||
end
|
||||
|
@ -1478,20 +1495,8 @@ mp.register_event('log-message', function(e)
|
|||
if e.level == 'trace' then return end
|
||||
|
||||
-- Use color for debug/v/warn/error/fatal messages.
|
||||
local style = ''
|
||||
if e.level == 'debug' then
|
||||
style = styles.debug
|
||||
elseif e.level == 'v' then
|
||||
style = styles.verbose
|
||||
elseif e.level == 'warn' then
|
||||
style = styles.warn
|
||||
elseif e.level == 'error' then
|
||||
style = styles.error
|
||||
elseif e.level == 'fatal' then
|
||||
style = styles.fatal
|
||||
end
|
||||
|
||||
log_add(style, '[' .. e.prefix .. '] ' .. e.text)
|
||||
log_add('[' .. e.prefix .. '] ' .. e.text, styles[e.level],
|
||||
terminal_styles[e.level])
|
||||
end)
|
||||
|
||||
collectgarbage()
|
||||
|
|
|
@ -49,9 +49,12 @@ function input.terminate()
|
|||
mp.commandv("script-message-to", "console", "disable")
|
||||
end
|
||||
|
||||
function input.log(message, style)
|
||||
mp.commandv("script-message-to", "console", "log",
|
||||
utils.format_json({ text = message, style = style }))
|
||||
function input.log(message, style, terminal_style)
|
||||
mp.commandv("script-message-to", "console", "log", utils.format_json({
|
||||
text = message,
|
||||
style = style,
|
||||
terminal_style = terminal_style,
|
||||
}))
|
||||
end
|
||||
|
||||
function input.log_error(message)
|
||||
|
|
Loading…
Reference in New Issue