[preview] Clean up preview scripts

- Use preview.sh instead of preview.rb by default
- Try bat syntax highlighter with the highest priority
- Remove "-v" option as the latest version of fzf sets up $LINES
- Allow users to customize the preview command via $FZF_PREVIEW_COMMAND
  - *EXPERIMENTAL / EVOLVING / UNDOCUMENTED*
  - Should be a command template with {} placeholder expression
    - e.g. "nl {}"
This commit is contained in:
Junegunn Choi
2018-11-09 15:57:17 +09:00
parent b73d141b74
commit 0dbcfb28c4
4 changed files with 28 additions and 40 deletions

View File

@@ -185,10 +185,10 @@ command! -bang Colors
" Augmenting Ag command using fzf#vim#with_preview function " Augmenting Ag command using fzf#vim#with_preview function
" * fzf#vim#with_preview([[options], preview window, [toggle keys...]]) " * fzf#vim#with_preview([[options], preview window, [toggle keys...]])
" * For syntax-highlighting, Ruby and any of the following tools are required: " * For syntax-highlighting, Ruby and any of the following tools are required:
" - Bat: https://github.com/sharkdp/bat
" - Highlight: http://www.andre-simon.de/doku/highlight/en/highlight.php " - Highlight: http://www.andre-simon.de/doku/highlight/en/highlight.php
" - CodeRay: http://coderay.rubychan.de/ " - CodeRay: http://coderay.rubychan.de/
" - Rouge: https://github.com/jneen/rouge " - Rouge: https://github.com/jneen/rouge
" - Bat: https://github.com/sharkdp/bat
" "
" :Ag - Start fzf with hidden preview window that can be enabled with "?" key " :Ag - Start fzf with hidden preview window that can be enabled with "?" key
" :Ag! - Start fzf in fullscreen and display the preview window above " :Ag! - Start fzf in fullscreen and display the preview window above

View File

@@ -32,7 +32,7 @@ let s:is_win = has('win32') || has('win64')
let s:layout_keys = ['window', 'up', 'down', 'left', 'right'] let s:layout_keys = ['window', 'up', 'down', 'left', 'right']
let s:bin_dir = expand('<sfile>:h:h:h').'/bin/' let s:bin_dir = expand('<sfile>:h:h:h').'/bin/'
let s:bin = { let s:bin = {
\ 'preview': s:bin_dir.(executable('ruby') ? 'preview.rb' : 'preview.sh'), \ 'preview': s:bin_dir.'preview.sh',
\ 'tags': s:bin_dir.'tags.pl' } \ 'tags': s:bin_dir.'tags.pl' }
let s:TYPE = {'dict': type({}), 'funcref': type(function('call')), 'string': type(''), 'list': type([])} let s:TYPE = {'dict': type({}), 'funcref': type(function('call')), 'string': type(''), 'list': type([])}
if s:is_win if s:is_win

View File

