Skip to content

Commit

Permalink
Fix liniting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonVanAssche committed Jul 19, 2024
1 parent 8fae3ec commit 23c7503
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 47 deletions.
97 changes: 53 additions & 44 deletions lua/date-time-inserter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ local M = {}

-- Default settings.
local settings = {
date_format = 'MMDDYYYY', -- DDMMYYYY, MMDDYYYY, YYYYMMDD or whatever you want.
-- As long as it's in the same format without spaces or other characters.
date_format = "MMDDYYYY", -- DDMMYYYY, MMDDYYYY, YYYYMMDD or whatever you want.
date_separator = "/", -- Character used to separate the date parts.

date_separator = '/', -- Character used to separate the date parts.
time_format = 12, -- Can be 24 or 12 for 24 hour or 12 hour time.
show_seconds = false, -- Whether to show seconds in the time (true) or not (false)

time_format = 12, -- Can be 24 or 12 for 24 hour or 12 hour time.

show_seconds = false, -- Whether to show seconds in the time (true) or not (false)

insert_date_map = '<leader>dt', -- Keymap to insert the date (in 'normal' mode).
insert_time_map = '<leader>tt', -- Keymap to insert the time (in 'normal' mode).
insert_date_time_map = '<leader>dtt', -- Keymap to insert the date and time (in 'normal' mode).
insert_date_map = "<leader>dt", -- Keymap to insert the date (in 'normal' mode).
insert_time_map = "<leader>tt", -- Keymap to insert the time (in 'normal' mode).
insert_date_time_map = "<leader>dtt", -- Keymap to insert the date and time (in 'normal' mode).
}

