finally made it works
This commit is contained in:
		
							
								
								
									
										157
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										157
									
								
								main.c
									
									
									
									
									
								
							| @@ -5,25 +5,24 @@ | ||||
|  | ||||
| int rows, cols; | ||||
|  | ||||
| typedef struct commandLine { | ||||
| 	int* string; | ||||
| 	int length; | ||||
| 	int position; | ||||
| } commandLine; | ||||
|  | ||||
| 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) { | ||||
| void printPrompt(commandLine cmdLine, int len) { | ||||
| 	mvprintw(rows - 1, 0, ">"); | ||||
| 	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); | ||||
| 	mvprintw(rows - 1, position + 1, ""); | ||||
| 	mvprintw(rows - 1, cmdLine.position + 1, ""); | ||||
| 	attroff(A_BLINK); | ||||
| } | ||||
|  | ||||
| @@ -33,19 +32,31 @@ void clearScreen() { | ||||
| 			mvprintw(i, j, " "); | ||||
| } | ||||
|  | ||||
| void printContent(int **content) { | ||||
| void printContent(commandLine *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]); | ||||
| 			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 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 ++)  | ||||
| 		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) { | ||||
| @@ -54,85 +65,133 @@ void insert(int *cmd_string, int toInsert , int position, int len) { | ||||
| 	cmd_string[position] = toInsert; | ||||
| } | ||||
|  | ||||
| void pop(int *cmd_string, int *position, int *len) { | ||||
| 	if (*position < 1) | ||||
| void pop(commandLine* cmdLine) { | ||||
| 	if (cmdLine->position < 1) | ||||
| 		return; | ||||
| 		 | ||||
| 	for (int i = *position; i < *len; i ++)  | ||||
| 		cmd_string[i] = cmd_string[i + 1]; | ||||
| 	for (int i = cmdLine->position; i < cmdLine->length; i ++)  | ||||
| 		cmdLine->string[i] = cmdLine->string[i + 1]; | ||||
|  | ||||
| 	cmd_string[(*len) - 1] = 0; | ||||
| 	cmdLine->string[(cmdLine->length) - 1] = 0; | ||||
|  | ||||
| 	(*len) --;	 | ||||
| 	(*position) --;	 | ||||
| 	cmdLine->length --;	 | ||||
| 	cmdLine->position --; | ||||
| } | ||||
|  | ||||
| //void printStringIntoAFile(char* string) { | ||||
| //	FILE* f = fopen("string.txt", "w"); | ||||
| //	fprintf(f, "%s\n", string); | ||||
| //	fclose(f); | ||||
| //} | ||||
| void resetBuffer (commandLine *buffer) { | ||||
| 	memset(buffer->string, 0, sizeof(int) * cols); | ||||
| 	buffer->length = 0; | ||||
| 	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[]) { | ||||
| 	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]; | ||||
| 	int historySize = 150; | ||||
| 	int historyPosition = 0; | ||||
|  | ||||
| 	memset(cmd_string, 0, 4 * cols); | ||||
| 	int **content; | ||||
| 	commandLine *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 ++) | ||||
| 		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 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){ | ||||
| 		ch = getch();	 | ||||
| 		switch (ch) { | ||||
| 			case 10: { | ||||
| 				insertInStartContent(content, cmd_string); | ||||
| 				len = 0; | ||||
| 				position = 0; | ||||
| 				memset(cmd_string, 0, 4 * cols); | ||||
| 				insertInStartString(content, buffer.string); | ||||
| 				insertInStartCommandLine(history, buffer, historySize); | ||||
| 	 | ||||
| 				historyPosition = 0; | ||||
| 				resetBuffer(&buffer); | ||||
| 			break; | ||||
| 			} | ||||
| 			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; | ||||
| 			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; | ||||
| 			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; | ||||
| 			default: | ||||
| 				insert(cmd_string, ch, position, len); | ||||
| 				position ++; | ||||
| 				len ++; | ||||
| 				insert(buffer.string, ch, buffer.position, buffer.length); | ||||
| 				buffer.position ++; | ||||
| 				buffer.length ++; | ||||
| 			break; | ||||
| 		} | ||||
|  | ||||
| 		clearScreen(); | ||||
| 		clearInput(); | ||||
| 		printContent(content); | ||||
| 		printPrompt(cmd_string, len, position); | ||||
| 		refresh(); | ||||
| 		printPrompt(buffer, buffer.length); | ||||
|  | ||||
| 		mvprintw(0, 0, "%i", historyPosition); | ||||
| 		 | ||||
| 		refresh(); | ||||
| 	} | ||||
| 	 | ||||
| 	endwin();	 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user