From eb13f2583f4d78edf1adf9f0579bb45ba38c26b8 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Mon, 23 Nov 2020 23:52:14 +0900 Subject: [PATCH] Support for encodings other than UTF-8 --- lib/youplot/command.rb | 8 +++++++- lib/youplot/command/parser.rb | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/youplot/command.rb b/lib/youplot/command.rb index fa0e7b6..a25aeba 100644 --- a/lib/youplot/command.rb +++ b/lib/youplot/command.rb @@ -30,6 +30,7 @@ module YouPlot pass = parser.pass output = parser.output fmt = parser.fmt + @encoding = parser.encoding @debug = parser.debug if command == :colors @@ -40,7 +41,12 @@ module YouPlot # Sometimes the input file does not end with a newline code. while (input = Kernel.gets(nil)) input.freeze - @data = Preprocessing.input(input, delimiter, headers, transpose) + @data = if @encoding + input2 = input.dup.force_encoding(@encoding).encode('utf-8') + Preprocessing.input(input2, delimiter, headers, transpose) + else + Preprocessing.input(input, delimiter, headers, transpose) + end pp @data if @debug plot = case command when :bar, :barplot diff --git a/lib/youplot/command/parser.rb b/lib/youplot/command/parser.rb index 0002e47..295ebaf 100644 --- a/lib/youplot/command/parser.rb +++ b/lib/youplot/command/parser.rb @@ -8,7 +8,7 @@ module YouPlot class Parser attr_reader :command, :params, :delimiter, :transpose, :headers, :pass, :output, :fmt, - :color_names, :debug + :color_names, :encoding, :debug def initialize @command = nil @@ -20,6 +20,7 @@ module YouPlot @pass = false @output = $stderr @fmt = 'xyy' + @encoding = nil @debug = false @color_names = false end @@ -81,6 +82,9 @@ module YouPlot opt.on('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v| @fmt = v end + opt.on('--encoding VAL', String, 'Specify the input encoding') do |v| + @encoding = v + end # Optparse adds the help option, but it doesn't show up in usage. # This is why you need the code below. opt.on('--help', 'print sub-command help menu') do