mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-05-05 22:31:11 +08:00
Make configuration files available (#30)
This commit is contained in:
parent
41c0d37a13
commit
9d69c4322c
@ -40,6 +40,24 @@ module YouPlot
|
||||
return
|
||||
end
|
||||
|
||||
# config command
|
||||
if @command == :config
|
||||
if ENV['MYYOUPLOTRC']
|
||||
puts "config file : #{ENV['MYYOUPLOTRC']}"
|
||||
puts parser.config.inspect
|
||||
else
|
||||
puts <<~EOS
|
||||
You don't have a config file. The default config file paths are:
|
||||
./.youplot.yml, ./.youplotrc, ~/.youplot.yml, ~/.youplotrc
|
||||
You can specify a config file with the environment variable MYYOUPLOTRC.
|
||||
File format is YAML. For example:
|
||||
width : 40
|
||||
height : 20
|
||||
EOS
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
# progressive mode
|
||||
if options[:progressive]
|
||||
stop = false
|
||||
|
@ -9,7 +9,8 @@ module YouPlot
|
||||
class Error < StandardError; end
|
||||
|
||||
attr_reader :command, :options, :params,
|
||||
:main_parser, :sub_parser
|
||||
:main_parser, :sub_parser,
|
||||
:config_file, :config
|
||||
|
||||
def initialize
|
||||
@command = nil
|
||||
@ -28,6 +29,54 @@ module YouPlot
|
||||
)
|
||||
|
||||
@params = Parameters.new
|
||||
|
||||
if @config_file = find_config_file
|
||||
ENV['MYYOUPLOTRC'] = @config_file
|
||||
@config = read_config_file(config_file)
|
||||
configure(config)
|
||||
end
|
||||
end
|
||||
|
||||
def candidate_paths
|
||||
paths = []
|
||||
paths << ENV['MYYOUPLOTRC'] if ENV['MYYOUPLOTRC']
|
||||
paths << '.youplot.yml'
|
||||
paths << '.youplotrc'
|
||||
paths << File.join(ENV['HOME'], '.youplotrc') if ENV['HOME']
|
||||
paths << File.join(ENV['HOME'], '.youplot.yml') if ENV['HOME']
|
||||
paths
|
||||
end
|
||||
|
||||
def find_config_file
|
||||
config_file_path = nil
|
||||
candidate_paths.each do |file|
|
||||
path = File.expand_path(file)
|
||||
if File.exist?(path)
|
||||
config_file_path = path
|
||||
break
|
||||
end
|
||||
end
|
||||
config_file_path
|
||||
end
|
||||
|
||||
def read_config_file(path)
|
||||
require 'yaml'
|
||||
YAML.load_file(path)
|
||||
end
|
||||
|
||||
def configure(config)
|
||||
option_member = @options.members
|
||||
params_member = @params.members
|
||||
config.each do |k, v|
|
||||
k = k.to_sym
|
||||
if option_member.include?(k)
|
||||
@options[k] = v
|
||||
elsif params_member.include?(k)
|
||||
@params[k] = v
|
||||
else
|
||||
raise Error, "Unknown option/param: #{k}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def create_base_parser
|
||||
@ -277,6 +326,8 @@ module YouPlot
|
||||
options[:color_names] = v
|
||||
end
|
||||
|
||||
when :config
|
||||
|
||||
else
|
||||
error_message = "uplot: unrecognized command '#{command}'"
|
||||
if YouPlot.run_as_executable?
|
||||
|
Loading…
Reference in New Issue
Block a user