12 enum { FALSE
= 0, TRUE
= 1 };
14 #define LENGTH(x) (sizeof(x) / sizeof(*x))
15 #define CONTROL(char) (char ^ 0x40)
16 #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
17 #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
20 * Fill the buffer apropriately with the lines and headers
23 fill_buffer(char *separator
)
25 /* fill buffer with string */
26 char string
[LINE_SIZE
];
28 Buffer
*buffer
= malloc(sizeof(Buffer
));
29 FILE *fp
= fopen("complete.c", "r");
32 die("Can not open file for reading.");
35 /* read the file into a doubly linked list of lines. */
36 while (fgets(string
, LINE_SIZE
, fp
)) {
39 last
= add_line(buffer
, buffer
->total
, string
, separator
, last
);
42 /* set the buffer stats. */
43 buffer
->current
= buffer
->first
;
49 * Parse the line content to determine if it is a header and identify the
53 parse_line(char *s
, char *separator
)
55 Line
*line
= malloc(sizeof(Line
));
56 char *sep
= strstr(s
, separator
);
57 int pos
= !sep
? (int) strlen(s
) - 1 : (int) (sep
- s
);
59 /* strip trailing newline */
60 s
[strlen(s
) - 1] = '\0';
62 /* fill line->content */
63 line
->content
= malloc((pos
+ 1) * sizeof(char));
64 strncpy(line
->content
, s
, pos
);
66 /* fill line->comment */
68 line
->comment
= malloc((strlen(s
) - pos
) * sizeof(char));
69 strcpy(line
->comment
, s
+ pos
);
78 * Add a line to the end of the current buffer.
80 * This requires to create a new line with a link to the previous line
81 * and to NULL as the next line.
83 * The previous line's 'next' should be relinked to this new line.
85 * The header's last line have to point to this last line
88 add_line(Buffer
*buffer
, int number
, char *string
, char *separator
, Line
*prev
)
90 /* allocate new line */
91 Line
*line
= malloc(sizeof(Line
));
92 line
= parse_line(string
, separator
);
96 line
->number
= number
;
98 /* interlink with previous line if exists */
100 buffer
->first
= line
;
110 * Set buffer->candidates to an array of lines that match and update
111 * buffer->matching to number of matching candidates.
114 filter_lines(Buffer
*buffer
)
116 Line
* line
= buffer
->first
;
118 buffer
->matching
= 0;
121 if (line_match_input(line
, "test")) {
122 line
->matches
= TRUE
;
125 line
->matches
= FALSE
;
133 * Check if line matches and return TRUE if so
136 line_match_input(Line
*line
, char *input
)
138 if (line
&& input
/* match */) {
146 * Print a line to stderr.
149 print_line(Line
*line
, int current
, int column
)
154 /* clean the line in case it was not empty */
155 fputs("\033[K", stderr
);
157 /* line number if option set */
160 fputs("\033[1m", stderr
);
162 fputs("\033[1;30m", stderr
);
165 fprintf(stderr
, "%7d\033[0m ", line
->number
);
171 /* highlight current line */
173 fputs("\033[1;33m", stderr
);
176 /* print content without overflowing terminal width */
177 for (i
= 0; i
< strlen(line
->content
) && chars
< column
; chars
++, i
++) {
179 /* handle tab as 8 spaces for output */
180 if (line
->content
[i
] == '\t') {
184 for (; chars
% 8 != 0; chars
++) {
188 fputc(line
->content
[i
], stderr
);
192 /* print spaces without overflowing terminal width */
193 for ( ; chars
<= 30 && chars
< column
; chars
++, i
++) {
197 /* comments in grey */
198 fputs("\033[1;30m", stderr
);
200 /* print comment without overflowing terminal width */
201 for (i
= 0; i
< strlen(line
->comment
) && chars
< column
; chars
++, i
++) {
203 /* handle tab as 8 spaces for output */
204 if (line
->comment
[i
] == '\t') {
208 for (; chars
% 8 != 7; chars
++) {
212 fputc(line
->comment
[i
], stderr
);
216 fputs("\033[0m\n", stderr
);
221 * Print a header title.
229 * Print all the lines from an array of pointer to lines.
231 * The total number oflines printed shall not excess 'count'.
234 print_lines(Buffer
*buffer
, int count
, int offset
, int columns
)
236 Line
*line
= buffer
->current
;
240 /* seek back from current line to the first line to print */
241 while (line
&& i
< count
- offset
) {
242 i
= line
->matches
? i
+ 1 : i
;
245 line
= line
? line
: buffer
->first
;
247 /* print up to count lines that match the input */
248 while (line
&& j
< count
) {
250 print_line(line
, line
== buffer
->current
, columns
);
257 /* continue up to the end of the screen clearing it */
258 for (; j
< count
; j
++) {
259 fputs("\r\033[K\n", stderr
);
264 * Update the screen interface and print all candidates.
266 * This also has to clear the previous lines.
269 update_screen(Buffer
*buffer
, int count
, int offset
)
272 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &w
);
274 fprintf(stderr
, "\n");
275 print_lines(buffer
, count
, offset
, w
.ws_col
);
277 /* go up to the prompt position and update it */
278 fprintf(stderr
, "\033[%dA", count
+ 1);
279 print_prompt(buffer
->matching
, buffer
->total
, buffer
->input
);
282 void clear_screen(int count
)
285 for (i
= 0; i
< count
+ 1; i
++) {
286 fputs("\r\033[K\n", stderr
);
289 fprintf(stderr
, "\033[%dA", count
+ 1);
293 * Set terminal to send one char at a time for interactive mode, and return the
294 * last terminal state.
300 struct termios termio_old
;
301 struct termios termio_new
;
303 /* set the terminal to send one key at a time. */
305 /* get the terminal's state */
306 if (tcgetattr(0, &termio_old
) < 0) {
307 die("Can not get terminal attributes with tcgetattr().");
310 /* create a new modified state by switching the binary flags */
311 termio_new
= termio_old
;
312 termio_new
.c_lflag
&= ~(ICANON
| ECHO
| IGNBRK
);
314 /* apply this state to current terminal now (TCSANOW) */
315 tcsetattr(STDIN_FILENO
, TCSANOW
, &termio_new
);
321 * Listen for the user input and call the appropriate functions.
324 get_input(Buffer
*buffer
, int count
, int offset
)
326 /* receive one character at a time from the terminal */
327 struct termios termio_old
= terminal_set();
329 /* get input char by char from the keyboard. */
330 while (do_key(getchar(), buffer
)) {
331 filter_lines(buffer
);
332 update_screen(buffer
, count
, offset
);
335 /* resets the terminal to the previous state. */
336 tcsetattr(STDIN_FILENO
, TCSANOW
, &termio_old
);
340 * Perform action associated with key
343 do_key(char key
, Buffer
*buffer
)
354 buffer
->input
[0] = '\0';
358 length
= strlen(buffer
->input
) - 1;
360 for (i
= length
; i
>= 0 && isspace(buffer
->input
[i
]); i
--) {
361 buffer
->input
[i
] = '\0';
364 length
= strlen(buffer
->input
) - 1;
365 for (i
= length
; i
>= 0 && !isspace(buffer
->input
[i
]); i
--) {
366 buffer
->input
[i
] = '\0';
372 case 127: /* backspace */
373 buffer
->input
[strlen(buffer
->input
) - 1] = '\0';
377 if (buffer
->current
->next
) {
378 buffer
->current
= buffer
->current
->next
;
384 if (buffer
->current
->prev
) {
385 buffer
->current
= buffer
->current
->prev
;
390 case CONTROL('J'): /* enter */
391 fputs("\r\033[K", stderr
);
392 puts(buffer
->current
->content
);
397 length
= strlen(buffer
->input
);
398 buffer
->input
[length
] = key
;
399 buffer
->input
[length
+ 1] = '\0';
407 * Print the prompt, before the input, with the number of candidates that
411 print_prompt(int matching
, int total
, char *input
)
413 fprintf(stderr
, "\r\033[K%5d/%-5d > %s", matching
, total
, input
);
417 * Reset the terminal state and exit with error.
419 void die(const char *s
)
421 /* tcsetattr(STDIN_FILENO, TCSANOW, &termio_old); */
422 fprintf(stderr
, "%s\n", s
);
428 main(int argc
, char *argv
[])
431 Buffer
*buffer
= NULL
;
432 char *separator
= "* ";
437 /* command line arguments */
438 for (i
= 0; i
<= argc
; i
++) {
439 if (argv
[i
][1] == '-') {
444 /* command line arguments */
445 buffer
= fill_buffer(separator
);
447 /* set the interface */
448 filter_lines(buffer
);
449 update_screen(buffer
, count
, offset
);
451 /* listen and interact to input */
452 get_input(buffer
, count
, offset
);