2020-05-15 18:57:50 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-03-04 13:27:47 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-05-15 18:57:50 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-15 18:57:50 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-02-16 00:19:20 +02:00
|
|
|
#include <AK/AnyOf.h>
|
2020-05-15 18:57:50 +02:00
|
|
|
#include <AK/Noncopyable.h>
|
2021-02-26 20:28:22 +01:00
|
|
|
#include <AK/Vector.h>
|
2021-04-16 22:58:51 +03:00
|
|
|
#include <LibVT/Attribute.h>
|
2021-06-19 03:53:03 +04:30
|
|
|
#include <LibVT/Position.h>
|
2020-05-15 18:57:50 +02:00
|
|
|
#include <LibVT/XtermColors.h>
|
|
|
|
|
|
|
|
namespace VT {
|
|
|
|
|
|
|
|
class Line {
|
|
|
|
AK_MAKE_NONCOPYABLE(Line);
|
|
|
|
AK_MAKE_NONMOVABLE(Line);
|
|
|
|
|
|
|
|
public:
|
2021-02-26 20:28:22 +01:00
|
|
|
explicit Line(size_t length);
|
2022-03-04 13:27:47 -07:00
|
|
|
~Line() = default;
|
2020-05-15 18:57:50 +02:00
|
|
|
|
2021-02-26 20:28:22 +01:00
|
|
|
struct Cell {
|
2021-03-01 11:05:31 +01:00
|
|
|
u32 code_point { ' ' };
|
2021-02-26 20:28:22 +01:00
|
|
|
Attribute attribute;
|
2021-06-19 03:53:03 +04:30
|
|
|
|
|
|
|
bool operator!=(Cell const& other) const { return code_point != other.code_point || attribute != other.attribute; }
|
2021-02-26 20:28:22 +01:00
|
|
|
};
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Attribute const& attribute_at(size_t index) const { return m_cells[index].attribute; }
|
2021-02-26 20:28:22 +01:00
|
|
|
Attribute& attribute_at(size_t index) { return m_cells[index].attribute; }
|
|
|
|
|
|
|
|
Cell& cell_at(size_t index) { return m_cells[index]; }
|
2022-04-01 20:58:27 +03:00
|
|
|
Cell const& cell_at(size_t index) const { return m_cells[index]; }
|
2021-02-26 20:28:22 +01:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void clear(Attribute const& attribute = Attribute())
|
2021-06-05 11:12:00 +02:00
|
|
|
{
|
2021-06-19 18:17:18 +04:30
|
|
|
m_terminated_at.clear();
|
2021-06-05 11:12:00 +02:00
|
|
|
clear_range(0, m_cells.size() - 1, attribute);
|
|
|
|
}
|
2022-04-01 20:58:27 +03:00
|
|
|
void clear_range(size_t first_column, size_t last_column, Attribute const& attribute = Attribute());
|
2020-05-15 18:57:50 +02:00
|
|
|
bool has_only_one_background_color() const;
|
|
|
|
|
2021-06-19 03:53:03 +04:30
|
|
|
bool is_empty() const
|
|
|
|
{
|
2021-07-25 14:21:27 -06:00
|
|
|
return !any_of(m_cells, [](auto& cell) { return cell != Cell(); });
|
2021-06-19 03:53:03 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
size_t length() const
|
|
|
|
{
|
|
|
|
return m_cells.size();
|
|
|
|
}
|
2021-06-24 20:07:33 +04:30
|
|
|
void set_length(size_t);
|
|
|
|
void rewrap(size_t new_length, Line* next_line, CursorPosition* cursor, bool cursor_is_on_next_line = true);
|
2020-05-15 18:57:50 +02:00
|
|
|
|
2020-08-05 16:31:20 -04:00
|
|
|
u32 code_point(size_t index) const
|
2020-05-17 11:32:31 +02:00
|
|
|
{
|
2021-02-26 20:28:22 +01:00
|
|
|
return m_cells[index].code_point;
|
2020-05-17 11:32:31 +02:00
|
|
|
}
|
|
|
|
|
2020-08-05 16:31:20 -04:00
|
|
|
void set_code_point(size_t index, u32 code_point)
|
2020-05-17 11:32:31 +02:00
|
|
|
{
|
2021-06-19 18:17:18 +04:30
|
|
|
if (m_terminated_at.has_value()) {
|
|
|
|
if (index > *m_terminated_at) {
|
|
|
|
m_terminated_at = index + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 20:28:22 +01:00
|
|
|
m_cells[index].code_point = code_point;
|
2020-05-17 11:32:31 +02:00
|
|
|
}
|
2020-05-15 18:57:50 +02:00
|
|
|
|
|
|
|
bool is_dirty() const { return m_dirty; }
|
|
|
|
void set_dirty(bool b) { m_dirty = b; }
|
|
|
|
|
2021-06-19 18:17:18 +04:30
|
|
|
Optional<u16> termination_column() const { return m_terminated_at; }
|
|
|
|
void set_terminated(u16 column) { m_terminated_at = column; }
|
|
|
|
|
2020-05-15 18:57:50 +02:00
|
|
|
private:
|
2021-06-24 20:07:33 +04:30
|
|
|
void take_cells_from_next_line(size_t new_length, Line* next_line, bool cursor_is_on_next_line, CursorPosition* cursor);
|
|
|
|
void push_cells_into_next_line(size_t new_length, Line* next_line, bool cursor_is_on_next_line, CursorPosition* cursor);
|
2021-06-19 03:53:03 +04:30
|
|
|
|
2021-02-26 20:28:22 +01:00
|
|
|
Vector<Cell> m_cells;
|
2020-05-15 18:57:50 +02:00
|
|
|
bool m_dirty { false };
|
2021-06-19 18:17:18 +04:30
|
|
|
// Note: The alignment is 8, so this member lives in the padding (that already existed before it was introduced)
|
|
|
|
[[no_unique_address]] Optional<u16> m_terminated_at;
|
2020-05-15 18:57:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|