Class: String
Instance Method Summary collapse
-
#align ⇒ Object
removes leading whitespace from a set of lines, preserving indentation relative to the first non-empty line.
-
#cabbrev(len) ⇒ Object
gives an abbreviated form of a string, showing some of the beginning and some of the end.
-
#labbrev(len) ⇒ Object
gives an abbreviated form of the string, showing as much of the beginning as possible.
-
#rabbrev(len) ⇒ Object
gives an abbreviated form of the string, showing as much of the end as possible.
-
#visual_center(width, padding = " ") ⇒ Object
like #center but uses #visual_length for proper ANSI sequence handling.
-
#visual_length ⇒ Object
gives the apparent length of the string (ignoring ASCII sequences).
-
#visual_length_delta ⇒ Object
gives the difference between the actual and apparent length of the string.
-
#visual_ljust(width, padding = " ") ⇒ Object
like #ljust but uses #visual_length for proper ANSI sequence handling.
-
#visual_rjust(width, padding = " ") ⇒ Object
like #rjust but uses #visual_length for proper ANSI sequence handling.
Instance Method Details
#align ⇒ Object
removes leading whitespace from a set of lines, preserving indentation relative to the first non-empty line
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/sitefuel/extensions/StringFormatting.rb', line 94 def align return self if empty? # split by lines lines = split("\n") # get rid of the first line if it is null if lines.first == '' lines = lines[1..-1] end # take the first line and extract the leading whitespace leading = lines.first[/^[ \t]*/] # iterate over each line dumping the leading whitespace lines = lines.map do |l| l.gsub!(Regexp.new('^'+leading), '') end lines.join("\n") end |
#cabbrev(len) ⇒ Object
gives an abbreviated form of a string, showing some of the beginning and some of the end.
"the quick brown dog".cabbrev 12 # => "the q... dog"
55 56 57 58 59 60 61 62 |
# File 'lib/sitefuel/extensions/StringFormatting.rb', line 55 def cabbrev(len) real_length = uncolored.length if real_length <= len self else self[0..(len/2-2).floor] + "..." + self[(real_length - len/2+2) .. (real_length)] end end |
#labbrev(len) ⇒ Object
gives an abbreviated form of the string, showing as much of the beginning as possible
"the quick brown dog".labbrev 12 # => "the quick ..."
69 70 71 72 73 74 75 |
# File 'lib/sitefuel/extensions/StringFormatting.rb', line 69 def labbrev(len) if uncolored.length <= len self else self[0..(len-4)] + '...' end end |
#rabbrev(len) ⇒ Object
gives an abbreviated form of the string, showing as much of the end as possible
"the quick brown dog".rabbrev 12 # => "...brown dog"
82 83 84 85 86 87 88 89 |
# File 'lib/sitefuel/extensions/StringFormatting.rb', line 82 def rabbrev(len) real_length = uncolored.length if real_length <= len self else '...' + self[(real_length - (len-3)) .. real_length] end end |
#visual_center(width, padding = " ") ⇒ Object
like #center but uses #visual_length for proper ANSI sequence handling
46 47 48 |
# File 'lib/sitefuel/extensions/StringFormatting.rb', line 46 def visual_center(width, padding=" ") center(width + visual_length_delta, padding) end |
#visual_length ⇒ Object
gives the apparent length of the string (ignoring ASCII sequences)
22 23 24 |
# File 'lib/sitefuel/extensions/StringFormatting.rb', line 22 def visual_length uncolored.length end |
#visual_length_delta ⇒ Object
gives the difference between the actual and apparent length of the string
28 29 30 |
# File 'lib/sitefuel/extensions/StringFormatting.rb', line 28 def visual_length_delta length - visual_length end |
#visual_ljust(width, padding = " ") ⇒ Object
like #ljust but uses #visual_length for proper ANSI sequence handling
34 35 36 |
# File 'lib/sitefuel/extensions/StringFormatting.rb', line 34 def visual_ljust(width, padding=" ") ljust(width + visual_length_delta, padding) end |
#visual_rjust(width, padding = " ") ⇒ Object
like #rjust but uses #visual_length for proper ANSI sequence handling
40 41 42 |
# File 'lib/sitefuel/extensions/StringFormatting.rb', line 40 def visual_rjust(width, padding=" ") rjust(width + visual_length_delta, padding) end |