From 4cc2b527d61109518961e9096debc05eed4bdd23 Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Mon, 14 Apr 2025 12:41:23 +0200 Subject: [PATCH] add cmd color --- src/gameske_funkce.cpp | 57 +++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/src/gameske_funkce.cpp b/src/gameske_funkce.cpp index c345d74..9e6f13a 100644 --- a/src/gameske_funkce.cpp +++ b/src/gameske_funkce.cpp @@ -1,6 +1,7 @@ #include "gameske_funkce.h" #include #include +#include #include #include #include @@ -98,38 +99,60 @@ void print_in_middle(WINDOW* win, int starty, int startx, int width, std::string spawncmd() { char cmd[100] = {0}; + int pos = 0; + int ch; - // Create command window WINDOW* cmd_win = newwin(3, 40, LINES - 3, 0); - if (cmd_win == NULL) { + if (cmd_win == NULL) return ""; - } - // Setup window keypad(cmd_win, TRUE); box(cmd_win, 0, 0); mvwprintw(cmd_win, 1, 1, "Command: "); - // Explicitly enable echo - echo(); - - // Make cursor visible during input + // Disable automatic echo, we'll handle it manually + noecho(); curs_set(1); - wrefresh(cmd_win); - // Get input with echo enabled - wgetnstr(cmd_win, cmd, 99); + wattron(cmd_win, COLOR_PAIR(COLOR_CYAN)); - // Display what was entered - mvprintw(LINES - 2, 0, "You Entered: %s", cmd); - refresh(); + // Get input character by character + while (pos < 99) { + ch = wgetch(cmd_win); - // Disable echo and hide cursor when done - noecho(); + 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++; + } + + wrefresh(cmd_win); + } + wattroff(cmd_win, COLOR_PAIR(COLOR_CYAN)); + + cmd[pos] = '\0'; // Ensure null termination + // Restore echo state as needed + echo(); curs_set(0); - napms(5000); + wclear(cmd_win); + wrefresh(cmd_win); delwin(cmd_win); return std::string(cmd); }