Sha256: edb7eccc5e1f1af91acadbace268bc4f2614518160280681a055af1a69ce0412

Contents?: true

Size: 609 Bytes

Versions: 4

Compression:

Stored size: 609 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

4 entries across 4 versions & 1 rubygems

Version Path
git-trend-0.0.7 lib/git_trend/ext/string.rb
git-trend-0.0.6 lib/git_trend/ext/string.rb
git-trend-0.0.5 lib/git_trend/ext/string.rb
git-trend-0.0.4 lib/git_trend/ext/string.rb