# ljust and %-8s count code points. A terminal counts columns, and the two
# agree only for text like "tea".
require "reline"

rows = [
  ["tea", 3],
  ["caf\u{e9}", 4],
  ["cafe\u{301}", 4],
  ["\u{7DD1}\u{8336}", 5],
  ["\u{1F375}", 6],
]

puts "ljust(8):"
rows.each { |name, price| puts "  #{name.ljust(8)}|#{price}" }
puts
puts "format(\"%-8s|%d\"):"
rows.each { |name, price| puts "  " + format("%-8s|%d", name, price) }
puts
puts "Three counts of the same five names:"
rows.each do |name, _|
  columns = Reline::Unicode.calculate_width(name)
  puts "  #{name}#{" " * (8 - columns)} length #{name.length}, grapheme_clusters #{name.grapheme_clusters.size}, terminal columns #{columns}"
end
puts
puts "Padding by columns instead:"
rows.each do |name, price|
  pad = " " * [8 - Reline::Unicode.calculate_width(name), 0].max
  puts "  #{name}#{pad}|#{price}"
end
