-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetinput.c
More file actions
124 lines (116 loc) · 3.06 KB
/
getinput.c
File metadata and controls
124 lines (116 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <ncurses.h>
#include <string.h>
#define CURSOR_POS (getcury(stdscr) - 1) * COLS + getcurx(stdscr)
#define LENGTH 2000
int main()
{
char input[LENGTH];
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
int length = 0;
printw("Getting some input %i/%i", length, LENGTH - 1);
move(1,0);
// printw("%i", getcurx(stdscr));
refresh();
int ch;
while ((ch = getch()) != '\n' && length < LENGTH - 1)
{
if (ch == KEY_BACKSPACE)
{
if (length > 0)
{
int y, x;
y = getcury(stdscr);
x = getcurx(stdscr);
for (int i = CURSOR_POS - 1; i < length - 1; i++)
{
input[i] = input[i+1];
}
length--;
input[length] = '\0';
// backspace moves cursor but not anymore with noecho();
if (x == 0)
{
clear();
printw("Getting some input %i/%i", length, LENGTH - 1);
move(1,0);
printw("%s", input);
move(y - 1, COLS - 1);
}
else
{
clear();
printw("Getting some input %i/%i", length, LENGTH - 1);
move(1,0);
printw("%s", input);
move(y, x - 1);
}
}
}
else if (ch == KEY_LEFT)
{
int y, x;
y = getcury(stdscr);
x = getcurx(stdscr);
if (length > 0 && CURSOR_POS > 0)
{
if (x == 0)
{
move(y - 1, COLS - 1);
}
else
{
move(y, x - 1);
}
}
}
else if (ch == KEY_RIGHT)
{
int y, x;
y = getcury(stdscr);
x = getcurx(stdscr);
if (CURSOR_POS < length)
{
if (x == COLS - 1)
{
move(y + 1, 0);
}
else
{
move(y, x + 1);
}
}
}
else if (ch < KEY_MIN || ch > KEY_MAX)
{
int y, x;
y = getcury(stdscr);
x = getcurx(stdscr);
for (int i = length; i > CURSOR_POS; i--)
{
input[i] = input[i - 1];
}
input[CURSOR_POS] = ch;
length++;
input[length] = '\0';
clear();
printw("Getting some input %i/%i", length, LENGTH - 1);
move(1,0);
printw("%s", input);
if (x == COLS - 1)
{
move(y + 1, 0);
}
else
{
move(y, x + 1);
}
}
}
input[length] = '\0';
endwin();
printf("Your input was {%s}\n", input);
return 0;
}