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

91
main.c
View File

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