mc-like_console/main.c

91 lines
1.7 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, " ");
attron(A_BLINK);
mvprintw(rows - 1, 0, " ");
attroff(A_BLINK);
}
//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(char* prompt) {
mvprintw(rows - 1, 0, "%s", prompt);
}
void clearScreen() {
for (int i = 0; i < rows; i ++)
for (int j = 0; j < cols; j ++)
mvprintw(i, j, " ");
}
void printContent(char (*content)[cols+1]) {
for (int i = rows - 2; i > 0; 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);
}
//void printStringIntoAFile(char* string) {
// FILE* f = fopen("string.txt", "w");
// fprintf(f, "%s\n", string);
// fclose(f);
//}
int main (int argc, char* argv[]) {
initscr();
cbreak();
keypad(stdscr, TRUE);
noecho();
start_color();
getmaxyx(stdscr, rows, cols);
char ch = ' ';
int len = 0;
char cmd_string[cols];
memset(cmd_string, '\0', cols);
char content[rows][cols];
for (int i = 0; i < rows; i ++)
memset(&(content[i]),'\0', cols);
while (ch = getch()){
switch (ch) {
case 10: {
insertInStartContent(content, cmd_string);
len = 0;
memset(cmd_string, '\0', cols);
break;
}
default:
cmd_string[len] = ch;
len ++;
break;
}
clearScreen();
printContent(content);
printPrompt(cmd_string);
refresh();
}
endwin();
return 0;
}