This commit is contained in:
PoliEcho 2025-04-14 12:21:16 +02:00
parent 37624e4878
commit f232404480
2 changed files with 24 additions and 12 deletions

View File

@ -1,10 +1,12 @@
#include "gameske_funkce.h" #include "gameske_funkce.h"
#include <curses.h>
#include <menu.h> #include <menu.h>
#include <unistd.h> #include <unistd.h>
#include <clocale> #include <clocale>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <string>
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 +96,40 @@ 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
// Create command window with error checking // Create command window
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;
} }
// Setup window
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: ");
// Explicitly enable echo
echo();
// Make cursor visible during input
curs_set(1);
wrefresh(cmd_win); wrefresh(cmd_win);
// Use correct size (99 to leave room for null terminator) // Get input with echo enabled
wgetnstr(cmd_win, cmd, 99); wgetnstr(cmd_win, cmd, 99);
// Display what was entered
mvprintw(LINES - 2, 0, "You Entered: %s", cmd); mvprintw(LINES - 2, 0, "You Entered: %s", cmd);
refresh(); // Refresh the standard screen refresh();
// Consider using a more interactive approach than sleep // Disable echo and hide cursor when done
napms(5000); // ncurses alternative to sleep noecho();
curs_set(0);
// Clean up resources napms(5000);
delwin(cmd_win); delwin(cmd_win);
return std::string(cmd);
} }

View File

@ -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,4 @@ 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();