From ba396de83a6613794ac2f1af5b6c9cf2bb616ff7 Mon Sep 17 00:00:00 2001 From: dharmx Date: Mon, 26 Dec 2022 03:09:44 +0530 Subject: [PATCH] feat: use python instead of bash --- .editorconfig | 29 ---- .github/README.md | 79 ++++++++++ .prettierrc.toml | 4 - .stylua.toml | 5 +- LICENSE.txt => LICENSE | 0 README.md | 75 --------- lua/telescope/_extensions/media_files.lua | 177 +++++++++------------- scripts/view.py | 32 ++++ scripts/vimg.bash | 127 ---------------- 9 files changed, 188 insertions(+), 340 deletions(-) delete mode 100644 .editorconfig create mode 100644 .github/README.md delete mode 100644 .prettierrc.toml rename LICENSE.txt => LICENSE (100%) delete mode 100644 README.md create mode 100755 scripts/view.py delete mode 100755 scripts/vimg.bash diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 2d4d856..0000000 --- a/.editorconfig +++ /dev/null @@ -1,29 +0,0 @@ -[scripts/*.bash] -indent_style = space -indent_size = 2 - -shell_variant = posix # like -ln=posix -binary_next_line = true # like -bn -switch_case_indent = true # like -ci -space_redirects = true # like -sr -keep_padding = true # like -kp -function_next_line = true # like -fn -never_split = true # like -ns - -[lua/**] -ignore = true - -[*.md] -ignore = true - -[*.txt] -ignore = true - -[.git/**] -ignore = true - -[*.toml] -ignore = true - -[*.json] -ignore = true diff --git a/.github/README.md b/.github/README.md new file mode 100644 index 0000000..ae8a965 --- /dev/null +++ b/.github/README.md @@ -0,0 +1,79 @@ +
+ +# telescope-media-files.nvim + +Preview IMAGES, PDF, EPUB, VIDEO, and FONTS from Neovim using Telescope. + +
+ +> NOTE: This plugin is only supported in Linux. + +## PACKER + +```lua +use({ + "nvim-telescope/telescope-media-files.nvim", + config = function() + require("telescope").load_extension("media_files") + end, + requires = { + "nvim-lua/plenary.nvim", + "nvim-telescope/telescope.nvim", + } +}) +``` + +## SETUP + +``` lua +require("telescope").load_extension("media_files") +``` + +## CONFIG + +This extension should be configured using `extensions` field inside Telescope. + +```lua +require("telescope").setup({ + extensions = { + media_files = { + geometry = { + x = -2, + y = -2, + width = 1, + height = 1, + }, + find_command = { + "rg", + "--files", + "--glob", + [[*.{]] .. "png,jpg,gif,mp4,webm,pdf" .. [[}]], + ".", + }, + on_confirm = function(filepath) + vim.fn.setreg(vim.v.register, filepath) + vim.notify("The image path has been copied!") + end, + }, +}) +``` + +## COMMANDS + +```viml +:Telescope media_files + +"Using lua function +lua require('telescope').extensions.media_files.media_files() +``` + +## Prerequisites + +- [Überzug](https://github.com/seebye/ueberzug) (required for image support) +- [fd](https://github.com/sharkdp/fd) / [rg](https://github.com/BurntSushi/ripgrep) / [find](https://man7.org/linux/man-pages/man1/find.1.html) or fdfind in Ubuntu/Debian. +- [ffmpegthumbnailer](https://github.com/dirkvdb/ffmpegthumbnailer) (optional, for video preview support) +- [pdftoppm](https://linux.die.net/man/1/pdftoppm) (optional, for pdf preview support. Available in the AUR as **poppler** package.) +- [epub-thumbnailer](https://github.com/marianosimone/epub-thumbnailer) (optional, for epub preview support.) +- [fontpreview](https://github.com/sdushantha/fontpreview) (optional, for font preview support) + +Credit to [vifmimg](https://github.com/cirala/vifmimg). diff --git a/.prettierrc.toml b/.prettierrc.toml deleted file mode 100644 index 001e51a..0000000 --- a/.prettierrc.toml +++ /dev/null @@ -1,4 +0,0 @@ -trailingComma = "es5" -tabWidth = 2 -semi = false -singleQuote = false diff --git a/.stylua.toml b/.stylua.toml index df96b7b..3c8091d 100644 --- a/.stylua.toml +++ b/.stylua.toml @@ -3,4 +3,7 @@ line_endings = "Unix" indent_type = "Spaces" indent_width = 2 quote_style = "AutoPreferDouble" -no_call_parentheses = true +call_parentheses = "Always" +collapse_simple_statement = "Never" + +# vim:ft=toml diff --git a/LICENSE.txt b/LICENSE similarity index 100% rename from LICENSE.txt rename to LICENSE diff --git a/README.md b/README.md deleted file mode 100644 index b9edc6d..0000000 --- a/README.md +++ /dev/null @@ -1,75 +0,0 @@ -# Telescope-media-files.nvim - -Preview images, pdf, epub, video, and fonts from Neovim using Telescope. - -## Install - -```viml -Plug 'nvim-lua/popup.nvim' -Plug 'nvim-lua/plenary.nvim' -Plug 'nvim-telescope/telescope.nvim' -Plug 'nvim-telescope/telescope-media-files.nvim' -``` - -## Setup - -``` lua -require('telescope').load_extension('media_files') -``` - -## Configuration - -This extension should be configured using `extensions` field inside Telescope -setup function. - -```lua -require'telescope'.setup { - extensions = { - media_files = { - -- filetypes whitelist - -- defaults to {"png", "jpg", "gif", "mp4", "webm", "pdf"} - filetypes = {"png", "webp", "jpg", "jpeg"}, - find_cmd = "rg" -- find command (defaults to `fd`) - -- default: copy entry's relative path to vim clipboard - on_enter = function(filepath) - vim.fn.setreg('+', filepath) - vim.notify("The image path has been copied to system clipboard!") - end, - -- offset of the preview image - offsets = { x = -1, y = -1 }, - -- width and height of the preview image - sizes = { width = -1, height = 0 }, - } - }, -} -``` - -## Available commands - -```viml -:Telescope media_files - -"Using lua function -lua require('telescope').extensions.media_files.media_files() -``` - -```lua --- Useful for plugin developer that use telescope-media-files on their plugin -require('telescope').extensions.media_files.media_files({}, function(filepath) - -- Your custom action to do when file is selected -end) -``` - -When you press `/` on a selected file, it will copy its -relative path to vim clipboard except when you edit `on_enter`. - -## Prerequisites - - - [Überzug](https://github.com/seebye/ueberzug) (required for image support) - - [fd](https://github.com/sharkdp/fd) / [rg](https://github.com/BurntSushi/ripgrep) / [find](https://man7.org/linux/man-pages/man1/find.1.html) or fdfind in Ubuntu/Debian. - - [ffmpegthumbnailer](https://github.com/dirkvdb/ffmpegthumbnailer) (optional, for video preview support) - - [pdftoppm](https://linux.die.net/man/1/pdftoppm) (optional, for pdf preview support. Available in the AUR as **poppler** package.) - - [epub-thumbnailer](https://github.com/marianosimone/epub-thumbnailer) (optional, for epub preview support.) - - [fontpreview](https://github.com/sdushantha/fontpreview) (optional, for font preview support) - -Credit to [vifmimg](https://github.com/cirala/vifmimg). diff --git a/lua/telescope/_extensions/media_files.lua b/lua/telescope/_extensions/media_files.lua index 21e1c3f..5e0d3c8 100644 --- a/lua/telescope/_extensions/media_files.lua +++ b/lua/telescope/_extensions/media_files.lua @@ -1,139 +1,108 @@ local present, telescope = pcall(require, "telescope") if not present then - error "This plugin requires telescope.nvim (https://github.com/nvim-telescope/telescope.nvim)" + vim.api.nvim_notify("This plugin requires telescope.nvim!", vim.log.levels.ERROR, { + title = "telescope-media-files.nvim", + prompt_title = "telescope-media-files.nvim", + icon = " ", + }) + return end -local utils = require "telescope.utils" +local utils = require("telescope.utils") +local actions = require("telescope.actions") +local finders = require("telescope.finders") +local pickers = require("telescope.pickers") +local previewers = require("telescope.previewers") +local config = require("telescope.config") + local defaulter = utils.make_default_callable -local actions = require "telescope.actions" -local action_state = require "telescope.actions.state" -local finders = require "telescope.finders" -local pickers = require "telescope.pickers" -local previewers = require "telescope.previewers" -local conf = require("telescope.config").values +local action_state = require("telescope.actions.state") -local M = {} +local defaults = { + geometry = { + x = -2, + y = -2, + width = 1, + height = 1, + }, + find_command = { + "rg", + "--files", + "--glob", + [[*.{]] .. "png,jpg,gif,mp4,webm,pdf" .. [[}]], + ".", + }, + on_confirm = function(filepath) + vim.fn.setreg(vim.v.register, filepath) + vim.notify("The image path has been copied!") + end, +} -local filetypes = {} -local find_cmd = "" -local on_enter -local offsets = {} -local sizes = {} +local function setup(options) + defaults = vim.tbl_deep_extend("force", defaults, vim.F.if_nil(options, {})) +end + +local base_directory = "" -M.base_directory = "" -M.media_preview = defaulter(function(opts) - return previewers.new_termopen_previewer { - get_command = opts.get_command or function(entry) - local tmp_table = vim.split(entry.value, "\t") - local preview = opts.get_preview_window() - if vim.tbl_isempty(tmp_table) then +local media_preview = defaulter(function(options) + return previewers.new_termopen_previewer({ + get_command = function(entry) + local filepath = vim.trim(entry.value) + local preview = options.get_preview_window() + if filepath:len() == 0 then return { "echo", "" } end return { - M.base_directory .. "/scripts/vimg.bash", - string.format([[%s]], tmp_table[1]), - preview.col + offsets.x, - preview.line + offsets.y, - preview.width + sizes.width, - preview.height + sizes.height, + base_directory .. "/scripts/view.py", + filepath, + preview.col + options.geometry.x, + preview.line + options.geometry.y, + preview.width + options.geometry.width, + preview.height + options.geometry.height, } end, - } + }) end, {}) -function M.media_files(opts, custom_on_enter) - local find_commands = { - find = { - "find", - ".", - "-iregex", - [[.*\.\(]] .. table.concat(filetypes, "\\|") .. [[\)$]], - }, - fd = { - "fd", - "--type", - "f", - "--regex", - [[.*.(]] .. table.concat(filetypes, "|") .. [[)$]], - ".", - }, - fdfind = { - "fdfind", - "--type", - "f", - "--regex", - [[.*.(]] .. table.concat(filetypes, "|") .. [[)$]], - ".", - }, - rg = { - "rg", - "--files", - "--glob", - [[*.{]] .. table.concat(filetypes, ",") .. [[}]], - ".", - }, - } - - if not vim.fn.executable(find_cmd) then - error("You don't have " .. find_cmd .. "! Install it first or use other finder.") - return - end - - if not find_commands[find_cmd] then - error(find_cmd .. " is not supported!") - return - end - +local function media_files(options) + options = vim.tbl_deep_extend("keep", vim.F.if_nil(options, {}), defaults) local sourced_file = require("plenary.debug_utils").sourced_filepath() - M.base_directory = vim.fn.fnamemodify(sourced_file, ":h:h:h:h") - opts = opts or {} - opts.attach_mappings = function(prompt_bufnr, map) + base_directory = vim.fn.fnamemodify(sourced_file, ":h:h:h:h") + + options.attach_mappings = function(buffer) actions.select_default:replace(function() - local entry = action_state.get_selected_entry() - actions.close(prompt_bufnr) - if entry[1] then - local filepath = entry[1] - custom_on_enter = custom_on_enter or on_enter - custom_on_enter(filepath) - end + actions.close(buffer) + options.on_confirm(action_state.get_selected_entry()[1]) end) return true end - opts.path_display = { "shorten" } - local popup_opts = {} - opts.get_preview_window = function() - return popup_opts.preview + local popup_options = {} + options.get_preview_window = function() + return popup_options.preview end - local picker = pickers.new(opts, { + + local picker = pickers.new(options, { prompt_title = "Media Files", - finder = finders.new_oneshot_job(find_commands[find_cmd], opts), - previewer = M.media_preview.new(opts), - sorter = conf.file_sorter(opts), + finder = finders.new_oneshot_job(options.find_command, options), + previewer = media_preview.new(options), + sorter = config.values.file_sorter(options), }) local line_count = vim.o.lines - vim.o.cmdheight if vim.o.laststatus ~= 0 then line_count = line_count - 1 end - popup_opts = picker:get_window_options(vim.o.columns, line_count) + popup_options = picker:get_window_options(vim.o.columns, line_count) picker:find() end -return require("telescope").register_extension { - setup = function(ext_config) - filetypes = ext_config.filetypes or { "png", "jpg", "gif", "mp4", "webm", "pdf" } - find_cmd = ext_config.find_cmd or "fd" - offsets = ext_config.offsets or { x = 1, y = 1 } - sizes = ext_config.sizes or { width = 1, height = 1 } - on_enter = ext_config.on_enter - or function(filepath) - vim.fn.setreg(vim.v.register, filepath) - vim.notify "The image path has been copied!" - end - end, +return telescope.register_extension({ + setup = setup, exports = { - media_files = M.media_files, + media_files = media_files, }, -} +}) + +---vim:filetype=lua:fileencoding=utf-8 diff --git a/scripts/view.py b/scripts/view.py new file mode 100755 index 0000000..3d44581 --- /dev/null +++ b/scripts/view.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +import sys +import time + +import ueberzug.lib.v0 as ueberzug + +if __name__ == "__main__": + with ueberzug.Canvas() as canvas: + path = sys.argv[1] + x = int(sys.argv[2]) + y = int(sys.argv[3]) + width = int(sys.argv[4]) + height = int(sys.argv[5]) + + placement = canvas.create_placement( + identifier="telescope-media-files.nvim", + width=width, + height=height, + x=x, + y=y, + scaler=ueberzug.ScalerOption.CONTAIN.value, + visibility=ueberzug.Visibility.VISIBLE, + path=path + ) + + while True: + with canvas.lazy_drawing: + pass + time.sleep(1) + +# vim:filetype=python diff --git a/scripts/vimg.bash b/scripts/vimg.bash deleted file mode 100755 index bb3087d..0000000 --- a/scripts/vimg.bash +++ /dev/null @@ -1,127 +0,0 @@ -#!/usr/bin/env bash - -case $(uname) in - Darwin) - echo "Not supported" - exit - ;; -esac - -declare -x UEBERZUG_FIFO -UEBERZUG_FIFO="$(mktemp --dry-run --suffix "vimg-$$-ueberzug")" - -declare -x PREVIEW_ID="preview" - -declare -x TMP_FOLDER="/tmp/vimg" -mkdir -p "$TMP_FOLDER" - -start_ueberzug() { - mkfifo "$UEBERZUG_FIFO" - - tail --follow "$UEBERZUG_FIFO" | ueberzug layer --silent --parser bash & -} - -finalise() { - 3>&- \ - exec - &>/dev/null \ - rm "$UEBERZUG_FIFO" - &>/dev/null \ - kill "$(jobs -p)" -} - - -draw_preview() { - cmd="" - if [[ "$1" == "imagepreview" ]]; then - >"$UEBERZUG_FIFO" declare -A -p cmd=( \ - [action]=add [identifier]="$PREVIEW_ID" \ - [x]="${3}" [y]="${4}" \ - [width]="${5}" [height]="${6}" \ - [path]="${2}") - - elif [[ "$1" == "gifpreview" ]]; then - file="${2##*/}" - path="$PWD/$2"; path="${path// /_}"; path="${path//\//_}" #replace space and / into _ - path="$TMP_FOLDER/$path" - - echo -ne "Loading preview... 0%\r" - frame_total=$(identify -format "%n\n" "$2" | head -1) - IFS=$'\n' read -r -d '' -a ticks < <(identify -format "%T\n" "$2" && printf '\0'); unset IFS - [[ $(find "$path"/ 2>/dev/null | wc -l) -ne $frame_total ]] \ - && (mkdir -p "$path" && convert -coalesce -resize 720x480\> "$2" "$path/$file.png" & disown) - - frame_index=0 - while true; do - frame_extracted=$(find "$path"/ 2>/dev/null| wc -l) - if [[ $frame_extracted -lt $frame_total ]]; then - echo -ne "Loading preview... $((frame_extracted*100/frame_total))%\r" - else - echo -ne " \r" - >"$UEBERZUG_FIFO" declare -A -p cmd=( \ - [action]=add [identifier]="$PREVIEW_ID" \ - [x]="${3}" [y]="${4}" \ - [width]="${5}" [height]="${6}" \ - [path]="$path/$file-$frame_index.png") - delay=$(bc <<< "scale=2; ${ticks[$frame_index]}/100") # 1 tick == 1/100s - sleep "$delay" - fi - frame_index=$((frame_index + 1)) - [[ $frame_index -ge $frame_total ]] && frame_index=0 - done - - elif [[ "$1" == "videopreview" ]]; then - path="${2##*/}" - echo -e "Loading preview..\nFile: $path" - ffmpegthumbnailer -i "$PWD/$path" -o "$TMP_FOLDER/$path.png" -s 0 -q 10 - >"$UEBERZUG_FIFO" declare -A -p cmd=( \ - [action]=add [identifier]="$PREVIEW_ID" \ - [x]="${3}" [y]="${4}" \ - [width]="${5}" [height]="${6}" \ - [path]="$TMP_FOLDER/$path.png") - - elif [[ "$1" == "pdfpreview" ]]; then - path="${2##*/}" - echo -e "Loading preview..\nFile: $path" - [[ ! -f "$TMP_FOLDER/$path.png" ]] && pdftoppm -png -singlefile "$path" "$TMP_FOLDER/$path" - >"$UEBERZUG_FIFO" declare -A -p cmd=( \ - [action]=add [identifier]="$PREVIEW_ID" \ - [x]="${3}" [y]="${4}" \ - [width]="${5}" [height]="${6}" \ - [path]="$TMP_FOLDER/$path.png") - - fi -} - -parse_options() { - extension="${1##*.}" - case $extension in - jpg | png | jpeg | webp) - draw_preview imagepreview "$1" "$2" "$3" "$4" "$5" - ;; - - gif) - draw_preview gifpreview "$1" "$2" "$3" "$4" "$5" - ;; - - avi | mp4 | wmv | dat | 3gp | ogv | mkv | mpg | mpeg | vob | m2v | mov | webm | mts | m4v | rm | qt | divx) - draw_preview videopreview "$1" "$2" "$3" "$4" "$5" - ;; - - pdf | epub) - draw_preview pdfpreview "$1" "$2" "$3" "$4" "$5" - ;; - - *) - echo -n "unknown file $1" - ;; - esac -} - - - - -trap finalise EXIT -start_ueberzug -parse_options "${@}" -read -r