module CLAide::Command::Banner::TextWrapper

Public Class Methods

strip_heredoc(string) click to toggle source

@return [String] Lifted straight from ActiveSupport. Thanks guys!

# 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_width() click to toggle source

@return [Fixnum] The width of the current terminal unless being piped.

# 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) click to toggle source

@return [String] Lifted straight from ActionView. Thanks guys!

# 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) click to toggle source

@return [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.

@param [String] string

The string to format.

@param [Fixnum] indent

The number of spaces to insert before the string.

@param [Fixnum] max_width

The maximum width to use to format the string if the terminal
is too wide.
# 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) click to toggle source

@return [String] Wraps a string to the terminal width taking into

account the given indentation.

@param [String] string

The string to indent.

@param [Fixnum] indent

The number of spaces to insert before the string.

@param [Fixnum] max_width

The maximum width to use to format the string if the terminal
is too wide.
# 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

Private Class Methods

calculate_terminal_width() click to toggle source
# File lib/claide/command/banner.rb, line 295
def self.calculate_terminal_width
  require 'io/console'
  STDOUT.winsize.last
rescue LoadError
  (system('which tput > /dev/null 2>&1') && `tput cols`.to_i) || 0
rescue
  0
end