History

console.lua: cache the command-list

Guido Cella committed 1 year ago

Commit f561efe1 parent d4dc19a1 file · +18 −11Browse files

Unified · Split

player/lua/console.lua+18−11
@@ -112,6 +112,7 @@ local completion_append · 111 unmodified lines
112112 local path_separator = platform == 'windows' and '\\' or '/'
113113 local completion_old_line
114114 local completion_old_cursor
115+local commands
115116
116117 local selectable_items
117118 local matches = {}
@@ -151,11 +152,18 @@ local function get_margin_x() · 33 unmodified lines
151152 return opts.margin_x > -1 and opts.margin_x or mp.get_property_native('osd-margin-x')
152153 end
153154
154−
155155 local function get_margin_y()
156156 return opts.margin_y > -1 and opts.margin_y or mp.get_property_native('osd-margin-y')
157157 end
158158
159+local function get_commands()
160+ if not commands then
161+ commands = mp.get_property_native('command-list')
162+ end
163+
164+ return commands
165+end
166+
159167
160168 -- Naive helper function to find the next UTF-8 character in 'str' after 'pos'
161169 -- by skipping continuation bytes. Assumes 'str' contains valid UTF-8.
@@ -1017,14 +1025,13 @@ local function maybe_exit() · 855 unmodified lines
10171025 end
10181026
10191027 local function help_command(param)
1020− local cmdlist = mp.get_property_native('command-list')
1021− table.sort(cmdlist, function(c1, c2)
1028+ table.sort(get_commands(), function(c1, c2)
10221029 return c1.name < c2.name
10231030 end)
10241031 local output = ''
10251032 if param == '' then
10261033 output = 'Available commands:\n'
1027− for _, cmd in ipairs(cmdlist) do
1034+ for _, cmd in ipairs(get_commands()) do
10281035 output = output .. ' ' .. cmd.name
10291036 end
10301037 output = output .. '\n'
@@ -1032,7 +1039,7 @@ local function help_command(param) · 1 unmodified line
10321039 output = output .. "ESC or Ctrl+d exits the console.\n"
10331040 else
10341041 local cmd = nil
1035− for _, curcmd in ipairs(cmdlist) do
1042+ for _, curcmd in ipairs(get_commands()) do
10361043 if curcmd.name:find(param, 1, true) then
10371044 cmd = curcmd
10381045 if curcmd.name == param then
@@ -1410,12 +1417,12 @@ local function text_input(info) · 371 unmodified lines
14101417 end
14111418
14121419 local function command_list()
1413− local commands = {}
1414− for i, command in ipairs(mp.get_property_native('command-list')) do
1415− commands[i] = command.name
1420+ local cmds = {}
1421+ for i, command in ipairs(get_commands()) do
1422+ cmds[i] = command.name
14161423 end
14171424
1418− return commands
1425+ return cmds
14191426 end
14201427
14211428 local function property_list()
@@ -1493,7 +1500,7 @@ local function list_option_value_list(option) · 71 unmodified lines
14931500 end
14941501
14951502 local function has_file_argument(candidate_command)
1496− for _, command in pairs(mp.get_property_native('command-list')) do
1503+ for _, command in pairs(get_commands()) do
14971504 if command.name == candidate_command then
14981505 return command.args[1] and
14991506 (command.args[1].name == 'filename' or command.args[1].name == 'url')
@@ -1587,7 +1594,7 @@ local function command_flags_at_2nd_argument_list(command) · 87 unmodified lines
15871594 end
15881595
15891596 local function handle_flags(command, arg_index, flags)
1590− for _, cmd in pairs(mp.get_property_native('command-list')) do
1597+ for _, cmd in pairs(get_commands()) do
15911598 if cmd.name == command then
15921599 if cmd.args[arg_index] and cmd.args[arg_index].type == 'Flags' then
15931600 break

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t