Skip to content

Commit

Permalink
more refactoring: made all variables and functions local
Browse files Browse the repository at this point in the history
to hopefully keep them out from the global scope

no idea if this is really needed though
  • Loading branch information
bnfour committed Nov 6, 2022
1 parent e86a55d commit 13a8ff9
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions per-folder-tracks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)

0 comments on commit 13a8ff9

Please sign in to comment.