Skip to content

Commit

Permalink
Refactor bind_from_options_configs()
Browse files Browse the repository at this point in the history
  • Loading branch information
natural-harmonia-gropius committed Jan 7, 2024
1 parent 4a73dd1 commit de5987b
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions inputevent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,9 @@ function unbind(key)
end
end

function bind_from_conf(path)
local meta, meta_error = utils.file_info(path)
if not meta or not meta.is_file then
return {}
end

function bind_from_conf(conf)
local kv = {}
for line in io.lines(path) do
for _, line in pairs(conf:split("\n")) do
line = line:trim()
if line ~= "" and line:sub(1, 1) ~= "#" then
local key, cmd, comment = line:match("%s*([%S]+)%s+(.-)%s+#%s*(.-)%s*$")
Expand Down Expand Up @@ -379,41 +374,46 @@ function bind_from_conf(path)
return parsed
end

function bind_from_json(path)
local meta, meta_error = utils.file_info(path)
if not meta or not meta.is_file then
return {}
end

local json_file = io.open(path, "r")
if not json_file then
return {}
end
local json = json_file:read("*all")
json_file:close()

function bind_from_json(json)
local parsed = utils.parse_json(json)
return parsed
end

function bind_from_options_configs()
for key, value in pairs(bind_map) do
unbind(key)
end

for index, value in ipairs(o.configs:split(",")) do
local path = value:trim()
local content = ""
local extension = ""
if path == "input.conf" then
local input_conf = mp.get_property_native("input-conf")
path = input_conf == "" and "~~/input.conf" or input_conf
end
path = mp.command_native({ "expand-path", path })
if (path:match("^memory://")) then
content = path:replace("^memory://", "")
extension = "conf"
else
path = mp.command_native({ "expand-path", path })
local meta, meta_error = utils.file_info(path)
if meta and meta.is_file then
local file = io.open(path, "r")
if file then
content = file:read("*all")
file:close()
extension = path:match("^.+%.(.+)$")
end
end
end

local parsed = {}
local splited_filename = path:split(".")
local extension = splited_filename[#splited_filename]
if extension == "conf" then
parsed = bind_from_conf(path)
parsed = bind_from_conf(content)
elseif extension == "json" then
parsed = bind_from_json(path)
parsed = bind_from_json(content)
end

if #parsed ~= 0 then
for _, v in ipairs(parsed) do
if v.key and v.on then
Expand All @@ -429,9 +429,6 @@ end

function on_options_configs_update(list)
if (list.configs) then
for key, value in pairs(bind_map) do
unbind(key)
end
bind_from_options_configs()
end
end
Expand Down

0 comments on commit de5987b

Please sign in to comment.