Rename all functions to reduce duplication
This commit is contained in:
248
KeybindSaver.lua
248
KeybindSaver.lua
@@ -10,6 +10,119 @@ frame:SetScript("OnEvent", function(self, event, addon)
|
||||
KeybindSaverDaved.sets = KeybindSaverDaved.sets or {}
|
||||
end)
|
||||
|
||||
---@param setName string
|
||||
---@return nil
|
||||
local function SaveKeybindSet(setName)
|
||||
if not setName or setName == "" then
|
||||
print("Set name cannot be empty")
|
||||
return
|
||||
end
|
||||
|
||||
---@type table<string, {key1: string, key2: string?}>
|
||||
local set = {}
|
||||
local numBindings = GetNumBindings()
|
||||
|
||||
for i = 1, numBindings do
|
||||
local command, _, key1, key2 = GetBinding(i)
|
||||
if key1 or key2 then set[command] = {
|
||||
key1 = key1,
|
||||
key2 = key2,
|
||||
} end
|
||||
end
|
||||
|
||||
KeybindSaverDaved.sets[setName] = set
|
||||
print(string.format("Saved keybind set '%s'!", setName))
|
||||
end
|
||||
|
||||
---@param setName string
|
||||
---@return nil
|
||||
local function RestoreKeybindSet(setName)
|
||||
if not setName or setName == "" then
|
||||
print("Set name cannot be empty")
|
||||
return
|
||||
end
|
||||
|
||||
local set = KeybindSaverDaved.sets[setName]
|
||||
if not set then
|
||||
print(string.format("No set with the name '%s' exists", setName))
|
||||
return
|
||||
end
|
||||
|
||||
-- Clear all current bindings first
|
||||
ClearOverrideBindings(frame)
|
||||
|
||||
-- Restore saved bindings
|
||||
for command, keys in pairs(set) do
|
||||
if keys.key1 then SetBinding(keys.key1, command) end
|
||||
if keys.key2 then SetBinding(keys.key2, command) end
|
||||
end
|
||||
|
||||
-- Save the changes
|
||||
SaveBindings(GetCurrentBindingSet())
|
||||
print(string.format("Restored keybind set '%s'", setName))
|
||||
end
|
||||
|
||||
---@param setName string
|
||||
---@return nil
|
||||
local function DeleteKeybindSet(setName)
|
||||
if not setName or setName == "" then
|
||||
print("Set name cannot be empty")
|
||||
return
|
||||
end
|
||||
|
||||
if not KeybindSaverDaved.sets[setName] then
|
||||
print(string.format("No set with the name '%s' exists", setName))
|
||||
return
|
||||
end
|
||||
|
||||
KeybindSaverDaved.sets[setName] = nil
|
||||
print(string.format("Deleted keybind set '%s'", setName))
|
||||
end
|
||||
|
||||
---@return nil
|
||||
local function ListKeybindSets()
|
||||
local sets = {}
|
||||
for setName in pairs(KeybindSaverDaved.sets) do
|
||||
table.insert(sets, setName)
|
||||
end
|
||||
table.sort(sets)
|
||||
|
||||
if #sets == 0 then
|
||||
print("No keybind sets found")
|
||||
else
|
||||
print("Saved keybind sets:")
|
||||
print(table.concat(sets, ", "))
|
||||
end
|
||||
end
|
||||
|
||||
---@param setName string
|
||||
---@param importStr string
|
||||
---@return nil
|
||||
local function ImportKeybindSet(setName, importStr)
|
||||
if not setName or setName == "" then
|
||||
print("Set name cannot be empty")
|
||||
return
|
||||
end
|
||||
|
||||
local set = {}
|
||||
local lines = { strsplit("\n", importStr) }
|
||||
|
||||
for _, line in ipairs(lines) do
|
||||
local command, key = strsplit(":", line)
|
||||
if command and key then
|
||||
if not set[command] then set[command] = {} end
|
||||
if not set[command].key1 then
|
||||
set[command].key1 = key
|
||||
else
|
||||
set[command].key2 = key
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
KeybindSaverDaved.sets[setName] = set
|
||||
print(string.format("Imported keybind set '%s'", setName))
|
||||
end
|
||||
|
||||
-- Create the import/export frame
|
||||
local importExportFrame = CreateFrame("Frame", "KBSImportExportFrame", UIParent)
|
||||
importExportFrame:SetSize(512, 512)
|
||||
@@ -56,99 +169,14 @@ importExportFrameTextBox:SetScript("OnEscapePressed", function(self)
|
||||
importExportFrame:Hide()
|
||||
if importingSet then
|
||||
local text = self:GetText()
|
||||
if text and text ~= "" then ImportSet(importingSet, text) end
|
||||
if text and text ~= "" then ImportKeybindSet(importingSet, text) end
|
||||
importingSet = nil
|
||||
end
|
||||
end)
|
||||
|
||||
---@param setName string
|
||||
---@return nil
|
||||
local function SaveSet(setName)
|
||||
if not setName or setName == "" then
|
||||
print("Set name cannot be empty")
|
||||
return
|
||||
end
|
||||
|
||||
---@type table<string, {key1: string, key2: string?}>
|
||||
local set = {}
|
||||
local numBindings = GetNumBindings()
|
||||
|
||||
for i = 1, numBindings do
|
||||
local command, _, key1, key2 = GetBinding(i)
|
||||
if key1 or key2 then set[command] = {
|
||||
key1 = key1,
|
||||
key2 = key2,
|
||||
} end
|
||||
end
|
||||
|
||||
KeybindSaverDaved.sets[setName] = set
|
||||
print(string.format("Saved keybind set '%s'!", setName))
|
||||
end
|
||||
|
||||
---@param setName string
|
||||
---@return nil
|
||||
local function RestoreSet(setName)
|
||||
if not setName or setName == "" then
|
||||
print("Set name cannot be empty")
|
||||
return
|
||||
end
|
||||
|
||||
local set = KeybindSaverDaved.sets[setName]
|
||||
if not set then
|
||||
print(string.format("No set with the name '%s' exists", setName))
|
||||
return
|
||||
end
|
||||
|
||||
-- Clear all current bindings first
|
||||
ClearOverrideBindings(frame)
|
||||
|
||||
-- Restore saved bindings
|
||||
for command, keys in pairs(set) do
|
||||
if keys.key1 then SetBinding(keys.key1, command) end
|
||||
if keys.key2 then SetBinding(keys.key2, command) end
|
||||
end
|
||||
|
||||
-- Save the changes
|
||||
SaveBindings(GetCurrentBindingSet())
|
||||
print(string.format("Restored keybind set '%s'", setName))
|
||||
end
|
||||
|
||||
---@param setName string
|
||||
---@return nil
|
||||
local function DeleteSet(setName)
|
||||
if not setName or setName == "" then
|
||||
print("Set name cannot be empty")
|
||||
return
|
||||
end
|
||||
|
||||
if not KeybindSaverDaved.sets[setName] then
|
||||
print(string.format("No set with the name '%s' exists", setName))
|
||||
return
|
||||
end
|
||||
|
||||
KeybindSaverDaved.sets[setName] = nil
|
||||
print(string.format("Deleted keybind set '%s'", setName))
|
||||
end
|
||||
|
||||
---@return nil
|
||||
local function ListSets()
|
||||
local sets = {}
|
||||
for setName in pairs(KeybindSaverDaved.sets) do
|
||||
table.insert(sets, setName)
|
||||
end
|
||||
table.sort(sets)
|
||||
|
||||
if #sets == 0 then
|
||||
print("No keybind sets found")
|
||||
else
|
||||
print("Saved keybind sets:")
|
||||
print(table.concat(sets, ", "))
|
||||
end
|
||||
end
|
||||
|
||||
---@param setName string
|
||||
---@return nil
|
||||
local function ExportSet(setName)
|
||||
local function ExportKeybindSet(setName)
|
||||
local set = KeybindSaverDaved.sets[setName]
|
||||
if not set then
|
||||
print(string.format("No set with the name '%s' exists", setName))
|
||||
@@ -168,36 +196,8 @@ local function ExportSet(setName)
|
||||
end
|
||||
|
||||
---@param setName string
|
||||
---@param importStr string
|
||||
---@return nil
|
||||
local function ImportSet(setName, importStr)
|
||||
if not setName or setName == "" then
|
||||
print("Set name cannot be empty")
|
||||
return
|
||||
end
|
||||
|
||||
local set = {}
|
||||
local lines = { strsplit("\n", importStr) }
|
||||
|
||||
for _, line in ipairs(lines) do
|
||||
local command, key = strsplit(":", line)
|
||||
if command and key then
|
||||
if not set[command] then set[command] = {} end
|
||||
if not set[command].key1 then
|
||||
set[command].key1 = key
|
||||
else
|
||||
set[command].key2 = key
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
KeybindSaverDaved.sets[setName] = set
|
||||
print(string.format("Imported keybind set '%s'", setName))
|
||||
end
|
||||
|
||||
---@param setName string
|
||||
---@return nil
|
||||
local function ImportSetDialogue(setName)
|
||||
local function ImportKeybindSetDialogue(setName)
|
||||
if not setName or setName == "" then
|
||||
print("Set name cannot be empty")
|
||||
return
|
||||
@@ -209,7 +209,7 @@ local function ImportSetDialogue(setName)
|
||||
end
|
||||
|
||||
---@return nil
|
||||
local function PrintUsage()
|
||||
local function PrintKeybindUsage()
|
||||
print("Keybind Saver Slash Commands:")
|
||||
print("/kbs save <set> - Saves your current keybinds under the given <set>")
|
||||
print("/kbs restore <set> - Restores the saved <set>")
|
||||
@@ -224,12 +224,12 @@ SlashCmdList["KBS"] = function(argv)
|
||||
local args = { strsplit(" ", argv) }
|
||||
local cmd = args[1]
|
||||
|
||||
if cmd == "save" then SaveSet(args[2]) end
|
||||
if cmd == "restore" then RestoreSet(args[2]) end
|
||||
if cmd == "delete" then DeleteSet(args[2]) end
|
||||
if cmd == "list" then ListSets() end
|
||||
if cmd == "export" then ExportSet(args[2]) end
|
||||
if cmd == "import" then ImportSetDialogue(args[2]) end
|
||||
if cmd == "save" then SaveKeybindSet(args[2]) end
|
||||
if cmd == "restore" then RestoreKeybindSet(args[2]) end
|
||||
if cmd == "delete" then DeleteKeybindSet(args[2]) end
|
||||
if cmd == "list" then ListKeybindSets() end
|
||||
if cmd == "export" then ExportKeybindSet(args[2]) end
|
||||
if cmd == "import" then ImportKeybindSetDialogue(args[2]) end
|
||||
|
||||
if cmd == "" or not cmd then PrintUsage() end
|
||||
if cmd == "" or not cmd then PrintKeybindUsage() end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user