Skip to content

Instantly share code, notes, and snippets.

@windwp
Last active April 25, 2023 21:00
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save windwp/ccbd2ca4696059ccdd91f8811453ae8f to your computer and use it in GitHub Desktop.
Save windwp/ccbd2ca4696059ccdd91f8811453ae8f to your computer and use it in GitHub Desktop.
telescope media preview

Telescope preview image and pdf and video Only work only Linux because Uberzug only support it

Prerequisites

Überzug fdfind ffmpegthumbnailer ImageMagick pdftoppm (Available in the AUR as poppler package.) epub-thumbnailer fontpreview

credit to https://github.com/cirala/vifmimg

local utils = require('telescope.utils')
local defaulter = utils.make_default_callable
local actions = require('telescope.actions')
local finders = require('telescope.finders')
local make_entry = require('telescope.make_entry')
local pickers = require('telescope.pickers')
local previewers = require('telescope.previewers')
local builtin = require('telescope.builtin')
local conf = require('telescope.config').values
local flatten = vim.tbl_flatten
local M={}
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
return {"echo", ""}
end
return {
'/home/windwp/.config/scripts/vimg.sh' ,
tmp_table[1],
preview.col ,
preview.line ,
preview.width ,
preview.height
}
end
}
end, {})
function M.media_files(opts)
opts = opts or {}
opts.attach_mappings= function(prompt_bufnr)
actions.goto_file_selection_edit:replace(function()
local entry = actions.get_selected_entry()
actions.close(prompt_bufnr)
if entry[1] then
local filename = entry[1]
local cmd="call setreg(v:register,'"..filename.."')";
print(vim.inspect(cmd))
vim.cmd(cmd)
end
end)
return true
end
opts.shorten_path = true
local filetype ={"png", "jpg", "mp4", "webm", "pdf" }
local find_command={
'find',
'.',
'-iregex',
[[.*\.\(]]..table.concat(filetype,"\\|") .. [[\)$]]
}
if 1 == vim.fn.executable("fd") then
find_command = {
'fdfind' ,
'--type', 'f',
'--regex',
[[.*.(]]..table.concat(filetype,"|") .. [[)$]],
'.'
}
elseif 1 == vim.fn.executable("fdfind") then
find_command = {
'fdfind' ,
'--type', 'f',
'--regex',
[[.*.(]]..table.concat(filetype,"|") .. [[)$]],
'.'
}
end
local popup_opts={}
opts.get_preview_window=function ()
return popup_opts.preview
end
local picker=pickers.new(opts, {
prompt_title = 'Media Files',
finder = finders.new_oneshot_job(
find_command,
opts
),
previewer = M.media_preview.new(opts),
sorter = conf.file_sorter(opts),
})
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)
picker:find()
end
return M
#!/bin/bash
case $(uname) in
Darwin)
echo "Not supported"
exit
;;
esac
SCRIPT=`realpath $0`
readonly BASH_BINARY="$(which bash)"
declare -x UEBERZUG_FIFO="$(mktemp --dry-run --suffix "vimg-$$-ueberzug")"
declare -x PREVIEW_ID="preview"
declare -x TMP_FOLDER="/tmp/vimg"
mkdir -p $TMP_FOLDER
function start_ueberzug {
mkfifo "${UEBERZUG_FIFO}"
tail --follow "$UEBERZUG_FIFO" | ueberzug layer --silent --parser bash &
}
function finalise {
3>&- \
exec
&>/dev/null \
rm "${UEBERZUG_FIFO}"
&>/dev/null \
kill $(jobs -p)
}
function draw_preview {
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" == "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
}
function parse_options {
extension="${1##*.}"
case $extension in
jpg | png)
draw_preview imagepreview $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)
draw_preview pdfpreview $1 $2 $3 $4 $5
;;
*)
echo -n "unknown file $1"
;;
esac
}
trap finalise EXIT
start_ueberzug
parse_options "${@}"
read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment