[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

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