screen size

main
Brett 2025-03-11 00:23:05 -04:00
parent 6bcb5ff2f5
commit 8c88c6296f
3 changed files with 48 additions and 9 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake) include(cmake/color.cmake)
set(BLT_VERSION 5.2.13) set(BLT_VERSION 5.2.14)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -52,6 +52,7 @@ namespace blt::logging
~status_bar_t() override; ~status_bar_t() override;
private: private:
i32 m_status_size; i32 m_status_size;
vec2i m_screen_size;
vec2i m_last_log_position; vec2i m_last_log_position;
vec2i m_begin_position; vec2i m_begin_position;
}; };

View File

@ -44,8 +44,10 @@ namespace blt::logging
if (isatty(fileno(stdin))) if (isatty(fileno(stdin)))
{ {
write(1, cmd, sizeof(cmd)); ssize_t i = write(1, cmd, sizeof(cmd));
read(0, buf, sizeof(buf)); (void) i;
i = read(0, buf, sizeof(buf));
(void) i;
int sep = 0; int sep = 0;
int end = 0; int end = 0;
@ -61,7 +63,6 @@ namespace blt::logging
} }
row = std::stoi(std::string(buf + 2, buf + sep)); row = std::stoi(std::string(buf + 2, buf + sep));
col = std::stoi(std::string(buf + sep + 1, buf + end)); col = std::stoi(std::string(buf + sep + 1, buf + end));
// printf("{Row: %d, Col: %d}", row, col);
} }
tcsetattr(0,TCSANOW, &save); tcsetattr(0,TCSANOW, &save);
@ -69,17 +70,54 @@ namespace blt::logging
return vec2i{row, col}; return vec2i{row, col};
} }
#define SIZE 100
vec2i get_screen_size() vec2i get_screen_size()
{ {
std::cout << ansi::cursor::move_to(9999, 9999); char in[SIZE] = "";
const auto pos = get_cursor_position(); int each = 0;
std::cout << ansi::cursor::lower_left_corner; int ch = 0;
std::cout << " "; int rows = 0;
return pos; int cols = 0;
termios original, changed;
// change terminal settings
tcgetattr( STDIN_FILENO, &original);
changed = original;
changed.c_lflag &= ~( ICANON | ECHO);
changed.c_cc[VMIN] = 1;
changed.c_cc[VTIME] = 0;
tcsetattr( STDIN_FILENO, TCSANOW, &changed);
// printf ( "\033[2J"); //clear screen
printf ( "\033[9999;9999H"); // cursor should move as far as it can
printf ( "\033[6n"); // ask for cursor position
while ( ( ch = getchar ()) != 'R') { // R terminates the response
if ( EOF == ch) {
break;
}
if ( isprint ( ch)) {
if ( each + 1 < SIZE) {
in[each] = ch;
each++;
in[each] = '\0';
}
}
}
printf ( "\033[1;1H"); // move to upper left corner
if ( 2 == sscanf ( in, "[%d;%d", &rows, &cols)) {
tcsetattr( STDIN_FILENO, TCSANOW, &original);
return {rows, cols};
}
throw std::runtime_error("Could not get screen size");
} }
status_bar_t::status_bar_t(const i32 status_size): m_status_size(status_size) status_bar_t::status_bar_t(const i32 status_size): m_status_size(status_size)
{ {
m_screen_size = get_screen_size();
std::cout << ansi::cursor::home << std::flush; std::cout << ansi::cursor::home << std::flush;
std::cout << ansi::erase::entire_screen << std::flush; std::cout << ansi::erase::entire_screen << std::flush;
m_begin_position = m_last_log_position = get_cursor_position(); m_begin_position = m_last_log_position = get_cursor_position();