#!/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 if action.ni then local ip if arg[2] then local a, b = arg[2]:match("(%d+):(%d+)") if a and b then -- a and b are valid numbers because they came from a match ip = { tonumber(a), tonumber(b) } end end if ip then deck = action.fn(config, deck, deckfname, ip) else io.write("Bad prompt index (must be cardnum:promptnum)\n") end elseif action.nq then local qs if arg[2] then qs = arg[2] end deck = action.fn(config, deck, deckfname, qs) else deck = action.fn(config, deck, deckfname) end if deck then loader.save(deckfname, deck) end else io.write("Failed to load deck\n") end else action.fn(config) end