^l to refresh screen
This commit is contained in:
parent
7a50220e3f
commit
b956854826
7 changed files with 106 additions and 17 deletions
|
@ -27,8 +27,8 @@ add_executable(
|
|||
include(CheckIPOSupported)
|
||||
check_ipo_supported(RESULT supported)
|
||||
|
||||
#set(CMAKE_BUILD_TYPE RelWithDebInfo)
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
set(CMAKE_BUILD_TYPE RelWithDebInfo)
|
||||
#set(CMAKE_BUILD_TYPE Debug)
|
||||
|
||||
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
|
||||
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
|
||||
|
|
90
console.cpp
90
console.cpp
|
@ -1,9 +1,17 @@
|
|||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
|
||||
#include "console.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
console::console(std::atomic_bool *const terminate) :
|
||||
terminate(terminate)
|
||||
{
|
||||
memset(screen_buffer, ' ', sizeof screen_buffer);
|
||||
|
||||
th = new std::thread(std::ref(*this));
|
||||
}
|
||||
|
||||
|
@ -16,32 +24,100 @@ console::~console()
|
|||
|
||||
bool console::poll_char()
|
||||
{
|
||||
return buffer.empty() == false;
|
||||
return input_buffer.empty() == false;
|
||||
}
|
||||
|
||||
uint8_t console::get_char()
|
||||
{
|
||||
if (buffer.empty())
|
||||
if (input_buffer.empty())
|
||||
return 0x00;
|
||||
|
||||
char c = buffer.at(0);
|
||||
char c = input_buffer.at(0);
|
||||
|
||||
buffer.erase(buffer.begin() + 0);
|
||||
input_buffer.erase(input_buffer.begin() + 0);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
void console::debug(const std::string fmt, ...)
|
||||
{
|
||||
char *buffer = nullptr;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
|
||||
int len = vasprintf(&buffer, fmt.c_str(), ap);
|
||||
|
||||
va_end(ap);
|
||||
|
||||
for(int i=0; i<len; i++)
|
||||
put_char(buffer[i]);
|
||||
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void console::put_char(const char c)
|
||||
{
|
||||
put_char_ll(c);
|
||||
|
||||
if (c == 13)
|
||||
tx = 0;
|
||||
else if (c == 10)
|
||||
ty++;
|
||||
else {
|
||||
screen_buffer[ty][tx++] = c;
|
||||
|
||||
if (tx == t_width) {
|
||||
tx = 0;
|
||||
|
||||
ty++;
|
||||
}
|
||||
}
|
||||
|
||||
if (ty == t_height) {
|
||||
memmove(&screen_buffer[0], &screen_buffer[1], sizeof(char) * t_width * (t_height - 1));
|
||||
|
||||
ty--;
|
||||
|
||||
memset(screen_buffer[t_height - 1], ' ', t_width);
|
||||
}
|
||||
}
|
||||
|
||||
void console::put_string_ll(const std::string & what)
|
||||
{
|
||||
for(int x=0; x<what.size(); x++)
|
||||
put_char_ll(what.at(x));
|
||||
}
|
||||
|
||||
void console::operator()()
|
||||
{
|
||||
debug("Console thread started");
|
||||
|
||||
while(!*terminate) {
|
||||
int c = wait_for_char(500);
|
||||
|
||||
if (c == -1)
|
||||
continue;
|
||||
|
||||
if (c == 3)
|
||||
if (c == 3) // ^c
|
||||
*terminate = true;
|
||||
else
|
||||
buffer.push_back(c);
|
||||
else if (c == 12) { // ^l
|
||||
put_string_ll(format("\033[2J"));
|
||||
|
||||
fprintf(stderr, "%d %d\n", tx, ty);
|
||||
|
||||
for(int row=0; row<t_height; row++) {
|
||||
put_string_ll(format("\033[%dH", row + 1));
|
||||
|
||||
put_string_ll(std::string(screen_buffer[row], t_width));
|
||||
}
|
||||
|
||||
put_string_ll(format("\033[%d;%dH", ty + 1, tx + 1));
|
||||
}
|
||||
else {
|
||||
input_buffer.push_back(c);
|
||||
}
|
||||
}
|
||||
|
||||
debug("Console thread terminating");
|
||||
}
|
||||
|
|
17
console.h
17
console.h
|
@ -5,6 +5,9 @@
|
|||
#include <vector>
|
||||
|
||||
|
||||
constexpr const int t_width { 80 };
|
||||
constexpr const int t_height { 25 };
|
||||
|
||||
class console
|
||||
{
|
||||
private:
|
||||
|
@ -12,11 +15,19 @@ private:
|
|||
|
||||
std::thread *th { nullptr };
|
||||
|
||||
std::vector<char> buffer;
|
||||
std::vector<char> input_buffer;
|
||||
|
||||
char screen_buffer[t_height][t_width];
|
||||
uint8_t tx { 0 };
|
||||
uint8_t ty { 0 };
|
||||
|
||||
protected:
|
||||
virtual int wait_for_char(const int timeout) = 0;
|
||||
|
||||
virtual void put_char_ll(const char c) = 0;
|
||||
|
||||
void put_string_ll(const std::string & what);
|
||||
|
||||
public:
|
||||
console(std::atomic_bool *const terminate);
|
||||
virtual ~console();
|
||||
|
@ -25,7 +36,9 @@ public:
|
|||
|
||||
uint8_t get_char();
|
||||
|
||||
virtual void put_char(const char c) = 0;
|
||||
void put_char(const char c);
|
||||
|
||||
void debug(const std::string fmt, ...);
|
||||
|
||||
virtual void resize_terminal() = 0;
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ int console_ncurses::wait_for_char(const int timeout)
|
|||
return -1;
|
||||
}
|
||||
|
||||
void console_ncurses::put_char(const char c)
|
||||
void console_ncurses::put_char_ll(const char c)
|
||||
{
|
||||
if (c >= 32 || (c != 12 && c != 27 && c != 13)) {
|
||||
wprintw(w_main->win, "%c", c);
|
||||
|
|
|
@ -13,11 +13,11 @@ private:
|
|||
protected:
|
||||
int wait_for_char(const int timeout) override;
|
||||
|
||||
void put_char_ll(const char c) override;
|
||||
|
||||
public:
|
||||
console_ncurses(std::atomic_bool *const terminate);
|
||||
virtual ~console_ncurses();
|
||||
|
||||
void put_char(const char c) override;
|
||||
|
||||
void resize_terminal() override;
|
||||
};
|
||||
|
|
|
@ -35,7 +35,7 @@ int console_posix::wait_for_char(const int timeout)
|
|||
return -1;
|
||||
}
|
||||
|
||||
void console_posix::put_char(const char c)
|
||||
void console_posix::put_char_ll(const char c)
|
||||
{
|
||||
printf("%c", c);
|
||||
|
||||
|
|
|
@ -11,11 +11,11 @@ private:
|
|||
protected:
|
||||
int wait_for_char(const int timeout) override;
|
||||
|
||||
void put_char_ll(const char c) override;
|
||||
|
||||
public:
|
||||
console_posix(std::atomic_bool *const terminate);
|
||||
virtual ~console_posix();
|
||||
|
||||
void put_char(const char c) override;
|
||||
|
||||
void resize_terminal() override;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue