mc-like_console/main.c

91 lines
1.7 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() {
2023-02-17 00:56:16 +03:00
for (int i = 0; i < cols; i ++)
2023-02-13 10:35:41 +03:00
mvprintw(rows - 1, i, " ");
2023-02-17 00:56:16 +03:00
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);
2023-02-13 10:35:41 +03:00
}
2023-02-17 00:56:16 +03:00
void clearScreen() {
for (int i = 0; i < rows; i ++)
for (int j = 0; j < cols; j ++)
mvprintw(i, j, " ");
2023-02-13 12:54:00 +03:00
}
2023-02-13 10:35:41 +03:00
2023-02-17 00:56:16 +03:00
void printContent(char (*content)[cols+1]) {
for (int i = rows - 2; i > 0; i --)
2023-02-13 11:08:57 +03:00
mvprintw(i, 0, "%s", content[rows - 2 - i]);
2023-02-13 10:35:41 +03:00
}
void insertInStartContent(char (*content)[cols+1], char* toInsert) {
2023-02-13 12:54:00 +03:00
for (int i = rows - 2; i > 0; i --)
2023-02-13 10:35:41 +03:00
strcpy(content[i], content[i - 1]);
2023-02-13 12:54:00 +03:00
strcpy(content[0], toInsert);
2023-02-13 10:35:41 +03:00
}
2023-02-17 00:56:16 +03:00
//void printStringIntoAFile(char* string) {
// FILE* f = fopen("string.txt", "w");
// fprintf(f, "%s\n", string);
// fclose(f);
//}
2023-02-13 10:35:41 +03:00
int main (int argc, char* argv[]) {
initscr();
2023-02-13 11:08:57 +03:00
cbreak();
2023-02-13 10:35:41 +03:00
keypad(stdscr, TRUE);
noecho();
2023-02-17 00:56:16 +03:00
start_color();
getmaxyx(stdscr, rows, cols);
2023-02-13 12:54:00 +03:00
2023-02-13 10:35:41 +03:00
char ch = ' ';
int len = 0;
2023-02-17 00:56:16 +03:00
char cmd_string[cols];
2023-02-13 10:35:41 +03:00
2023-02-17 00:56:16 +03:00
memset(cmd_string, '\0', cols);
char content[rows][cols];
2023-02-13 12:54:00 +03:00
for (int i = 0; i < rows; i ++)
2023-02-17 00:56:16 +03:00
memset(&(content[i]),'\0', cols);
2023-02-13 10:35:41 +03:00
while (ch = getch()){
switch (ch) {
2023-02-13 11:08:57 +03:00
case 10: {
insertInStartContent(content, cmd_string);
2023-02-13 10:35:41 +03:00
len = 0;
2023-02-17 00:56:16 +03:00
memset(cmd_string, '\0', cols);
2023-02-13 10:35:41 +03:00
break;
2023-02-13 11:08:57 +03:00
}
2023-02-13 10:35:41 +03:00
default:
2023-02-13 11:08:57 +03:00
cmd_string[len] = ch;
2023-02-13 10:35:41 +03:00
len ++;
break;
}
2023-02-17 00:56:16 +03:00
clearScreen();
2023-02-13 10:35:41 +03:00
printContent(content);
2023-02-17 00:56:16 +03:00
printPrompt(cmd_string);
2023-02-13 10:35:41 +03:00
refresh();
}
endwin();
return 0;
}