Module: Squared::Common::Format

Includes:
Squared::Common
Defined in:
lib/squared/common/format.rb

Instance Method Summary collapse

Methods included from Squared::Common

#__get__, #__set__, #as_a, #finalize!, #message

Instance Method Details

#apply_style(data, key, *args, empty: true) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/squared/common/format.rb', line 158

def apply_style(data, key, *args, empty: true)
  return unless !data.is_a?(Symbol) || (data = __get__(:theme)[data])

  set = ->(k, v) { data[k.to_sym] = check_style(v, empty: empty) }
  if key.is_a?(Hash)
    key.each { |k, v| set.(k, v || args.flatten) }
  else
    set.(key, args.flatten)
  end
end

#check_style(*args, empty: true) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/squared/common/format.rb', line 141

def check_style(*args, empty: true)
  ret = []
  colors = __get__(:colors)
  as_a(args, flat: true).each do |val|
    if !val.is_a?(Numeric)
      val = val.to_sym
      ret << val if colors.key?(val) || TEXT_STYLE.include?(val)
    elsif val >= 0 && val <= 256
      ret << val
    elsif val < 0 && (b = val.to_s.split('.')[1])
      b = b[0..2]
      ret << "-0.#{b}".to_f unless b.to_i > 255
    end
  end
  !empty && ret.empty? ? nil : ret
end

#emphasize(val, title: nil, cols: nil, sub: nil, pipe: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/squared/common/format.rb', line 36

def emphasize(val, title: nil, cols: nil, sub: nil, pipe: nil)
  n = 0
  if title
    title = title.to_s
    n = title.size
  end
  if val.is_a?(::Array)
    lines = val
  else
    lines = val.to_s.lines.map(&:chomp)
    lines[0] = "#{val.class}: #{lines.first}" if (err = val.is_a?(::StandardError))
  end
  n = cols || [n, lines.max_by(&:size).size].max
  if $stdout.tty?
    require 'io/console'
    (n = [n, $stdout.winsize[1] - 4].min) rescue nil
  end
  out = []
  bord = '-' * (n + 4)
  sub = as_a(sub)
  pr = lambda do |line|
    s = line.ljust(n)
    sub.each { |h| s = sub_style(s, **h) }
    "| #{s} |"
  end
  out << bord
  out.push(pr.(title), bord) if title
  lines.each { |line| out << pr.(line) }
  out << bord
  if block_given?
    yield out
  elsif pipe.respond_to?(:puts)
    pipe.puts out
  else
    err ? warn(out) : puts(out)
  end
end

#enable_aixtermObject



29
30
31
32
33
34
# File 'lib/squared/common/format.rb', line 29

def enable_aixterm
  unless (colors = __get__(:colors)).frozen?
    colors.merge!(AIX_TERM)
  end
  block_given? ? yield(self) : self
end

#log_message(level, *args, subject: nil, hint: nil, color: true) ⇒ Object



198
199
200
201
202
# File 'lib/squared/common/format.rb', line 198

def log_message(level, *args, subject: nil, hint: nil, color: true)
  msg = [log_title(level, color: color)]
  msg << (color ? sub_style(subject, :underline) : subject) if subject
  message(msg.join(' '), *args, hint: hint)
end

#log_title(level, color: true) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/squared/common/format.rb', line 169

def log_title(level, color: true)
  level = if level.is_a?(::Numeric)
            case level
            when Logger::DEBUG
              :debug
            when Logger::INFO
              :info
            when Logger::WARN
              :warn
            when Logger::ERROR
              :error
            when Logger::FATAL
              :fatal
            else
              :unknown
            end
          else
            level.to_s.downcase.to_sym
          end
  theme = __get__(:theme)[:logger]
  val = theme[level] || theme[level = :unknown]
  level = +level.to_s.upcase
  case level
  when 'WARN', 'ERROR', 'FATAL'
    level += '!'
  end
  color ? sub_style(level, *val) : level
end

#sub_style(val, *args, styles: nil, pat: nil, index: 1) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/squared/common/format.rb', line 74

def sub_style(val, *args, styles: nil, pat: nil, index: 1)
  return val unless ENV['NO_COLOR'].to_s.empty?

  if pat && index != 0
    return val unless (data = pat.match(val))

    ret = index == -1 ? data.to_a[1..-1] : data[index]
  else
    ret = val
    index = 0
  end
  wrap = ->(s, n) { "\x1B[#{n.join(';')}m#{s}\x1B[0m" }
  code = []
  args.concat(as_a(styles)).each_with_index do |type, i|
    next unless type

    if index == -1
      s = ret[i]
      next ret[i] = '' if s.nil?
    else
      s = ret
    end
    if type.is_a?(Numeric)
      f, b = type.to_s.split('.')
      s = wrap.(s, ['38', '5', f]) if f[0] != '-' && f.to_i <= 255
      if b
        b = b[0..2]
        s = wrap.(s, ['48', '5', b]) unless b.to_i > 255
      end
    else
      t = type.to_sym
      if (c = __get__(:colors)[t])
        if index == -1
          s = wrap.(s, [c])
        else
          code << c
        end
      else
        next unless (n = TEXT_STYLE.index { |style| style == t })

        s = "\x1B[#{n + 1}m#{s}\x1B[#{n == 0 ? 22 : n + 21}m"
      end
    end
    if index == -1
      ret[i] = s
    else
      ret = s
    end
  end
  return ret.join if index == -1

  ret = wrap.(ret, code) unless code.empty?
  return ret unless data

  out = +''
  data.to_a.each_with_index do |group, i|
    next if i == 0

    if i == index
      out << ret
    elsif !group.nil?
      out << group
    end
  end
  out
end