10 * Print a line to stderr.
13 draw_line(Line
*line
, int current
, 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)] = "";
20 strncat(output
, "\033[K", cols
);
23 strncat(output
, current
? "\033[1;31m>" : " ", cols
--);
25 if (opt
->line_numbers
) {
26 strcat(output
, current
? "\033[1;37m" : "\033[1;30m");
27 sprintf(output
, "%7d \033[0m", line
->number
);
30 strncat(output
, " ", cols
--);
33 /* highlight current line */
35 strcat(output
, "\033[1;33m");
37 /* print content without overflowing terminal width */
38 strncat(output
, content
, cols
);
39 cols
-= strlen(content
);
41 /* shift without overflowing terminal width */
42 if (!line
->header
&& line
->comment
[0] != '\0') {
43 /* MAX with '1' as \033[0C still move 1 to the right */
44 sprintf(content
, "\033[%dC\033[1;30m", MIN(40, cols
));
45 cols
-= MIN(40, cols
);
48 /* print comment without overflowing terminal width */
49 strncat(output
, comment
, cols
);
50 cols
-= strlen(comment
);
52 strcat(output
, "\033[0m\n");
54 fputs(output
, stderr
);
62 * Print all the lines from an array of pointer to lines.
64 * The total number oflines printed shall not excess 'count'.
67 draw_lines(Buffer
*buffer
, int count
, int cols
, Opt
*opt
)
69 Line
*line
= buffer
->current
;
73 /* seek back from current line to the first line to print */
74 while (line
&& i
< count
- OFFSET
) {
75 i
= line
->matches
? i
+ 1 : i
;
78 line
= line
? line
: buffer
->first
;
80 /* print up to count lines that match the input */
81 while (line
&& j
< count
) {
83 draw_line(line
, line
== buffer
->current
, cols
, opt
);
90 /* continue up to the end of the screen clearing it */
91 for (; j
< count
; j
++)
92 fputs("\r\033[K\n", stderr
);
97 * Update the screen interface and print all candidates.
99 * This also has to clear the previous lines.
102 draw_screen(Buffer
*buffer
, int tty_fd
, Opt
*opt
)
107 ioctl(tty_fd
, TIOCGWINSZ
, &w
);
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
--);
150 /* 0 also has one digit*/
151 cols
-= !matching
? 1 : 0;
154 fprintf(stderr
, "\r%s\033[K> ", opt
->prompt
);
155 cols
-= 2 + strlen(opt
->prompt
);
157 /* input without overflowing terminal width */
158 for (i
= 0; i
< strlen(input
) && cols
> 0; cols
--, i
++)
159 fputc(input
[i
], stderr
);
161 /* save the cursor position at the end of the input */
162 fputs("\033[s", stderr
);
165 fputs("\033[1;30m", stderr
);
167 /* go to the end of the line */
168 fprintf(stderr
, "\033[%dC", cols
);
170 /* total match and line count at the end of the line */
171 fprintf(stderr
, "%d/%d", matching
, total
);
173 /* restore cursor position at the end of the input */
174 fputs("\033[0m\033[u", stderr
);