Module: Flor::Djan

Extended by:
Djan
Included in:
Djan
Defined in:
lib/flor/djan.rb

Instance Method Summary collapse

Instance Method Details

#adjust(x, opts) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/flor/djan.rb', line 69

def adjust(x, opts)

  i = opts[:indent]
  w = opts[:width]

  return opts unless i && w && (i + len(x, opts) < w)
  opts.merge(indent: nil)
end

#array_to_d(x, out, opts) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/flor/djan.rb', line 174

def array_to_d(x, out, opts)

  inner = opts.delete(:inner)

  indent_space(out, opts)

  return c_inf('[]', out, opts) if x.empty?

  opts = adjust(x, opts)

  unless inner
    c_inf('[', out, opts); space(out, opts)
  end

  x.each_with_index do |e, i|
    to_d(e, out, indent(opts, first: i == 0))
    if i < x.size - 1
      c_inf(',', out, opts)
      newline_or_space(out, opts)
    end
  end

  unless inner
    space(out, opts); c_inf(']', out, opts)
  end
end

#boolean_to_d(x, out, opts) ⇒ Object



223
224
225
226
# File 'lib/flor/djan.rb', line 223

def boolean_to_d(x, out, opts)

  indent_space(out, opts); x ? c_tru(x, out, opts) : c_fal(x, out, opts)
end

#c_fal(s, out, opts) ⇒ Object



242
# File 'lib/flor/djan.rb', line 242

def c_fal(s, out, opts); out << opts[:c].red(s); end

#c_inf(s, out, opts) ⇒ Object



238
# File 'lib/flor/djan.rb', line 238

def c_inf(s, out, opts); out << opts[:c].dark_gray(s); end

#c_nil(s, out, opts) ⇒ Object



240
# File 'lib/flor/djan.rb', line 240

def c_nil(s, out, opts); out << opts[:c].dark_gray(s); end

#c_num(s, out, opts) ⇒ Object

out << opts.brown(s).tap { |x| p [ x, x.encoding ] } out << opts.brown(s).encode(‘UTF-8’)



249
# File 'lib/flor/djan.rb', line 249

def c_num(s, out, opts); out << opts[:c].light_blue(s); end

#c_str(s, out, opts) ⇒ Object

def c_str(s, out, opts); out << opts.brown(s); end



244
245
246
247
248
# File 'lib/flor/djan.rb', line 244

def c_str(s, out, opts)
  out << opts[:c].brown(s)
  #out << opts[:c].brown(s).tap { |x| p [ x, x.encoding ] }
  #out << opts[:c].brown(s).encode('UTF-8')
end

#c_tru(s, out, opts) ⇒ Object



241
# File 'lib/flor/djan.rb', line 241

def c_tru(s, out, opts); out << opts[:c].green(s); end

#indent(opts, os = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/flor/djan.rb', line 109

def indent(opts, os={})

  if kt = os[:keytab]
    opts.merge(indent: nil, keytab: kt)
  elsif i = opts[:indent]
    opts.merge(indent: i + (os[:inc] || 1) * 2, first: os[:first])
  else
    opts
  end
end

#indent_space(out, opts) ⇒ Object



102
103
104
105
106
107
# File 'lib/flor/djan.rb', line 102

def indent_space(out, opts)

  return if opts.delete(:first)
  i = opts[:indent]
  out << ' ' * i if i
end

#len(x, opts) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/flor/djan.rb', line 59

def len(x, opts)

  opts = opts.merge(c: Flor.no_colours, indent: nil, width: nil)
  o = StringIO.new

  to_d(x, o, opts)

  o.string.length
end

#newline(out, opts) ⇒ Object



78
79
80
81
# File 'lib/flor/djan.rb', line 78

def newline(out, opts)

  out << "\n"
end

#newline_or_space(out, opts) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/flor/djan.rb', line 88

def newline_or_space(out, opts)

  if kt = opts[:keytab]
    out << ' ' * kt
    :indent
  elsif opts[:indent]
    newline(out, opts)
    :newline
  elsif ! opts[:compact]
    space(out, opts)
    :space
  end
end

#nil_to_d(x, out, opts) ⇒ Object



233
234
235
236
# File 'lib/flor/djan.rb', line 233

def nil_to_d(x, out, opts)

  indent_space(out, opts); c_nil('null', out, opts)
end

#num_to_d(x, out, opts) ⇒ Object



228
229
230
231
# File 'lib/flor/djan.rb', line 228

def num_to_d(x, out, opts)

  indent_space(out, opts); c_num(x, out, opts)
end

#object_to_d(x, out, opts) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/flor/djan.rb', line 120

def object_to_d(x, out, opts)

  inner = opts.delete(:inner)

  indent_space(out, opts)

  return c_inf('{}', out, opts) if x.empty?

  opts = adjust(x, opts)

  unless inner
    c_inf('{', out, opts); space(out, opts)
  end

  key_max_len =
    if opts[:compact]
      nil
    else
      i = opts[:indent]
      w = opts[:width]
        #
      kml, vml =
        x.inject([ 0, 0 ]) { |(kl, vl), (k, v)|
          [ [ kl, len(k, opts) ].max, [ vl, len(v, opts) ].max ] }
      kml += 1
        #
      if i && w && i + kml + 1 + vml < w
        kml
      else
        nil
      end
    end

  x.each_with_index do |(k, v), ii|

    kl = string_to_d(k, out, indent(opts, first: ii == 0))
    c_inf(':', out, opts)

    kt = key_max_len ? key_max_len - kl : nil
    r = newline_or_space(out, opts.merge(keytab: kt))

    to_d(v, out, indent(opts, inc: 2, keytab: r == :newline ? kt : 1))

    if ii < x.size - 1
      c_inf(',', out, opts)
      newline_or_space(out, opts)
    end
  end

  unless inner
    space(out, opts); c_inf('}', out, opts)
  end
end

#space(out, opts, force = false) ⇒ Object



83
84
85
86
# File 'lib/flor/djan.rb', line 83

def space(out, opts, force=false)

  out << ' ' if force || ! opts[:compact]
end

#string_to_d(x, out, opts) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/flor/djan.rb', line 201

def string_to_d(x, out, opts)

  x = x.to_s

  indent_space(out, opts)

  if (
    opts[:json] ||
    x.match(/\A[^: \b\f\n\r\t"',()\[\]{}#\\+%\/><^!=-]+\z/) == nil ||
    x.to_i.to_s == x ||
    x.to_f.to_s == x ||
    opts[:str_escape].include?(x)
  ) then
    s = x.inspect
    c_str(s, out, opts)
    s.length
  else
    c_str(x, out, opts)
    x.length
  end
end

#to_d(x, out, opts) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/flor/djan.rb', line 46

def to_d(x, out, opts)

  case x
  when nil then nil_to_d(x, out, opts)
  when String then string_to_d(x, out, opts)
  when Hash then object_to_d(x, out, opts)
  when Array then array_to_d(x, out, opts)
  when TrueClass then boolean_to_d(x.to_s, out, opts)
  when FalseClass then boolean_to_d(x.to_s, out, opts)
  else num_to_d(x.to_s, out, opts)
  end
end