alternative optimize

This commit is contained in:
kepler155c@gmail.com 2019-11-09 22:56:36 -07:00
parent 8a5d30a441
commit c9fd7efc26
1 changed files with 27 additions and 43 deletions

View File

@ -3,62 +3,46 @@ local Config = require('opus.config')
local Util = require('opus.util') local Util = require('opus.util')
local function getConfig() local function getConfig()
return Config.load('alternate', { return Config.load('alternate', {
default = { shell = {
shell = 'sys/apps/shell.lua', 'sys/apps/shell.lua',
lua = 'sys/apps/Lua.lua', 'rom/programs/shell',
files = 'sys/apps/Files.lua', },
}, lua = {
choices = { 'sys/apps/Lua.lua',
shell = { 'rom/programs/lua.lua',
'sys/apps/shell.lua', },
'rom/programs/shell', files = {
}, 'sys/apps/Files.lua',
lua = { }
'sys/apps/Lua.lua', })
'rom/programs/lua.lua',
},
files = {
'sys/apps/Files.lua',
}
}
})
end end
local Alt = { } local Alt = { }
function Alt.get(key) function Alt.get(key)
return getConfig().default[key] return getConfig()[key][1]
end end
function Alt.set(key, value) function Alt.set(key, value)
Alt.addChoice(key, value) local config = getConfig()
Array.removeByValue(config[key], value)
local config = getConfig() table.insert(config[key], 1, value)
config.default[key] = value Config.update('alternate', config)
Config.update('alternate', config)
end end
function Alt.remove(key, value) function Alt.remove(key, value)
local config = getConfig() local config = getConfig()
Array.removeByValue(config[key], value)
Array.removeByValue(config.choices[key], value) Config.update('alternate', config)
if config.default[key] == value then
config.default[key] = config.choices[key][1]
end
Config.update('alternate', config)
end end
function Alt.addChoice(key, value) function Alt.add(key, value)
local config = getConfig() local config = getConfig()
if not Util.contains(config[key], value) then
if not config.choices[key] then table.insert(config[key], value)
config.choices[key] = { } Config.update('alternate', config)
end end
if not Util.contains(config.choices[key], value) then
table.insert(config.choices[key], value)
Config.update('alternate', config)
end
end end
return Alt return Alt