mc-like_console/main.c

69 lines
1.2 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;
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;
}