Compare commits
4 Commits
37624e4878
...
6001970eca
Author | SHA1 | Date | |
---|---|---|---|
6001970eca | |||
4cc2b527d6 | |||
2c3461b855 | |||
f232404480 |
@ -1,10 +1,17 @@
|
|||||||
#include "gameske_funkce.h"
|
#include "gameske_funkce.h"
|
||||||
|
#include <curses.h>
|
||||||
#include <menu.h>
|
#include <menu.h>
|
||||||
|
#include <ncurses.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <chrono>
|
||||||
#include <clocale>
|
#include <clocale>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#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 spawn_menu(uint16_t begin_y, uint16_t begin_x, const char** choices,
|
||||||
size_t n_choices) {
|
size_t n_choices) {
|
||||||
@ -94,31 +101,108 @@ void print_in_middle(WINDOW* win, int starty, int startx, int width,
|
|||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
void spawncmd() {
|
std::string spawncmd() {
|
||||||
// Use a fixed-size array instead of dynamic allocation
|
char cmd[100] = {0};
|
||||||
char cmd[100] = {0}; // Initialize to prevent undefined behavior
|
int pos = 0;
|
||||||
|
int ch;
|
||||||
|
|
||||||
// Create command window with error checking
|
|
||||||
WINDOW* cmd_win = newwin(3, 40, LINES - 3, 0);
|
WINDOW* cmd_win = newwin(3, 40, LINES - 3, 0);
|
||||||
if (cmd_win == NULL) {
|
if (cmd_win == NULL)
|
||||||
// Handle window creation failure
|
return "";
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
keypad(cmd_win, TRUE);
|
keypad(cmd_win, TRUE);
|
||||||
box(cmd_win, 0, 0);
|
box(cmd_win, 0, 0);
|
||||||
mvwprintw(cmd_win, 1, 1, "Command: ");
|
mvwprintw(cmd_win, 1, 1, "Command: ");
|
||||||
|
curs_set(1);
|
||||||
wrefresh(cmd_win);
|
wrefresh(cmd_win);
|
||||||
|
|
||||||
// Use correct size (99 to leave room for null terminator)
|
wattron(cmd_win, COLOR_PAIR(COLOR_CYAN));
|
||||||
wgetnstr(cmd_win, cmd, 99);
|
|
||||||
|
|
||||||
mvprintw(LINES - 2, 0, "You Entered: %s", cmd);
|
// Get input character by character
|
||||||
refresh(); // Refresh the standard screen
|
while (pos < 99) {
|
||||||
|
ch = wgetch(cmd_win);
|
||||||
|
|
||||||
// Consider using a more interactive approach than sleep
|
if (ch == '\n' || ch == KEY_ENTER) {
|
||||||
napms(5000); // ncurses alternative to sleep
|
// Enter key pressed, end input
|
||||||
|
break;
|
||||||
|
} else if (ch == KEY_BACKSPACE || ch == 127) {
|
||||||
|
// Backspace key
|
||||||
|
if (pos > 0) {
|
||||||
|
pos--;
|
||||||
|
// Move cursor back and erase the character
|
||||||
|
wmove(cmd_win, 1, 10 + pos);
|
||||||
|
waddch(cmd_win, ' ');
|
||||||
|
wmove(cmd_win, 1, 10 + pos);
|
||||||
|
}
|
||||||
|
} else if (ch == KEY_DC) {
|
||||||
|
// Delete key - not implemented in this simple example
|
||||||
|
} else if (isprint(ch)) {
|
||||||
|
// Printable character
|
||||||
|
cmd[pos] = ch;
|
||||||
|
mvwaddch(cmd_win, 1, 10 + pos, ch); // Echo the character
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
|
||||||
// Clean up resources
|
wrefresh(cmd_win);
|
||||||
|
}
|
||||||
|
wattroff(cmd_win, COLOR_PAIR(COLOR_CYAN));
|
||||||
|
|
||||||
|
cmd[pos] = '\0'; // Ensure null termination
|
||||||
|
// Restore echo state as needed
|
||||||
|
curs_set(0);
|
||||||
|
|
||||||
|
wclear(cmd_win);
|
||||||
|
wrefresh(cmd_win);
|
||||||
delwin(cmd_win);
|
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();
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||||
|
|
||||||
@ -8,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,
|
void print_in_middle(WINDOW* win, int starty, int startx, int width,
|
||||||
char* string, chtype color);
|
char* string, chtype color);
|
||||||
|
|
||||||
void spawncmd();
|
std::string spawncmd();
|
||||||
|
|
||||||
|
void async_clock_init();
|
@ -24,8 +24,12 @@ void menu() {
|
|||||||
start_color();
|
start_color();
|
||||||
cbreak();
|
cbreak();
|
||||||
noecho();
|
noecho();
|
||||||
|
curs_set(0); // Makes cursor invisible
|
||||||
keypad(stdscr, TRUE);
|
keypad(stdscr, TRUE);
|
||||||
init_pair(1, COLOR_RED, COLOR_BLACK);
|
for (uint8_t i = 0; i < 8; i++) {
|
||||||
|
init_pair(i, i, COLOR_BLACK);
|
||||||
|
}
|
||||||
|
async_clock_init();
|
||||||
|
|
||||||
/* Create items */
|
/* Create items */
|
||||||
n_choices = ARRAY_SIZE(choices);
|
n_choices = ARRAY_SIZE(choices);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user