@@ -1,18 +1,20 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# #
# usage: ./preview.rb [-v] FILENAME[:LINE][:IGNORED] # usage: ./preview.rb FILENAME[:LINE][:IGNORED]
require 'open3'
require 'shellwords' require 'shellwords'
COMMAND = %[(highlight -O ansi -l {} || coderay {} || rougify {} || bat --style=numbers --color=always {} || cat {}) 2> /dev/null] COMMAND = ENV.fetch(
'FZF_PREVIEW_COMMAND',
%[bat --style=numbers --color=always {} || highlight -O ansi -l {} || coderay {} || rougify {} || cat {}]
)
ANSI = /\x1b\[[0-9;]*m/ ANSI = /\x1b\[[0-9;]*m/
REVERSE = "\x1b[7m" REVERSE = "\x1b[7m"
RESET = "\x1b[m" RESET = "\x1b[m"
split = ARGV.delete('-v')
def usage def usage
puts "usage: #$0 [-v] FILENAME[:LINENO][:IGNORED]" puts "usage: #$0 FILENAME[:LINENO][:IGNORED]"
exit 1 exit 1
end end
@@ -37,17 +39,16 @@ if `file --mime "#{file}"` =~ /binary/
end end
center = (center || 0).to_i center = (center || 0).to_i
height =
if ENV['LINES'] if ENV['LINES']
height = ENV['LINES'].to_i ENV['LINES'].to_i
else else
height = File.readable?('/dev/tty') ? `stty size < /dev/tty`.split.first.to_i : 40 File.readable?('/dev/tty') ? `stty size < /dev/tty`.split.first.to_i : 40
height /= 2 if split
height -= 2 # preview border
end end
offset = [1, center - height / 3].max offset = [1, center - height / 3].max
IO.popen(['sh', '-c', COMMAND.gsub('{}', Shellwords.shellescape(path))]) do |io| Open3.popen3(COMMAND.gsub('{}', Shellwords.shellescape(path))) do |_in, out, _err|
io.each_line.drop(offset - 1).take(height).each_with_index do |line, lno| out.each_line.drop(offset - 1).take(height).each_with_index do |line, lno|
if lno + offset == center if lno + offset == center
puts REVERSE + line.chomp.gsub(ANSI) { |m| m + REVERSE } + RESET puts REVERSE + line.chomp.gsub(ANSI) { |m| m + REVERSE } + RESET
else else

View File

@@ -3,13 +3,8 @@
REVERSE="\x1b[7m" REVERSE="\x1b[7m"
RESET="\x1b[m" RESET="\x1b[m"
if [ "$1" == "-v" ]; then
SPLIT=1
shift
fi
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "usage: $0 [-v] FILENAME[:LINENO][:IGNORED]" echo "usage: $0 FILENAME[:LINENO][:IGNORED]"
exit 1 exit 1
fi fi
@@ -33,7 +28,7 @@ if [[ "$(file --mime "$FILE")" =~ binary ]]; then
fi fi
if [ -z "$CENTER" ]; then if [ -z "$CENTER" ]; then
CENTER=1 CENTER=0
fi fi
if [ -z "$LINES" ]; then if [ -z "$LINES" ]; then
@@ -42,26 +37,18 @@ if [ -z "$LINES" ]; then
else else
LINES=40 LINES=40
fi fi
if [ -n "$SPLIT" ]; then
LINES=$(($LINES/2)) # using horizontal split
fi
LINES=$(($LINES-2)) # remove preview border
fi fi
FIRST=$(($CENTER-$LINES/3)) FIRST=$(($CENTER-$LINES/3))
FIRST=$(($FIRST < 1 ? 1 : $FIRST)) FIRST=$(($FIRST < 1 ? 1 : $FIRST))
LAST=$((${FIRST}+${LINES}-1)) LAST=$((${FIRST}+${LINES}-1))
if which bat >/dev/null; then DEFAULT_COMMAND="bat --style=numbers --color=always {} || highlight -O ansi -l {} || coderay {} || rougify {} || cat {}"
CAT="bat --style=numbers --color=always" CMD=${FZF_PREVIEW_COMMAND:-$DEFAULT_COMMAND}
$CAT $FILE | awk "NR >= $FIRST && NR <= $LAST { \ CMD=${CMD//{\}/$(printf %q "$FILE")}
eval "$CMD" 2> /dev/null | awk "NR >= $FIRST && NR <= $LAST { \
if (NR == $CENTER) \ if (NR == $CENTER) \
{ gsub(/\x1b[[0-9;]*m/, \"&$REVERSE\"); printf(\"$REVERSE%s\n$RESET\", \$0); } \ { gsub(/\x1b[[0-9;]*m/, \"&$REVERSE\"); printf(\"$REVERSE%s\n$RESET\", \$0); } \
else printf(\"$RESET%s\n\", \$0); \ else printf(\"$RESET%s\n\", \$0); \
}" }"
else
awk "NR >= $FIRST && NR <= $LAST { \
if (NR == $CENTER) printf(\"$REVERSE%5d %s\n$RESET\", NR, \$0); \
else printf(\"%5d %s\n\", NR, \$0); \
}" $FILE
fi