Skip to content

Commit

Permalink
console.lua: cache the command-list
Browse files Browse the repository at this point in the history
  • Loading branch information
guidocella authored and kasper93 committed Feb 24, 2025
1 parent d4dc19a commit f561efe
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ local completion_append
local path_separator = platform == 'windows' and '\\' or '/'
local completion_old_line
local completion_old_cursor
local commands

local selectable_items
local matches = {}
Expand Down Expand Up @@ -151,11 +152,18 @@ local function get_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')
end

local function get_commands()
if not commands then
commands = mp.get_property_native('command-list')
end

return commands
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.
Expand Down Expand Up @@ -1017,22 +1025,21 @@ local function maybe_exit()
end

local function help_command(param)
local cmdlist = mp.get_property_native('command-list')
table.sort(cmdlist, function(c1, c2)
table.sort(get_commands(), function(c1, c2)
return c1.name < c2.name
end)
local output = ''
if param == '' then
output = 'Available commands:\n'
for _, cmd in ipairs(cmdlist) do
for _, cmd in ipairs(get_commands()) do
output = output .. ' ' .. cmd.name
end
output = output .. '\n'
output = output .. 'Use "help command" to show information about a command.\n'
output = output .. "ESC or Ctrl+d exits the console.\n"
else
local cmd = nil
for _, curcmd in ipairs(cmdlist) do
for _, curcmd in ipairs(get_commands()) do
if curcmd.name:find(param, 1, true) then
cmd = curcmd
if curcmd.name == param then
Expand Down Expand Up @@ -1410,12 +1417,12 @@ local function text_input(info)
end

local function command_list()
local commands = {}
for i, command in ipairs(mp.get_property_native('command-list')) do
commands[i] = command.name
local cmds = {}
for i, command in ipairs(get_commands()) do
cmds[i] = command.name
end

return commands
return cmds
end

local function property_list()
Expand Down Expand Up @@ -1493,7 +1500,7 @@ local function list_option_value_list(option)
end

local function has_file_argument(candidate_command)
for _, command in pairs(mp.get_property_native('command-list')) do
for _, command in pairs(get_commands()) do
if command.name == candidate_command then
return command.args[1] and
(command.args[1].name == 'filename' or command.args[1].name == 'url')
Expand Down Expand Up @@ -1587,7 +1594,7 @@ local function command_flags_at_2nd_argument_list(command)
end

local function handle_flags(command, arg_index, flags)
for _, cmd in pairs(mp.get_property_native('command-list')) do
for _, cmd in pairs(get_commands()) do
if cmd.name == command then
if cmd.args[arg_index] and cmd.args[arg_index].type == 'Flags' then
break
Expand Down

0 comments on commit f561efe

Please sign in to comment.