Support for encodings other than UTF-8

This commit is contained in:
kojix2 2020-11-23 23:52:14 +09:00
parent 1697360b6b
commit eb13f2583f
2 changed files with 12 additions and 2 deletions

View File

@ -30,6 +30,7 @@ module YouPlot
pass = parser.pass pass = parser.pass
output = parser.output output = parser.output
fmt = parser.fmt fmt = parser.fmt
@encoding = parser.encoding
@debug = parser.debug @debug = parser.debug
if command == :colors if command == :colors
@ -40,7 +41,12 @@ module YouPlot
# Sometimes the input file does not end with a newline code. # Sometimes the input file does not end with a newline code.
while (input = Kernel.gets(nil)) while (input = Kernel.gets(nil))
input.freeze 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 pp @data if @debug
plot = case command plot = case command
when :bar, :barplot when :bar, :barplot

View File

@ -8,7 +8,7 @@ module YouPlot
class Parser class Parser
attr_reader :command, :params, attr_reader :command, :params,
:delimiter, :transpose, :headers, :pass, :output, :fmt, :delimiter, :transpose, :headers, :pass, :output, :fmt,
:color_names, :debug :color_names, :encoding, :debug
def initialize def initialize
@command = nil @command = nil
@ -20,6 +20,7 @@ module YouPlot
@pass = false @pass = false
@output = $stderr @output = $stderr
@fmt = 'xyy' @fmt = 'xyy'
@encoding = nil
@debug = false @debug = false
@color_names = false @color_names = false
end 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| 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 @fmt = v
end 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. # Optparse adds the help option, but it doesn't show up in usage.
# This is why you need the code below. # This is why you need the code below.
opt.on('--help', 'print sub-command help menu') do opt.on('--help', 'print sub-command help menu') do