Serenity Operating System
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
8#include <LibVT/Line.h>
9
10namespace VT {
11
12Line::Line(size_t length)
13{
14 set_length(length);
15}
16
17void Line::rewrap(size_t new_length, Line* next_line, CursorPosition* cursor, bool cursor_is_on_next_line)
18{
19 size_t old_length = length();
20 if (old_length == new_length)
21 return;
22
23 // Drop the empty cells
24 if (m_terminated_at.has_value() && m_cells.size() > m_terminated_at.value())
25 m_cells.remove(m_terminated_at.value(), m_cells.size() - m_terminated_at.value());
26
27 if (!next_line)
28 return set_length(new_length);
29
30 if (old_length < new_length)
31 take_cells_from_next_line(new_length, next_line, cursor_is_on_next_line, cursor);
32 else
33 push_cells_into_next_line(new_length, next_line, cursor_is_on_next_line, cursor);
34}
35
36void Line::set_length(size_t new_length)
37{
38 m_cells.resize(new_length);
39 if (m_terminated_at.has_value())
40 m_terminated_at = min(*m_terminated_at, new_length);
41}
42
43void Line::push_cells_into_next_line(size_t new_length, Line* next_line, bool cursor_is_on_next_line, CursorPosition* cursor)
44{
45 if (is_empty())
46 return;
47
48 if (length() <= new_length)
49 return;
50
51 // Push as many cells as _wouldn't_ fit into the next line.
52 auto cells_to_preserve = !next_line->m_terminated_at.has_value() && next_line->is_empty() ? 0 : m_terminated_at.value_or(0);
53 auto preserved_cells = max(new_length, cells_to_preserve);
54 auto cells_to_push_into_next_line = length() - preserved_cells;
55 if (!cells_to_push_into_next_line)
56 return;
57
58 if (next_line->m_terminated_at.has_value())
59 next_line->m_terminated_at = next_line->m_terminated_at.value() + cells_to_push_into_next_line;
60
61 if (m_terminated_at.has_value() && cells_to_preserve == 0) {
62 m_terminated_at.clear();
63 if (!next_line->m_terminated_at.has_value())
64 next_line->m_terminated_at = cells_to_push_into_next_line;
65 }
66
67 if (cursor) {
68 if (cursor_is_on_next_line) {
69 cursor->column += cells_to_push_into_next_line;
70 } else if (cursor->column >= preserved_cells) {
71 cursor->row++;
72 cursor->column = cursor->column - preserved_cells;
73 }
74 }
75
76 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));
77 m_cells.remove(m_cells.size() - cells_to_push_into_next_line, cells_to_push_into_next_line);
78 if (m_terminated_at.has_value())
79 m_terminated_at = m_terminated_at.value() - cells_to_push_into_next_line;
80}
81
82void Line::take_cells_from_next_line(size_t new_length, Line* next_line, bool cursor_is_on_next_line, CursorPosition* cursor)
83{
84 // Take as many cells as would fit from the next line
85 if (m_terminated_at.has_value())
86 return;
87
88 if (length() >= new_length)
89 return;
90
91 auto cells_to_grab_from_next_line = min(new_length - length(), next_line->length());
92 auto clear_next_line = false;
93 if (next_line->m_terminated_at.has_value()) {
94 if (cells_to_grab_from_next_line >= *next_line->m_terminated_at) {
95 m_terminated_at = length() + *next_line->m_terminated_at;
96 next_line->m_terminated_at.clear();
97 clear_next_line = true;
98 } else {
99 next_line->m_terminated_at = next_line->m_terminated_at.value() - cells_to_grab_from_next_line;
100 }
101 }
102
103 if (cells_to_grab_from_next_line) {
104 if (cursor && cursor_is_on_next_line) {
105 if (cursor->column <= cells_to_grab_from_next_line) {
106 cursor->row--;
107 cursor->column += m_cells.size();
108 } else {
109 cursor->column -= cells_to_grab_from_next_line;
110 }
111 }
112 MUST(m_cells.try_append(next_line->m_cells.data(), cells_to_grab_from_next_line));
113 next_line->m_cells.remove(0, cells_to_grab_from_next_line);
114 }
115
116 if (clear_next_line)
117 next_line->m_cells.clear();
118}
119
120void Line::clear_range(size_t first_column, size_t last_column, Attribute const& attribute)
121{
122 VERIFY(first_column <= last_column);
123 VERIFY(last_column < m_cells.size());
124 for (size_t i = first_column; i <= last_column; ++i) {
125 auto& cell = m_cells[i];
126 if (!m_dirty)
127 m_dirty = cell.code_point != ' ' || cell.attribute != attribute;
128 cell = Cell { .code_point = ' ', .attribute = attribute };
129 }
130}
131
132bool Line::has_only_one_background_color() const
133{
134 if (!length())
135 return true;
136 // FIXME: Cache this result?
137 auto color = attribute_at(0).effective_background_color();
138 for (size_t i = 1; i < length(); ++i) {
139 if (attribute_at(i).effective_background_color() != color)
140 return false;
141 }
142 return true;
143}
144
145}