-- Validate whether the configured date format is valid.
Expand All @@ -22,32 +19,44 @@ local settings = {
local function validate_date_format(date_format)
-- Check if the date format has the correct length.
if string.len(date_format) ~= 8 then
print('INVALID_DATE_FORMAT: Date format must be 8 characters long (e.g. MMDDYYYY).')
return 'MMDDYYYY'
print("INVALID_DATE_FORMAT: Date format must be 8 characters long (e.g. MMDDYYYY).")
return "MMDDYYYY"
end

-- Check wheter the date format contains all the required characters.
if not string.find(date_format, 'M') or not string.find(date_format, 'D') or not string.find(date_format, 'Y') then
print('INVALID_DATE_FORMAT: Date format must contain the characters M, D and Y (e.g. MMDDYYYY).')
return 'MMDDYYYY'
if
not string.find(date_format, "M")
or not string.find(date_format, "D")
or not string.find(date_format, "Y")
then
print(
"INVALID_DATE_FORMAT: Date format must contain the characters M, D and Y (e.g. MMDDYYYY)."
)
return "MMDDYYYY"
end

-- Check if the date format contains the string 'DD' once.
if string.find(date_format, 'DD') == nil then
print('INVALID_DATE_FORMAT: Date format must contain exactly one occurrence of the \'DD\' string (e.g. MMDDYYYY).')
return 'MMDDYYYY'
if string.find(date_format, "DD") == nil then
print(
'INVALID_DATE_FORMAT: Date format must contain exactly one occurrence of the "DD" string (e.g. MMDDYYYY).'
)
return "MMDDYYYY"
end

-- Check if the date format contains the string 'MM' once.
if string.find(date_format, 'MM') == nil then
print('INVALID_DATE_FORMAT: Date format must contain exactly one occurrence of the \'MM\' string (e.g. MMDDYYYY).')
return 'MMDDYYYY'
if string.find(date_format, "MM") == nil then
print(
'INVALID_DATE_FORMAT: Date format must contain exactly one occurrence of the "MM" string (e.g. MMDDYYYY).'
)
return "MMDDYYYY"
end

-- Check if the date format contains the string 'YYYY' once.
if string.find(date_format, 'YYYY') == nil then
print('INVALID_DATE_FORMAT: Date format must contain exactly one occurrence of the \'YYYY\' string (e.g. MMDDYYYY).')
return 'MMDDYYYY'
if string.find(date_format, "YYYY") == nil then
print(
'INVALID_DATE_FORMAT: Date format must contain exactly one occurrence of the "YYYY" string (e.g. MMDDYYYY).'
)
return "MMDDYYYY"
end

return date_format
Expand All @@ -58,7 +67,7 @@ end
-- @param time_format The time format to validate.
local function validate_time_format(time_format)
if time_format ~= 12 and time_format ~= 24 then
print('INVALID_TIME_FORMAT: Time format must be either 12 or 24.')
print("INVALID_TIME_FORMAT: Time format must be either 12 or 24.")
return 12
end

Expand All @@ -73,7 +82,7 @@ local function convert_date_to_config_format(date)
-- Fall back to the default format if the user specified an invalid format.
local date_format = validate_date_format(string.upper(settings.date_format))
local separator = settings.date_separator
local new_date = ''
local new_date = ""

-- Extract the year, month and day from the date string
local year = date:sub(1, 4)
Expand All @@ -84,17 +93,17 @@ local function convert_date_to_config_format(date)
-- This allows us to easily access the different parts of the date
-- when constructing the final string with the desired format.
local date_table = {
['Y'] = year,
['M'] = month,
['D'] = day,
["Y"] = year,
["M"] = month,
["D"] = day,
}

-- Loop through the date format and add the date to the new date
-- in the order specified by the user
-- e.g. MMDDYYYY -> 12/31/2022
-- e.g. DDMMYYYY -> 31/12/2022
local incuded = {}
for char in date_format:gmatch('.') do
for char in date_format:gmatch(".") do
if not incuded[char] then
new_date = new_date .. date_table[char] .. separator
incuded[char] = true
Expand Down Expand Up @@ -126,21 +135,21 @@ local function convert_time_to_config_format(time)
if time_format == 12 then
if tonumber(hour) > 12 then
hour = tostring(tonumber(hour) - 12)
time = hour .. ':' .. minute .. ' PM'
time = hour .. ":" .. minute .. " PM"
else
-- Check whether the time should be displayed with seconds.
if settings.show_seconds then
time = hour .. ':' .. minute .. ':' .. second .. ' AM'
time = hour .. ":" .. minute .. ":" .. second .. " AM"
else
time = hour .. ':' .. minute .. ' AM'
time = hour .. ":" .. minute .. " AM"
end
end
else
-- Check whether the time should be displayed with seconds.
if settings.show_seconds then
time = hour .. ':' .. minute .. ':' .. second
time = hour .. ":" .. minute .. ":" .. second
else
time = hour .. ':' .. minute
time = hour .. ":" .. minute
end
end

Expand All @@ -153,13 +162,13 @@ end
local function set_keymap(key, command)
local keymap = vim.keymap.set
local opts = { noremap = true, silent = true }
keymap('n', key, command, opts)
keymap("n", key, command, opts)
end

-- Check if a setting is nil or empty.
-- @param setting: The setting to check.
local function setting_is_empty(setting)
return setting == nil or setting == ''
return setting == nil or setting == ""
end

-- Set the settings, if any where passed.
Expand Down Expand Up @@ -190,43 +199,43 @@ end
-- Example: 12/31/2022
M.insert_date = function()
-- Get the current date.
local date = os.date('%Y-%m-%d')
local date = os.date("%Y-%m-%d")

-- Convert the date to the format specified in the settings.
local date_format = convert_date_to_config_format(date)

-- Insert the date at the cursor position.
vim.api.nvim_put({date_format}, 'c', true, true)
vim.api.nvim_put({ date_format }, "c", true, true)
end

-- Insert the current time into the current buffer.
-- The time will be inserted at the cursor position.
-- Example: 13:00:00 PM
M.insert_time = function()
-- Get the current time.
local time = os.date('%H:%M:%S')
local time = os.date("%H:%M:%S")

-- Convert the time to the format specified in the settings.
local time_format = convert_time_to_config_format(time)

-- Insert the time at the cursor position.
vim.api.nvim_put({time_format}, 'c', true, true)
vim.api.nvim_put({ time_format }, "c", true, true)
end

-- Insert the current date and time into the current buffer.
-- The date and time will be inserted at the cursor position.
-- Example: 12/31/2022 at 1:00:00 PM
M.insert_date_time = function()
-- Get the current date and time.
local date = os.date('%Y-%m-%d')
local time = os.date('%H:%M:%S')
local date = os.date("%Y-%m-%d")
local time = os.date("%H:%M:%S")

-- Convert the date and time to the format specified in the settings.
local date_format = convert_date_to_config_format(date)
local time_format = convert_time_to_config_format(time)

-- Insert the date and time at the cursor position.
vim.api.nvim_put({date_format .. ' at ' .. time_format}, 'c', true, true)
vim.api.nvim_put({ date_format .. " at " .. time_format }, "c", true, true)
end

return M
12 changes: 9 additions & 3 deletions plugin/date-time-inserter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ if _G.loaded_date_time_inserter then
end

-- Assign commands to the functionality of the plugin.
vim.api.nvim_create_user_command('InsertDate', function() require('date-time-inserter').insert_date() end, {})
vim.api.nvim_create_user_command('InsertTime', function() require('date-time-inserter').insert_time() end, {})
vim.api.nvim_create_user_command('InsertDateTime', function() require('date-time-inserter').insert_date_time() end, {})
vim.api.nvim_create_user_command("InsertDate", function()
require("date-time-inserter").insert_date()
end, {})
vim.api.nvim_create_user_command("InsertTime", function()
require("date-time-inserter").insert_time()
end, {})
vim.api.nvim_create_user_command("InsertDateTime", function()
require("date-time-inserter").insert_date_time()
end, {})

-- Set the plugin as loaded.
_G.loaded_date_time_inserter = true

0 comments on commit 23c7503

Please sign in to comment.