Line.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibVT/Line.h>
  8. namespace VT {
  9. Line::Line(size_t length)
  10. {
  11. set_length(length);
  12. }
  13. void Line::rewrap(size_t new_length, Line* next_line, CursorPosition* cursor, bool cursor_is_on_next_line)
  14. {
  15. size_t old_length = length();
  16. if (old_length == new_length)
  17. return;
  18. // Drop the empty cells
  19. if (m_terminated_at.has_value() && m_cells.size() > m_terminated_at.value())
  20. m_cells.remove(m_terminated_at.value(), m_cells.size() - m_terminated_at.value());
  21. if (!next_line)
  22. return set_length(new_length);
  23. if (old_length < new_length)
  24. take_cells_from_next_line(new_length, next_line, cursor_is_on_next_line, cursor);
  25. else
  26. push_cells_into_next_line(new_length, next_line, cursor_is_on_next_line, cursor);
  27. }
  28. void Line::set_length(size_t new_length)
  29. {
  30. m_cells.resize(new_length);
  31. if (m_terminated_at.has_value())
  32. m_terminated_at = min(*m_terminated_at, 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. MUST(next_line->m_cells.try_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. MUST(m_cells.try_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, Attribute const& 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. }