YouPlot/lib/youplot/command.rb

218 lines
5.2 KiB
Ruby
Raw Normal View History

2020-09-18 23:08:09 +08:00
# frozen_string_literal: true
2020-12-21 13:25:52 +08:00
require_relative 'dsv'
require_relative 'parser'
# FIXME
require_relative 'backends/unicode_plot'
2020-11-23 12:09:16 +08:00
module YouPlot
2020-08-15 21:12:42 +08:00
Data = Struct.new(:headers, :series)
2020-07-29 16:01:39 +08:00
class Command
2020-12-14 14:54:54 +08:00
attr_accessor :command, :params, :options
attr_reader :data, :parser
2020-08-15 16:10:41 +08:00
def initialize(argv = ARGV)
@argv = argv
@parser = Parser.new
2020-12-14 14:54:54 +08:00
@command = nil
@params = nil
@options = nil
@backend = YouPlot::Backends::UnicodePlot
2020-09-15 17:51:32 +08:00
end
2021-01-19 23:07:23 +08:00
def run_as_executable
YouPlot.run_as_executable = true
run
end
2020-07-29 16:01:39 +08:00
def run
parser.parse_options(@argv)
2020-12-14 14:54:54 +08:00
@command ||= parser.command
@options ||= parser.options
@params ||= parser.params
2020-09-15 17:58:34 +08:00
2021-05-29 13:59:42 +08:00
# color command
2020-12-17 11:37:27 +08:00
if %i[colors color colours colour].include? @command
plot = create_plot
output_plot(plot)
2021-05-29 13:59:42 +08:00
return
end
# progressive mode
if options[:progressive]
2020-12-21 19:51:52 +08:00
stop = false
Signal.trap(:INT) { stop = true }
2021-05-29 13:59:42 +08:00
# make cursor invisible
options[:output].print "\e[?25l"
# mainloop
2020-12-21 19:51:52 +08:00
while (input = Kernel.gets)
2021-01-19 19:05:36 +08:00
n = main_progressive(input)
2020-12-21 19:51:52 +08:00
break if stop
2021-01-19 19:05:36 +08:00
options[:output].print "\e[#{n}F"
2020-12-21 19:51:52 +08:00
end
2021-05-29 13:59:42 +08:00
2020-12-21 19:51:52 +08:00
options[:output].print "\e[0J"
2021-05-29 13:59:42 +08:00
# make cursor visible
options[:output].print "\e[?25h"
# normal mode
2020-12-17 11:37:27 +08:00
else
2020-12-21 19:51:52 +08:00
# Sometimes the input file does not end with a newline code.
while (input = Kernel.gets(nil))
main(input)
2020-12-17 11:37:27 +08:00
end
2020-12-14 21:02:18 +08:00
end
end
2020-12-14 21:02:18 +08:00
private
def main(input)
2021-05-29 13:59:42 +08:00
# Outputs input data to a file or stdout.
2020-12-14 21:02:18 +08:00
output_data(input)
2021-05-29 13:59:42 +08:00
@data = parse_dsv(input)
2020-12-14 21:02:18 +08:00
2021-05-29 13:59:42 +08:00
# Debug mode, show parsed results
2020-12-14 21:02:18 +08:00
pp @data if options[:debug]
2021-05-29 13:59:42 +08:00
# When run as a program instead of a library
2021-01-22 10:07:33 +08:00
if YouPlot.run_as_executable?
begin
plot = create_plot
rescue ArgumentError => e
2021-05-29 13:59:42 +08:00
# Show only one line of error.
2021-01-22 10:07:33 +08:00
warn e.backtrace[0]
2021-05-29 13:59:42 +08:00
# Show error message in purple
2021-01-22 10:07:33 +08:00
warn "\e[35m#{e}\e[0m"
2021-05-29 13:59:42 +08:00
# Explicitly terminated with exit code: 1
2021-01-22 10:07:33 +08:00
exit 1
end
2021-05-29 13:59:42 +08:00
# When running YouPlot as a library (e.g. for testing)
2021-01-22 10:07:33 +08:00
else
plot = create_plot
end
2021-05-29 13:59:42 +08:00
2020-12-14 21:02:18 +08:00
output_plot(plot)
end
2020-12-21 19:51:52 +08:00
def main_progressive(input)
2020-12-21 16:09:47 +08:00
output_data(input)
2020-12-21 20:42:32 +08: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 16:09:47 +08:00
@raw_data << input
2020-12-21 19:51:52 +08:00
# FIXME
2021-05-29 13:59:42 +08:00
@data = parse_dsv(@raw_data)
2020-12-21 16:09:47 +08:00
plot = create_plot
output_plot_progressive(plot)
end
2021-05-29 13:59:42 +08: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 13:59:42 +08:00
begin
2021-05-29 13:59:42 +08: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 13:59:42 +08:00
data
2020-12-14 21:02:18 +08:00
end
def create_plot
case command
when :bar, :barplot
@backend.barplot(data, params, options[:fmt])
when :count, :c
2021-05-31 21:50:11 +08:00
@backend.barplot(data, params, count: true, reverse: options[:reverse])
2020-12-14 21:02:18 +08: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 11:37:27 +08:00
when :colors, :color, :colours, :colour
@backend.colors(options[:color_names])
2020-12-14 21:02:18 +08: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 17:13:03 +08:00
end
end
2020-12-14 21:02:18 +08:00
end
end
2020-07-29 16:01:39 +08:00
2020-12-14 21:02:18 +08:00
def output_plot(plot)
case options[:output]
2021-05-29 15:17:59 +08:00
when IO, StringIO
2020-12-14 21:02:18 +08:00
plot.render(options[:output])
2021-05-29 15:17:59 +08:00
when String, Tempfile
2020-12-14 21:02:18 +08:00
File.open(options[:output], 'w') do |f|
plot.render(f)
end
2020-07-30 09:37:20 +08:00
end
2020-07-29 16:01:39 +08:00
end
2020-12-21 16:09:47 +08:00
def output_plot_progressive(plot)
case options[:output]
2021-05-29 15:17:59 +08:00
when IO, StringIO
2020-12-21 16:09:47 +08:00
# RefactorMe
2020-12-21 19:51:52 +08:00
out = StringIO.new(String.new)
def out.tty?
true
end
2020-12-21 16:09:47 +08: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 19:51:52 +08:00
options[:output].print "\e[0J"
2020-12-21 16:09:47 +08:00
options[:output].flush
2021-01-19 19:05:36 +08:00
out.string.lines.size
2020-12-21 16:09:47 +08:00
else
2020-12-21 19:51:52 +08:00
raise 'In progressive mode, output to a file is not possible.'
2020-12-21 16:09:47 +08:00
end
end
2020-07-29 16:01:39 +08:00
end
end