Skip to content

Commit

Permalink
feat: add support for rg
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Jeong authored and wjdwndud0114 committed Sep 15, 2023
1 parent 5c14eab commit b24cd1f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ in the excellent
[nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons).

Handles color codes as well, using the icon colors from the aforementioned
library. Intended to take in output from [fd](https://github.com/sharkdp/fd), to
library. Intended to take in output from [fd](https://github.com/sharkdp/fd) or
[rg](https://github.com/BurntSushi/ripgrep), to
then be used as input for [fzf-lua](https://github.com/ibhagwan/fzf-lua) in
Neovim.

Expand All @@ -15,7 +16,7 @@ or `fzf` directly would feel much snappier. I concluded (correctly) that this is
because of the extra processing `fzf-lua` is doing to, amongst other things,
prepend the file icon. By using a custom `fzf-lua` action that invokes `fd` and
pipes the results through this binary, you still get the file icons, but with
the snapiness of running `fd`/`fzf` directly.
the snapiness of running `fd`/`fzf` directly. Same benefits for `rg` as well.

You could use this project in Neovim using `fzf-lua` as follows:

Expand All @@ -30,6 +31,18 @@ local function fzf_files()
end

vim.keymap.set('n', '<C-p>', fzf_files)

local function fzf_live_grep()
fzf.fzf_live(
'rg --column --line-number --no-heading --color=always --smart-case -- <query> | /path/to/file-web-devicon', {
actions = fzf.defaults.actions.files,
prompt = 'Rg> ',
fzf_opts = { ['--nth'] = 2, ['--delimiter'] = fzf.utils.nbsp },
previewer = 'builtin',
})
end

vim.keymap.set('n', '<C-s>', fzf_live_grep)
```

_Note_: This project has been tested only on MacOS. I don't know whether it will
Expand Down
24 changes: 22 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ fn get_icon<'a>(
)
}

fn extract_filepath(input: &[u8]) -> String {
let mut result = Vec::new();
let mut in_escape = false;

for &byte in input.iter() {
match byte {
b'\x1B' if !in_escape => in_escape = true,
b'm' | b'G' | b'K' if in_escape => in_escape = false,
b':' if !in_escape => break,
byte if !in_escape && !byte.is_ascii_whitespace() => {
result.push(byte.to_ascii_lowercase());
}
_ => {}
}
}

String::from_utf8_lossy(&result).to_string()
}

fn main() {
let icons_by_filename = &icons::ICONS_BY_FILENAME;
let icons_by_extension = &icons::ICONS_BY_FILE_EXTENSION;
Expand All @@ -51,8 +70,9 @@ fn main() {
let input = stdin();
let mut buffer = String::new();
while input.read_line(&mut buffer).unwrap() > 0 {
let trimmed_lowercase = buffer.trim().to_lowercase();
let path = Path::new(&trimmed_lowercase);
let filepath = extract_filepath(&buffer.as_bytes());
let path = Path::new(&filepath);

let (icon, (r, g, b)) = get_icon(path, icons_by_filename, icons_by_extension, default_icon);

print!("\x1b[38;2;{r};{g};{b}m{icon}\x1b[0m{non_breaking_space}{buffer}");
Expand Down

0 comments on commit b24cd1f

Please sign in to comment.