finally made it works

This commit is contained in:
leca 2023-02-25 01:55:04 +03:00
parent 770ecde668
commit f910dc9387
1 changed files with 112 additions and 53 deletions

157
main.c
View File

@ -5,25 +5,24 @@
int rows, cols; int rows, cols;
typedef struct commandLine {
int* string;
int length;
int position;
} commandLine;
void clearInput() { void clearInput() {
for (int i = 0; i < cols; i ++) for (int i = 0; i < cols; i ++)
mvprintw(rows - 1, i, " "); mvprintw(rows - 1, i, " ");
} }
//void printContentIntoAFile(char (*content)[cols+1]) { void printPrompt(commandLine cmdLine, int len) {
// 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) {
mvprintw(rows - 1, 0, ">"); mvprintw(rows - 1, 0, ">");
for (int i = 0; i < len; i ++) for (int i = 0; i < len; i ++)
mvprintw(rows - 1, i + 1, "%c", prompt[i]); mvprintw(rows - 1, i + 1, "%c", cmdLine.string[i]);
attron(A_BLINK); attron(A_BLINK);
mvprintw(rows - 1, position + 1, ""); mvprintw(rows - 1, cmdLine.position + 1, "");
attroff(A_BLINK); attroff(A_BLINK);
} }
@ -33,19 +32,31 @@ void clearScreen() {
mvprintw(i, j, " "); mvprintw(i, j, " ");
} }
void printContent(int **content) { void printContent(commandLine *content) {
for (int i = 0; i < rows - 2; i ++) for (int i = 0; i < rows - 2; i ++)
for (int j = 0; j < cols; j ++) for (int j = 0; j < cols; j ++)
mvprintw(rows - 2 - i, j, "%c", content[i][j]); mvprintw(rows - 2 - i, j, "%c", content[i].string[j]);
} }
void insertInStartContent(int **content, int* toInsert ) { void insertInStartString(commandLine *cmdLine, int* toInsert ) {
for (int i = rows - 2; i > 0 ; i --) for (int i = rows - 2; i > 0 ; i --)
for (int j = 0; j < cols; j ++) for (int j = 0; j < cols; j ++)
content[i][j] = content[i - 1][j]; cmdLine[i].string[j] = cmdLine[i - 1].string[j];
for (int i = 0; i < cols; i ++) for (int i = 0; i < cols; i ++)
content[0][i] = toInsert[i]; cmdLine[0].string[i] = toInsert[i];
}
void insertInStartCommandLine(commandLine *cmdLine, commandLine toInsert, int arrayLength) {
for (int i = arrayLength - 1; i > 0 ; i --) {
for (int j = 0; j < cmdLine[i - 1].length; j ++)
cmdLine[i].string[j] = cmdLine[i - 1].string[j];
cmdLine[i].length = cmdLine[i - 1].length;
cmdLine[i].position = cmdLine[i - 1].position;
}
for (int i = 0; i < toInsert.length; i ++)
cmdLine[0].string[i] = toInsert.string[i];
cmdLine[0].length = toInsert.length;
cmdLine[0].position = toInsert.position;
} }
void insert(int *cmd_string, int toInsert , int position, int len) { void insert(int *cmd_string, int toInsert , int position, int len) {
@ -54,85 +65,133 @@ void insert(int *cmd_string, int toInsert , int position, int len) {
cmd_string[position] = toInsert; cmd_string[position] = toInsert;
} }
void pop(int *cmd_string, int *position, int *len) { void pop(commandLine* cmdLine) {
if (*position < 1) if (cmdLine->position < 1)
return; return;
for (int i = *position; i < *len; i ++) for (int i = cmdLine->position; i < cmdLine->length; i ++)
cmd_string[i] = cmd_string[i + 1]; cmdLine->string[i] = cmdLine->string[i + 1];
cmd_string[(*len) - 1] = 0; cmdLine->string[(cmdLine->length) - 1] = 0;
(*len) --; cmdLine->length --;
(*position) --; cmdLine->position --;
} }
//void printStringIntoAFile(char* string) { void resetBuffer (commandLine *buffer) {
// FILE* f = fopen("string.txt", "w"); memset(buffer->string, 0, sizeof(int) * cols);
// fprintf(f, "%s\n", string); buffer->length = 0;
// fclose(f); buffer->position = 0;
//} }
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;
}
int main (int argc, char* argv[]) { int main (int argc, char* argv[]) {
initscr(); initscr();
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
noecho(); noecho();
start_color(); start_color();
//cbreak();
keypad(stdscr, true); keypad(stdscr, true);
getmaxyx(stdscr, rows, cols); getmaxyx(stdscr, rows, cols);
int ch = ' '; int ch = ' ';
int len = 0; int historySize = 150;
int position = 0; int historyPosition = 0;
int cmd_string[cols];
memset(cmd_string, 0, 4 * cols); commandLine *content;
int **content; commandLine *history;
commandLine buffer;
content = malloc(rows * sizeof (*content)); content = malloc(rows * sizeof (commandLine));
history = malloc(historySize * sizeof (commandLine));
for (int i = 0; i < rows; i ++) for (int i = 0; i < rows; i ++)
content[i] = malloc(cols * sizeof(*content[i])); content[i].string = malloc(cols * sizeof(int));
for (int i = 0; i < historySize; i ++)
history[i].string = malloc(cols * sizeof(int));
buffer.string = malloc(cols * sizeof(int));
for (int i = 0; i < rows; i ++) for (int i = 0; i < rows; i ++)
for (int j = 0; j < cols; j ++) for (int j = 0; j < cols; j ++)
content[i][j] = 0; content[i].string[j] = 0;
printPrompt(cmd_string, len, position); for (int i = 0; i < historySize; i ++)
for (int j = 0; j < cols; j ++)
history[i].string[j] = 0;
printPrompt(buffer, buffer.length);
while (1){ while (1){
ch = getch(); ch = getch();
switch (ch) { switch (ch) {
case 10: { case 10: {
insertInStartContent(content, cmd_string); insertInStartString(content, buffer.string);
len = 0; insertInStartCommandLine(history, buffer, historySize);
position = 0;
memset(cmd_string, 0, 4 * cols); historyPosition = 0;
resetBuffer(&buffer);
break; break;
} }
case KEY_LEFT: case KEY_LEFT:
position -= position > 0? 1:0; if (historyPosition > 0)
buffer.position -= buffer.position > 0? 1 : 0;
else
buffer.position -= buffer.position > 0? 1 : 0;
break; break;
case KEY_RIGHT: case KEY_RIGHT:
position += position < len? 1:0; 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);
if (historyPosition > 0) copyCommandLine(&buffer, history[historyPosition - 1]);
break;
case KEY_DOWN:
historyPosition -= historyPosition > 0? 1 : 0;
resetBuffer(&buffer);
if (historyPosition > 0) copyCommandLine(&buffer, history[historyPosition - 1]);
break; break;
case KEY_BACKSPACE: case KEY_BACKSPACE:
pop(cmd_string, &position, &len); 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]);
printf("\n");
}
break; break;
default: default:
insert(cmd_string, ch, position, len); insert(buffer.string, ch, buffer.position, buffer.length);
position ++; buffer.position ++;
len ++; buffer.length ++;
break; break;
} }
clearScreen(); clearScreen();
clearInput(); clearInput();
printContent(content); printContent(content);
printPrompt(cmd_string, len, position); printPrompt(buffer, buffer.length);
refresh();
mvprintw(0, 0, "%i", historyPosition);
refresh();
} }
endwin(); endwin();