Remade char ch -> int ch, added arrow navigation and backspace

This commit is contained in:
leca 2023-02-18 20:19:01 +03:00
parent 28041ebdf4
commit 756065eaec
1 changed files with 73 additions and 24 deletions

93
main.c
View File

@ -8,9 +8,6 @@ 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]) {
@ -20,8 +17,13 @@ void clearInput() {
// fclose(f);
//}
void printPrompt(char* prompt) {
mvprintw(rows - 1, 0, "%s", prompt);
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);
}
void clearScreen() {
@ -30,15 +32,38 @@ void clearScreen() {
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 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(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 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];
}
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;
}
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) --;
}
//void printStringIntoAFile(char* string) {
@ -49,38 +74,62 @@ void insertInStartContent(char (*content)[cols+1], char* toInsert) {
int main (int argc, char* argv[]) {
initscr();
cbreak();
keypad(stdscr, TRUE);
noecho();
start_color();
//cbreak();
keypad(stdscr, true);
getmaxyx(stdscr, rows, cols);
char ch = ' ';
int ch = ' ';
int len = 0;
char cmd_string[cols];
int position = 0;
int cmd_string[cols];
memset(cmd_string, 0, 4 * cols);
int **content;
content = malloc(rows * sizeof (*content));
memset(cmd_string, '\0', cols);
char content[rows][cols];
for (int i = 0; i < rows; i ++)
memset(&(content[i]),'\0', cols);
content[i] = malloc(cols * sizeof(*content[i]));
while (ch = getch()){
for (int i = 0; i < rows; i ++)
for (int j = 0; j < cols; j ++)
content[i][j] = 0;
printPrompt(cmd_string, len, position);
while (1){
ch = getch();
switch (ch) {
case 10: {
insertInStartContent(content, cmd_string);
len = 0;
memset(cmd_string, '\0', cols);
position = 0;
memset(cmd_string, 0, 4 * cols);
break;
}
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;
default:
cmd_string[len] = ch;
insert(cmd_string, ch, position, len);
position ++;
len ++;
break;
}
clearScreen();
clearInput();
printContent(content);
printPrompt(cmd_string);
printPrompt(cmd_string, len, position);
refresh();
}