From c3edf7924aba1078b7727994d7eb870a45b172db Mon Sep 17 00:00:00 2001 From: Zeno Jiricek Date: Tue, 27 Dec 2022 20:49:07 +1030 Subject: [PATCH] Provide download tool and updated README --- README.md | 60 ++++++++- lua/telescope-fzf-native/download_library.lua | 122 ++++++++++++++++++ lua/telescope-fzf-native/init.lua | 5 + 3 files changed, 183 insertions(+), 4 deletions(-) create mode 100644 lua/telescope-fzf-native/download_library.lua create mode 100644 lua/telescope-fzf-native/init.lua diff --git a/README.md b/README.md index bc78527..5fc135c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# telescope-fzf-native.nvim +``# telescope-fzf-native.nvim **fzf-native** is a `c` port of **[fzf][fzf]**. It only covers the algorithm and implements few functions to support calculating the score. @@ -29,14 +29,65 @@ available for telescope (as native component or as lua component). ## Installation -To get **fzf-native** working, you need to build it with either `cmake` or `make`. As of now, we do not ship binaries. -Both install methods will be supported going forward. +`telescope-fzf-native` is mostly a native binary. We do not commit this to the +repo, so you'll need to include a post install step to get it downloaded from +our github releases. + +```lua +use { + 'nvim-telescope/telescope-fzf-native.nvim', + run = function() require('telescope-fzf-native').download_library() end +} +``` + +Normally this tries to detect your operating system and defaults to downloading +the latest version. + +For other package managers, you'll want to look at the postinstall step: + +- [`packer.nvim`](https://github.com/wbthomason/packer.nvim) wants `run` +- [`lazy.nvim`](https://github.com/folke/lazy.nvim) will want you to use `build` +- [`vimplug`](https://github.com/junegunn/vim-plug) will probably want some kind `do` involving `:lua` :shrug: + + +```vim +Plug 'nvim-telescope/telescope-fzf-native.nvim', { + 'do': ':lua require("telescope-fzf-native").download_library()' +} +``` + +### download options + +```lua +use { + 'nvim-telescope/telescope-fzf-native.nvim', + run = function() + require('telescope-fzf-native').download_library({ + platform = 'windows' -- windows | ubuntu | macos + compiler = 'cc', -- windows: cc, unix: gcc | clang + version = 'latest' -- any release name found on our github releases page + }) + end +} +``` + +> 🤚 Note +> +> You'll need to have both `curl` and `sh` shell installed. +> +> On windows, this is done by installing git, and on linux and mac this should already be solved. + + + +## Building yourself + +If you want to build **fzf-native** yourself, you will need either `cmake` or `make`. ### CMake (Windows, Linux, MacOS) This requires: -- CMake, and the Microsoft C++ Build Tools on Windows +- CMake, and the Microsoft C++ Build Tools (vcc) on Windows - CMake, make, and GCC or Clang on Linux and MacOS #### vim-plug @@ -47,6 +98,7 @@ Plug 'nvim-telescope/telescope-fzf-native.nvim', { 'do': 'cmake -S. -Bbuild -DCM #### packer.nvim + ```lua use {'nvim-telescope/telescope-fzf-native.nvim', run = 'cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build' } ``` diff --git a/lua/telescope-fzf-native/download_library.lua b/lua/telescope-fzf-native/download_library.lua new file mode 100644 index 0000000..c20cfc2 --- /dev/null +++ b/lua/telescope-fzf-native/download_library.lua @@ -0,0 +1,122 @@ +local uv = vim.loop +local plugin_path = string.sub(debug.getinfo(1).source, 2, #"/lua/telescope-fzf-native/download_library.lua" * -1) +local releases_url = "https://github.com/nvim-telescope/telescope-fzf-native.nvim/releases/download" + +local get_platform = function() + if vim.fn.has("win32") == 1 then + return 'windows' + end + + if vim.fn.has("mac") then + return 'macos' + end + + return "ubuntu" +end + +local get_valid_compiler = function(platform) + if platform == 'windows' then + return 'cc' + elseif platform == 'macos' then + return 'gcc' + elseif platform == 'linux' then + return 'gcc' + 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) + assert(not open_err, open_err) + uv.fs_write(fd, txt, -1, function(write_err) + assert(not write_err, write_err) + uv.fs_close(fd, function(close_err) + assert(not close_err, close_err) + end) + end) + end) +end + +local spawn = function(cmd, on_exit) + local stdout = uv.new_pipe() + + local buffer = "" + + local real_cmd = table.remove(cmd, 1) + + uv.spawn(real_cmd, { + args = cmd, + stdio = { nil, stdout }, + verbatim = true + }, function() + stdout:read_stop() + 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 + buffer = buffer .. data + 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 command = nil + + if platform == 'windows' then + command = { + 'curl', '-L', + string.format("%s/%s/windows-2019-%s-libfzf.dll", releases_url, version, compiler), + '-o', path_join({ plugin_path, 'build', 'libfzf.dll' }) + } + end + + if platform == 'ubuntu' then + command = { + "curl", "-L", + string.format("%s/%s/ubuntu-%s-libfzf.so", releases_url, version, compiler), + "-o", path_join({ plugin_path, 'build', 'libfzf.so' }) + } + end + + if platform == 'macos' then + command = { + "curl", "-L", + string.format("%s/%s/macos-%s-libfzf.so", releases_url, version, compiler), + "-o", path_join({ plugin_path, 'build', 'libfzf.so' }) + } + end + + -- + -- Ensure the Build directory exists + -- + -- using format, becase we need to run the command in a subshell on windows. + -- + spawn({ + 'sh', + '-c', + string.format("' mkdir %s'", path_join({ plugin_path, 'build' })) + }) + + -- + -- Curl the download + -- + spawn(command) + +end + +return download diff --git a/lua/telescope-fzf-native/init.lua b/lua/telescope-fzf-native/init.lua new file mode 100644 index 0000000..e9d7f32 --- /dev/null +++ b/lua/telescope-fzf-native/init.lua @@ -0,0 +1,5 @@ +local download_library = require('telescope-fzf-native.download_library') + +return { + download_library = download_library +}