Xenith/xenith/ui/tui.cpp
2026-10-01 17:53:20 +07:00

150 lines
3.7 KiB
C++

#include "tui.h"
#include <iostream>
namespace ui
{
Tui::Tui()
{
ConsoleSize cs = get_console_size();
int panel_w = (cs.width / 100) * 27;
std::cout << "Ширина (колонок): " << cs.width << "\n";
std::cout << "Высота (строк): " << cs.height << "\n";
clear();
hide_cursor();
set_foreground(Color::White);
apply_color();
draw_border((cs.width / 100) * 27, cs.height, border_clip);
draw_border(cs.width - ((cs.width / 100) * 27), cs.height, border_clip, (cs.width / 100) * 27);
draw_button(2, 2, panel_w - 2, 3, "Chat", border_clip);
draw_button(2, 2 + 3, panel_w - 2, 3, "Models", border_clip);
draw_button(2, 2 + 6, panel_w - 2, 3, "Settings", border_clip);
draw_button(2, 2 + 9, panel_w - 2, 3, "Server", border_clip);
//std::cout << "\033[?1000h\033[?1002h\033[?1006h" << std::flush;
}
Tui::~Tui()
= default;
void Tui::update()
{
}
bool Tui::is_running() const
{
return running;
}
Tui::ConsoleSize Tui::get_console_size()
{
ConsoleSize size;
winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0) {
size.width = w.ws_col;
size.height = w.ws_row;
}
return size;
}
void Tui::apply_color() const
{
std::cout << "\033["<< foreground << ";" << background <<"m" << std::flush;
}
void Tui::move_cursor(int x, int y)
{
std::cout << "\033[" << y << ";" << x << "H" << std::flush;
}
void Tui::clear()
{
std::cout << "\033c" << std::flush;
}
void Tui::draw_border(int w, int h, border_charset charset, int x, int y)
{
move_cursor(x, y);
std::cout << charset.top_left;
for (int q = 0; q < w - 2; q++) std::cout << charset.top;
std::cout << charset.top_right <<"\n";
move_cursor_x(x);
for (int i = 0; i < h - 2; i++)
{
std::cout << charset.vertical;
for (int q = 0; q < w - 2; q++) std::cout << charset.bg;
std::cout << charset.vertical;
std::cout << "\n";
move_cursor_x(x);
}
std::cout << charset.bottom_left;
for (int q = 0; q < w - 2; q++) std::cout << charset.bottom;
std::cout << charset.bottom_right << std::flush;
}
void Tui::hide_cursor()
{
std::cout << "\033[?25l" << std::flush;
}
void Tui::show_cursor()
{
std::cout << "\033[?25h" << std::flush;
}
void Tui::set_background(Color clr)
{
background = static_cast<int>(clr);
}
void Tui::set_foreground(Color clr)
{
foreground = static_cast<int>(clr);
}
void Tui::move_cursor_right(int n)
{
if (n > 0) {
std::cout << "\033[" << n << "C" << std::flush;
}
}
void Tui::move_cursor_x(int col)
{
if (col > 0) {
std::cout << "\033[" << col << "G" << std::flush;
}
}
void Tui::move_cursor_y(int row)
{
if (row > 0) {
std::cout << "\033[" << row << "d" << std::flush;
}
}
void Tui::invert_color()
{
std::cout << "\033[7m" << std::flush;
}
void Tui::draw_button(int x, int y, int w, int h, std::string_view label, border_charset charset)
{
draw_border(w, h, charset, x, y);
int text_y = y + (h / 2);
int text_x = x + ((w - static_cast<int>(label.length())) / 2);
move_cursor(text_x, text_y);
std::cout << label;
std::cout << std::flush;
}
void Tui::invert_color_off()
{
std::cout << "\033[27m" << std::flush;
}
}