#include #include #include #include int rows, cols; void clearInput() { for (int i = 0; i < cols; i ++) mvprintw(rows - 1, i, " "); } //void printContentIntoAFile(char (*content)[cols+1]) { // FILE* f = fopen("content.txt", "w"); // for (int i = 0; i < rows - 2; i ++) // fprintf(f, "%s\n", content[i]); // fclose(f); //} void printPrompt(int* prompt, int len, int position) { for (int i = 0; i < len; i ++) mvprintw(rows - 1, i, "%c", prompt[i]); attron(A_BLINK); mvprintw(rows - 1, position, ""); attroff(A_BLINK); } void clearScreen() { for (int i = 0; i < rows; i ++) for (int j = 0; j < cols; j ++) mvprintw(i, j, " "); } void printContent(int **content) { for (int i = 0; i < rows - 2; i ++) for (int j = 0; j < cols; j ++) mvprintw(rows - 2 - i, j, "%c", content[i][j]); } void insertInStartContent(int **content, int* toInsert ) { for (int i = rows - 2; i > 0 ; i --) for (int j = 0; j < cols; j ++) content[i][j] = content[i - 1][j]; for (int i = 0; i < cols; i ++) content[0][i] = toInsert[i]; } void insert(int *cmd_string, int toInsert , int position, int len) { for (int i = len + 1; i > position; i --) cmd_string[i] = cmd_string[i - 1]; cmd_string[position] = toInsert; } void pop(int *cmd_string, int *position, int *len) { if (*position < 1) return; for (int i = *position; i < *len; i ++) cmd_string[i] = cmd_string[i + 1]; cmd_string[(*len) - 1] = 0; (*len) --; (*position) --; } //void printStringIntoAFile(char* string) { // FILE* f = fopen("string.txt", "w"); // fprintf(f, "%s\n", string); // fclose(f); //} int main (int argc, char* argv[]) { initscr(); keypad(stdscr, TRUE); noecho(); start_color(); //cbreak(); keypad(stdscr, true); getmaxyx(stdscr, rows, cols); int ch = ' '; int len = 0; int position = 0; int cmd_string[cols]; memset(cmd_string, 0, 4 * cols); int **content; content = malloc(rows * sizeof (*content)); for (int i = 0; i < rows; i ++) content[i] = malloc(cols * sizeof(*content[i])); for (int i = 0; i < rows; i ++) for (int j = 0; j < cols; j ++) content[i][j] = 0; printPrompt(cmd_string, len, position); while (1){ ch = getch(); switch (ch) { case 10: { insertInStartContent(content, cmd_string); len = 0; position = 0; memset(cmd_string, 0, 4 * cols); break; } case KEY_LEFT: position -= position > 0? 1:0; break; case KEY_RIGHT: position += position < len? 1:0; break; case KEY_BACKSPACE: pop(cmd_string, &position, &len); break; default: insert(cmd_string, ch, position, len); position ++; len ++; break; } clearScreen(); clearInput(); printContent(content); printPrompt(cmd_string, len, position); refresh(); } endwin(); return 0; }