Skip to content

Commit

Permalink
move function
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsumoto-ren committed Nov 4, 2024
1 parent fbc6b5c commit a0094d6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
37 changes: 37 additions & 0 deletions cfg_mgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/gpl.html
Config management, validation, loading.
]]

local mp = require('mp')
local mpopt = require('mp.options')
local msg = require('mp.msg')
local h = require('helpers')
local utils = require('mp.utils')

local min_side_px = 42
local max_side_px = 640
Expand Down Expand Up @@ -179,7 +181,42 @@ local function next_profile()
reload_from_disk()
end

local function create_config_file()
local name = default_profile_filename
local parent, child = utils.split_path(mp.get_script_directory())
parent, child = utils.split_path(parent:gsub("/$", ""))

local config_filepath = utils.join_path(utils.join_path(parent, "script-opts"), string.format('%s.conf', name))
local example_config_filepath = utils.join_path(mp.get_script_directory(), ".github/RELEASE/subs2srs.conf")

local file_info = utils.file_info(config_filepath)
if file_info and file_info.is_file then
print("config already exists")
return
end

local handle = io.open(example_config_filepath, 'r')
if handle == nil then
return
end

local content = handle:read("*a")
handle:close()

handle = io.open(config_filepath, 'w')
if handle == nil then
h.notify(string.format("Couldn't open %s.", config_filepath), "error", 4)
return
end

handle:write(string.format("# Written by %s on %s.\n", name, os.date()))
handle:write(content)
handle:close()
h.notify("Settings saved.", "info", 2)
end

local function init(config_table, profiles_table)
create_config_file()
self.config, self.profiles = config_table, profiles_table
-- 'subs2srs' is the main profile, it is always loaded. 'active profile' overrides it afterwards.
-- initial state is saved to another table to maintain consistency when cycling through incomplete profiles.
Expand Down
36 changes: 0 additions & 36 deletions subs2srs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ For complete usage guide, see <https://github.com/Ajatt-Tools/mpvacious/blob/mas
]]

local mp = require('mp')
local utils = require('mp.utils')
local OSD = require('osd_styler')
local cfg_mgr = require('cfg_mgr')
local encoder = require('encoder')
Expand Down Expand Up @@ -578,40 +577,6 @@ end
------------------------------------------------------------
-- main

local function create_config_file()
local NAME = "subs2srs"
local parent, child = utils.split_path(mp.get_script_directory())
parent, child = utils.split_path(parent:gsub("/$", ""))

local config_filepath = utils.join_path(utils.join_path(parent, "script-opts"), string.format('%s.conf', NAME))
local example_config_filepath = utils.join_path(mp.get_script_directory(), ".github/RELEASE/subs2srs.conf")

local file_info = utils.file_info(config_filepath)
if file_info and file_info.is_file then
print("config already exists")
return
end

local handle = io.open(example_config_filepath, 'r')
if handle == nil then
return
end

local content = handle:read("*a")
handle:close()

handle = io.open(config_filepath, 'w')
if handle == nil then
h.notify(string.format("Couldn't open %s.", config_filepath), "error", 4)
return
end

handle:write(string.format("# Written by %s on %s.\n", NAME, os.date()))
handle:write(content)
handle:close()
h.notify("Settings saved.", "info", 2)
end

local main = (function()
local main_executed = false
return function()
Expand All @@ -621,7 +586,6 @@ local main = (function()
main_executed = true
end

create_config_file()
cfg_mgr.init(config, profiles)
ankiconnect.init(config, platform)
forvo.init(config, platform)
Expand Down

0 comments on commit a0094d6

Please sign in to comment.