From a618300ceb55a1d4778b7bf428372a7bed206f12 Mon Sep 17 00:00:00 2001 From: Thang Do Date: Thu, 12 Dec 2024 20:07:56 +1030 Subject: [PATCH] Minor bug fixes to validate user input --- lua/aoc/api.lua | 7 +++---- lua/aoc/cache.lua | 2 -- lua/aoc/init.lua | 10 ++++++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lua/aoc/api.lua b/lua/aoc/api.lua index f96f900..df86e7d 100644 --- a/lua/aoc/api.lua +++ b/lua/aoc/api.lua @@ -1,6 +1,5 @@ local cache = require "aoc.cache" local curl = require "plenary.curl" -local inspect = require "vim.inspect" local cfg = require "aoc.config" ---@class APIWrapper @@ -22,12 +21,12 @@ local validate_args = function(day, year) local d = tonumber(day) local y = tonumber(year) - if d < 1 or d > 31 then - vim.api.nvim_err_writeln("Invalid day: " .. d) + if not d or d < 1 or d > 31 then + vim.api.nvim_err_writeln("Invalid day: " .. day) return false end - if y < 2015 or y > tonumber(os.date "%Y") then + if not y and y < 2015 or y > tonumber(os.date "%Y") then vim.api.nvim_err_writeln("Invalid year: " .. year) return false end diff --git a/lua/aoc/cache.lua b/lua/aoc/cache.lua index b69f378..5c04c66 100644 --- a/lua/aoc/cache.lua +++ b/lua/aoc/cache.lua @@ -1,5 +1,3 @@ -local inspect = require "vim.inspect" - ---@class PuzzleCache local M = {} diff --git a/lua/aoc/init.lua b/lua/aoc/init.lua index 51817e0..89e647c 100644 --- a/lua/aoc/init.lua +++ b/lua/aoc/init.lua @@ -5,6 +5,14 @@ local cache = require "aoc.cache" ---@class AOC local M = {} +---Strip input of any leading/trailing spaces +---@param s string +---@return string +local trim = function(s) + s, _ = string.gsub(s, "%s+", "") + return s +end + ---@param args any M.setup = function(args) cfg.init(args) @@ -14,6 +22,8 @@ M.setup = function(args) local year = vim.fn.input "Year: " vim.api.nvim_out_write "\n" + day = trim(day) + year = trim(year) api.save_puzzle_input(day, year) end, {})