diff options
| author | Ariel D'Alessandro <dalessandro.ariel@gmail.com> | 2023-07-04 23:36:03 +0200 |
|---|---|---|
| committer | Ariel D'Alessandro <dalessandro.ariel@gmail.com> | 2023-07-04 23:36:03 +0200 |
| commit | e6056a447b6df27f9ac72725a827a1ee85d45921 (patch) | |
| tree | d3920d1799c907b97590b204148072d37fc8968c /lua | |
| parent | 9071da77d8543db7ef3640b75fe32729dc7c6743 (diff) | |
| download | nvim-conf-e6056a447b6df27f9ac72725a827a1ee85d45921.tar.gz nvim-conf-e6056a447b6df27f9ac72725a827a1ee85d45921.zip | |
implement img viewer in the telescope
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/alpha/telescope/setup.lua | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lua/alpha/telescope/setup.lua b/lua/alpha/telescope/setup.lua index 5758746..0e368c7 100644 --- a/lua/alpha/telescope/setup.lua +++ b/lua/alpha/telescope/setup.lua @@ -11,8 +11,57 @@ local set_prompt_to_entry_value = function(prompt_bufnr) action_state.get_current_picker(prompt_bufnr):reset_prompt(entry.ordinal) end +local filetype_hook = function(filepath, bufnr, opts) + -- Here for example you can say: if ft == "xyz" then this_regex_highlighing else nothing end + -- + local is_image = function(filepath) + local image_extensions = { "png", "jpg" } -- Supported image formats + local split_path = vim.split(filepath:lower(), ".", { plain = true }) + local extension = split_path[#split_path] + return vim.tbl_contains(image_extensions, extension) + end + if not is_image(filepath) then + return true + end + local ui = require "image.ui" + local options = require "image.options" + local dimensions = require "image.dimensions" + local api = require "image.api" + local config = require "image.config" + local user_opts = { + render = { + foreground_color = true, + background_color = true, + }, + } + local global_opts = vim.tbl_deep_extend("force", config.DEFAULT_OPTS, user_opts) + + local ascii_width, ascii_height, horizontal_padding, vertical_padding, img_width, img_height = + dimensions.calculate_ascii_width_height(opts.winid, filepath, global_opts) + options.set_options_before_render(bufnr) + ui.buf_clear(bufnr) + local label = ui.create_label( + filepath, + ascii_width, + horizontal_padding, + global_opts.render.show_image_dimensions, + img_width, + img_height + ) + + api.get_ascii_data_sync(filepath, ascii_width, ascii_height, global_opts, function(ascii_data) + ui.buf_insert_data_with_padding(bufnr, ascii_data, horizontal_padding, vertical_padding, label, global_opts) + + options.set_options_after_render(bufnr) + end) + return false +end + require("telescope").setup { defaults = { + preview = { + filetype_hook = filetype_hook, + }, prompt_prefix = "❯ ", selection_caret = "❯ ", @@ -100,6 +149,7 @@ require("telescope").setup { path = "~/.local/share/nvim/databases/telescope_history.sqlite3", limit = 100, }, + -- buffer_previewer_maker = new_maker, }, pickers = { |