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
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

View File

@ -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