Module: CLAide::Command::Banner::TextWrapper

Defined in:
lib/claide/command/banner.rb

Private helpers collapse

Class Method Details

.strip_heredoc(string) ⇒ String

Returns Lifted straight from ActiveSupport. Thanks guys!.

Returns:

  • (String)

    Lifted straight from ActiveSupport. Thanks guys!



275
276
277
278
279
280
281
# File 'lib/claide/command/banner.rb', line 275

def self.strip_heredoc(string)
  if min = string.scan(/^[ \t]*(?=\S)/).min
    string.gsub(/^[ \t]{#{min.size}}/, '')
  else
    string
  end
end

.terminal_widthFixnum

Returns The width of the current terminal unless being piped.

Returns:

  • (Fixnum)

    The width of the current terminal unless being piped.



288
289
290
291
292
293
# File 'lib/claide/command/banner.rb', line 288

def self.terminal_width
  @terminal_width ||=
    (!ENV['CLAIDE_DISABLE_AUTO_WRAP'] &&
     STDOUT.tty? &&
     calculate_terminal_width) || 0
end

.word_wrap(line, line_width) ⇒ String

Returns Lifted straight from ActionView. Thanks guys!.

Returns:

  • (String)

    Lifted straight from ActionView. Thanks guys!



269
270
271
# File 'lib/claide/command/banner.rb', line 269

def self.word_wrap(line, line_width)
  line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip
end

.wrap_formatted_text(string, indent = 0, max_width = 80) ⇒ String

Returns Wraps a formatted string (e.g. markdown) by stripping heredoc indentation and wrapping by word to the terminal width taking into account a maximum one, and indenting the string. Code lines (i.e. indented by four spaces) are not wrapped.

Parameters:

  • string (String)

    The string to format.

  • indent (Fixnum) (defaults to: 0)

    The number of spaces to insert before the string.

  • max_width (Fixnum) (defaults to: 80)

    The maximum width to use to format the string if the terminal is too wide.

Returns:

  • (String)

    Wraps a formatted string (e.g. markdown) by stripping heredoc indentation and wrapping by word to the terminal width taking into account a maximum one, and indenting the string. Code lines (i.e. indented by four spaces) are not wrapped.



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/claide/command/banner.rb', line 228

def self.wrap_formatted_text(string, indent = 0, max_width = 80)
  paragraphs = strip_heredoc(string).split("\n\n")
  paragraphs = paragraphs.map do |paragraph|
    if paragraph.start_with?(' ' * 4)
      paragraph.gsub!(/\n/, "\n#{' ' * indent}")
    else
      paragraph = wrap_with_indent(paragraph, indent, max_width)
    end
    paragraph.insert(0, ' ' * indent).rstrip
  end
  paragraphs.join("\n\n")
end

.wrap_with_indent(string, indent = 0, max_width = 80) ⇒ String

Returns Wraps a string to the terminal width taking into account the given indentation.

Parameters:

  • string (String)

    The string to indent.

  • indent (Fixnum) (defaults to: 0)

    The number of spaces to insert before the string.

  • max_width (Fixnum) (defaults to: 80)

    The maximum width to use to format the string if the terminal is too wide.

Returns:

  • (String)

    Wraps a string to the terminal width taking into account the given indentation.



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/claide/command/banner.rb', line 254

def self.wrap_with_indent(string, indent = 0, max_width = 80)
  if terminal_width == 0
    width = max_width
  else
    width = [terminal_width, max_width].min
  end

  full_line = string.gsub("\n", ' ')
  available_width = width - indent
  space = ' ' * indent
  word_wrap(full_line, available_width).split("\n").join("\n#{space}")
end