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)

150
151
152
# File 'lib/cli/core_ext.rb', line 150

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

#bosh_valid_id?Boolean

Returns:

  • (Boolean)

154
155
156
# File 'lib/cli/core_ext.rb', line 154

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

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


169
170
171
# File 'lib/cli/core_ext.rb', line 169

def columnize(width = 80, left_margin = 0)
  Bosh::Cli::LineWrap.new(width, left_margin).wrap(self)
end

#indent(margin = 2) ⇒ Object


173
174
175
176
177
# File 'lib/cli/core_ext.rb', line 173

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

#make_color(color_code) ⇒ Object


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

def make_color(color_code)
  # invalid color
  return self if !COLOR_CODES[color_code]

  # output disabled
  return self if !Bosh::Cli::Config.output

  if Bosh::Cli::Config.use_color?
    "#{COLOR_CODES[color_code]}#{self}\e[0m"
  else
    self
  end
end

#make_greenObject


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

def make_green
  make_color(:green)
end

#make_redObject


124
125
126
# File 'lib/cli/core_ext.rb', line 124

def make_red
  make_color(:red)
end

#make_yellowObject


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

def make_yellow
  make_color(:yellow)
end

#truncate(limit = 30) ⇒ Object


158
159
160
161
162
163
164
165
166
167
# File 'lib/cli/core_ext.rb', line 158

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