Skip to content

Commit

Permalink
Merge branch 'main' into github/templates
Browse files Browse the repository at this point in the history
  • Loading branch information
DrKJeff16 committed Jun 22, 2024
2 parents 4ee82ed + 083cdb5 commit ca98268
Show file tree
Hide file tree
Showing 9 changed files with 374 additions and 359 deletions.
149 changes: 78 additions & 71 deletions lua/user/check/exists.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
---@diagnostic disable:unused-local
---@diagnostic disable:unused-function
---@diagnostic disable:need-check-nil
---@diagnostic disable:missing-fields

require('user.types.user.check')

local Value = require('user.check.value')

local is_nil = Value.is_nil
Expand All @@ -14,26 +10,62 @@ local is_num = Value.is_num
local is_fun = Value.is_fun
local empty = Value.empty

---@type User.Check.Existance
M = {
module = function(mod, return_mod)
return_mod = is_bool(return_mod) and return_mod or false

---@type boolean
local res
---@type unknown
local m
res, m = pcall(require, mod)

if return_mod then
return not is_nil(m) and m or nil
else
return res
---@type fun(mod: string, return_mod: boolean?): boolean|unknown|nil
local function module(mod, return_mod)
return_mod = is_bool(return_mod) and return_mod or false

---@type boolean
local res
---@type unknown
local m
res, m = pcall(require, mod)

if return_mod then
return not is_nil(m) and m or nil
else
return res
end
end

---@type fun(mod: string|string[], need_all: boolean?): boolean|table<string, boolean>
local function modules(mod, need_all)
local exists = module

if not (is_str(mod) or is_tbl(mod)) or empty(mod) then
error('`(user.check.exists.modules)`: Input is neither a string nor a table.')
end

need_all = is_bool(need_all) and need_all or false

---@type boolean|table<string, boolean>
local res = false

if is_str(mod) then
res = exists(mod)
else
res = {}

for _, v in next, mod do
local r = exists(v)

if need_all then
res[v] = r
else
res = r

-- Break when a module is not found.
if not r then
break
end
end
end
end,
}
end

return res
end

function M.vim_has(expr)
---@type fun(expr: string|string[]): boolean
local function vim_has(expr)
if is_str(expr) then
return vim.fn.has(expr) == 1
end
Expand All @@ -42,7 +74,7 @@ function M.vim_has(expr)
local res = false

for _, v in next, expr do
if not M.vim_has(v) then
if not vim_has(v) then
return false
end
end
Expand All @@ -53,7 +85,8 @@ function M.vim_has(expr)
return false
end

function M.vim_exists(expr)
---@type fun(expr: string|string[]): boolean
local function vim_exists(expr)
local exists = vim.fn.exists

if is_str(expr) then
Expand All @@ -63,7 +96,7 @@ function M.vim_exists(expr)
if is_tbl(expr) and not empty(expr) then
local res = false
for _, v in next, expr do
res = M.vim_exists(v)
res = vim_exists(v)

if not res then
break
Expand All @@ -76,10 +109,7 @@ function M.vim_exists(expr)
return false
end

function M.vim_isdir(path)
return (is_str(path) and not empty(path)) and (vim.fn.isdirectory(path) == 1) or false
end

---@type fun(vars: string|string[], fallback: fun()?): boolean
local function env_vars(vars, fallback)
local environment = vim.fn.environ()

Expand Down Expand Up @@ -110,11 +140,8 @@ local function env_vars(vars, fallback)
return res
end

M.env_vars = env_vars

function M.executable(exe, fallback)
local executable = vim.fn.executable

---@type fun(exe: string|string[], fallback: fun()?): boolean
local function executable(exe, fallback)
if not (is_str(exe) or is_tbl(exe)) then
error('(user.check.exists.executable): Argument type is neither string nor table')
end
Expand All @@ -124,10 +151,10 @@ function M.executable(exe, fallback)
local res = false

if is_str(exe) then
res = executable(exe) == 1
res = vim.fn.executable(exe) == 1
elseif is_tbl(exe) then
for _, v in next, exe do
res = M.executable(v)
res = executable(v)

if not res then
break
Expand All @@ -142,40 +169,20 @@ function M.executable(exe, fallback)
return res
end

function M.modules(mod, need_all)
local exists = M.module

if not (is_str(mod) or is_tbl(mod)) or empty(mod) then
error('`(user.check.exists.modules)`: Input is neither a string nor a table.')
end

need_all = is_bool(need_all) and need_all or false

---@type boolean|table<string, boolean>
local res = false

if is_str(mod) then
res = exists(mod)
else
res = {}

for _, v in next, mod do
local r = exists(v)

if need_all then
res[v] = r
else
res = r

-- Break when a module is not found.
if not r then
break
end
end
end
end

return res
---@type fun(path: string): boolean
local function vim_isdir(path)
return (is_str(path) and not empty(path)) and (vim.fn.isdirectory(path) == 1) or false
end

