mc-like_console/main.c

77 lines
1.5 KiB
C

#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 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 printContent(char (*content)[cols+1]) {
for (int i = rows - 2; i > 1; i --)
mvprintw(i, 0, "%s", content[rows - 2 - i]);
}
void insertInStartContent(char (*content)[cols+1], char* toInsert) {
for (int i = rows - 2; 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* cmd_string = malloc(cols);
char (*content)[cols] = malloc(sizeof(char[rows][cols+1]));
for (int i = 0; i < rows; i ++)
memset(content[i],0,sizeof(content[i]));
getmaxyx(stdscr, rows, cols);
while (ch = getch()){
switch (ch) {
case 10: {
FILE* f = fopen("test.txt","w");
fprintf(f, "test: %s", cmd_string);
fclose(f);
insertInStartContent(content, cmd_string);
len = 0;
clearInput(rows, cols);
memset(cmd_string,0,cols * sizeof(char));
refresh();
printContentIntoAFile(content);
break;
}
default:
mvprintw(rows - 1 , len, "%c", ch);
cmd_string[len] = ch;
len ++;
break;
}
printContent(content);
mvprintw(rows - 1, len, "");
refresh();
}
endwin();
return 0;
}