mc-like_console/main.c

140 lines
2.8 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
}
//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(int* prompt, int len, int position) {
for (int i = 0; i < len; i ++)
mvprintw(rows - 1, i, "%c", prompt[i]);
attron(A_BLINK);
mvprintw(rows - 1, position, "");
attroff(A_BLINK);
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, " ");
}
void printContent(int **content) {
for (int i = 0; i < rows - 2; i ++)
for (int j = 0; j < cols; j ++)
mvprintw(rows - 2 - i, j, "%c", content[i][j]);
}
void insertInStartContent(int **content, int* toInsert ) {
for (int i = rows - 2; i > 0 ; i --)
for (int j = 0; j < cols; j ++)
content[i][j] = content[i - 1][j];
for (int i = 0; i < cols; i ++)
content[0][i] = toInsert[i];
2023-02-13 12:54:00 +03:00
}
2023-02-13 10:35:41 +03:00
void insert(int *cmd_string, int toInsert , int position, int len) {
for (int i = len + 1; i > position; i --)
cmd_string[i] = cmd_string[i - 1];
cmd_string[position] = toInsert;
2023-02-13 10:35:41 +03:00
}
void pop(int *cmd_string, int *position, int *len) {
if (*position < 1)
return;
for (int i = *position; i < *len; i ++)
cmd_string[i] = cmd_string[i + 1];
cmd_string[(*len) - 1] = 0;
(*len) --;
(*position) --;
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();
keypad(stdscr, TRUE);
noecho();
2023-02-17 00:56:16 +03:00
start_color();
//cbreak();
keypad(stdscr, true);
2023-02-17 00:56:16 +03:00
getmaxyx(stdscr, rows, cols);
2023-02-13 12:54:00 +03:00
int ch = ' ';
2023-02-13 10:35:41 +03:00
int len = 0;
int position = 0;
int cmd_string[cols];
memset(cmd_string, 0, 4 * cols);
int **content;
content = malloc(rows * sizeof (*content));
for (int i = 0; i < rows; i ++)
content[i] = malloc(cols * sizeof(*content[i]));
2023-02-13 10:35:41 +03:00
2023-02-13 12:54:00 +03:00
for (int i = 0; i < rows; i ++)
for (int j = 0; j < cols; j ++)
content[i][j] = 0;
2023-02-13 10:35:41 +03:00
printPrompt(cmd_string, len, position);
while (1){
ch = getch();
2023-02-13 10:35:41 +03:00
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;
position = 0;
memset(cmd_string, 0, 4 * cols);
2023-02-13 10:35:41 +03:00
break;
2023-02-13 11:08:57 +03:00
}
case KEY_LEFT:
position -= position > 0? 1:0;
break;
case KEY_RIGHT:
position += position < len? 1:0;
break;
case KEY_BACKSPACE:
pop(cmd_string, &position, &len);
break;
2023-02-13 10:35:41 +03:00
default:
insert(cmd_string, ch, position, len);
position ++;
2023-02-13 10:35:41 +03:00
len ++;
break;
}
2023-02-17 00:56:16 +03:00
clearScreen();
clearInput();
2023-02-13 10:35:41 +03:00
printContent(content);
printPrompt(cmd_string, len, position);
2023-02-13 10:35:41 +03:00
refresh();
}
endwin();
return 0;
}