Class: TableTennis::Config
- Inherits:
-
Util::MagicOptions
- Object
- Util::MagicOptions
- TableTennis::Config
- Defined in:
- lib/table_tennis/config.rb
Overview
Table configuration options, with schema validation courtesy of MagicOptions.
Constant Summary collapse
- OPTIONS =
{ coerce: true, # if true, we try to coerce strings into numbers color_scales: nil, # columns => color scale color: nil, # true/false/nil (detect) columns: nil, # array of symbols, or inferred from rows debug: false, # true for debug output delims: true, # true for numeric delimeters digits: 3, # format floats headers: nil, # columns => header strings, or inferred from columns layout: true, # true/false/int or hash of columns -> width. true to infer mark: nil, # lambda returning boolean or symbol to mark rows in output placeholder: "—", # placeholder for empty cells. default is emdash row_numbers: false, # show line numbers? save: nil, # csv file path to save the table when created search: nil, # string/regex to highlight in output separators: true, # if true, show separators between columns strftime: nil, # string for formatting dates theme: nil, # :dark, :light or :ansi. :dark is the default title: nil, # string for table title, if any titleize: false, # if true, titleize column names zebra: false, # turn on zebra stripes }.freeze
- SCHEMA =
{ coerce: :bool, color_scales: -> do Config.magic_validate!(:color_scales, _1, {Symbol => Symbol}) if !(invalid = _1.values - Util::Scale::SCALES.keys).empty? raise ArgumentError, "invalid color scale(s): #{invalid.inspect}" end end, color: :bool, columns: :symbols, debug: :bool, delims: :bool, digits: (0..10), headers: {sym: :str}, layout: -> do return if _1 == true || _1 == false || _1.is_a?(Integer) Config.magic_validate!(:layout, _1, {Symbol => Integer}) end, mark: :proc, placeholder: :str, row_numbers: :bool, save: :str, search: -> do if !(_1.is_a?(String) || _1.is_a?(Regexp)) raise ArgumentError, "expected string/regex" end end, separators: :bool, strftime: :str, theme: %i[dark light ansi], title: :str, titleize: :bool, zebra: :bool, }
Constants inherited from Util::MagicOptions
Util::MagicOptions::MAGIC_ALIASES, Util::MagicOptions::MAGIC_PRETTY
Instance Attribute Summary
Attributes inherited from Util::MagicOptions
#magic_attributes, #magic_values
Class Method Summary collapse
-
.detect_color? ⇒ Boolean
helpers.
- .detect_theme ⇒ Object
-
.terminal_dark? ⇒ Boolean
is this a dark terminal?.
Instance Method Summary collapse
-
#color_scales=(value) ⇒ Object
(also: #color_scale=)
override a few setters to coerce values.
-
#initialize(options = {}, &block) ⇒ Config
constructor
A new instance of Config.
- #placeholder=(value) ⇒ Object
- #title=(value) ⇒ Object
Methods inherited from Util::MagicOptions
#inspect, #magic_add_attribute, magic_coerce, #magic_get, magic_is_a?, magic_pretty, magic_resolve, #magic_sanity, #magic_set, magic_validate!, #to_h, #update!, validate_any_of, validate_array, validate_class, validate_hash, validate_range, validate_regexp
Constructor Details
#initialize(options = {}, &block) ⇒ Config
Returns a new instance of Config.
66 67 68 69 70 71 72 73 |
# File 'lib/table_tennis/config.rb', line 66 def initialize( = {}, &block) # assemble from OPTIONS, defaults and options = [OPTIONS, TableTennis.defaults, ].reduce { _1.merge(_2 || {}) } [:color] = Config.detect_color? if [:color].nil? [:debug] = true if ENV["TT_DEBUG"] [:theme] = Config.detect_theme if [:theme].nil? super(SCHEMA, , &block) end |
Class Method Details
.detect_color? ⇒ Boolean
helpers
101 102 103 104 105 106 |
# File 'lib/table_tennis/config.rb', line 101 def self.detect_color? return false if ENV["NO_COLOR"] || ENV["CI"] return true if ENV["FORCE_COLOR"] == "1" return false if !($stdout.tty? && $stderr.tty?) Paint.detect_mode > 0 end |
.detect_theme ⇒ Object
108 109 110 111 112 113 |
# File 'lib/table_tennis/config.rb', line 108 def self.detect_theme case terminal_dark? when true, nil then :dark when false then :light end end |
Instance Method Details
#color_scales=(value) ⇒ Object Also known as: color_scale=
override a few setters to coerce values
79 80 81 82 83 84 |
# File 'lib/table_tennis/config.rb', line 79 def color_scales=(value) if value.is_a?(Array) || value.is_a?(Symbol) value = Array(value).to_h { [_1, :g] } end self[:color_scales] = value end |
#placeholder=(value) ⇒ Object
87 88 89 90 |
# File 'lib/table_tennis/config.rb', line 87 def placeholder=(value) value = "" if value.nil? self[:placeholder] = value end |
#title=(value) ⇒ Object
92 93 94 95 |
# File 'lib/table_tennis/config.rb', line 92 def title=(value) value = value.to_s if value.is_a?(Symbol) self[:title] = value end |