diff --git a/README.md b/README.md index 4018aa0..9fc24c0 100644 --- a/README.md +++ b/README.md @@ -51,41 +51,16 @@ character's case to complete the round. ## Installation -## MUST USE NEOVIM 5.x!!!!!! - 1. Use your favorite plugin manager to install! Only works on Nvim, the one true vim. -```viml -Plug 'ThePrimeagen/vim-be-good' -``` - -### Docker - -If you would like, you can use docker to run the game. Doing this will -automatically use the correct version of neovim for you, as well as run the -game immediately when neovim starts. - -#### Stable image - -[This image](https://github.com/brandoncc/docker-vim-be-good/blob/master/stable/Dockerfile) always runs the version of the game that was bundled when the image -was built. Images are generally built within one day of the main branch -receiving new commits, but you won't get the new images unless you manually run -`docker pull brandoncc/vim-be-good:stable` periodically. - -```bash -docker run -it --rm brandoncc/vim-be-good:stable -``` - -#### "Latest" image - -[This image](https://github.com/brandoncc/docker-vim-be-good/blob/master/latest/Dockerfile) runs `:PlugUpdate` before running neovim. This adds about one second -to the startup time of the game. The trade-off is that you are always playing -the latest version of the game, as long as your machine is able to access -Github.com to pull it. - -```bash -docker run -it --rm brandoncc/vim-be-good:latest +```lua +use({ + "max397574/vim-be-good", + config=function() + require"vim-be-good".setup() + end +}) ``` ## Playing the games. diff --git a/lua/vim-be-good/bind.lua b/lua/vim-be-good/bind.lua index 555cf5e..b199ccb 100644 --- a/lua/vim-be-good/bind.lua +++ b/lua/vim-be-good/bind.lua @@ -1,6 +1,8 @@ local function bind(t, k, ...) - local args = {...} or {} - return function(...) return t[k](t, unpack(args), ...) end + local args = { ... } or {} + return function(...) + return t[k](t, unpack(args), ...) + end end return bind diff --git a/lua/vim-be-good/buffer.lua b/lua/vim-be-good/buffer.lua index 0ce48c3..b6b1ba9 100644 --- a/lua/vim-be-good/buffer.lua +++ b/lua/vim-be-good/buffer.lua @@ -45,9 +45,7 @@ function Buffer:_scheduledOnLine() end for _, fn in ipairs(self.onChangeList) do - - local ok, errMessage = pcall( - fn, buf, changedtick, firstline, lastline, linedata, more) + local ok, errMessage = pcall(fn, buf, changedtick, firstline, lastline, linedata, more) if not ok then log.info("Buffer:_scheduledOnLine: is not ok", errMessage) @@ -74,12 +72,11 @@ end function Buffer:attach() vim.api.nvim_buf_attach(self.bufh, true, { - on_lines = bind(self, "onLine") + on_lines = bind(self, "onLine"), }) end function Buffer:render(lines) - local idx = 1 local instructionLen = #self.instructions local lastRenderedLen = #self.lastRendered @@ -89,13 +86,11 @@ function Buffer:render(lines) self.lastRendered = lines if self.debugLineStr ~= nil then - vim.api.nvim_buf_set_lines( - self.bufh, 0, 1, false, {self.debugLineStr}) + vim.api.nvim_buf_set_lines(self.bufh, 0, 1, false, { self.debugLineStr }) end if instructionLen > 0 then - vim.api.nvim_buf_set_lines( - self.bufh, idx, idx + instructionLen, false, self.instructions) + vim.api.nvim_buf_set_lines(self.bufh, idx, idx + instructionLen, false, self.instructions) idx = idx + instructionLen end @@ -118,16 +113,14 @@ function Buffer:clearGameLines() local startOffset = table.getn(self.instructions) + 1 local len = table.getn(self.lastRendered) - vim.api.nvim_buf_set_lines( - self.bufh, startOffset, startOffset + len, false, createEmpty(len)) + vim.api.nvim_buf_set_lines(self.bufh, startOffset, startOffset + len, false, createEmpty(len)) end function Buffer:getGameLines() local startOffset = table.getn(self.instructions) + 1 local len = table.getn(self.lastRendered) - local lines = vim.api.nvim_buf_get_lines( - self.bufh, startOffset, startOffset + len, false) + local lines = vim.api.nvim_buf_get_lines(self.bufh, startOffset, startOffset + len, false) log.info("Buffer:getGameLines", startOffset, len, vim.inspect(lines)) @@ -137,8 +130,7 @@ end function Buffer:clear() local len = #self.lastRenderedInstruction + 1 + (#self.lastRendered or 0) - vim.api.nvim_buf_set_lines( - self.bufh, 0, len, false, createEmpty(len)) + vim.api.nvim_buf_set_lines(self.bufh, 0, len, false, createEmpty(len)) end function Buffer:onChange(cb) @@ -146,7 +138,6 @@ function Buffer:onChange(cb) end function Buffer:removeListener(cb) - local idx = 1 local found = false while idx <= #self.onChangeList and found == false do @@ -163,4 +154,3 @@ function Buffer:removeListener(cb) end return Buffer - diff --git a/lua/vim-be-good/empty-lines.lua b/lua/vim-be-good/empty-lines.lua index c767c57..113bf0a 100644 --- a/lua/vim-be-good/empty-lines.lua +++ b/lua/vim-be-good/empty-lines.lua @@ -1,2 +1 @@ - return createEmpty diff --git a/lua/vim-be-good/game-runner.lua b/lua/vim-be-good/game-runner.lua index 43641b4..219e127 100644 --- a/lua/vim-be-good/game-runner.lua +++ b/lua/vim-be-good/game-runner.lua @@ -1,12 +1,12 @@ -local bind = require("vim-be-good.bind"); -local types = require("vim-be-good.types"); -local GameUtils = require("vim-be-good.game-utils"); -local RelativeRound = require("vim-be-good.games.relative"); -local CiRound = require("vim-be-good.games.ci"); -local HjklRound = require("vim-be-good.games.hjkl"); -local WhackAMoleRound = require("vim-be-good.games.whackamole"); -local log = require("vim-be-good.log"); -local statistics = require("vim-be-good.statistics"); +local bind = require("vim-be-good.bind") +local types = require("vim-be-good.types") +local GameUtils = require("vim-be-good.game-utils") +local RelativeRound = require("vim-be-good.games.relative") +local CiRound = require("vim-be-good.games.ci") +local HjklRound = require("vim-be-good.games.hjkl") +local WhackAMoleRound = require("vim-be-good.games.whackamole") +local log = require("vim-be-good.log") +local statistics = require("vim-be-good.statistics") Stats = statistics:new() @@ -82,7 +82,7 @@ function GameRunner:new(selectedGames, difficulty, window, onFinished) timings = {}, games = {}, }, - state = states.playing + state = states.playing, } self.__index = self @@ -138,7 +138,9 @@ function GameRunner:init() vim.schedule(function() self.window.buffer:setInstructions({}) self.window.buffer:clear() - self:countdown(3, function() self:run() end) + self:countdown(3, function() + self:run() + end) end) end @@ -162,10 +164,13 @@ function GameRunner:checkForNext() local item = expectedLines[idx] log.info("GameRunner:checkForNext: compared", vim.inspect(lines), vim.inspect(expectedLines)) - log.info("GameRunner:checkForNext: deleted line is", item, + log.info( + "GameRunner:checkForNext: deleted line is", + item, item == endStates.menu, item == endStates.replay, - item == endStates.quit) + item == endStates.quit + ) local foundKey = nil for k, v in pairs(endStates) do @@ -177,7 +182,7 @@ function GameRunner:checkForNext() -- todo implement this correctly.... if foundKey then - self.onFinished(self, foundKey) + self.onFinished(self, foundKey) else log.info("GameRunner:checkForNext Some line was changed that is insignificant, rerendering") self.window.buffer:render(expectedLines) @@ -233,7 +238,9 @@ function GameRunner:endRound(success) end self.currentRound = self.currentRound + 1 - vim.schedule_wrap(function() self:run() end)() + vim.schedule_wrap(function() + self:run() + end)() end function GameRunner:close() @@ -243,8 +250,9 @@ function GameRunner:close() end function GameRunner:renderEndGame() - self.window.buffer:debugLine(string.format( - "Round %d / %d", self.currentRound, self.config.roundCount)) + self.window.buffer:debugLine( + string.format("Round %d / %d", self.currentRound, self.config.roundCount) + ) local lines = {} local sum = 0 @@ -256,7 +264,14 @@ function GameRunner:renderEndGame() self.ended = true -- TODO: Make this a bit better especially with random. - table.insert(lines, string.format("%d / %d completed successfully", self.results.successes, self.config.roundCount)) + table.insert( + lines, + string.format( + "%d / %d completed successfully", + self.results.successes, + self.config.roundCount + ) + ) table.insert(lines, string.format("Average %.2f", sum / self.config.roundCount)) table.insert(lines, string.format("Game Type %s", self.results.games[1])) @@ -287,8 +302,9 @@ function GameRunner:run() local roundConfig = self.round:getConfig() log.info("RoundName:", self.round:name()) - self.window.buffer:debugLine(string.format( - "Round %d / %d", self.currentRound, self.config.roundCount)) + self.window.buffer:debugLine( + string.format("Round %d / %d", self.currentRound, self.config.roundCount) + ) self.window.buffer:setInstructions(self.round.getInstructions()) local lines, cursorLine, cursorCol = self.round:render() @@ -303,7 +319,7 @@ function GameRunner:run() log.info("Setting current line to", cursorLine, cursorCol) if cursorLine > 0 then - vim.api.nvim_win_set_cursor(0, {cursorLine, cursorCol}) + vim.api.nvim_win_set_cursor(0, { cursorLine, cursorCol }) end self.startTime = GameUtils.getTime() @@ -324,8 +340,6 @@ function GameRunner:run() self:endRound() end, roundConfig.roundTime) - end return GameRunner - diff --git a/lua/vim-be-good/game-utils.lua b/lua/vim-be-good/game-utils.lua index c151384..847123c 100644 --- a/lua/vim-be-good/game-utils.lua +++ b/lua/vim-be-good/game-utils.lua @@ -1,4 +1,3 @@ - local difficultyToTime = { ["noob"] = 100000, ["easy"] = 10000, @@ -48,7 +47,7 @@ local extraWords = { "zar", } -local spaceByte = string.byte(' ') +local spaceByte = string.byte(" ") local function compareTable(a, b) local found = true @@ -145,6 +144,5 @@ return { getTime = getTime, compareTable = compareTable, trimString = trimString, - trimLines = trimLines + trimLines = trimLines, } - diff --git a/lua/vim-be-good/games/ci.lua b/lua/vim-be-good/games/ci.lua index 125aa27..db232f8 100644 --- a/lua/vim-be-good/games/ci.lua +++ b/lua/vim-be-good/games/ci.lua @@ -3,7 +3,7 @@ local log = require("vim-be-good.log") local gameLineCount = 20 local instructions = { - "Replace the outer container (if (...) { ... } or [ ... ]) with \"bar\"", + 'Replace the outer container (if (...) { ... } or [ ... ]) with "bar"', "", "e.g.:", "[ [", @@ -18,7 +18,7 @@ local instructions = { local CiRound = {} function CiRound:new(difficulty, window) - log.info("New", difficulty, window) + log.info("New", difficulty, window) local round = { window = window, difficulty = difficulty, @@ -82,15 +82,15 @@ function CiRound:render() lines[insertionIndex] = "if (" .. self.config.randomWord .. ") {" lines[insertionIndex + 2] = " if (" .. GameUtils.getRandomWord() .. ") { " lines[insertionIndex + 3] = " " .. GameUtils.getRandomWord() - lines[insertionIndex + 4] = " }"; - lines[insertionIndex + 5] = "}"; + lines[insertionIndex + 4] = " }" + lines[insertionIndex + 5] = "}" else - lines[insertionIndex] = "["; + lines[insertionIndex] = "[" lines[insertionIndex + 1] = " " .. GameUtils.getRandomWord() .. "," lines[insertionIndex + 2] = " " .. GameUtils.getRandomWord() .. "," lines[insertionIndex + 3] = " " .. GameUtils.getRandomWord() .. "," lines[insertionIndex + 4] = " " .. GameUtils.getRandomWord() .. "," - lines[insertionIndex + 5] = "]"; + lines[insertionIndex + 5] = "]" end return lines, cursorIdx diff --git a/lua/vim-be-good/games/hjkl.lua b/lua/vim-be-good/games/hjkl.lua index cd9bb0f..d4b3274 100644 --- a/lua/vim-be-good/games/hjkl.lua +++ b/lua/vim-be-good/games/hjkl.lua @@ -7,7 +7,7 @@ local boardSizeOptions = { medium = 7, hard = 8, nightmare = 9, - tpope = 10 + tpope = 10, } local instructions = { @@ -35,7 +35,7 @@ end function HjklRound:getConfig() log.info("getConfig", self.difficulty, GameUtils.difficultyToTime[self.difficulty]) return { - roundTime = GameUtils.difficultyToTime[self.difficulty] + roundTime = GameUtils.difficultyToTime[self.difficulty], } end @@ -69,20 +69,19 @@ function HjklRound:render() local cursorCol = 1 local cursorLine = 1 - while (xLine == cursorLine or xCol == cursorCol ) do + while xLine == cursorLine or xCol == cursorCol do xCol = self:getRandomNumber(boardSize) xLine = self:getRandomNumber(boardSize) cursorCol = self:getRandomNumber(boardSize) cursorLine = self:getRandomNumber(boardSize) end - log.info("HjklRound:render xLine: ", xLine, " xCol: ", xCol) + log.info("HjklRound:render xLine: ", xLine, " xCol: ", xCol) log.info("HjklRound:render cursorLine: ", cursorLine, " cursorCol: ", cursorCol) local idx = 1 while idx <= #lines do local line = lines[idx] - for i = 1, boardSize,1 - do + for i = 1, boardSize, 1 do if xLine == idx and xCol == i then line = line .. "x" else @@ -101,5 +100,3 @@ function HjklRound:name() end return HjklRound - - diff --git a/lua/vim-be-good/games/relative.lua b/lua/vim-be-good/games/relative.lua index 25f39e5..820a9cf 100644 --- a/lua/vim-be-good/games/relative.lua +++ b/lua/vim-be-good/games/relative.lua @@ -12,7 +12,7 @@ local randomOffset = { local instructions = { "Test your ability to hop by relative line numbers", - "To win the game, delete the line that says \"DELETE_ME\"", + 'To win the game, delete the line that says "DELETE_ME"', } local RelativeRound = {} @@ -36,7 +36,7 @@ end function RelativeRound:getConfig() log.info("getConfig", self.difficulty, GameUtils.difficultyToTime[self.difficulty]) return { - roundTime = GameUtils.difficultyToTime[self.difficulty] + roundTime = GameUtils.difficultyToTime[self.difficulty], } end @@ -78,5 +78,3 @@ function RelativeRound:name() end return RelativeRound - - diff --git a/lua/vim-be-good/games/whackamole.lua b/lua/vim-be-good/games/whackamole.lua index 41cac16..8071847 100644 --- a/lua/vim-be-good/games/whackamole.lua +++ b/lua/vim-be-good/games/whackamole.lua @@ -27,7 +27,7 @@ end function WhackAMoleRound:getConfig() log.info("getConfig", self.difficulty, GameUtils.difficultyToTime[self.difficulty]) return { - roundTime = GameUtils.difficultyToTime[self.difficulty] + roundTime = GameUtils.difficultyToTime[self.difficulty], } end @@ -48,7 +48,7 @@ function WhackAMoleRound:render() local chosenLocation = 0 while not foundLocation do local location = math.random(1, string.len(sentence)) - if sentence:sub(location,location) ~= " " then + if sentence:sub(location, location) ~= " " then chosenLocation = location foundLocation = true end @@ -56,8 +56,8 @@ function WhackAMoleRound:render() local pointerLine = "" local winSentence = "" - for idx=1, #sentence do - local c = sentence:sub(idx,idx) + for idx = 1, #sentence do + local c = sentence:sub(idx, idx) if idx == chosenLocation then pointerLine = pointerLine .. "^" if c == string.lower(c) then @@ -67,14 +67,14 @@ function WhackAMoleRound:render() end else pointerLine = pointerLine .. " " - winSentence = winSentence .. c + winSentence = winSentence .. c end end lines[2] = pointerLine self.__winLine = winSentence - local cursorIdx = 1 + local cursorIdx = 1 return lines, cursorIdx end @@ -84,5 +84,3 @@ function WhackAMoleRound:name() end return WhackAMoleRound - - diff --git a/lua/vim-be-good/init.lua b/lua/vim-be-good/init.lua index 5c0e395..552b2e7 100644 --- a/lua/vim-be-good/init.lua +++ b/lua/vim-be-good/init.lua @@ -1,13 +1,15 @@ -local WindowHandler = require("vim-be-good.window"); -local Menu = require("vim-be-good.menu"); -local GameRunner = require("vim-be-good.game-runner"); +local vimbegood = {} + +local WindowHandler = require("vim-be-good.window") +local Menu = require("vim-be-good.menu") +local GameRunner = require("vim-be-good.game-runner") local log = require("vim-be-good.log") math.randomseed(os.time()) local windowHandler -local function onVimResize() +function vimbegood.onVimResize() print("Testing onVimResize") if windowHandler then if not windowHandler:onResize() then @@ -16,7 +18,7 @@ local function onVimResize() end end -local function menu() +function vimbegood.menu() log.info("------------------ STARTING THE GAME -----------------------------") endItAll = nil hasEverythingEnded = false @@ -76,18 +78,24 @@ local function menu() endItAll() end end) - end onMenuSelect = function(gameString, difficulty) menu:close() log.info("onResults", gameString, difficulty) - local gameRunner = GameRunner:new({gameString}, difficulty, windowHandler, function(game, nextState) - onGameFinish(gameString, difficulty, game, nextState) - end) + local gameRunner = GameRunner:new( + { gameString }, + difficulty, + windowHandler, + function(game, nextState) + onGameFinish(gameString, difficulty, game, nextState) + end + ) - ok, msg = pcall(function() gameRunner:init() end, debug.traceback) + ok, msg = pcall(function() + gameRunner:init() + end, debug.traceback) if not ok then log.info("Error: Menu:new callback", msg) end @@ -96,9 +104,17 @@ local function menu() createMenu() end -return { - menu = menu, - onVimResize = onVimResize -} +function vimbegood.setup() + vim.api.nvim_create_user_command("VimBeGood", function() + vimbegood.menu() + end, { desc = "Start Vim-be-good" }) +end +vim.api.nvim_create_autocmd("VimResized", { + callback = function() + vimbegood.onVimResize() + end, + group = vim.api.nvim_create_augroup("VimBeGood", {}), +}) +return vimbegood diff --git a/lua/vim-be-good/log.lua b/lua/vim-be-good/log.lua index 5a05920..e9c7c1d 100644 --- a/lua/vim-be-good/log.lua +++ b/lua/vim-be-good/log.lua @@ -8,35 +8,35 @@ -- User configuration section local default_config = { - should_inspect = false, + should_inspect = false, - -- Name of the plugin. Prepended to log messages - plugin = 'VimBeGood', + -- Name of the plugin. Prepended to log messages + plugin = "VimBeGood", - -- Should print the output to neovim while running - use_console = vim.g["vim_be_good_log_console"] or false, + -- Should print the output to neovim while running + use_console = vim.g["vim_be_good_log_console"] or false, - -- Should highlighting be used in console (using echohl) - highlights = true, + -- Should highlighting be used in console (using echohl) + highlights = true, - -- Should write to a file - use_file = vim.g["vim_be_good_log_file"] or false, + -- Should write to a file + use_file = vim.g["vim_be_good_log_file"] or false, - -- Any messages above this level will be logged. - level = "trace", + -- Any messages above this level will be logged. + level = "trace", - -- Level configuration - modes = { - { name = "trace", hl = "Comment", }, - { name = "debug", hl = "Comment", }, - { name = "info", hl = "None", }, - { name = "warn", hl = "WarningMsg", }, - { name = "error", hl = "ErrorMsg", }, - { name = "fatal", hl = "ErrorMsg", }, - }, + -- Level configuration + modes = { + { name = "trace", hl = "Comment" }, + { name = "debug", hl = "Comment" }, + { name = "info", hl = "None" }, + { name = "warn", hl = "WarningMsg" }, + { name = "error", hl = "ErrorMsg" }, + { name = "fatal", hl = "ErrorMsg" }, + }, - -- Can limit the number of decimals displayed for floats - float_precision = 0.01, + -- Can limit the number of decimals displayed for floats + float_precision = 0.01, } -- {{{ NO NEED TO CHANGE @@ -45,109 +45,103 @@ local log = {} local unpack = unpack or table.unpack log.new = function(config, standalone) - config = vim.tbl_deep_extend("force", default_config, config) - - local outfile = string.format('%s/%s.log', vim.api.nvim_call_function('stdpath', {'data'}), config.plugin) - - local obj - if standalone then - obj = log - else - obj = {} - end - - local levels = {} - for i, v in ipairs(config.modes) do - levels[v.name] = i - end - - local round = function(x, increment) - increment = increment or 1 - x = x / increment - return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * increment - end - - local make_string = function(...) - local t = {} - for i = 1, select('#', ...) do - local x = select(i, ...) - - if type(x) == "number" and config.float_precision then - x = tostring(round(x, config.float_precision)) - elseif type(x) == "table" and config.should_inspect then - x = vim.inspect(x) - else - x = tostring(x) - end - - t[#t + 1] = x - end - return table.concat(t, " ") - end + config = vim.tbl_deep_extend("force", default_config, config) + local outfile = + string.format("%s/%s.log", vim.api.nvim_call_function("stdpath", { "data" }), config.plugin) - local log_at_level = function(level, level_config, message_maker, ...) - -- Return early if we're below the config.level - if level < levels[config.level] then - return + local obj + if standalone then + obj = log + else + obj = {} end - local nameupper = level_config.name:upper() - - local msg = message_maker(...) - local info = debug.getinfo(2, "Sl") - local lineinfo = info.short_src .. ":" .. info.currentline - - -- Output to console - if config.use_console then - local console_string = string.format( - "[%-6s%s] %s: %s", - nameupper, - os.date("%H:%M:%S"), - lineinfo, - msg - ) - - if config.highlights and level_config.hl then - vim.cmd(string.format("echohl %s", level_config.hl)) - end - - local split_console = vim.split(console_string, "\n") - for _, v in ipairs(split_console) do - vim.cmd(string.format([[echom "[%s] %s"]], config.plugin, vim.fn.escape(v, '"'))) - end - - if config.highlights and level_config.hl then - vim.cmd("echohl NONE") - end + + local levels = {} + for i, v in ipairs(config.modes) do + levels[v.name] = i end - -- Output to log file - if config.use_file then - local fp = io.open(outfile, "a") - local str = string.format("[%-6s%s] %s: %s\n", - nameupper, os.date(), lineinfo, msg) - fp:write(str) - fp:close() + local round = function(x, increment) + increment = increment or 1 + x = x / increment + return (x > 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)) * increment end - end - for i, x in ipairs(config.modes) do - obj[x.name] = function(...) - return log_at_level(i, x, make_string, ...) + local make_string = function(...) + local t = {} + for i = 1, select("#", ...) do + local x = select(i, ...) + + if type(x) == "number" and config.float_precision then + x = tostring(round(x, config.float_precision)) + elseif type(x) == "table" and config.should_inspect then + x = vim.inspect(x) + else + x = tostring(x) + end + + t[#t + 1] = x + end + return table.concat(t, " ") + end + + local log_at_level = function(level, level_config, message_maker, ...) + -- Return early if we're below the config.level + if level < levels[config.level] then + return + end + local nameupper = level_config.name:upper() + + local msg = message_maker(...) + local info = debug.getinfo(2, "Sl") + local lineinfo = info.short_src .. ":" .. info.currentline + + -- Output to console + if config.use_console then + local console_string = + string.format("[%-6s%s] %s: %s", nameupper, os.date("%H:%M:%S"), lineinfo, msg) + + if config.highlights and level_config.hl then + vim.cmd(string.format("echohl %s", level_config.hl)) + end + + local split_console = vim.split(console_string, "\n") + for _, v in ipairs(split_console) do + vim.cmd(string.format([[echom "[%s] %s"]], config.plugin, vim.fn.escape(v, '"'))) + end + + if config.highlights and level_config.hl then + vim.cmd("echohl NONE") + end + end + + -- Output to log file + if config.use_file then + local fp = io.open(outfile, "a") + local str = string.format("[%-6s%s] %s: %s\n", nameupper, os.date(), lineinfo, msg) + fp:write(str) + fp:close() + end end - obj[("fmt_%s" ):format(x.name)] = function() - return log_at_level(i, x, function(...) - local passed = {...} - local fmt = table.remove(passed, 1) - local inspected = {} - for _, v in ipairs(passed) do - table.insert(inspected, vim.inspect(v)) + for i, x in ipairs(config.modes) do + obj[x.name] = function(...) + return log_at_level(i, x, make_string, ...) + end + + obj[("fmt_%s"):format(x.name)] = function() + return log_at_level(i, x, function(...) + local passed = { ... } + local fmt = table.remove(passed, 1) + local inspected = {} + for _, v in ipairs(passed) do + table.insert(inspected, vim.inspect(v)) + end + return string.format(fmt, unpack(inspected)) + end) end - return string.format(fmt, unpack(inspected)) - end) end - end end log.new(default_config, true) diff --git a/lua/vim-be-good/menu.lua b/lua/vim-be-good/menu.lua index afbb899..7c9dee1 100644 --- a/lua/vim-be-good/menu.lua +++ b/lua/vim-be-good/menu.lua @@ -23,7 +23,7 @@ local instructions = { "intended to help you improve your vim proficiency.", "delete a line to select the line. If you delete a difficulty,", "it will select that difficulty, but if you delete a game it ", - "will start the game." + "will start the game.", } local credits = { @@ -63,8 +63,7 @@ function Menu:new(window, onResults) end local function getMenuLength() - return #types.games + #types.difficulty + #gameHeader + - #difficultyHeader + #credits + return #types.games + #types.difficulty + #gameHeader + #difficultyHeader + #credits end local function getTableChanges(lines, compareSet, startIdx) @@ -138,7 +137,7 @@ end function Menu:render() self.window.buffer:clearGameLines() - local lines = { } + local lines = {} for idx = 1, #gameHeader do table.insert(lines, gameHeader[idx]) end @@ -167,4 +166,3 @@ function Menu:close() end return Menu - diff --git a/lua/vim-be-good/scheduler.lua b/lua/vim-be-good/scheduler.lua index 1970285..1821275 100644 --- a/lua/vim-be-good/scheduler.lua +++ b/lua/vim-be-good/scheduler.lua @@ -18,5 +18,4 @@ local function run() end) end - return schedule diff --git a/lua/vim-be-good/statistics.lua b/lua/vim-be-good/statistics.lua index 40673ec..c7ea8ce 100644 --- a/lua/vim-be-good/statistics.lua +++ b/lua/vim-be-good/statistics.lua @@ -1,9 +1,8 @@ local log = require("vim-be-good.log") -local default_config = { - plugin = 'VimBeGoodStats', +local default_config = { + plugin = "VimBeGoodStats", save_statistics = vim.g["vim_be_good_save_statistics"] or false, - } local statistics = {} @@ -13,8 +12,12 @@ function statistics:new(config) config = vim.tbl_deep_extend("force", default_config, config) local stats = { - file = string.format('%s/%s.log', vim.api.nvim_call_function('stdpath', {'data'}), config.plugin), - saveStats = config.save_statistics + file = string.format( + "%s/%s.log", + vim.api.nvim_call_function("stdpath", { "data" }), + config.plugin + ), + saveStats = config.save_statistics, } self.__index = self return setmetatable(stats, self) @@ -23,8 +26,15 @@ end function statistics:logResult(result) if self.saveStats then local fp = io.open(self.file, "a") - local str = string.format("%s,%s,%s,%s,%s,%f\n", - result.timestamp, result.roundNum, result.difficulty, result.roundName, result.success, result.time) + local str = string.format( + "%s,%s,%s,%s,%s,%f\n", + result.timestamp, + result.roundNum, + result.difficulty, + result.roundName, + result.success, + result.time + ) fp:write(str) fp:close() end diff --git a/lua/vim-be-good/types.lua b/lua/vim-be-good/types.lua index 5a1281b..ddb0dd0 100644 --- a/lua/vim-be-good/types.lua +++ b/lua/vim-be-good/types.lua @@ -17,6 +17,5 @@ local games = { return { difficulty = difficulty, - games = games + games = games, } - diff --git a/lua/vim-be-good/window.lua b/lua/vim-be-good/window.lua index da12afb..c8720e6 100644 --- a/lua/vim-be-good/window.lua +++ b/lua/vim-be-good/window.lua @@ -22,7 +22,6 @@ local function generateConfig(rowPadding, colPadding) end function WindowHandler:new(rowPadding, colPadding) - local newWindow = { config = generateConfig(rowPadding, colPadding), rowPadding = rowPadding, @@ -85,4 +84,3 @@ function WindowHandler:onResize() end return WindowHandler - diff --git a/plugin/vim-be-good.vim b/plugin/vim-be-good.vim deleted file mode 100644 index 962119f..0000000 --- a/plugin/vim-be-good.vim +++ /dev/null @@ -1,16 +0,0 @@ -if has('nvim-0.5') - fun! VimBeGood() - " dont forget to remove this one.... - lua for k in pairs(package.loaded) do if k:match("^vim%-be%-good") then package.loaded[k] = nil end end - lua require("vim-be-good").menu() - endfun - - com! VimBeGood call VimBeGood() - - augroup VimBeGood - autocmd! - autocmd VimResized * :lua require("vim-be-good").onVimResize() - augroup END -else - echo 'You need nvim v0.5 or above to get better at vim' -endif diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..770afcd --- /dev/null +++ b/stylua.toml @@ -0,0 +1,5 @@ +column_width = 100 +indent_type = "Spaces" +indent_width = 4 +quote_style = "AutoPreferDouble" +no_call_parentheses = false