Skip to content

Commit

Permalink
Minor bug fixes to validate user input (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
csessh authored Dec 12, 2024
1 parent 0e0f6e0 commit 20ab685
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 3 additions & 4 deletions lua/aoc/api.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lua/aoc/cache.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local inspect = require "vim.inspect"

---@class PuzzleCache
local M = {}

Expand Down
10 changes: 10 additions & 0 deletions lua/aoc/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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, {})

Expand Down

0 comments on commit 20ab685

Please sign in to comment.