1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#include "arch/vga/text/buffer.hpp"
#include "arch/vga/text/attribute.hpp"
#include <algorithm>
#include <bit>
#include <cstddef>
#include <span>
#include <string_view>
#include <utility>
namespace arch::vga::text
{
buffer::buffer(std::size_t width, std::size_t height, cell * start, std::size_t position)
: m_width{width}
, m_height{height}
, m_buffer{start, m_width * m_height}
, m_position{position}
{}
auto buffer::clear() -> void
{
m_position = 0;
std::ranges::fill(m_buffer, std::pair{'\0', static_cast<std::byte>(0x00)});
}
auto buffer::write(std::string_view code_points, attribute attribute) -> void
{
std::ranges::for_each(code_points, [&](auto code_point) -> void { write(code_point, attribute); });
}
auto buffer::write(char code_point, attribute attribute) -> void
{
if (m_position + 1 > m_height * m_width)
{
scroll();
}
if (!handle_special_code_point(code_point, attribute))
{
do_write(code_point, attribute);
}
};
auto buffer::newline() -> void
{
auto free_glyphs_in_line = m_width - column();
m_position += free_glyphs_in_line;
}
auto buffer::scroll(std::size_t nof_lines) -> void
{
auto scroll_count = std::min(nof_lines, m_height);
auto scroll_start = m_buffer.begin() + (scroll_count * m_width);
std::ranges::move(scroll_start, m_buffer.end(), m_buffer.begin());
auto clear_start = m_buffer.begin() + (m_height - scroll_count) * m_width;
std::ranges::fill(clear_start, m_buffer.end(), cell{});
m_position = (line() - scroll_count) * m_width;
}
auto buffer::column() const noexcept -> std::ptrdiff_t
{
return m_position % m_width;
}
auto buffer::line() const noexcept -> std::ptrdiff_t
{
return m_position / m_width;
}
auto buffer::handle_special_code_point(char code_point, attribute attribute) -> bool
{
switch (code_point)
{
case '\n':
newline();
return true;
case '\r':
m_position -= column();
return true;
case '\t':
do_write(" ", attribute);
return true;
default:
return false;
}
}
auto buffer::do_write(std::string_view code_points, attribute attribute) -> void
{
std::ranges::for_each(code_points, [&](auto code_point) -> void { do_write(code_point, attribute); });
}
auto buffer::do_write(char code_point, attribute attribute) -> void
{
m_buffer[m_position++] = std::pair{code_point, std::bit_cast<std::byte>(attribute)};
}
} // namespace arch::vga::text
|