10 * Print a line to stderr.
13 draw_line(Line
*line
, int current
, int cols
, Opt
*opt
)
17 char *content
= expand_tabs(line
->content
);
18 char *comment
= expand_tabs(line
->comment
);
21 fputs("\033[K", stderr
);
24 fputs(current
? "\033[1;31m>" : " ", stderr
);
28 if (opt
->line_numbers
) {
29 fprintf(stderr
, current
30 ? "\033[1;37m%7d\033[0m "
31 : "\033[1;30m%7d\033[0m ",
39 /* highlight current line */
41 fputs("\033[1;33m", stderr
);
43 /* print content without overflowing terminal width */
44 for (i
= 0; i
< strlen(content
) && n
< cols
; n
++, i
++)
45 fputc(content
[i
], stderr
);
47 /* shift without overflowing terminal width */
48 if (!line
->header
&& line
->comment
[0] != '\0') {
49 /* MAX with '1' as \033[0C still move 1 to the right */
50 fprintf(stderr
, "\033[%dC", MAX(40 - n
, 1));
54 /* comments in grey */
55 fputs("\033[1;30m", stderr
);
57 /* print comment without overflowing terminal width */
58 for (i
= 0; i
< strlen(comment
) && n
< cols
; n
++, i
++)
59 fputc(comment
[i
], stderr
);
61 fputs("\033[0m\n", stderr
);
69 * Print all the lines from an array of pointer to lines.
71 * The total number oflines printed shall not excess 'count'.
74 draw_lines(Buffer
*buffer
, int count
, int cols
, Opt
*opt
)
76 Line
*line
= buffer
->current
;
80 /* seek back from current line to the first line to print */
81 while (line
&& i
< count
- OFFSET
) {
82 i
= line
->matches
? i
+ 1 : i
;
85 line
= line
? line
: buffer
->first
;
87 /* print up to count lines that match the input */
88 while (line
&& j
< count
) {
90 draw_line(line
, line
== buffer
->current
, cols
, opt
);
97 /* continue up to the end of the screen clearing it */
98 for (; j
< count
; j
++)
99 fputs("\r\033[K\n", stderr
);
104 * Update the screen interface and print all candidates.
106 * This also has to clear the previous lines.
109 draw_screen(Buffer
*buffer
, int tty_fd
, Opt
*opt
)
114 ioctl(tty_fd
, TIOCGWINSZ
, &w
);
115 count
= MIN(opt
->lines
, w
.ws_row
- 2);
118 draw_lines(buffer
, count
, w
.ws_col
, opt
);
120 /* go up to the prompt position and update it */
121 fprintf(stderr
, "\033[%dA", count
+ 1);
122 draw_prompt(buffer
, w
.ws_col
, opt
);
127 draw_clear(int lines
)
131 for (i
= 0; i
< lines
+ 1; i
++)
132 fputs("\r\033[K\n", stderr
);
133 fprintf(stderr
, "\033[%dA", lines
+ 1);
138 * Print the prompt, before the input, with the number of candidates that
142 draw_prompt(Buffer
*buffer
, int cols
, Opt
*opt
)
145 int matching
= buffer
->matching
;
146 int total
= buffer
->total
;
147 char *input
= expand_tabs(buffer
->input
);
148 char *suggest
= expand_tabs(buffer
->current
->content
);
150 /* for the '/' separator between the numbers */
153 /* number of digits */
154 for (i
= matching
; i
; i
/= 10, cols
--);
155 for (i
= total
; i
; i
/= 10, cols
--);
157 /* 0 also has one digit*/
158 cols
-= !matching
? 1 : 0;
161 fprintf(stderr
, "\r%s\033[K> ", opt
->prompt
);
162 cols
-= 2 + strlen(opt
->prompt
);
164 /* input without overflowing terminal width */
165 for (i
= 0; i
< strlen(input
) && cols
> 0; cols
--, i
++)
166 fputc(input
[i
], stderr
);
168 /* save the cursor position at the end of the input */
169 fputs("\033[s", stderr
);
172 fputs("\033[1;30m", stderr
);
174 /* go to the end of the line */
175 fprintf(stderr
, "\033[%dC", cols
);
177 /* total match and line count at the end of the line */
178 fprintf(stderr
, "%d/%d", matching
, total
);
180 /* restore cursor position at the end of the input */
181 fputs("\033[0m\033[u", stderr
);