---@type User.Check.Existance
local M = {
module = module,
vim_has = vim_has,
vim_exists = vim_exists,
env_vars = env_vars,
executable = executable,
modules = modules,
vim_isdir = vim_isdir,
}

return M
38 changes: 19 additions & 19 deletions lua/user/check/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ local Exists = require('user.check.exists')
--- Checking Utilities
---@type User.Check
local M = {
value = Value,
exists = Exists,
value = require('user.check.value'),
exists = require('user.check.exists'),

--- Check whether Nvim is running in a Linux Console rather than a `pty`.
--- ---
--- ## Description
--- This function can be useful for (un)loading certain elements that conflict with the Linux console, for example.
--- ---
--- ## Return
--- A boolean that confirms whether the environment is a Linux Console.
--- ---
in_console = function()
local env = vim.fn.environ()
---@type string
local TERM = env['TERM']

--- TODO: This is not a good enough check. Must find a better solution.
return vim.tbl_contains({ 'screen', 'linux' }, TERM)
end,
}

--- Check whether Nvim is running in a Linux Console rather than a `pty`.
--- ---
--- ## Description
--- This function can be useful for (un)loading certain elements that conflict with the Linux console, for example.
--- ---
--- ## Return
--- A boolean that confirms whether the environment is a Linux Console.
--- ---
function M.in_console()
local env = vim.fn.environ()
---@type string
local TERM = env['TERM']

--- TODO: This is not a good enough check. Must find a better solution.
return vim.tbl_contains({ 'screen', 'linux' }, TERM)
end

function M.new()
local self = setmetatable({}, { __index = M })

Expand Down
73 changes: 43 additions & 30 deletions lua/user/check/value.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,41 @@

require('user.types.user.check')

--- Checks whether a value is `nil`, i.e. non existant or explicitly set as nil.
--- ## Parameters
---
--- * `var`: Any data type to be checked if it's nil.
--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**.
--- Otherwise it will be flagged as non-existant and the function will return `true`.
---
--- * `multiple`: Tell the function you're checking for multiple values. (Default: `false`).
--- If set to `true`, every element of the table will be checked.
--- If **any** element doesn't exist or is `nil`, the function automatically returns false.
---
--- ## Return
--- A boolean value indicating whether the data is `nil` or doesn't exist.
--- ---
local function is_nil(var, multiple)
multiple = (multiple ~= nil and type(multiple) == 'boolean') and multiple or false

if not multiple then
return var == nil
end

--- Treat `var` as a table from here on
if type(var) ~= 'table' or vim.tbl_isempty(var) then
return false
end

for _, v in next, var do
if v ~= nil then
return false
end
end

return true
end

---@type User.Check.Value
local M = {
-- NOTE: We define `is_nil` first as it's used by the other checkers.
Expand All @@ -23,33 +58,12 @@ local M = {
--- ## Return
--- A boolean value indicating whether the data is `nil` or doesn't exist.
--- ---
is_nil = function(var, multiple)
multiple = (multiple ~= nil and type(multiple) == 'boolean') and multiple or false

if not multiple then
return var == nil
end

--- Treat `var` as a table from here on
if type(var) ~= 'table' or vim.tbl_isempty(var) then
return false
end

for _, v in next, var do
if v ~= nil then
return false
end
end

return true
end,
is_nil = is_nil,
}

---@type fun(t: Types): ValueFunc
local function type_fun(t)
return function(var, multiple)
local is_nil = M.is_nil

multiple = (not is_nil(multiple) and type(multiple) == 'boolean') and multiple or false

if not multiple then
Expand Down Expand Up @@ -164,7 +178,6 @@ M.is_tbl = type_fun('table')
--- A boolean value indicating whether the data is an integer or not.
--- ---
function M.is_int(var, multiple)
local is_nil = M.is_nil
local is_tbl = M.is_tbl
local is_bool = M.is_bool
local is_num = M.is_num
Expand Down Expand Up @@ -222,8 +235,7 @@ function M.empty(v)
return true
end

function M.fields(fields, T)
local is_nil = M.is_nil
local function fields(field, T)
local is_tbl = M.is_tbl
local is_str = M.is_str
local is_num = M.is_num
Expand All @@ -233,22 +245,23 @@ function M.fields(fields, T)
error('(user.check.value.fields): Cannot look up a field in the following type: ' .. type(T))
end

if not (is_str(fields) or is_num(fields) or is_tbl(fields)) or empty(fields) then
if not (is_str(field) or is_num(field) or is_tbl(field)) or empty(field) then
error('(user.check.value.fields): Field type `' .. type(T) .. '` not parseable')
end

if not is_tbl(fields) then
return not is_nil(T[fields])
if not is_tbl(field) then
return not is_nil(T[field])
end

for _, v in next, fields do
if not M.fields(v, T) then
for _, v in next, field do
if not fields(v, T) then
return false
end
end

return true
end
M.fields = fields

function M.tbl_values(values, T, return_keys)
local is_tbl = M.is_tbl
Expand Down
Loading

0 comments on commit ca98268

Please sign in to comment.