diff options
Diffstat (limited to 'start/fzf/bin')
-rw-r--r-- | start/fzf/bin/preview.rb | 59 | ||||
-rw-r--r-- | start/fzf/bin/preview.sh | 54 | ||||
-rw-r--r-- | start/fzf/bin/tags.pl | 15 |
3 files changed, 128 insertions, 0 deletions
diff --git a/start/fzf/bin/preview.rb b/start/fzf/bin/preview.rb new file mode 100644 index 0000000..cc8260b --- /dev/null +++ b/start/fzf/bin/preview.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby +# +# usage: ./preview.rb FILENAME[:LINE][:IGNORED] + +require 'open3' +require 'shellwords' + +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" + +def usage + puts "usage: #$0 FILENAME[:LINENO][:IGNORED]" + exit 1 +end + +usage if ARGV.empty? + +file, center, extra = ARGV.first.split(':') +if ARGV.first =~ /^[A-Z]:\\/ + file << ':' + center + center = extra +end +usage unless file + +path = File.expand_path(file) +unless File.readable? path + puts "File not found: #{file}" + exit 1 +end + +if `file --dereference --mime "#{file}"` =~ /binary/ + puts "#{file} is a binary file" + exit 0 +end + +center = (center || 0).to_i +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 + +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 + puts line + end + end +end +print RESET diff --git a/start/fzf/bin/preview.sh b/start/fzf/bin/preview.sh new file mode 100644 index 0000000..41ebfb9 --- /dev/null +++ b/start/fzf/bin/preview.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +REVERSE="\x1b[7m" +RESET="\x1b[m" + +if [ -z "$1" ]; then + echo "usage: $0 FILENAME[:LINENO][:IGNORED]" + exit 1 +fi + +IFS=':' read -r -a INPUT <<< "$1" +FILE=${INPUT[0]} +CENTER=${INPUT[1]} + +if [[ $1 =~ ^[A-Z]:\\ ]]; then + FILE=$FILE:${INPUT[1]} + CENTER=${INPUT[2]} +fi + +if [ ! -r "$FILE" ]; then + echo "File not found ${FILE}" + exit 1 +fi + +if [[ "$(file --dereference --mime "$FILE")" =~ binary ]]; then + echo "$1 is a binary file" + exit 0 +fi + +if [ -z "$CENTER" ]; then + CENTER=0 +fi + +if [ -z "$LINES" ]; then + if [ -r /dev/tty ]; then + LINES=$(stty size < /dev/tty | awk '{print $1}') + else + LINES=40 + fi +fi + +FIRST=$(($CENTER-$LINES/3)) +FIRST=$(($FIRST < 1 ? 1 : $FIRST)) +LAST=$((${FIRST}+${LINES}-1)) + +DEFAULT_COMMAND="bat --style=numbers --color=always {} || highlight -O ansi -l {} || coderay {} || rougify {} || cat {}" +CMD=${FZF_PREVIEW_COMMAND:-$DEFAULT_COMMAND} +CMD=${CMD//{\}/$(printf %q "$FILE")} + +eval "$CMD" 2> /dev/null | awk "NR >= $FIRST && NR <= $LAST { \ + if (NR == $CENTER) \ + { gsub(/\x1b[[0-9;]*m/, \"&$REVERSE\"); printf(\"$REVERSE%s\n$RESET\", \$0); } \ + else printf(\"$RESET%s\n\", \$0); \ + }" diff --git a/start/fzf/bin/tags.pl b/start/fzf/bin/tags.pl new file mode 100644 index 0000000..4208c16 --- /dev/null +++ b/start/fzf/bin/tags.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl + +use strict; + +foreach my $file (@ARGV) { + open my $lines, $file; + while (<$lines>) { + unless (/^\!/) { + s/^[^\t]*/sprintf("%-24s", $&)/e; + s/$/\t$file/; + print; + } + } + close $lines; +} |