| ... | ... |
@@ -16,6 +16,7 @@ int choices_n = 0; |
| 16 | 16 |
const char **choices = NULL; |
| 17 | 17 |
double *choices_score = NULL; |
| 18 | 18 |
size_t *choices_sorted = NULL; |
| 19 |
+int current_selection = 0; |
|
| 19 | 20 |
|
| 20 | 21 |
void resize_choices(int new_capacity){
|
| 21 | 22 |
choices = realloc(choices, new_capacity * sizeof(const char *)); |
| ... | ... |
@@ -70,8 +71,8 @@ static int cmpchoice(const void *p1, const void *p2) {
|
| 70 | 71 |
return -1; |
| 71 | 72 |
} |
| 72 | 73 |
|
| 73 |
- |
|
| 74 | 74 |
void run_search(char *needle){
|
| 75 |
+ current_selection = 0; |
|
| 75 | 76 |
choices_available = 0; |
| 76 | 77 |
int i; |
| 77 | 78 |
for(i = 0; i < choices_n; i++){
|
| ... | ... |
@@ -139,8 +140,11 @@ void draw(){
|
| 139 | 140 |
const char *prompt = "> "; |
| 140 | 141 |
clear(); |
| 141 | 142 |
fprintf(ttyout, "%s%s\n", prompt, search); |
| 142 |
- run_search(search); |
|
| 143 | 143 |
for(i = 0; line < 10 && i < choices_available; i++){
|
| 144 |
+ if(i == current_selection) |
|
| 145 |
+ fprintf(ttyout, "%c%c7m", 0x1b, '['); |
|
| 146 |
+ else |
|
| 147 |
+ fprintf(ttyout, "%c%c0m", 0x1b, '['); |
|
| 144 | 148 |
fprintf(ttyout, "%s\n", choices[choices_sorted[i]]); |
| 145 | 149 |
line++; |
| 146 | 150 |
} |
| ... | ... |
@@ -153,10 +157,9 @@ void emit(){
|
| 153 | 157 |
/* ttyout should be flushed before outputting on stdout */ |
| 154 | 158 |
fclose(ttyout); |
| 155 | 159 |
|
| 156 |
- run_search(search); |
|
| 157 | 160 |
if(choices_available){
|
| 158 |
- /* output the first result */ |
|
| 159 |
- printf("%s\n", choices[choices_sorted[0]]);
|
|
| 161 |
+ /* output the selected result */ |
|
| 162 |
+ printf("%s\n", choices[choices_sorted[current_selection]]);
|
|
| 160 | 163 |
}else{
|
| 161 | 164 |
/* No match, output the query instead */ |
| 162 | 165 |
printf("%s\n", search);
|
| ... | ... |
@@ -166,24 +169,31 @@ void emit(){
|
| 166 | 169 |
} |
| 167 | 170 |
|
| 168 | 171 |
void run(){
|
| 169 |
- draw(); |
|
| 172 |
+ run_search(search); |
|
| 170 | 173 |
char ch; |
| 171 | 174 |
do {
|
| 175 |
+ draw(); |
|
| 172 | 176 |
ch = ttygetchar(); |
| 173 | 177 |
if(isprint(ch)){
|
| 174 | 178 |
/* FIXME: overflow */ |
| 175 | 179 |
search[search_size++] = ch; |
| 176 | 180 |
search[search_size] = '\0'; |
| 177 |
- draw(); |
|
| 181 |
+ run_search(search); |
|
| 178 | 182 |
}else if(ch == 127){ /* DEL */
|
| 179 | 183 |
if(search_size) |
| 180 | 184 |
search[--search_size] = '\0'; |
| 181 |
- draw(); |
|
| 185 |
+ run_search(search); |
|
| 182 | 186 |
}else if(ch == 23){ /* C-W */
|
| 183 | 187 |
if(search_size) |
| 184 | 188 |
search[--search_size] = '\0'; |
| 185 | 189 |
while(search_size && !isspace(search[--search_size])) |
| 186 | 190 |
search[search_size] = '\0'; |
| 191 |
+ run_search(search); |
|
| 192 |
+ }else if(ch == 14){ /* C-N */
|
|
| 193 |
+ current_selection = (current_selection + 1) % 10; |
|
| 194 |
+ draw(); |
|
| 195 |
+ }else if(ch == 16){ /* C-P */
|
|
| 196 |
+ current_selection = (current_selection + 9) % 10; |
|
| 187 | 197 |
draw(); |
| 188 | 198 |
}else if(ch == 10){ /* Enter */
|
| 189 | 199 |
clear(); |