10 * Print a line to stderr.
13 draw_line(Line
*line
, int current
, const int cols
, Opt
*opt
)
15 char *content
= expand_tabs(line
->content
);
16 char *comment
= expand_tabs(line
->comment
);
17 char output
[LINE_SIZE
* sizeof(char)] = "\033[K";
20 if (opt
->line_numbers
&& !line
->header
) {
21 strcat(output
, current
? "\033[1;37m" : "\033[1;30m");
22 sprintf(output
+ strlen(output
), "%7d\033[m ", line
->number
);
24 strcat(output
, current
? "\033[1;31m > " : " ");
29 /* highlight current line */
31 strcat(output
, "\033[1;33m");
34 strncat(output
, content
, cols
- n
);
38 if (!line
->header
&& line
->comment
[0] != '\0') {
39 /* MAX with '1' as \033[0C still move 1 to the right */
40 sprintf(output
+ strlen(output
), "\033[%dC",
43 } else if (line
->header
)
46 strcat(output
, "\033[1;30m");
47 strncat(output
, comment
, cols
- n
);
50 strcat(output
, "\033[m\n");
52 fputs(output
, stderr
);
60 * Print all the lines from an array of pointer to lines.
62 * The total number oflines printed shall not excess 'count'.
65 draw_lines(Buffer
*buffer
, int count
, int cols
, Opt
*opt
)
67 Line
*line
= buffer
->current
;
71 /* seek back from current line to the first line to print */
72 while (line
&& i
< count
- OFFSET
) {
73 i
= line
->matches
? i
+ 1 : i
;
76 line
= line
? line
: buffer
->first
;
78 /* print up to count lines that match the input */
79 while (line
&& j
< count
) {
81 draw_line(line
, line
== buffer
->current
, cols
, opt
);
88 /* continue up to the end of the screen clearing it */
89 for (; j
< count
; j
++)
90 fputs("\r\033[K\n", stderr
);
95 * Update the screen interface and print all candidates.
97 * This also has to clear the previous lines.
100 draw_screen(Buffer
*buffer
, int tty_fd
, Opt
*opt
)
105 if (ioctl(tty_fd
, TIOCGWINSZ
, &w
) < 0)
106 die("could not get terminal size");
108 count
= MIN(opt
->lines
, w
.ws_row
- 2);
111 draw_lines(buffer
, count
, w
.ws_col
, opt
);
113 /* go up to the prompt position and update it */
114 fprintf(stderr
, "\033[%dA", count
+ 1);
115 draw_prompt(buffer
, w
.ws_col
, opt
);
120 draw_clear(int lines
)
124 for (i
= 0; i
< lines
+ 1; i
++)
125 fputs("\r\033[K\n", stderr
);
126 fprintf(stderr
, "\033[%dA", lines
+ 1);
131 * Print the prompt, before the input, with the number of candidates that
135 draw_prompt(Buffer
*buffer
, int cols
, Opt
*opt
)
138 int matching
= buffer
->matching
;
139 int total
= buffer
->total
;
140 char *input
= expand_tabs(buffer
->input
);
141 char *suggest
= expand_tabs(buffer
->current
->content
);
143 /* for the '/' separator between the numbers */
146 /* number of digits */
147 for (i
= matching
; i
; i
/= 10, cols
--);
148 for (i
= total
; i
; i
/= 10, cols
--);
149 cols
-= !matching
? 1 : 0; /* 0 also has one digit*/
152 fprintf(stderr
, "\r%-6s\033[K\033[1m>\033[m ", opt
->prompt
);
153 cols
-= 2 + MAX(strlen(opt
->prompt
), 6);
155 /* input without overflowing terminal width */
156 for (i
= 0; i
< strlen(input
) && cols
> 0; cols
--, i
++)
157 fputc(input
[i
], stderr
);
159 /* save the cursor position at the end of the input */
160 fputs("\033[s", stderr
);
163 fputs("\033[1;30m", stderr
);
165 /* go to the end of the line */
166 fprintf(stderr
, "\033[%dC", cols
);
168 /* total match and line count at the end of the line */
169 fprintf(stderr
, "%d/%d", matching
, total
);
171 /* restore cursor position at the end of the input */
172 fputs("\033[m\033[u", stderr
);