Initial commit

This commit is contained in:
leca 2023-02-13 10:35:41 +03:00
parent e86f5c78ff
commit 66b6b36754
2 changed files with 105 additions and 0 deletions

68
main.c Normal file
View File

@ -0,0 +1,68 @@
#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
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;
}

37
test.c Normal file
View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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]);
}
}