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";
21 strcat(output
, current
? "\033[1;31m>" : " ");
25 if (opt
->line_numbers
) {
26 strcat(output
, current
? "\033[1;37m" : "\033[1;30m");
27 sprintf(output
+ strlen(output
), "%7d \033[0m", line
->number
);
34 /* highlight current line */
36 strcat(output
, "\033[1;32m");
39 strncat(output
, content
, cols
- n
);
43 if (!line
->header
&& line
->comment
[0] != '\0') {
44 /* MAX with '1' as \033[0C still move 1 to the right */
45 sprintf(output
+ strlen(output
), "\033[%dC",
48 } else if (line
->header
)
51 strcat(output
, "\033[1;30m");
52 strncat(output
, comment
, cols
- n
);
55 strcat(output
, "\033[0m\n");
57 fputs(output
, stderr
);
65 * Print all the lines from an array of pointer to lines.
67 * The total number oflines printed shall not excess 'count'.
70 draw_lines(Buffer
*buffer
, int count
, int cols
, Opt
*opt
)
72 Line
*line
= buffer
->current
;
76 /* seek back from current line to the first line to print */
77 while (line
&& i
< count
- OFFSET
) {
78 i
= line
->matches
? i
+ 1 : i
;
81 line
= line
? line
: buffer
->first
;
83 /* print up to count lines that match the input */
84 while (line
&& j
< count
) {
86 draw_line(line
, line
== buffer
->current
, cols
, opt
);
93 /* continue up to the end of the screen clearing it */
94 for (; j
< count
; j
++)
95 fputs("\r\033[K\n", stderr
);
100 * Update the screen interface and print all candidates.
102 * This also has to clear the previous lines.
105 draw_screen(Buffer
*buffer
, int tty_fd
, Opt
*opt
)
110 if (ioctl(tty_fd
, TIOCGWINSZ
, &w
) < 0)
111 die("could not get terminal size");
113 count
= MIN(opt
->lines
, w
.ws_row
- 2);
116 draw_lines(buffer
, count
, w
.ws_col
, opt
);
118 /* go up to the prompt position and update it */
119 fprintf(stderr
, "\033[%dA", count
+ 1);
120 draw_prompt(buffer
, w
.ws_col
, opt
);
125 draw_clear(int lines
)
129 for (i
= 0; i
< lines
+ 1; i
++)
130 fputs("\r\033[K\n", stderr
);
131 fprintf(stderr
, "\033[%dA", lines
+ 1);
136 * Print the prompt, before the input, with the number of candidates that
140 draw_prompt(Buffer
*buffer
, int cols
, Opt
*opt
)
143 int matching
= buffer
->matching
;
144 int total
= buffer
->total
;
145 char *input
= expand_tabs(buffer
->input
);
146 char *suggest
= expand_tabs(buffer
->current
->content
);
148 /* for the '/' separator between the numbers */
151 /* number of digits */
152 for (i
= matching
; i
; i
/= 10, cols
--);
153 for (i
= total
; i
; i
/= 10, cols
--);
154 cols
-= !matching
? 1 : 0; /* 0 also has one digit*/
157 fprintf(stderr
, "\r%s\033[K> ", opt
->prompt
);
158 cols
-= 2 + strlen(opt
->prompt
);
160 /* input without overflowing terminal width */
161 for (i
= 0; i
< strlen(input
) && cols
> 0; cols
--, i
++)
162 fputc(input
[i
], stderr
);
164 /* save the cursor position at the end of the input */
165 fputs("\033[s", stderr
);
168 fputs("\033[1;30m", stderr
);
170 /* go to the end of the line */
171 fprintf(stderr
, "\033[%dC", cols
);
173 /* total match and line count at the end of the line */
174 fprintf(stderr
, "%d/%d", matching
, total
);
176 /* restore cursor position at the end of the input */
177 fputs("\033[0m\033[u", stderr
);