Class: TTY::Logger::Formatters::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/logger/formatters/text.rb

Overview

Format data suitable for text reading

Constant Summary collapse

SPACE =
" "
LPAREN =
"("
RPAREN =
")"
LBRACE =
"{"
RBRACE =
"}"
LBRACKET =
"["
RBRACKET =
"]"
ELLIPSIS =
"..."
LITERAL_TRUE =
"true"
LITERAL_FALSE =
"false"
LITERAL_NIL =
"nil"
SINGLE_QUOTE_REGEX =
/'/.freeze
ESCAPE_DOUBLE_QUOTE =
"\""
ESCAPE_STR_REGEX =
/[ ="|{}()\[\]^$+*?.-]/.freeze
NUM_REGEX =
/^-?\d*(?:\.\d+)?\d+$/.freeze

Instance Method Summary collapse

Instance Method Details

#dump(obj, max_bytes: 2**12, max_depth: 3) ⇒ String

Dump data in a single formatted line

Parameters:

  • obj (Hash)

    the object to serialize as text

Returns:

  • (String)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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