Sha256: 8c34e4bd76577d7d8cdbe4cd07cb45c21af0ba15c5821716a0ab52cf656b2b33
Contents?: true
Size: 610 Bytes
Versions: 9
Compression:
Stored size: 610 Bytes
Contents
class String def mb_width each_char.inject(0) { |sum, c| sum += c.ascii_only? ? 1 : 2 } end def mb_slice(width) return '' if empty? max_size = width - 3 # 3 is '...' size. extraction_size = 0 extraction = '' each_char do |c| char_size = c.ascii_only? ? 1 : 2 if extraction_size + char_size > max_size extraction << '...' break end extraction_size += char_size extraction << c end extraction end def mb_ljust(width, padding = ' ') padding_size = [0, width - mb_width].max self + padding * padding_size end end
Version data entries
9 entries across 9 versions & 1 rubygems