Skip to content

Commit

Permalink
fix: ffi calls for macos
Browse files Browse the repository at this point in the history
  • Loading branch information
Conni2461 committed Sep 10, 2023
1 parent 0232372 commit 7851629
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 40 deletions.
16 changes: 12 additions & 4 deletions lua/plenary/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,17 @@ local function shorten_len(filename, len, exclude)
end

local shorten = (function()
local fallback = function(filename)
return shorten_len(filename, 1)
end

if jit and path.sep ~= "\\" then
local ffi = require "ffi"
ffi.cdef [[
typedef unsigned char char_u;
void shorten_dir(char_u *str);
]]
return function(filename)
local ffi_func = function(filename)
if not filename or is_uri(filename) then
return filename
end
Expand All @@ -447,10 +451,14 @@ local shorten = (function()
ffi.C.shorten_dir(c_str)
return ffi.string(c_str)
end
local ok = pcall(ffi_func, "/tmp/path/file.lua")
if ok then
return ffi_func
else
return fallback
end
end
return function(filename)
return shorten_len(filename, 1)
end
return fallback
end)()

function Path:shorten(len, exclude)
Expand Down
61 changes: 39 additions & 22 deletions lua/plenary/scandir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@ local gen_date = (function()
end)()

local get_username = (function()
local fallback = function(tbl, id)
if not tbl then
return id
end
if tbl[id] then
return tbl[id]
end
tbl[id] = tostring(id)
return id
end

if jit and os_sep ~= "\\" then
local ffi = require "ffi"
ffi.cdef [[
Expand All @@ -340,7 +351,7 @@ local get_username = (function()
passwd *getpwuid(uid_t uid);
]]

return function(tbl, id)
local ffi_func = function(tbl, id)
if tbl[id] then
return tbl[id]
end
Expand All @@ -354,21 +365,30 @@ local get_username = (function()
tbl[id] = name
return name
end
else
return function(tbl, id)
if not tbl then
return id
end
if tbl[id] then
return tbl[id]
end
tbl[id] = tostring(id)
return id

local ok = pcall(ffi_func, {}, 1000)
if ok then
return ffi_func
else
return fallback
end
else
return fallback
end
end)()

local get_groupname = (function()
local fallback = function(tbl, id)
if not tbl then
return id
end
if tbl[id] then
return tbl[id]
end
tbl[id] = tostring(id)
return id
end

if jit and os_sep ~= "\\" then
local ffi = require "ffi"
ffi.cdef [[
Expand All @@ -384,7 +404,7 @@ local get_groupname = (function()
group *getgrgid(gid_t gid);
]]

return function(tbl, id)
local ffi_func = function(tbl, id)
if tbl[id] then
return tbl[id]
end
Expand All @@ -398,17 +418,14 @@ local get_groupname = (function()
tbl[id] = name
return name
end
else
return function(tbl, id)
if not tbl then
return id
end
if tbl[id] then
return tbl[id]
end
tbl[id] = tostring(id)
return id
local ok = pcall(ffi_func, {}, 1000)
if ok then
return ffi_func
else
return fallback
end
else
return fallback
end
end)()

Expand Down
44 changes: 30 additions & 14 deletions lua/plenary/strings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,48 @@ local path = require("plenary.path").path
local M = {}

M.strdisplaywidth = (function()
local fallback = function(str, col)
str = tostring(str)
if vim.in_fast_event() then
return #str - (col or 0)
end
return vim.fn.strdisplaywidth(str, col)
end

if jit and path.sep ~= [[\]] then
local ffi = require "ffi"
ffi.cdef [[
typedef unsigned char char_u;
int linetabsize_col(int startcol, char_u *s);
]]

return function(str, col)
local ffi_func = function(str, col)
str = tostring(str)
local startcol = col or 0
local s = ffi.new("char[?]", #str + 1)
ffi.copy(s, str)
return ffi.C.linetabsize_col(startcol, s) - startcol
end
else
return function(str, col)
str = tostring(str)
if vim.in_fast_event() then
return #str - (col or 0)
end
return vim.fn.strdisplaywidth(str, col)

local ok = pcall(ffi_func, "hello")
if ok then
return ffi_func
else
return fallback
end
else
return fallback
end
end)()

M.strcharpart = (function()
local fallback = function(str, nchar, charlen)
if vim.in_fast_event() then
return str:sub(nchar + 1, charlen)
end
return vim.fn.strcharpart(str, nchar, charlen)
end

if jit and path.sep ~= [[\]] then
local ffi = require "ffi"
ffi.cdef [[
Expand All @@ -42,6 +58,11 @@ M.strcharpart = (function()
return ffi.C.utf_ptr2len(c_str)
end

local ok = pcall(utf_ptr2len, "🔭")
if not ok then
return fallback
end

return function(str, nchar, charlen)
local nbyte = 0
if nchar > 0 then
Expand Down Expand Up @@ -83,12 +104,7 @@ M.strcharpart = (function()
return str:sub(nbyte + 1, nbyte + len)
end
else
return function(str, nchar, charlen)
if vim.in_fast_event() then
return str:sub(nchar + 1, charlen)
end
return vim.fn.strcharpart(str, nchar, charlen)
end
return fallback
end
end)()

Expand Down

0 comments on commit 7851629

Please sign in to comment.