aboutsummaryrefslogtreecommitdiff
path: root/src/engine/graphics/statusline.cpp
blob: 3968faeac81589c848589f6ee25735692a99b769 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "statusline.hpp"

#include "engine/escape.hpp"
#include "util/color.hpp"

#include <string>
#include <utility>

StatusLine::StatusLine(
	std::shared_ptr<ICursorController> cursor_controller,
	std::shared_ptr<IWindow> window) noexcept
	: _cursor_controller(std::move(cursor_controller)), _window(std::move(window))
{
}

void StatusLine::initialize_background() noexcept
{
	const auto previous_position = _move_to_statusline(0);

	auto background_color = get_background_esc_seq(STATUSBAR_COLOR);

	fmt::print("{}{}", background_color, std::string(_window->size().get_width(), ' '));
	fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC));

	_move_back(previous_position);
}

void StatusLine::set_status(
	const StatusLineSection &section,
	const std::string_view &status,
	std::size_t start) noexcept
{
	_clear_section(section, start);

	auto section_start = _get_section_start_x(section);

	const auto previous_position =
		_move_to_statusline(section_start + static_cast<int32_t>(start));

	auto background_color = get_background_esc_seq(STATUSBAR_COLOR);

	auto section_length = _sections_lengths[section];

	fmt::print("{}{}", background_color, status);
	fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC));

	_move_back(previous_position);
}

void StatusLine::set_section_length(
	const StatusLineSection &section,
	uint32_t length) noexcept
{
	_sections_lengths[section] = length;
}

auto StatusLine::_move_to_statusline(int32_t x) noexcept -> Vector2
{
	const auto previous_position = _cursor_controller->where();

	const auto window_size = _window->size();

	_cursor_controller->hide();

	auto window_height = static_cast<Vector2::Value>(window_size.get_height());

	_cursor_controller->move_to(Vector2({.x = x, .y = window_height}), true);

	return previous_position;
}

void StatusLine::_move_back(Vector2 previous_position) noexcept
{
	_cursor_controller->move_to(previous_position, true);
	_cursor_controller->show();
}

auto StatusLine::_get_section_start_x(const StatusLineSection &section) const noexcept
	-> int32_t
{
	int32_t section_start = 0;

	auto section_index = static_cast<int32_t>(section);

	while (section_index > 0)
	{
		section_start += static_cast<int32_t>(
			_sections_lengths.at(StatusLineSection(section_index - 1)));

		section_index--;
	}

	return section_start;
}

void StatusLine::_clear_section(
	const StatusLineSection &section,
	std::size_t start) noexcept
{
	auto section_start = _get_section_start_x(section);

	auto start_int32 = static_cast<int32_t>(start);

	const auto previous_position = _move_to_statusline(section_start + start_int32);

	auto background_color = get_background_esc_seq(STATUSBAR_COLOR);

	auto section_length = _sections_lengths[section];

	auto start_uint32 = static_cast<uint32_t>(start);

	fmt::print("{}{}", background_color, std::string(section_length - start_uint32, ' '));
	fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC));

	_move_back(previous_position);
}