Module: HammerCLI::Output::Utils

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

Constant Summary collapse

TAGS_MAPPING =
{
  flat: :serialized,
  data: :structured,
  screen: :rich_text,
  file: :plain_text
}.freeze

Class Method Summary collapse

Class Method Details

.real_char_length(ch) ⇒ Object



22
23
24
# File 'lib/hammer_cli/output/utils.rb', line 22

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

.real_length(value) ⇒ Object



17
18
19
20
# File 'lib/hammer_cli/output/utils.rb', line 17

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

.real_truncate(value, required_size) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hammer_cli/output/utils.rb', line 26

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

.tag_to_feature(tag) ⇒ Object



13
14
15
# File 'lib/hammer_cli/output/utils.rb', line 13

def self.tag_to_feature(tag)
  TAGS_MAPPING[tag]
end