class String
Public Instance Methods
adjusted_size(has_invisible)
click to toggle source
# File lib/solvebio/tabulate.rb, line 542 def adjusted_size(has_invisible) return has_invisible ? self.strip_invisible.size : self.size end
afterpoint()
click to toggle source
Symbols after a decimal point, -1 if the string lacks the decimal point.
"123.45".afterpoint => 2 "1001".afterpoint => -1 "eggs".afterpoint => -1 "123e45".afterpoint => 2
# File lib/solvebio/tabulate.rb, line 524 def afterpoint if self.number? if self.int? return -1 else pos = self.rindex('.') || -1 pos = self.downcase().rindex('e') if pos < 0 if pos >= 0 return self.size - pos - 1 else return -1 # no point end end else return -1 # not a number end end
padboth(width, has_invisible=true)
click to toggle source
Center string with uneven space on the right
'\u044f\u0439\u0446\u0430'.padboth(6) => ' \u044f\u0439\u0446\u0430 ' 'abc'.padboth(2) => 'abc' 'abc'.padboth(6) => ' abc '
# File lib/solvebio/tabulate.rb, line 584 def padboth(width, has_invisible=true) s_width = self.adjusted_size(has_invisible) return self if s_width >= width pad_size = width - s_width pad_left = ' ' * (pad_size/2) pad_right = ' ' * ((pad_size + 1)/ 2) pad_left + self + pad_right end
padleft(width, has_invisible=true)
click to toggle source
Flush right.
'\u044f\u0439\u0446\u0430'.padleft(6) => ' \u044f\u0439\u0446\u0430' 'abc'.padleft(2) => 'abc'
# File lib/solvebio/tabulate.rb, line 564 def padleft(width, has_invisible=true) s_width = self.adjusted_size(has_invisible) s_width < width ? (' ' * (width - s_width)) + self : self end
padright(width, has_invisible=true)
click to toggle source
Flush left.
padright(6, '\u044f\u0439\u0446\u0430') => '\u044f\u0439\u0446\u0430 ' padright(2, 'abc') => 'abc'
# File lib/solvebio/tabulate.rb, line 573 def padright(width, has_invisible=true) s_width = self.adjusted_size(has_invisible) s_width < width ? self + (' ' * (width - s_width)) : self end
strip_invisible()
click to toggle source
Remove invisible ANSI color codes.
# File lib/solvebio/tabulate.rb, line 594 def strip_invisible return self.gsub(SolveBio::Tabulate::INVISIBILE_CODES, '') end
visible_width()
click to toggle source
Visible width of a printed string. ANSI color codes are removed.
['\x1b[31mhello\x1b[0m' "world"].map{|s| s.visible_width} => [5, 5]
# File lib/solvebio/tabulate.rb, line 550 def visible_width # if self.kind_of?(_text_type) or self.kind_of?(_binary_type) return self.strip_invisible.size # else # return _text_type(s).size # end end