-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
155 lines (113 loc) · 3.86 KB
/
main.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <locale.h>
#include <pthread.h>
#include <ncurses.h>
#include "include/process.h"
#include "include/runtimeParser.h"
#include "include/configParser.h"
typedef struct winData {
WINDOW * win;
char userInput [128];
int spKey;
} winData;
void *parsingThread (void *data) {
winData *pWin = (winData *) data;
int selectedResult = 0;
struct config *config = configParser ();
int resultCount = config -> resultCount;
int resultSize = 32;
while (1 == 1) {
char userInputCopy [128];
strcpy (userInputCopy, pWin -> userInput);
// wait when user complete typing
/* usleep(100000); // 0.1 sec */
usleep(10000); // 0.01 sec
if (strcmp (userInputCopy, pWin -> userInput) != 0) {
selectedResult = 0;
continue;
}
char *result [resultCount];
for (int i = 0; i < resultCount; i++) {
result [i] = (i == 0) ? malloc (256) : malloc (resultSize);
strcpy (result [i], "");
}
parse (userInputCopy, result, resultCount, resultSize);
werase (pWin -> win);
if (pWin -> spKey == KEY_ENTER) {
strcpy (pWin -> userInput, strtok (result [selectedResult], "\n"));
break;
}
int findedResults = 0;
for (int i = 0; i < resultCount; i++) {
if (strcmp (result[i], "") != 0) {
if (i == selectedResult)
wattron (pWin -> win, COLOR_PAIR(1));
mvwprintw (pWin -> win, i + 1, 1, "%i. %s", i + 1, result [i]);
wattroff (pWin -> win, COLOR_PAIR(1));
findedResults++;
}
free (result [i]);
}
if (pWin -> spKey == KEY_UP && selectedResult > 0)
selectedResult -= 1;
else if (pWin -> spKey == KEY_DOWN && selectedResult < findedResults - 1)
selectedResult += 1;
pWin -> spKey = -1;
box (pWin -> win, 0, 0);
wrefresh (pWin -> win);
}
return 0;
}
int main (void) {
setlocale(LC_ALL, "");
int maxCol, maxRow;
winData parsingWin;
memset(parsingWin.userInput, '\0', sizeof (parsingWin.userInput));
struct config *config = configParser ();
initscr ();
raw ();
nonl ();
start_color ();
init_pair (1, COLOR_BLACK, COLOR_WHITE);
getmaxyx (stdscr, maxRow, maxCol);
mvprintw (1, (maxCol / 2) - (strlen ("Enter your command:") / 2), "Enter your command:");
refresh ();
WINDOW *inputWin = newwin (3, maxCol - config -> padding * 2, 3, config -> padding);
keypad (inputWin, TRUE);
box (inputWin, 0, 0);
wrefresh(inputWin);
pthread_t thread_id;
parsingWin.win = newwin (maxRow - 8, maxCol - config -> padding * 2, 7, config -> padding);
pthread_create (&thread_id, NULL, parsingThread, (void *) &parsingWin);
int ch;
int pos = 0;
while (1 == 1) {
wmove (inputWin, 1, pos + 1);
ch = wgetch (inputWin);
if (ch == KEY_ENTER || ch == '\r') {
parsingWin.spKey = KEY_ENTER;
break;
} else if (isprint (ch)) {
parsingWin.userInput [pos++] = ch;
} else if (ch == 27) {
pthread_cancel (thread_id);
strcpy (parsingWin.userInput, "");
break;
} else if (ch == KEY_BACKSPACE && pos > 0) {
parsingWin.userInput [--pos] = '\0';
wclrtoeol(inputWin);
box (inputWin, 0, 0);
} else if (ch == KEY_UP || ch == config -> upKeycode) {
parsingWin.spKey = KEY_UP;
} else if (ch == KEY_DOWN || ch == config -> downKeycode)
parsingWin.spKey = KEY_DOWN;
}
pthread_join (thread_id, 0);
startProcess (parsingWin.userInput);
endwin ();
return EXIT_SUCCESS;
}