Module: BoshStringExtensions

Included in:
String
Defined in:
lib/bosh/gen/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)


148
149
150
# File 'lib/bosh/gen/core-ext.rb', line 148

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

#bosh_valid_id?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/bosh/gen/core-ext.rb', line 152

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

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



167
168
169
# File 'lib/bosh/gen/core-ext.rb', line 167

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

#indent(margin = 2) ⇒ Object



171
172
173
174
175
# File 'lib/bosh/gen/core-ext.rb', line 171

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

#make_color(color_code) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/bosh/gen/core-ext.rb', line 134

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



126
127
128
# File 'lib/bosh/gen/core-ext.rb', line 126

def make_green
  make_color(:green)
end

#make_redObject



122
123
124
# File 'lib/bosh/gen/core-ext.rb', line 122

def make_red
  make_color(:red)
end

#make_yellowObject



130
131
132
# File 'lib/bosh/gen/core-ext.rb', line 130

def make_yellow
  make_color(:yellow)
end

#truncate(limit = 30) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/bosh/gen/core-ext.rb', line 156

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