Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/extstring.rb,
lib/extstring.rb

Instance Method Summary collapse

Instance Method Details

#box(opt = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/extstring.rb', line 16

def box(opt = {})
  upleft = "\u250F"
  upright = "\u2513"
  downleft = "\u2517"
  downright = "\u251B"

  vert = "\u2503"
  hor = "\u2501"

  max_width = size[0] - 5

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

  size = `stty size`.split.map { |x| x.to_i }.reverse

  left ||= 0
  top ||= 0
  padding ||= 0
  # width ||= max_width
  # width = max_width if max_width < width

  gsub!("\t", '    ')
  width = self.split("\n").max {|l1, l2| l1.length <=> l2.length}.length + 2* padding 


  box = ''
  box << "\n" * top
  # top border
  box << ' ' * left << upleft.dup << hor.dup * width << upright.dup << "\n"
  # top padding
  box << ' ' * left << vert.dup << ' ' * width << vert.dup << "\n"
  # lines
  self.split("\n").each do |line|
    if(line && !line.empty?)
      nl = ' ' * left << vert.dup << ' ' * padding << line << ' ' * padding
      # wanted length : width + left margin + width of the first vertical border sign
      until(nl.length >= ( width + left +1) )
        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 = {}) ⇒ Object



71
72
73
# File 'lib/extstring.rb', line 71

def box!(opt = {})
  self.replace(String.new(box(opt)))
end

#end_with?(str) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
# File 'lib/extstring.rb', line 5

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