mc-like_console/main.c

199 lines
4.5 KiB
C
Raw Normal View History

2023-02-13 10:35:41 +03:00
#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
int rows, cols;
2023-02-25 01:55:04 +03:00
typedef struct commandLine {
int* string;
int length;
int position;
} commandLine;
2023-02-13 10:35:41 +03:00
void clearInput() {
2023-02-17 00:56:16 +03:00
for (int i = 0; i < cols; i ++)
2023-02-13 10:35:41 +03:00
mvprintw(rows - 1, i, " ");
2023-02-17 00:56:16 +03:00
}
2023-02-25 01:55:04 +03:00
void printPrompt(commandLine cmdLine, int len) {
2023-02-18 20:30:02 +03:00
mvprintw(rows - 1, 0, ">");
for (int i = 0; i < len; i ++)
2023-02-25 01:55:04 +03:00
mvprintw(rows - 1, i + 1, "%c", cmdLine.string[i]);
attron(A_BLINK);
2023-02-25 01:55:04 +03:00
mvprintw(rows - 1, cmdLine.position + 1, "");
attroff(A_BLINK);
2023-02-13 10:35:41 +03:00
}
2023-02-17 00:56:16 +03:00
void clearScreen() {
for (int i = 0; i < rows; i ++)
for (int j = 0; j < cols; j ++)
mvprintw(i, j, " ");
}
2023-02-25 01:55:04 +03:00
void printContent(commandLine *content) {
for (int i = 0; i < rows - 2; i ++)
for (int j = 0; j < cols; j ++)
2023-02-25 01:55:04 +03:00
mvprintw(rows - 2 - i, j, "%c", content[i].string[j]);
}
2023-02-25 01:55:04 +03:00
void insertInStartString(commandLine *cmdLine, int* toInsert ) {
for (int i = rows - 2; i > 0 ; i --)
for (int j = 0; j < cols; j ++)
2023-02-25 01:55:04 +03:00
cmdLine[i].string[j] = cmdLine[i - 1].string[j];
2023-02-25 01:58:58 +03:00
for (int i = 0; i < cols; i ++)
2023-02-25 01:55:04 +03:00
cmdLine[0].string[i] = toInsert[i];
}
2023-02-25 17:48:55 +03:00
void copyCommandLine(commandLine* destination, commandLine source) {
for (int i = 0; i < source.length; i ++)
destination->string[i] = source.string[i];
destination->position = source.position;
destination->length = source.length;
}
2023-02-25 01:55:04 +03:00
void insertInStartCommandLine(commandLine *cmdLine, commandLine toInsert, int arrayLength) {
for (int i = arrayLength - 1; i > 0 ; i --) {
2023-02-25 17:48:55 +03:00
copyCommandLine(&(cmdLine[i]), cmdLine[i - 1]);
2023-02-25 01:55:04 +03:00
}
2023-02-25 17:48:55 +03:00
copyCommandLine(&(cmdLine[0]), toInsert);
2023-02-13 12:54:00 +03:00
}
2023-02-13 10:35:41 +03:00
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];
2023-02-25 01:58:58 +03:00
cmd_string[position] = toInsert;
2023-02-13 10:35:41 +03:00
}
2023-02-25 01:55:04 +03:00
void pop(commandLine* cmdLine) {
if (cmdLine->position < 1)
return;
2023-02-25 01:55:04 +03:00
for (int i = cmdLine->position; i < cmdLine->length; i ++)
cmdLine->string[i] = cmdLine->string[i + 1];
cmdLine->string[(cmdLine->length) - 1] = 0;
2023-02-25 01:55:04 +03:00
cmdLine->length --;
cmdLine->position --;
}
2023-02-25 01:55:04 +03:00
void resetBuffer (commandLine *buffer) {
memset(buffer->string, 0, sizeof(int) * cols);
buffer->length = 0;
buffer->position = 0;
2023-02-13 10:35:41 +03:00
}
2023-02-25 01:58:58 +03:00
2023-02-17 00:56:16 +03:00
2023-02-13 10:35:41 +03:00
int main (int argc, char* argv[]) {
initscr();
keypad(stdscr, TRUE);
noecho();
2023-02-17 00:56:16 +03:00
start_color();
keypad(stdscr, true);
2023-02-17 00:56:16 +03:00
getmaxyx(stdscr, rows, cols);
2023-02-13 12:54:00 +03:00
int ch = ' ';
2023-02-25 01:55:04 +03:00
int historySize = 150;
int historyPosition = 0;
2023-02-25 01:55:04 +03:00
commandLine *content;
commandLine *history;
commandLine buffer;
content = malloc(rows * sizeof (commandLine));
history = malloc(historySize * sizeof (commandLine));
2023-02-25 01:55:04 +03:00
for (int i = 0; i < rows; i ++)
content[i].string = malloc(cols * sizeof(int));
for (int i = 0; i < historySize; i ++)
history[i].string = malloc(cols * sizeof(int));
2023-02-13 10:35:41 +03:00
2023-02-25 01:55:04 +03:00
buffer.string = malloc(cols * sizeof(int));
2023-02-13 12:54:00 +03:00
for (int i = 0; i < rows; i ++)
for (int j = 0; j < cols; j ++)
2023-02-25 01:55:04 +03:00
content[i].string[j] = 0;
for (int i = 0; i < historySize; i ++)
for (int j = 0; j < cols; j ++)
history[i].string[j] = 0;
2023-02-13 10:35:41 +03:00
2023-02-25 01:55:04 +03:00
printPrompt(buffer, buffer.length);
while (1){
ch = getch();
2023-02-13 10:35:41 +03:00
switch (ch) {
2023-02-13 11:08:57 +03:00
case 10: {
2023-02-25 01:55:04 +03:00
insertInStartString(content, buffer.string);
insertInStartCommandLine(history, buffer, historySize);
historyPosition = 0;
resetBuffer(&buffer);
2023-02-13 10:35:41 +03:00
break;
2023-02-13 11:08:57 +03:00
}
case KEY_LEFT:
2023-02-25 01:55:04 +03:00
if (historyPosition > 0)
buffer.position -= buffer.position > 0? 1 : 0;
else
buffer.position -= buffer.position > 0? 1 : 0;
break;
case KEY_RIGHT:
2023-02-25 01:55:04 +03:00
if (historyPosition > 0)
buffer.position += buffer.position < buffer.length? 1 : 0;
else
buffer.position += buffer.position > 0? 1 : 0;
break;
case KEY_UP:
historyPosition += historyPosition < historySize? 1 : 0;
resetBuffer(&buffer);
2023-02-25 17:48:55 +03:00
if (historyPosition > 0)
copyCommandLine(&buffer, history[historyPosition - 1]);
2023-02-25 01:55:04 +03:00
break;
case KEY_DOWN:
historyPosition -= historyPosition > 0? 1 : 0;
resetBuffer(&buffer);
2023-02-25 17:48:55 +03:00
if (historyPosition > 0)
copyCommandLine(&buffer, history[historyPosition - 1]);
break;
case KEY_BACKSPACE:
2023-02-25 01:55:04 +03:00
pop(&buffer);
break;
case KEY_F(1):
endwin();
for (int i = 0; i < 10; i ++) {
printf("%i (%i symbols): ", i, history[i].length);
for (int j = 0; j < history[i].length; j ++)
printf("%c", history[i].string[j]);
2023-02-25 01:58:58 +03:00
2023-02-25 01:55:04 +03:00
printf("\n");
}
break;
2023-02-13 10:35:41 +03:00
default:
2023-02-25 01:55:04 +03:00
insert(buffer.string, ch, buffer.position, buffer.length);
buffer.position ++;
buffer.length ++;
2023-02-13 10:35:41 +03:00
break;
}
2023-02-25 01:55:04 +03:00
2023-02-17 00:56:16 +03:00
clearScreen();
clearInput();
2023-02-13 10:35:41 +03:00
printContent(content);
2023-02-25 01:55:04 +03:00
printPrompt(buffer, buffer.length);
2023-02-13 10:35:41 +03:00
2023-02-25 01:55:04 +03:00
mvprintw(0, 0, "%i", historyPosition);
refresh();
2023-02-13 10:35:41 +03:00
}
endwin();
return 0;
}