class String

Constants

CHARS_OF_WIDTH_OF_0
CHARS_OF_WIDTH_OF_1
CHARS_OF_WIDTH_OF_2
CHAR_CODES_OF_WIDTH_0
CHAR_CODES_OF_WIDTH_1
MULTI_CHAR_OF_WIDTH_1
MULTI_CHAR_OF_WIDTH_2

Public Instance Methods

tljust(width) click to toggle source
# File lib/terminal/table.rb, line 61
def tljust(width)
  if width > twidth
    self + ' ' * (width - twidth)
  else
    self
  end
end
twidth() click to toggle source
# File lib/terminal/table.rb, line 18
def twidth
  result = 0

  MULTI_CHAR_OF_WIDTH_1.each do |c|
    if include?(c)
      result += 1 * scan(c).size
      gsub!(c, '')
    end
  end

  MULTI_CHAR_OF_WIDTH_2.each do |c|
    if include?(c)
      result += 2 * scan(c).size
      gsub!(c, '')
    end
  end

  chars.inject(result) do |result, c|
    if c.ord <= 126
      result += 1
    elsif CHAR_CODES_OF_WIDTH_0.find { |code| c.ord.to_s == code }
      # zero width
      result += 0
    elsif CHAR_CODES_OF_WIDTH_1.find { |code| c.ord.to_s == code }
      # zero width
      result += 1
    elsif CHARS_OF_WIDTH_OF_0.include?(c)
      # zero width
      result += 0
    elsif CHARS_OF_WIDTH_OF_1.include?(c)
      result += 1
    elsif CHARS_OF_WIDTH_OF_2.include?(c)
      result += 2
    elsif c == ' ' # ord == 8198
      result += 1
    elsif Emoji.find_by_unicode(c)
      result += 1
    else
      result += 2
    end
  end
end