Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/extstring.rb,
lib/extstring.rb
Overview
Not obsolete
Instance Method Summary collapse
-
#box(opt = {}) ⇒ Object
Draws a box around the content.
- #box!(opt = {}) ⇒ Object
- #end_with?(str) ⇒ Boolean
-
#wrap(width, delimiter = "\n") ⇒ Object
A wrap-function which works here, but may fail everywhere else.
Instance Method Details
#box(opt = {}) ⇒ Object
Draws a box around the content. TODO: the :padding option is currently disabled as it confuses me.
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/extstring.rb', line 41 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 = {}) ⇒ Object
107 108 109 |
# File 'lib/extstring.rb', line 107 def box!(opt = {}) self.replace(String.new(box(opt))) end |
#end_with?(str) ⇒ Boolean
25 26 27 28 29 30 31 |
# File 'lib/extstring.rb', line 25 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") ⇒ Object
A wrap-function which works here, but may fail everywhere else.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/extstring.rb', line 112 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 |