diff --git a/main.c b/main.c new file mode 100644 index 0000000..e37cb0f --- /dev/null +++ b/main.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +int rows, cols; + +void clearInput() { + for (int i = 0; i < cols; i ++) { + mvprintw(rows - 1, i, " "); + } +} + +void printContent(char (*content)[cols+1]) { + + for (int i = rows - 2; i > 1; i --) { + mvprintw(rows - i, 0, "%s", content[rows - 2 - i]); + } +} + +void insertInStartContent(char (*content)[cols+1], char* toInsert) { + for (int i = rows; i > 0; i --) { + strcpy(content[i], content[i - 1]); + } + strcpy(content[0], toInsert); +} + +int main (int argc, char* argv[]) { + initscr(); + // cbreak(); + keypad(stdscr, TRUE); + + noecho(); + char ch = ' '; + int len = 0; + + char (*content)[cols+1] = malloc(sizeof(char[rows][cols+1])); + + for (int i = 0; i < rows; i ++) { + strcpy(content[i], " "); + } + + getmaxyx(stdscr, rows, cols); + + while (ch = getch()){ + switch (ch) { + case 10: + char* toInsert = malloc(cols+1); + mvscanw(rows - 1, 0, "%s", toInsert); + insertInStartContent(content, toInsert); + len = 0; + clearInput(rows, cols); + refresh(); + break; + default: + mvprintw(rows - 1 , len, "%c", ch); + len ++; + break; + } + printContent(content); + mvprintw(rows - 1, len, ""); + refresh(); + + } + + endwin(); + return 0; +} diff --git a/test.c b/test.c new file mode 100644 index 0000000..5e4a117 --- /dev/null +++ b/test.c @@ -0,0 +1,37 @@ +#include +#include +#include + +int cols, rows; + +void insert (char (*content)[cols], char* toInsert) { + int i = rows; + for (i; i > 0; i --) { + strcpy(content[i], content[i - 1]); + } + strcpy(content[i], toInsert); +} + +int main () { + cols = 20; + rows = 3; + + char (*content)[cols] = malloc(sizeof(char[rows][cols+1]) * 10); + + strcpy(content[0], "Test string 1"); + strcpy(content[1], "Test string 2"); + + printf("Array before insert: \n"); + + for (int i = 0; i < 3; i ++) { + printf("%s\n", content[i]); + } + + insert(content, "test string 0"); + + printf("Array after insert: \n"); + + for (int i = 0; i < 3; i ++) { + printf("%s\n", content[i]); + } +}