#!/usr/bin/env ruby require 'tmpdir' require 'isna' require 'optparse' options = {} options[:title] = 'title' options[:x_label] = 'x' options[:y_label] = 'y' options[:graph_width] = 800 options[:graph_height] = 600 options[:output] = 'output' # option[title] = 'default value' # option[y_label] = 'default value' # option[x_label] = 'default value' # options[:x_max] = 0 # options[:x_min] = 0 # options[:y_max] = 0 # options[:y_min] = 0 # option[graph_width] = 'default value' # option[graph_height] = 'default value' # option[output] = 'default value' OptionParser.new do |opts| basename = File.basename($0).to_ansi.green.to_s options_hint = "[OPTIONS]".to_ansi.blink.red.to_s opts.banner = "Usage: #{basename} #{options_hint}" opts.on('-t', '--title [TITLE]', 'Title.') do |value| options[:title] = value end opts.on('-y', '--y-label [TEXT]', 'Y label.') do |value| options[:y_label] = value end opts.on('-x', '--x-label [TEXT]', 'X label.') do |value| options[:x_label] = value end opts.on('-m', '--x-max [NUMBER]', 'X max.') do |value| options[:x_max] = value end opts.on('-i', '--x-min [NUMBER]', 'X min.') do |value| options[:x_min] = value end opts.on('-a', '--y-max [NUMBER]', 'Y max.') do |value| options[:y_max] = value end opts.on('-n', '--y-min [NUMBER]', 'Y min.') do |value| options[:y_min] = value end opts.on('-g', '--graph-width [NUMBER]', 'Graph width.') do |value| options[:graph_width] = value end opts.on('-r', '--graph-height [NUMBER]', 'Graph height.') do |value| options[:graph_height] = value end opts.on('-o', '--output [SNAKE]', 'Output.') do |value| options[:output] = value end opts.on('-s', '--stats', 'Stats.') do |value| options[:stats] = value end end.parse! required_options = [ :title, :y_label, :x_label, :graph_width, :graph_height, :output, ] ranges = [ :x_max, :x_min, :y_max, :y_min, ] need_ranges = ranges.any? { |k| options[k] } if need_ranges ranges.each do |r| required_options.push(r) end end required_options.each do |option| unless options[option] $stderr.puts "Can not run #{option.to_s.to_ansi.red.to_s} was not given." exit 1 end end def pretty_options(hash, width = 20) r = '' hash.keys.sort.each do |key| k = key.to_s.ljust(width, '.') v = hash[key].to_s.rjust(width, '.') r << [k, v] * ' ' + "\n" end r end puts pretty_options(options) Dir.mktmpdir do |dir| data_path = "#{dir}/xy.data" script_path = "#{dir}/script.gnuplot" File.open(data_path, 'w+') do |file| STDIN.each_line do |line| file.puts line end end File.open(script_path, 'w+') do |script| script.puts "set term png size #{options[:graph_width]},#{options[:graph_height]}" script.puts "set output \"#{options[:output]}.png\"" script.puts "set title \"#{options[:title]}\"" script.puts "set xlabel \"#{options[:x_label]}\"" script.puts "set ylabel \"#{options[:y_label]}\"" script.puts 'set grid' script.puts 'set timestamp' if options[:stats] script.puts "stats '#{data_path}'" end if need_ranges script.puts "plot [#{options[:x_min]}:#{options[:x_max]}][#{options[:y_min]}:#{options[:y_max]}] '#{data_path}' notitle with points linecolor rgb '#FF0000'" else script.puts "plot '#{data_path}' notitle with points linecolor rgb '#FF0000'" end script.puts 'quit' end system "gnuplot #{script_path}" puts "open #{options[:output]}.png" end