Files
YouPlot/lib/youplot/command.rb

218 lines
5.2 KiB
Ruby
Raw Normal View History

2020-09-19 00:08:09 +09:00
# frozen_string_literal: true
2020-12-21 14:25:52 +09:00
require_relative 'dsv'
require_relative 'parser'
# FIXME
require_relative 'backends/unicode_plot'
2020-11-23 13:09:16 +09:00
module YouPlot
2020-08-15 22:12:42 +09:00
Data = Struct.new(:headers, :series)
2020-07-29 17:01:39 +09:00
class Command
2020-12-14 15:54:54 +09:00
attr_accessor :command, :params, :options
attr_reader :data, :parser
2020-08-15 17:10:41 +09:00
def initialize(argv = ARGV)
@argv = argv
@parser = Parser.new
2020-12-14 15:54:54 +09:00
@command = nil
@params = nil
@options = nil
@backend = YouPlot::Backends::UnicodePlot
2020-09-15 18:51:32 +09:00
end
2021-01-20 00:07:23 +09:00
def run_as_executable
YouPlot.run_as_executable = true
run
end
2020-07-29 17:01:39 +09:00
def run
parser.parse_options(@argv)
2020-12-14 15:54:54 +09:00
@command ||= parser.command
@options ||= parser.options
@params ||= parser.params
2020-09-15 18:58:34 +09:00
2021-05-29 14:59:42 +09:00
# color command
2020-12-17 12:37:27 +09:00
if %i[colors color colours colour].include? @command
plot = create_plot
output_plot(plot)
2021-05-29 14:59:42 +09:00
return
end
# progressive mode
if options[:progressive]
2020-12-21 20:51:52 +09:00
stop = false
Signal.trap(:INT) { stop = true }
2021-05-29 14:59:42 +09:00
# make cursor invisible
options[:output].print "\e[?25l"
# mainloop
2020-12-21 20:51:52 +09:00
while (input = Kernel.gets)
2021-01-19 20:05:36 +09:00
n = main_progressive(input)
2020-12-21 20:51:52 +09:00
break if stop
2021-01-19 20:05:36 +09:00
options[:output].print "\e[#{n}F"
2020-12-21 20:51:52 +09:00
end
2021-05-29 14:59:42 +09:00
2020-12-21 20:51:52 +09:00
options[:output].print "\e[0J"
2021-05-29 14:59:42 +09:00
# make cursor visible
options[:output].print "\e[?25h"
# normal mode
2020-12-17 12:37:27 +09:00
else
2020-12-21 20:51:52 +09:00
# Sometimes the input file does not end with a newline code.
while (input = Kernel.gets(nil))
main(input)
2020-12-17 12:37:27 +09:00
end
2020-12-14 22:02:18 +09:00
end
end
2020-12-14 22:02:18 +09:00
private
def main(input)
2021-05-29 14:59:42 +09:00
# Outputs input data to a file or stdout.
2020-12-14 22:02:18 +09:00
output_data(input)
2021-05-29 14:59:42 +09:00
@data = parse_dsv(input)
2020-12-14 22:02:18 +09:00
2021-05-29 14:59:42 +09:00
# Debug mode, show parsed results
2020-12-14 22:02:18 +09:00
pp @data if options[:debug]
2021-05-29 14:59:42 +09:00
# When run as a program instead of a library
2021-01-22 11:07:33 +09:00
if YouPlot.run_as_executable?
begin
plot = create_plot
rescue ArgumentError => e
2021-05-29 14:59:42 +09:00
# Show only one line of error.
2021-01-22 11:07:33 +09:00
warn e.backtrace[0]
2021-05-29 14:59:42 +09:00
# Show error message in purple
2021-01-22 11:07:33 +09:00
warn "\e[35m#{e}\e[0m"
2021-05-29 14:59:42 +09:00
# Explicitly terminated with exit code: 1
2021-01-22 11:07:33 +09:00
exit 1
end
2021-05-29 14:59:42 +09:00
# When running YouPlot as a library (e.g. for testing)
2021-01-22 11:07:33 +09:00
else
plot = create_plot
end
2021-05-29 14:59:42 +09:00
2020-12-14 22:02:18 +09:00
output_plot(plot)
end
2020-12-21 20:51:52 +09:00
def main_progressive(input)
2020-12-21 17:09:47 +09:00
output_data(input)
2020-12-21 21:42:32 +09:00
# FIXME
# Worked around the problem of not being able to draw
# plots when there is only one header line.
if @raw_data.nil?
@raw_data = String.new
if options[:headers]
@raw_data << input
return
end
end
2020-12-21 17:09:47 +09:00
@raw_data << input
2020-12-21 20:51:52 +09:00
# FIXME
2021-05-29 14:59:42 +09:00
@data = parse_dsv(@raw_data)
2020-12-21 17:09:47 +09:00
plot = create_plot
output_plot_progressive(plot)
end
2021-05-29 14:59:42 +09:00
def parse_dsv(input)
# If encoding is specified, convert to UTF-8
if options[:encoding]
input.force_encoding(options[:encoding])
.encode!('utf-8')
end
2021-05-29 14:59:42 +09:00
begin
2021-05-29 14:59:42 +09:00
data = DSV.parse(input, options[:delimiter], options[:headers], options[:transpose])
rescue CSV::MalformedCSVError => e
warn 'Failed to parse the text. '
warn 'Please try to set the correct character encoding with --encoding option.'
raise e
end
2021-05-29 14:59:42 +09:00
data
2020-12-14 22:02:18 +09:00
end
def create_plot
case command
when :bar, :barplot
@backend.barplot(data, params, options[:fmt])
when :count, :c
2021-05-31 22:50:11 +09:00
@backend.barplot(data, params, count: true, reverse: options[:reverse])
2020-12-14 22:02:18 +09:00
when :hist, :histogram
@backend.histogram(data, params)
when :line, :lineplot
@backend.line(data, params, options[:fmt])
when :lines, :lineplots
@backend.lines(data, params, options[:fmt])
when :scatter, :s
@backend.scatter(data, params, options[:fmt])
when :density, :d
@backend.density(data, params, options[:fmt])
when :box, :boxplot
@backend.boxplot(data, params)
2020-12-17 12:37:27 +09:00
when :colors, :color, :colours, :colour
@backend.colors(options[:color_names])
2020-12-14 22:02:18 +09:00
else
raise "unrecognized plot_type: #{command}"
end
end
def output_data(input)
# Pass the input to subsequent pipelines
case options[:pass]
when IO
options[:pass].print(input)
else
if options[:pass]
File.open(options[:pass], 'w') do |f|
f.print(input)
2020-09-29 18:13:03 +09:00
end
end
2020-12-14 22:02:18 +09:00
end
end
2020-07-29 17:01:39 +09:00
2020-12-14 22:02:18 +09:00
def output_plot(plot)
case options[:output]
2021-05-29 16:17:59 +09:00
when IO, StringIO
2020-12-14 22:02:18 +09:00
plot.render(options[:output])
2021-05-29 16:17:59 +09:00
when String, Tempfile
2020-12-14 22:02:18 +09:00
File.open(options[:output], 'w') do |f|
plot.render(f)
end
2020-07-30 10:37:20 +09:00
end
2020-07-29 17:01:39 +09:00
end
2020-12-21 17:09:47 +09:00
def output_plot_progressive(plot)
case options[:output]
2021-05-29 16:17:59 +09:00
when IO, StringIO
2020-12-21 17:09:47 +09:00
# RefactorMe
2020-12-21 20:51:52 +09:00
out = StringIO.new(String.new)
def out.tty?
true
end
2020-12-21 17:09:47 +09:00
plot.render(out)
lines = out.string.lines
lines.each do |line|
options[:output].print line.chomp
options[:output].print "\e[0K"
options[:output].puts
end
2020-12-21 20:51:52 +09:00
options[:output].print "\e[0J"
2020-12-21 17:09:47 +09:00
options[:output].flush
2021-01-19 20:05:36 +09:00
out.string.lines.size
2020-12-21 17:09:47 +09:00
else
2020-12-21 20:51:52 +09:00
raise 'In progressive mode, output to a file is not possible.'
2020-12-21 17:09:47 +09:00
end
end
2020-07-29 17:01:39 +09:00
end
end