Module: HammerCLI::Output::Utils

Defined in:
lib/hammer_cli/output/utils.rb

Class Method Summary collapse

Class Method Details

.real_char_length(ch) ⇒ Object



11
12
13
# File 'lib/hammer_cli/output/utils.rb', line 11

def self.real_char_length(ch)
  Unicode::DisplayWidth.of(ch)
end

.real_length(value) ⇒ Object



6
7
8
9
# File 'lib/hammer_cli/output/utils.rb', line 6

def self.real_length(value)
  decolorized = value.gsub(/\033\[[^m]*m/, '')
  Unicode::DisplayWidth.of(decolorized)
end

.real_truncate(value, required_size) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hammer_cli/output/utils.rb', line 15

def self.real_truncate(value, required_size)
  size = 0
  index = 0
  has_colors = false
  in_color = false
  value.each_char do |ch|
    if in_color
      in_color = false if ch == "m"
    elsif ch == "\e"
      has_colors = in_color = true
    else
      increment = real_char_length(ch)
      if size + increment > required_size
        if has_colors
          return value[0..index-1] + "\e[0m", size
        else
          return value[0..index-1], size
        end
      else
        size += increment
      end
    end
    index += 1
  end
  return value, size
end