Skip to content

Commit

Permalink
Provide download tool and updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
airtonix committed Dec 27, 2022
1 parent 5f7e200 commit c3edf79
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 4 deletions.
60 changes: 56 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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' }
```
Expand Down
122 changes: 122 additions & 0 deletions lua/telescope-fzf-native/download_library.lua
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions lua/telescope-fzf-native/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local download_library = require('telescope-fzf-native.download_library')

return {
download_library = download_library
}

0 comments on commit c3edf79

Please sign in to comment.