Skip to content

Commit 20ab685

Browse files
authoredDec 12, 2024
Minor bug fixes to validate user input (#5)
1 parent 0e0f6e0 commit 20ab685

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed
 

‎lua/aoc/api.lua

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local cache = require "aoc.cache"
22
local curl = require "plenary.curl"
3-
local inspect = require "vim.inspect"
43
local cfg = require "aoc.config"
54

65
---@class APIWrapper
@@ -22,12 +21,12 @@ local validate_args = function(day, year)
2221
local d = tonumber(day)
2322
local y = tonumber(year)
2423

25-
if d < 1 or d > 31 then
26-
vim.api.nvim_err_writeln("Invalid day: " .. d)
24+
if not d or d < 1 or d > 31 then
25+
vim.api.nvim_err_writeln("Invalid day: " .. day)
2726
return false
2827
end
2928

30-
if y < 2015 or y > tonumber(os.date "%Y") then
29+
if not y and y < 2015 or y > tonumber(os.date "%Y") then
3130
vim.api.nvim_err_writeln("Invalid year: " .. year)
3231
return false
3332
end

‎lua/aoc/cache.lua

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local inspect = require "vim.inspect"
2-
31
---@class PuzzleCache
42
local M = {}
53

‎lua/aoc/init.lua

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ local cache = require "aoc.cache"
55
---@class AOC
66
local M = {}
77

8+
---Strip input of any leading/trailing spaces
9+
---@param s string
10+
---@return string
11+
local trim = function(s)
12+
s, _ = string.gsub(s, "%s+", "")
13+
return s
14+
end
15+
816
---@param args any
917
M.setup = function(args)
1018
cfg.init(args)
@@ -14,6 +22,8 @@ M.setup = function(args)
1422
local year = vim.fn.input "Year: "
1523
vim.api.nvim_out_write "\n"
1624

25+
day = trim(day)
26+
year = trim(year)
1727
api.save_puzzle_input(day, year)
1828
end, {})
1929

0 commit comments

Comments
 (0)