class TTY::Logger::Formatters::Text
Format data suitable for text reading
Constants
- ELLIPSIS
- ESCAPE_DOUBLE_QUOTE
- ESCAPE_STR_REGEX
- LBRACE
- LBRACKET
- LITERAL_FALSE
- LITERAL_NIL
- LITERAL_TRUE
- LPAREN
- NUM_REGEX
- RBRACE
- RBRACKET
- RPAREN
- SINGLE_QUOTE_REGEX
- SPACE
Public Instance Methods
dump(obj, max_bytes: 2**12, max_depth: 3)
click to toggle source
Dump data in a single formatted line
@param [Hash] obj
the object to serialize as text
@return [String]
@api public
# File lib/tty/logger/formatters/text.rb, line 32 def dump(obj, max_bytes: 2**12, max_depth: 3) bytesize = 0 line = obj.each_with_object([]) do |(k, v), acc| str = "#{dump_key(k)}=#{dump_val(v, max_depth)}" items = acc.size - 1 if bytesize + str.bytesize + items > max_bytes if bytesize + items + (acc[-1].bytesize - ELLIPSIS.bytesize) > max_bytes acc.pop end acc << ELLIPSIS break acc else bytesize += str.bytesize acc << str end end line.join(SPACE) end
Private Instance Methods
dump_key(key)
click to toggle source
# File lib/tty/logger/formatters/text.rb, line 56 def dump_key(key) key = key.to_s case key when SINGLE_QUOTE_REGEX key.inspect when ESCAPE_STR_REGEX ESCAPE_DOUBLE_QUOTE + key.inspect[1..-2] + ESCAPE_DOUBLE_QUOTE else key end end
dump_val(val, depth)
click to toggle source
# File lib/tty/logger/formatters/text.rb, line 68 def dump_val(val, depth) case val when Hash then enc_obj(val, depth - 1) when Array then enc_arr(val, depth - 1) when String, Symbol then enc_str(val) when Complex then enc_cpx(val) when Float then enc_float(val) when Numeric then enc_num(val) when Time then enc_time(val) when TrueClass then LITERAL_TRUE when FalseClass then LITERAL_FALSE when NilClass then LITERAL_NIL else val end end
enc_arr(array, depth)
click to toggle source
# File lib/tty/logger/formatters/text.rb, line 93 def enc_arr(array, depth) return LBRACKET + ELLIPSIS + RBRACKET if depth.zero? LBRACKET + array.map { |v| dump_val(v, depth) }.join(SPACE) + RBRACKET end
enc_cpx(val)
click to toggle source
# File lib/tty/logger/formatters/text.rb, line 99 def enc_cpx(val) LPAREN + val.to_s + RPAREN end
enc_float(val)
click to toggle source
# File lib/tty/logger/formatters/text.rb, line 103 def enc_float(val) ("%f" % val).sub(/0*?$/, "") end
enc_num(val)
click to toggle source
# File lib/tty/logger/formatters/text.rb, line 107 def enc_num(val) val end
enc_obj(obj, depth)
click to toggle source
# File lib/tty/logger/formatters/text.rb, line 85 def enc_obj(obj, depth) return LBRACE + ELLIPSIS + RBRACE if depth.zero? LBRACE + obj.map { |k, v| "#{dump_key(k)}=#{dump_val(v, depth)}" } .join(SPACE) + RBRACE end
enc_str(str)
click to toggle source
# File lib/tty/logger/formatters/text.rb, line 111 def enc_str(str) str = str.to_s case str when SINGLE_QUOTE_REGEX str.inspect when ESCAPE_STR_REGEX, LITERAL_TRUE, LITERAL_FALSE, LITERAL_NIL, NUM_REGEX ESCAPE_DOUBLE_QUOTE + str.inspect[1..-2] + ESCAPE_DOUBLE_QUOTE else str end end
enc_time(time)
click to toggle source
# File lib/tty/logger/formatters/text.rb, line 124 def enc_time(time) time.strftime("%FT%T%:z") end