class String

Not obsolete

Public Instance Methods

box(opt = {}) click to toggle source

Draws a box around the content. TODO: the :padding option is currently disabled as it confuses me.

# File lib/extstring.rb, line 48
def box(opt = {})
  # ---------------
  # Welcome to hell
  # ---------------

  color = opt[:color]

  upleft = color ? colorize("\u250F", color) : "\u250F"
  upright = color ? colorize("\u2513", color) : "\u2513"
  downleft = color ? colorize("\u2517", color) : "\u2517"
  downright = color ? colorize("\u251B", color) : "\u251B"

  vert = color ? colorize("\u2503", color) : "\u2503"
  hor = color ? colorize("\u2501", color) : "\u2501"

  left = opt[:left]
  top = opt[:top]
  # padding = opt[:padding]

  min_width = opt[:min_width]

  size = `stty size`.split.map { |x| x.to_i }.reverse
  left ||= 0
  top ||= 0
  # padding ||= 0
  
  min_width ||= 15
  
  # max_width is the width of the screen - left margin - 2x border
  max_width = size[0] - left - 2

  gsub!("\t", '    ')
  width = max_width
  # wrap lines, maybe
  wrap(width , /^\d+\)/)
  # determine the width of the box
  width = self.split("\n").max {|l1, l2| l1.chomp.length <=> l2.chomp.length}.length  
  width = [width, min_width].max

  box = ''
  # top margin
  box << "\n" * top
  # top border, left margin
  box << ' ' * left << upleft.dup << hor.dup * width   << upright.dup << "\n"
  # top padding, left margin and so on ...
  box << ' ' * left << vert.dup << ' ' * width << vert.dup << "\n"
  # lines
  self.split("\n").each do |line|
    if(line && !line.empty?)
      nl = ' ' * left << vert.dup << "%-#{width}s" %line
      while(nl.length < width )
        nl << ' '
      end
      nl << vert.dup << "\n"
    else
      nl = ' ' * left << vert.dup << ' ' * width  << vert.dup << "\n"
    end
    box << nl
  end
  # bottom padding
  box << ' ' * left << vert.dup << ' ' * width << vert.dup << "\n"
  # bottom border
  box << ' ' * left << downleft.dup << hor.dup * width << downright.dup
  return box
end
box!(opt = {}) click to toggle source
# File lib/extstring.rb, line 114
def box!(opt = {})
  self.replace(String.new(box(opt)))
end
end_with?(str) click to toggle source
# File lib/extstring.rb, line 32
def end_with?(str)
  isso = false;
  if(str.respond_to?(:to_str) && self.include?(str))
    isso = (slice(rindex(str), (size()-1)) == str)
  end
  return isso
end
wrap(width, delimiter = "\n") click to toggle source

A wrap-function which works here, but may fail everywhere else.

# File lib/extstring.rb, line 119
def wrap(width, delimiter = "\n")
  # YEAH. Ruby is crap because its regular expressions do not work as they
  # should with special characters. And your coffee mug has a speech
  # impediment. Face it!
  words = self.scan(/\p{P}*[\wöÖüÜäÄßéèàôûùîçÇœæŒÆ]+\p{P}*/)
 
  lines = []
  lines << ''
  li = 0
  words.each do |w|
    w.strip!
    if( w.length + lines[li].length + 1 > width || w != words.first && w.match(delimiter) )
      lines[li].squeeze!(' ')
      lines << ''
      li += 1
    else
    end
    lines[li] << w 
    lines[li] << ' ' if w != words.last
  end
  self.replace(lines.join("\n"))
end