add async clock
All checks were successful
build_test / build (push) Successful in 1m52s

This commit is contained in:
PoliEcho 2025-04-14 13:34:32 +02:00
parent 4cc2b527d6
commit 6001970eca
3 changed files with 59 additions and 5 deletions

View File

@ -3,11 +3,15 @@
#include <menu.h>
#include <ncurses.h>
#include <unistd.h>
#include <chrono>
#include <clocale>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <format>
#include <iostream>
#include <string>
#include <thread>
size_t spawn_menu(uint16_t begin_y, uint16_t begin_x, const char** choices,
size_t n_choices) {
@ -109,9 +113,6 @@ std::string spawncmd() {
keypad(cmd_win, TRUE);
box(cmd_win, 0, 0);
mvwprintw(cmd_win, 1, 1, "Command: ");
// Disable automatic echo, we'll handle it manually
noecho();
curs_set(1);
wrefresh(cmd_win);
@ -148,7 +149,6 @@ std::string spawncmd() {
cmd[pos] = '\0'; // Ensure null termination
// Restore echo state as needed
echo();
curs_set(0);
wclear(cmd_win);
@ -156,3 +156,53 @@ std::string spawncmd() {
delwin(cmd_win);
return std::string(cmd);
}
void async_clock(WINDOW* win) {
while (true) {
auto now = std::chrono::system_clock::now();
std::string time_str;
try {
// Format just the time (HH:MM:SS)
time_str = std::format("{:%T}", now);
// Remove fractional seconds
size_t dot_pos = time_str.find('.');
if (dot_pos != std::string::npos) {
time_str.erase(dot_pos);
}
} catch (const std::exception& e) {
std::clog << "Format error: " << e.what() << std::endl;
time_str = "Error";
}
// Clear window content but preserve border
werase(win);
wborder(win, 0, 0, 0, 0, ACS_TTEE, 0, ACS_BTEE, 0);
// Print the time in the window
mvwprintw(win, 1, 1, "%s", time_str.c_str());
wrefresh(win);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void show_clock_text(WINDOW* text_win) {
std::this_thread::sleep_for(std::chrono::seconds(1));
wborder(text_win, 0, ' ', 0, 0, 0, ACS_HLINE, 0, ACS_HLINE);
mvwprintw(text_win, 1, 1, "správný čas na podání trestního oznámení je");
wrefresh(text_win);
}
void async_clock_init() {
//memory leak
WINDOW* win = newwin(3, 10, LINES - 3, COLS - 10);
box(win, 0, 0);
wrefresh(win);
std::thread clock_thread(async_clock, win);
clock_thread.detach();
//memory leak
WINDOW* text_win = newwin(3, 52, LINES - 3, COLS - 10 - 44);
std::thread text_thread(show_clock_text, text_win);
text_thread.detach();
}

View File

@ -9,4 +9,6 @@ size_t spawn_menu(uint16_t begin_y, uint16_t begin_x, const char** choices,
void print_in_middle(WINDOW* win, int starty, int startx, int width,
char* string, chtype color);
std::string spawncmd();
std::string spawncmd();
void async_clock_init();

View File

@ -24,10 +24,12 @@ void menu() {
start_color();
cbreak();
noecho();
curs_set(0); // Makes cursor invisible
keypad(stdscr, TRUE);
for (uint8_t i = 0; i < 8; i++) {
init_pair(i, i, COLOR_BLACK);
}
async_clock_init();
/* Create items */
n_choices = ARRAY_SIZE(choices);