Module: BoshStringExtensions

Included in:
String
Defined in:
lib/cli/core_ext.rb

Constant Summary collapse

COLOR_CODES =
{
  :red => "\e[0m\e[31m",
  :green => "\e[0m\e[32m",
  :yellow => "\e[0m\e[33m"
}

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/cli/core_ext.rb', line 127

def blank?
  self =~ /^\s*$/
end

#bosh_valid_id?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/cli/core_ext.rb', line 131

def bosh_valid_id?
  self =~ Bosh::Cli::Config::VALID_ID
end

#colorize(color_code) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cli/core_ext.rb', line 115

def colorize(color_code)
  if Bosh::Cli::Config.output &&
     Bosh::Cli::Config.output.tty? &&
     Bosh::Cli::Config.colorize &&
     COLOR_CODES[color_code]

    "#{COLOR_CODES[color_code]}#{self}\e[0m"
  else
    self
  end
end

#columnize(width = 80, left_margin = 0) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cli/core_ext.rb', line 146

def columnize(width = 80, left_margin = 0)
  result = ""
  buf = ""
  self.split(/\s+/).each do |word|
    if buf.size + word.size > width
      result << buf << "\n" << " " * left_margin
      buf = word + " "
    else
      buf << word << " "
    end

  end
  result + buf
end

#greenObject



107
108
109
# File 'lib/cli/core_ext.rb', line 107

def green
  colorize(:green)
end

#indent(margin = 2) ⇒ Object



161
162
163
164
165
# File 'lib/cli/core_ext.rb', line 161

def indent(margin = 2)
  self.split("\n").map { |line|
    " " * margin + line
  }.join("\n")
end

#redObject



103
104
105
# File 'lib/cli/core_ext.rb', line 103

def red
  colorize(:red)
end

#truncate(limit = 30) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/cli/core_ext.rb', line 135

def truncate(limit = 30)
  return "" if self.blank?
  etc = "..."
  stripped = self.strip[0..limit]
  if stripped.length > limit
    stripped.gsub(/\s+?(\S+)?$/, "") + etc
  else
    stripped
  end
end

#yellowObject



111
112
113
# File 'lib/cli/core_ext.rb', line 111

def yellow
  colorize(:yellow)
end