Class: TableTennis::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/table_tennis/config.rb

Overview

Store the table configuration options, with lots of validation.

Constant Summary collapse

OPTIONS =
{
  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
  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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Config

Returns a new instance of Config.

Yields:

  • (_self)

Yield Parameters:



29
30
31
32
33
34
35
36
37
# File 'lib/table_tennis/config.rb', line 29

def initialize(options = {}, &block)
  options = [OPTIONS, TableTennis.defaults, options].reduce { _1.merge(_2 || {}) }
  options[:color] = Config.detect_color? if options[:color].nil?
  options[:theme] = Config.detect_theme if options[:theme].nil?
  options[:debug] = true if ENV["TT_DEBUG"]
  options.each { self[_1] = _2 }

  yield self if block_given?
end

Class Method Details

.detect_color?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'lib/table_tennis/config.rb', line 78

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_themeObject



85
86
87
88
89
90
# File 'lib/table_tennis/config.rb', line 85

def self.detect_theme
  case terminal_dark?
  when true, nil then :dark
  when false then :light
  end
end

.terminal_dark?Boolean

is this a dark terminal?

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/table_tennis/config.rb', line 72

def self.terminal_dark?
  if (bg = Util::Termbg.bg)
    Util::Colors.dark?(bg)
  end
end

Instance Method Details

#[](key) ⇒ Object

Raises:

  • (ArgumentError)


155
156
157
158
# File 'lib/table_tennis/config.rb', line 155

def [](key)
  raise ArgumentError, "unknown TableTennis.#{key}" if !respond_to?(key)
  send(key)
end

#[]=(key, value) ⇒ Object

Raises:

  • (ArgumentError)


160
161
162
163
# File 'lib/table_tennis/config.rb', line 160

def []=(key, value)
  raise ArgumentError, "unknown TableTennis.#{key}=" if !respond_to?(:"#{key}=")
  send(:"#{key}=", value)
end

#color_scales=(value) ⇒ Object Also known as: color_scale=

complex writers



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/table_tennis/config.rb', line 96

def color_scales=(value)
  case value
  when Array then value = value.to_h { [_1, :g] }
  when Symbol then value = {value => :g}
  end
  value.to_h { [_1, :g] } if value.is_a?(Array)
  @color_scales = validate(:color_scales, value) do
    if !value.is_a?(Hash)
      "expected hash"
    elsif value.keys.any? { !_1.is_a?(Symbol) }
      "keys must be symbols"
    elsif value.values.any? { !_1.is_a?(Symbol) }
      "values must be symbols"
    elsif value.values.any? { !Util::Scale::SCALES.include?(_1) }
      "values must be the name of a color scale"
    end
  end
end

#columns=(value) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/table_tennis/config.rb', line 116

def columns=(value)
  @columns = validate(:columns, value) do
    if !(value.is_a?(Array) && !value.empty? && value.all? { _1.is_a?(Symbol) })
      "expected array of symbols"
    end
  end
end

#inspectObject



165
166
167
168
# File 'lib/table_tennis/config.rb', line 165

def inspect
  options = to_h.map { "@#{_1}=#{_2.inspect}" }.join(", ")
  "#<Config #{options}>"
end

#layout=(value) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/table_tennis/config.rb', line 142

def layout=(value)
  @layout = validate(:layout, value) do
    next if [true, false].include?(value) || value.is_a?(Integer)
    if !value.is_a?(Hash)
      "expected boolean, int or hash"
    elsif value.keys.any? { !_1.is_a?(Symbol) }
      "keys must be symbols"
    elsif value.values.any? { !_1.is_a?(Integer) }
      "values must be ints"
    end
  end
end

#search=(value) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/table_tennis/config.rb', line 134

def search=(value)
  @search = validate(:search, value) do
    if !(value.is_a?(String) || value.is_a?(Regexp))
      "expected string/regex"
    end
  end
end

#theme=(value) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/table_tennis/config.rb', line 124

def theme=(value)
  @theme = validate(:theme, value) do
    if !value.is_a?(Symbol)
      "expected symbol"
    elsif !Theme::THEMES.key?(value)
      "expected one of #{Theme::THEMES.keys.inspect}"
    end
  end
end

#to_hObject



170
171
172
# File 'lib/table_tennis/config.rb', line 170

def to_h
  OPTIONS.keys.to_h { [_1, self[_1]] }.compact
end