-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreview
executable file
·52 lines (42 loc) · 1.21 KB
/
preview
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env ruby
#
# Provides coloured syntax highlighting for source code.
# http://www.andre-simon.de/doku/highlight/en/highlight.php
#
# usage: preview [-v] FILENAME[:LINE][:IGNORED]
require 'shellwords'
COMMAND = %[(highlight -O ansi -l {} || coderay {} || rougify {} || cat {}) 2> /dev/null]
ANSI = /\x1b\[[0-9;]*m/
REVERSE = "\x1b[7m"
RESET = "\x1b[m"
split = ARGV.delete('-v')
def usage
puts "usage: #$0 [-v] FILENAME[:LINENO][:IGNORED]"
exit 1
end
usage if ARGV.empty?
file, center = ARGV.first.split(':')
usage unless file
path = File.expand_path(file)
unless File.readable? path
puts "File not found: #{file}"
exit 1
end
if `file --mime "#{file}"` =~ /binary/
puts "#{file} is a binary file"
exit 0
end
center = (center || 0).to_i
height = File.readable?('/dev/tty') ? `stty size < /dev/tty`.split.first.to_i : 40
height /= 2 if split
height -= 2 # preview border
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|
if lno + offset == center
puts REVERSE + line.chomp.gsub(ANSI) { |m| m + REVERSE } + RESET
else
puts line
end
end
end