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)


139
140
141
# File 'lib/cli/core_ext.rb', line 139

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

#bosh_valid_id?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/cli/core_ext.rb', line 143

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

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



158
159
160
# File 'lib/cli/core_ext.rb', line 158

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

#indent(margin = 2) ⇒ Object



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

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

#make_color(color_code) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cli/core_ext.rb', line 127

def make_color(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

#make_greenObject



119
120
121
# File 'lib/cli/core_ext.rb', line 119

def make_green
  make_color(:green)
end

#make_redObject



115
116
117
# File 'lib/cli/core_ext.rb', line 115

def make_red
  make_color(:red)
end

#make_yellowObject



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

def make_yellow
  make_color(:yellow)
end

#truncate(limit = 30) ⇒ Object



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

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