class Reline::Unicode

def self.split_by_width(str, max_width, encoding = str.encoding)

def self.split_by_width(str, max_width, encoding = str.encoding)
  lines = [String.new(encoding: encoding)]
  height = 1
  width = 0
  rest = str.encode(Encoding::UTF_8)
  in_zero_width = false
  seq = String.new(encoding: encoding)
  rest.scan(WIDTH_SCANNER) do |non_printing_start, non_printing_end, csi, osc, gc|
    case
    when non_printing_start
      in_zero_width = true
      lines.last << NON_PRINTING_START
    when non_printing_end
      in_zero_width = false
      lines.last << NON_PRINTING_END
    when csi
      lines.last << csi
      seq << csi
    when osc
      lines.last << osc
      seq << osc
    when gc
      unless in_zero_width
        mbchar_width = get_mbchar_width(gc)
        if (width += mbchar_width) > max_width
          width = mbchar_width
          lines << nil
          lines << seq.dup
          height += 1
        end
      end
      lines.last << gc
    end
  end
  # The cursor moves to next line in first
  if width == max_width
    lines << nil
    lines << String.new(encoding: encoding)
    height += 1
  end
  [lines, height]
end