Debian Codegraph @

bin/term_display

raw symbols SCIP
1 #!/usr/bin/env ruby
2
3 require 'tempfile'
4 require 'term/ansicolor'
5 require 'open-uri'
6 require 'tins/xt'
7 include Tins::GO
8
9 def provide_ppm_file(filename, opts)
10 pnmtopnm = `which pnmtopnm`
11 if pnmtopnm.empty?
12 STDERR.puts "You might consider installing netpbm to enable image conversion/scaling!"
13 File.new(filename, 'rb')
14 else
15 cmd = [ pnmtopnm.chomp! ]
16 ext = File.extname(filename)
17 scale = `which pamscale`.chomp!
18 console_scale = "#{scale} -#{opts['s']} #{opts['C']} #{opts['R']}"
19 cmd.unshift console_scale
20 aspect_scaling = "#{scale} -xscale #{opts['a']}"
21 cmd.unshift aspect_scaling
22 convert_to_pnm =
23 case ext
24 when /\A(\.ppm\d*|\.pnm|\z)/
25 ''
26 when /\A.(jpe?g|exif|jfif)\z/i
27 'jpegtopnm'
28 when /\A.tiff?\z/i
29 'tifftopnm'
30 else
31 "#{ext[1..-1].downcase}topnm"
32 end
33 unless convert_to_pnm.empty?
34 convert_to_pnm = `which #{convert_to_pnm}`.chomp!
35 convert_to_pnm.empty? and fail "unknown pnm converter #{convert_to_pnm}"
36 cmd.unshift convert_to_pnm
37 end
38 temp = Tempfile.open(File.basename($0))
39 cmd *= '|'
40 STDERR.puts "Executing #{cmd.inspect}"
41 IO.popen(cmd, 'r+') do |converter|
42 open(filename, 'rb') do |input|
43 until input.eof?
44 converter.write input.read(8192)
45 end
46 converter.close_write
47 end
48 until converter.eof?
49 temp.write(converter.read)
50 end
51 end
52 temp.tap(&:rewind)
53 end
54 end
55
56 def usage(rc = 0)
57 puts <<EOT
58 Usage: #$0 [OPTIONS] FILENAME"
59
60 Options are
61
62 -m METRIC for distance (METRIC = #{Term::ANSIColor::RGBColorMetrics.metrics * '|'})
63 -g yes|no use/don't use gray values, defaults to yes
64 -s xyfit|xyfill scaling strategy, defaults to xyfit
65 -a ASPECT x:y aspect, defaults to 2.2
66 -C COLS number of columns for rendering with aspect, defaults to max
67 -R ROWS number of rows for rendering with aspect, defaults to max - 1
68 -t use true colors
69 -h this help
70
71 EOT
72 exit rc
73 end
74
75 opts = go 'hm:g:s:a:C:R:t'
76 opts['h'] and usage
77 filename = ARGV.shift or usage 1
78 metric = Term::ANSIColor::RGBColorMetrics.metric(opts['m'] || 'CIELab')
79 gray = (opts['g'] || 'yes') == 'yes'
80 opts['s'] ||= 'xyfit'
81 opts['a'] ||= '2.2'
82 opts['C'] ||= Tins::Terminal.cols
83 opts['R'] ||= [ Tins::Terminal.rows - 1, 0 ].max
84
85 file = provide_ppm_file(filename, opts)
86 ppm = Term::ANSIColor::PPMReader.new(
87 file,
88 metric: metric,
89 gray: gray,
90 true_coloring: opts[?t]
91 )
92
93 puts ppm