Compare commits
4 Commits
37624e4878
...
6001970eca
Author | SHA1 | Date | |
---|---|---|---|
6001970eca | |||
4cc2b527d6 | |||
2c3461b855 | |||
f232404480 |
@ -1,10 +1,17 @@
|
||||
#include "gameske_funkce.h"
|
||||
#include <curses.h>
|
||||
#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) {
|
||||
@ -94,31 +101,108 @@ void print_in_middle(WINDOW* win, int starty, int startx, int width,
|
||||
refresh();
|
||||
}
|
||||
|
||||
void spawncmd() {
|
||||
// Use a fixed-size array instead of dynamic allocation
|
||||
char cmd[100] = {0}; // Initialize to prevent undefined behavior
|
||||
std::string spawncmd() {
|
||||
char cmd[100] = {0};
|
||||
int pos = 0;
|
||||
int ch;
|
||||
|
||||
// Create command window with error checking
|
||||
WINDOW* cmd_win = newwin(3, 40, LINES - 3, 0);
|
||||
if (cmd_win == NULL) {
|
||||
// Handle window creation failure
|
||||
return;
|
||||
}
|
||||
if (cmd_win == NULL)
|
||||
return "";
|
||||
|
||||
keypad(cmd_win, TRUE);
|
||||
box(cmd_win, 0, 0);
|
||||
mvwprintw(cmd_win, 1, 1, "Command: ");
|
||||
curs_set(1);
|
||||
wrefresh(cmd_win);
|
||||
|
||||
// Use correct size (99 to leave room for null terminator)
|
||||
wgetnstr(cmd_win, cmd, 99);
|
||||
wattron(cmd_win, COLOR_PAIR(COLOR_CYAN));
|
||||
|
||||
mvprintw(LINES - 2, 0, "You Entered: %s", cmd);
|
||||
refresh(); // Refresh the standard screen
|
||||
// Get input character by character
|
||||
while (pos < 99) {
|
||||
ch = wgetch(cmd_win);
|
||||
|
||||
// Consider using a more interactive approach than sleep
|
||||
napms(5000); // ncurses alternative to sleep
|
||||
if (ch == '\n' || ch == KEY_ENTER) {
|
||||
// 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);
|
||||
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 <string>
|
||||
|
||||
#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,
|
||||
char* string, chtype color);
|
||||
|
||||
void spawncmd();
|
||||
std::string spawncmd();
|
||||
|
||||
void async_clock_init();
|
@ -24,8 +24,12 @@ void menu() {
|
||||
start_color();
|
||||
cbreak();
|
||||
noecho();
|
||||
curs_set(0); // Makes cursor invisible
|
||||
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 */
|
||||
n_choices = ARRAY_SIZE(choices);
|
||||
|
Loading…
x
Reference in New Issue
Block a user