Made it work

This commit is contained in:
leca 2023-02-17 00:56:16 +03:00
parent b141712359
commit 28041ebdf4
3 changed files with 40 additions and 63 deletions

BIN
console

Binary file not shown.

58
main.c
View File

@ -6,20 +6,32 @@
int rows, cols;
void clearInput() {
for (int i = 0; i < cols; i ++) {
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 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 > 1; i --)
for (int i = rows - 2; i > 0; i --)
mvprintw(i, 0, "%s", content[rows - 2 - i]);
}
@ -29,44 +41,46 @@ void insertInStartContent(char (*content)[cols+1], char* toInsert) {
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 = malloc(cols);
char (*content)[cols] = malloc(sizeof(char[rows][cols+1]));
char cmd_string[cols];
memset(cmd_string, '\0', cols);
char content[rows][cols];
for (int i = 0; i < rows; i ++)
memset(content[i],0,sizeof(content[i]));
getmaxyx(stdscr, rows, cols);
memset(&(content[i]),'\0', 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);
memset(cmd_string, '\0', cols);
break;
}
default:
mvprintw(rows - 1 , len, "%c", ch);
cmd_string[len] = ch;
len ++;
break;
}
clearScreen();
printContent(content);
mvprintw(rows - 1, len, "");
printPrompt(cmd_string);
refresh();
}

37
test.c
View File

@ -1,37 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cols, rows;
void insert (char (*content)[cols], char* toInsert) {
int i = rows;
for (i; i > 0; i --) {
strcpy(content[i], content[i - 1]);
}
strcpy(content[i], toInsert);
}
int main () {
cols = 50;
rows = 3;
char (*content)[cols] = malloc(sizeof(char[rows][cols+1]));
strcpy(content[0], "Test string 1");
strcpy(content[1], "Test string 2");
printf("Array before insert: \n");
for (int i = 0; i < 3; i ++) {
printf("%s\n", content[i]);
}
insert(content, "test string 0");
printf("Array after insert: \n");
for (int i = 0; i < 3; i ++) {
printf("%s\n", content[i]);
}
}