Skip to content

Commit

Permalink
downloader works now
Browse files Browse the repository at this point in the history
  • Loading branch information
airtonix committed Dec 27, 2022
1 parent e493671 commit 353dcaf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 64 deletions.
54 changes: 27 additions & 27 deletions lua/fzf_lib.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local ffi = require "ffi"

local library_path = (function()
local dirname = string.sub(debug.getinfo(1).source, 2, #"/fzf_lib.lua" * -1)
if package.config:sub(1, 1) == "\\" then
return dirname .. "../build/libfzf.dll"
else
return dirname .. "../build/libfzf.so"
end
local dirname = string.sub(debug.getinfo(1).source, 2, #"/fzf_lib.lua" * -1)
if package.config:sub(1, 1) == "\\" then
return dirname .. "../build/libfzf.dll"
else
return dirname .. "../build/libfzf.so"
end
end)()
local native = ffi.load(library_path)

Expand Down Expand Up @@ -44,42 +44,42 @@ ffi.cdef [[
local fzf = {}

fzf.get_score = function(input, pattern_struct, slab)
return native.fzf_get_score(input, pattern_struct, slab)
return native.fzf_get_score(input, pattern_struct, slab)
end

fzf.get_pos = function(input, pattern_struct, slab)
local pos = native.fzf_get_positions(input, pattern_struct, slab)
if pos == nil then
return
end

local res = {}
for i = 1, tonumber(pos.size) do
res[i] = pos.data[i - 1] + 1
end
native.fzf_free_positions(pos)

return res
local pos = native.fzf_get_positions(input, pattern_struct, slab)
if pos == nil then
return
end

local res = {}
for i = 1, tonumber(pos.size) do
res[i] = pos.data[i - 1] + 1
end
native.fzf_free_positions(pos)

return res
end

fzf.parse_pattern = function(pattern, case_mode, fuzzy)
case_mode = case_mode == nil and 0 or case_mode
fuzzy = fuzzy == nil and true or fuzzy
local c_str = ffi.new("char[?]", #pattern + 1)
ffi.copy(c_str, pattern)
return native.fzf_parse_pattern(case_mode, false, c_str, fuzzy)
case_mode = case_mode == nil and 0 or case_mode
fuzzy = fuzzy == nil and true or fuzzy
local c_str = ffi.new("char[?]", #pattern + 1)
ffi.copy(c_str, pattern)
return native.fzf_parse_pattern(case_mode, false, c_str, fuzzy)
end

fzf.free_pattern = function(p)
native.fzf_free_pattern(p)
native.fzf_free_pattern(p)
end

fzf.allocate_slab = function()
return native.fzf_make_default_slab()
return native.fzf_make_default_slab()
end

fzf.free_slab = function(s)
native.fzf_free_slab(s)
native.fzf_free_slab(s)
end

return fzf
76 changes: 39 additions & 37 deletions lua/telescope-fzf-native/download_library.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
local uv = vim.loop
local plugin_path = string.sub(debug.getinfo(1).source, 2, #"/telescope-fzf-native.lua" * -1)
local plugin_path = string.sub(debug.getinfo(1).source, 2, #"/lua/telescope-fzf-native/download_library.lua" * -1)
local releases_url = "https://github.com/airtonix/telescope-fzf-native.nvim/releases/download"


local get_platform = function()
if vim.fn.has("win32") == 1 then
return 'windows'
Expand All @@ -26,6 +25,11 @@ local get_valid_compiler = function(platform)
end
end

local path_join = function(strings)
local path_separator = (platform == "windows") and '\\' or '/'

return table.concat(strings, path_separator)
end

local write_async = function(path, txt, flag)
uv.fs_open(path, flag, 438, function(open_err, fd)
Expand All @@ -42,76 +46,74 @@ end

local spawn = function(cmd, on_exit)
local stdout = uv.new_pipe()
local out = ""

local buffer = ""

local real_cmd = table.remove(cmd, 1)

uv.spawn(real_cmd, {
args = cmd,
stdio = { nil, stdout },
verbatim = true
}, function()
stdout:read_stop()
on_exit(out)
if (type(on_exit) == "function") then
on_exit(buffer)
end
end)

uv.read_start(stdout, function(err, data)
assert(not err, err)
if data then
out = out .. data
buffer = buffer .. data
end
end)
end


-- spawn({ "gh", "api", "repos/nvim-telescope/telescope-fzf-native.nvim/actions/releases" }, function(json)
-- local mes = vim.json.decode(json)
-- local id = mes.artifacts[1].id
-- spawn(
-- { "gh", "api", "repos/nvim-telescope/telescope-fzf-native.nvim/actions/artifacts/" .. id .. "/zip" },
-- function(zip)
-- write_async("libfzf.zip", zip, "w")
-- end
-- )
-- end)
--
end


local download = function(options)
options = options or {}
local platform = options.platform or get_platform()
local compiler = options.compiler or get_valid_compiler(platform)
local version = options.version or "latest"
local path_separator = (platform == "windows") and '\\' or '/'
local library_path = (platform == "windows")
and table.concat({ 'build', 'libfzf.dll' }, path_separator)
or table.concat({ 'build', 'libfzf.so' }, path_separator)
local version = options.version or "dev"

local url = ""
local command = nil

if platform == 'windows' then
url = string.format("%s/%s/windows-2019-%s-libfzf.dll", releases_url, version, compiler)
command = {
'curl', '-L',
string.format("%s/%s/windows-2019-%s-libfzf.dll", releases_url, version, compiler),
'-o', path_join({ 'build', 'libfzf.dll' })
}
end

if platform == 'ubuntu' then
url = string.format("%s/%s/ubuntu-%s-libfzf.so", releases_url, version, compiler)
command = {
"curl", "-L",
string.format("%s/%s/ubuntu-%s-libfzf.so", releases_url, version, compiler),
"-o", path_join({ 'build', 'libfzf.so' })
}
end

if platform == 'macos' then
url = string.format("%s/%s/macos-%s-libfzf.so", releases_url, version, compiler)
command = {
"curl", "-L",
string.format("%s/%s/macos-%s-libfzf.so", releases_url, version, compiler),
"-o", path_join({ 'build', 'libfzf.so' })
}
end

-- Curl the download
--
-- windows :
-- by default has a powershell alias called curl which for our purseposes does the same thing as unix curl
-- macos and linux: should have curl installed by default
-- Ensure the Build directory exists
--
spawn({ 'sh', '-c', '"mkdir ./build"' })

print('downloading ' .. url)
spawn({ 'mkdir', 'build' })
spawn({ 'curl', url }, function(filecontents)
write_async(library_path, filecontents, "w")
print(string.format("installed: %s", library_path))
end)
--
-- Curl the download
--
print("installing", vim.json.encode(command))
spawn(command, function() print('installed') end)

end

Expand Down

0 comments on commit 353dcaf

Please sign in to comment.