From 8db1306e07ae7ab4ed9b02277e8a351b9f995f79 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Wed, 20 Jan 2021 00:07:23 +0900 Subject: [PATCH] Do not exit when using as a library --- exe/uplot | 2 +- exe/youplot | 2 +- lib/youplot.rb | 6 ++++++ lib/youplot/backends/unicode_plot_backend.rb | 4 ++-- lib/youplot/command.rb | 5 +++++ lib/youplot/command/parser.rb | 4 ++-- lib/youplot/dsv.rb | 4 ++-- 7 files changed, 19 insertions(+), 8 deletions(-) diff --git a/exe/uplot b/exe/uplot index e35433e..2fa7e0d 100755 --- a/exe/uplot +++ b/exe/uplot @@ -3,4 +3,4 @@ require 'youplot' -YouPlot::Command.new.run +YouPlot::Command.new.run_as_executable diff --git a/exe/youplot b/exe/youplot index e35433e..2fa7e0d 100755 --- a/exe/youplot +++ b/exe/youplot @@ -3,4 +3,4 @@ require 'youplot' -YouPlot::Command.new.run +YouPlot::Command.new.run_as_executable diff --git a/lib/youplot.rb b/lib/youplot.rb index b2b926f..aa26681 100644 --- a/lib/youplot.rb +++ b/lib/youplot.rb @@ -6,4 +6,10 @@ require 'youplot/dsv' require 'youplot/command' module YouPlot + class << self + attr_accessor :run_as_executable + def run_as_executable? + @run_as_executable + end + end end diff --git a/lib/youplot/backends/unicode_plot_backend.rb b/lib/youplot/backends/unicode_plot_backend.rb index 0740c99..6caa89c 100644 --- a/lib/youplot/backends/unicode_plot_backend.rb +++ b/lib/youplot/backends/unicode_plot_backend.rb @@ -181,14 +181,14 @@ module YouPlot warn " Headers: \e[35m#{data.headers.inspect}\e[0m" warn " The first item is: \e[35m\"#{series[0][0]}\"\e[0m" warn " The last item is : \e[35m\"#{series[0][-1]}\"\e[0m" - exit 1 + exit 1 if YouPlot.run_as_executable? end if fmt == 'xyxy' && series.size.odd? warn 'YouPlot: In the xyxy format, the number of series must be even.' warn '' warn " Number of series: \e[35m#{series.size}\e[0m" warn " Headers: \e[35m#{data.headers.inspect}\e[0m" - exit 1 + exit 1 if YouPlot.run_as_executable? end end end diff --git a/lib/youplot/command.rb b/lib/youplot/command.rb index 7114e3a..aff1fe0 100644 --- a/lib/youplot/command.rb +++ b/lib/youplot/command.rb @@ -22,6 +22,11 @@ module YouPlot @backend = YouPlot::Backends::UnicodePlotBackend end + def run_as_executable + YouPlot.run_as_executable = true + run + end + def run parser.parse_options(@argv) @command ||= parser.command diff --git a/lib/youplot/command/parser.rb b/lib/youplot/command/parser.rb index 0adf84b..3c9706b 100644 --- a/lib/youplot/command/parser.rb +++ b/lib/youplot/command/parser.rb @@ -93,7 +93,7 @@ module YouPlot # This is why you need the code below. parser.on('--help', 'print sub-command help menu') do puts parser.help - exit + exit if YouPlot.run_as_executable? end parser.on('--debug', TrueClass, 'print preprocessed data') do |v| options[:debug] = v @@ -136,7 +136,7 @@ module YouPlot main_parser.on('--help', 'print sub-command help menu') do puts main_parser.banner puts - exit + exit if YouPlot.run_as_executable? end end diff --git a/lib/youplot/dsv.rb b/lib/youplot/dsv.rb index 55841ca..2d7e751 100644 --- a/lib/youplot/dsv.rb +++ b/lib/youplot/dsv.rb @@ -25,10 +25,10 @@ module YouPlot Data.new(headers, series) elsif h_size > s_size warn "\e[35mThe number of headers is greater than the number of series.\e[0m" - exit 1 + exit 1 if YouPlot.run_as_executable? elsif h_size < s_size warn "\e[35mThe number of headers is less than the number of series.\e[0m" - exit 1 + exit 1 if YouPlot.run_as_executable? end end end