You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.8 KiB

3 months ago
  1. require_relative 'ansi_colors'
  2. module Semantic
  3. # AnsiWrapper cares about Ansi Colour Code \e[...
  4. class AnsiWrapper
  5. include AnsiColors
  6. TAB_TO_SPACES = 2
  7. def self.wrap(text, length, prefix = '', continuation = prefix)
  8. if visible_length(prefix) != visible_length(continuation)
  9. raise "continuation <#{continuation.inspect}> should have the same length as prefix <#{prefix.inspect}>"
  10. end
  11. return unless text
  12. text = text.gsub("\t", ' ' * TAB_TO_SPACES)
  13. lines = split_text_to_lines(text, length - visible_length(prefix))
  14. lines = inject_continuation_and_ansi_colors_to_lines(lines, prefix, continuation)
  15. lines.join("\n")
  16. end
  17. private_class_method def self.inject_continuation_and_ansi_colors_to_lines(lines, prefix, continuation)
  18. last_ansi = ''
  19. lines.each_with_index.map do |line, index|
  20. current = index.zero? ? prefix : continuation
  21. current += last_ansi unless last_ansi.empty? || last_ansi == CLEAR
  22. current += line
  23. last_ansi = scan_for_actual_ansi(line, last_ansi)
  24. current += CLEAR if last_ansi.empty? || last_ansi != CLEAR
  25. current
  26. end
  27. end
  28. private_class_method def self.scan_for_actual_ansi(line, last_ansi)
  29. line.scan(ANSI_REGEX).each do |match|
  30. ansi_code = match.to_s
  31. if ansi_code == CLEAR
  32. last_ansi = CLEAR
  33. else
  34. last_ansi += ansi_code
  35. end
  36. end
  37. last_ansi
  38. end
  39. private_class_method def self.split_text_to_lines(text, length)
  40. lines = text.split("\n")
  41. sublines = lines.map do |line|
  42. visible_length(line) > length ? visible_split(line, length) : [line]
  43. end
  44. sublines.flatten
  45. end
  46. private_class_method def self.visible_length(line)
  47. raise 'line should not contain carriage return character!' if line.match "\n"
  48. ansi_code_length = line.scan(ANSI_REGEX).map(&:length).sum
  49. line.length - ansi_code_length
  50. end
  51. # TODO: might be refactored with less complexity
  52. private_class_method def self.visible_split(line, length, stack = '') # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  53. before, ansi_code, after = line.partition(ANSI_REGEX)
  54. stack_length = visible_length(stack)
  55. visible_length = before.length + stack_length
  56. if visible_length == length
  57. ["#{stack}#{before}#{ansi_code}"] + visible_split(after, length)
  58. elsif visible_length > length
  59. first_line = stack + before[0...length - stack_length]
  60. tail = before[length - stack_length..] + ansi_code + after
  61. [first_line] + visible_split(tail, length)
  62. elsif ansi_code.length.positive?
  63. visible_split(after, length, "#{stack}#{before}#{ansi_code}")
  64. else
  65. ["#{stack}#{before}#{ansi_code}"]
  66. end
  67. end
  68. end
  69. end