From 13a8ff9bc1a549eaeb22e6cd6cac97a82df9ed18 Mon Sep 17 00:00:00 2001 From: Bn4 Date: Mon, 7 Nov 2022 00:01:26 +0700 Subject: [PATCH] more refactoring: made all variables and functions local to hopefully keep them out from the global scope no idea if this is really needed though --- per-folder-tracks.lua | 64 +++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/per-folder-tracks.lua b/per-folder-tracks.lua index bf33c05..29ea720 100644 --- a/per-folder-tracks.lua +++ b/per-folder-tracks.lua @@ -9,44 +9,19 @@ ]] -- configuration -hotkey = "y" -file_name = ".mpv" -- relative path by default +local hotkey = "y" +local file_name = ".mpv" -- relative path by default -- settings definition -- if changing, please note that spaces in these will break the very "sophisticated" parsing logic -audio_key = "AUDIO" -sub_key = "SUB" - --- load event handler: load and apply settings from file, if available -function on_load(event) - local config = parse_config() - if config ~= nil then - local available_tracks = get_number_of_tracks() - try_set_property("aid", config[audio_key], available_tracks.audio) - try_set_property("sid", config[sub_key], available_tracks.sub) - end -end - --- keypress event handler: store current audio and sub track indices to a configured file --- overwrites existing file -function on_keypress(event) - local f = io.open(file_name, "w") - if f ~= nil then - f:write(string.format("%s %s\n", audio_key, mp.get_property("aid"))) - f:write(string.format("%s %s\n", sub_key, mp.get_property("sid"))) - f:close() - mp.osd_message("Stored audio+sub track selection") - else - mp.osd_message("Error writing file!") - end - -end +local audio_key = "AUDIO" +local sub_key = "SUB" -- tries to parse the configuration file: -- if file does not exist, returns nil -- else returns a table from every "[key][space][value]" pairs found in file -- if a key is set more than once, returns last set value -function parse_config() +local function parse_config() local file = io.open(file_name, "r") if file ~= nil then local parsed_keyvalues = {} @@ -67,7 +42,7 @@ end -- silently does nothing if provided track is invalid -- TODO: consider returning some kind of error code to be verified by the caller, -- which may do something in that case (eg. showing error message on the OSD) -function try_set_property(property, value, maximum) +local function try_set_property(property, value, maximum) if property ~= nil and value ~= nil then -- prevents switching to tracks with numbers higher than available in the file -- because overshoots like this turn audio/subtitles off @@ -78,7 +53,7 @@ function try_set_property(property, value, maximum) end -- returns number of audio and sub tracks present in the file -function get_number_of_tracks() +local function get_number_of_tracks() local result = { audio = 0, sub = 0 } local tracks_count = tonumber(mp.get_property("track-list/count")) for i = 0, tracks_count - 1 do @@ -92,5 +67,30 @@ function get_number_of_tracks() return result end +-- load event handler: load and apply settings from file, if available +local function on_load(event) + local config = parse_config() + if config ~= nil then + local available_tracks = get_number_of_tracks() + try_set_property("aid", config[audio_key], available_tracks.audio) + try_set_property("sid", config[sub_key], available_tracks.sub) + end +end + +-- keypress event handler: store current audio and sub track indices to a configured file +-- overwrites existing file +local function on_keypress(event) + local f = io.open(file_name, "w") + if f ~= nil then + f:write(string.format("%s %s\n", audio_key, mp.get_property("aid"))) + f:write(string.format("%s %s\n", sub_key, mp.get_property("sid"))) + f:close() + mp.osd_message("Stored audio+sub track selection") + else + mp.osd_message("Error writing file!") + end + +end + mp.register_event("file-loaded", on_load) mp.add_key_binding(hotkey, on_keypress)