DKL9 GitList
Repositories
DKL9 home
memoire
Code
Commits
Branches
Tags
Search
Tree:
aaef744
Branches
Tags
master
memoire
main.lua
Radically refactor main interface
John Smith
commited
aaef744
at 2022-302 00:35:58
main.lua
Blame
History
Raw
#!/usr/bin/env lua --[[ the main user interface for memoire ]] local SRC_DIR = arg[0]:match("^.*/") or "" Prompt = dofile(SRC_DIR .. "prompt.lua") review = dofile(SRC_DIR .. "review.lua") loader = dofile(SRC_DIR .. "loader.lua") recrw = dofile(SRC_DIR .. "recrw.lua") ACTIONS = dofile(SRC_DIR .. "actions.lua") --[[ check action based on first command-line argument; if nothing valid given, default to help ]] local action = nil if arg[1] then local fc = arg[1]:sub(1, 1) action = ACTIONS[fc] or ACTIONS.h else action = ACTIONS.h end -- load settings from config Recfile local confdir = os.getenv("HOME") .. "/.config/memoire/" local config = { DeckFile = { "" } } do local fh = io.open(confdir .. "config.rec") if fh then local ft = fh:read("a") local ct = recrw.read(ft) if ct and ct[1] then config = ct[1] else io.write("Erroneous configuration file\n") end else io.write("Could not open configuration file in " .. confdir .. "\n") end end --[[ gets config option `name`; if multiple values were given, provide the `ind`th one (or the first, if `ind` is not given) return `nil` if no such option available ]] function config:get(name, ind) return (self[name] or {})[ind or 1] end if not config.DeckFile then io.write("No deck files specified -- aborting\n") os.exit(true, true) end if #config.DeckFile > 1 then io.write("Select deck to use (by number):\n") for i, n in ipairs(config.DeckFile) do io.write(string.format("\t%d. %s\n", i, n)) end local rl = io.read("l") local si = tonumber(rl) if si and si <= #config.DeckFile and si >= 1 then deckfname = config.DeckFile[si] elseif si then io.write("Selected deck out of range -- aborting\n") os.exit(true, true) else io.write("Input not a number -- aborting") os.exit(true, true) end else deckfname = config.DeckFile[1] end deckfname = os.getenv("HOME") .. "/" .. deckfname math.randomseed(os.time()) -- do requested action if action.nd then local deck = loader.load(deckfname, config) if deck then deck = action.fn(config, deck, deckfname) if deck then loader.save(deckfname, deck) end else io.write("Failed to load deck\n") end else action.fn(config) end