0
0
mirror of https://github.com/mpv-player/mpv.git synced 2024-09-20 12:02:23 +02:00
mpv/player/lua/input.lua
Guido Cella dfecc9f083 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.
2024-01-14 23:26:07 +01:00

70 lines
2.2 KiB
Lua

--[[
This file is part of mpv.
mpv is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
mpv is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with mpv. If not, see <http://www.gnu.org/licenses/>.
]]
local utils = require "mp.utils"
local input = {}
function input.get(t)
mp.commandv("script-message-to", "console", "get-input",
mp.get_script_name(), utils.format_json({
prompt = t.prompt,
default_text = t.default_text,
cursor_position = t.cursor_position,
id = t.id,
}))
mp.register_script_message("input-event", function (type, text, cursor_position)
if t[type] then
local suggestions, completion_start_position = t[type](text, cursor_position)
if type == "complete" and suggestions then
mp.commandv("script-message-to", "console", "complete",
utils.format_json(suggestions), completion_start_position)
end
end
if type == "closed" then
mp.unregister_script_message("input-event")
end
end)
return true
end
function input.terminate()
mp.commandv("script-message-to", "console", "disable")
end
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)
mp.commandv("script-message-to", "console", "log",
utils.format_json({ text = message, error = true }))
end
function input.set_log(log)
mp.commandv("script-message-to", "console", "set-log", utils.format_json(log))
end
return input