Line.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibVT/Line.h>
  7. namespace VT {
  8. Line::Line(size_t length)
  9. {
  10. set_length(length);
  11. }
  12. Line::~Line()
  13. {
  14. }
  15. void Line::rewrap(size_t new_length, Line* next_line, CursorPosition* cursor, bool cursor_is_on_next_line)
  16. {
  17. size_t old_length = length();
  18. if (old_length == new_length)
  19. return;
  20. // Drop the empty cells
  21. if (m_terminated_at.has_value() && m_cells.size() > m_terminated_at.value())
  22. m_cells.remove(m_terminated_at.value(), m_cells.size() - m_terminated_at.value());
  23. if (!next_line)
  24. return m_cells.resize(new_length);
  25. if (old_length < new_length)
  26. take_cells_from_next_line(new_length, next_line, cursor_is_on_next_line, cursor);
  27. else
  28. push_cells_into_next_line(new_length, next_line, cursor_is_on_next_line, cursor);
  29. }
  30. void Line::set_length(size_t new_length)
  31. {
  32. m_cells.resize(new_length);
  33. }
  34. void Line::push_cells_into_next_line(size_t new_length, Line* next_line, bool cursor_is_on_next_line, CursorPosition* cursor)
  35. {
  36. if (is_empty())
  37. return;
  38. if (length() <= new_length)
  39. return;
  40. // Push as many cells as _wouldn't_ fit into the next line.
  41. auto cells_to_preserve = !next_line->m_terminated_at.has_value() && next_line->is_empty() ? 0 : m_terminated_at.value_or(0);
  42. auto preserved_cells = max(new_length, cells_to_preserve);
  43. auto cells_to_push_into_next_line = length() - preserved_cells;
  44. if (!cells_to_push_into_next_line)
  45. return;
  46. if (next_line->m_terminated_at.has_value())
  47. next_line->m_terminated_at = next_line->m_terminated_at.value() + cells_to_push_into_next_line;
  48. if (m_terminated_at.has_value() && cells_to_preserve == 0) {
  49. m_terminated_at.clear();
  50. if (!next_line->m_terminated_at.has_value())
  51. next_line->m_terminated_at = cells_to_push_into_next_line;
  52. }
  53. if (cursor) {
  54. if (cursor_is_on_next_line) {
  55. cursor->column += cells_to_push_into_next_line;
  56. } else if (cursor->column >= preserved_cells) {
  57. cursor->row++;
  58. cursor->column = cursor->column - preserved_cells;
  59. }
  60. }
  61. next_line->m_cells.prepend(m_cells.span().slice_from_end(cells_to_push_into_next_line).data(), cells_to_push_into_next_line);
  62. m_cells.remove(m_cells.size() - cells_to_push_into_next_line, cells_to_push_into_next_line);
  63. if (m_terminated_at.has_value())
  64. m_terminated_at = m_terminated_at.value() - cells_to_push_into_next_line;
  65. }
  66. void Line::take_cells_from_next_line(size_t new_length, Line* next_line, bool cursor_is_on_next_line, CursorPosition* cursor)
  67. {
  68. // Take as many cells as would fit from the next line
  69. if (m_terminated_at.has_value())
  70. return;
  71. if (length() >= new_length)
  72. return;
  73. auto cells_to_grab_from_next_line = min(new_length - length(), next_line->length());
  74. auto clear_next_line = false;
  75. if (next_line->m_terminated_at.has_value()) {
  76. if (cells_to_grab_from_next_line == *next_line->m_terminated_at) {
  77. m_terminated_at = length() + *next_line->m_terminated_at;
  78. next_line->m_terminated_at.clear();
  79. clear_next_line = true;
  80. } else {
  81. next_line->m_terminated_at = next_line->m_terminated_at.value() - cells_to_grab_from_next_line;
  82. }
  83. }
  84. if (cells_to_grab_from_next_line) {
  85. if (cursor && cursor_is_on_next_line) {
  86. if (cursor->column <= cells_to_grab_from_next_line) {
  87. cursor->row--;
  88. cursor->column += m_cells.size();
  89. } else {
  90. cursor->column -= cells_to_grab_from_next_line;
  91. }
  92. }
  93. m_cells.append(next_line->m_cells.data(), cells_to_grab_from_next_line);
  94. next_line->m_cells.remove(0, cells_to_grab_from_next_line);
  95. }
  96. if (clear_next_line)
  97. next_line->m_cells.clear();
  98. }
  99. void Line::clear_range(size_t first_column, size_t last_column, const Attribute& attribute)
  100. {
  101. VERIFY(first_column <= last_column);
  102. VERIFY(last_column < m_cells.size());
  103. for (size_t i = first_column; i <= last_column; ++i) {
  104. auto& cell = m_cells[i];
  105. if (!m_dirty)
  106. m_dirty = cell.code_point != ' ' || cell.attribute != attribute;
  107. cell = Cell { .code_point = ' ', .attribute = attribute };
  108. }
  109. }
  110. bool Line::has_only_one_background_color() const
  111. {
  112. if (!length())
  113. return true;
  114. // FIXME: Cache this result?
  115. auto color = attribute_at(0).effective_background_color();
  116. for (size_t i = 1; i < length(); ++i) {
  117. if (attribute_at(i).effective_background_color() != color)
  118. return false;
  119. }
  120. return true;
  121. }
  122. }