Added a todo file
[iomenu.git] / draw.c
blob5e0aed404e945ddc5dc4b3a5c8c294f77ad27770
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <sys/ioctl.h>
6 #include "draw.h"
7 #include "util.h"
11 * Print a line to stderr.
13 void
14 draw_line(Line *line, int current, int cols, Opt *opt)
16 size_t i;
17 int n = 0;
18 char *content = expand_tabs(line->content);
19 char *comment = expand_tabs(line->comment);
21 /* clean the line in case it was not empty */
22 fputs("\033[K", stderr);
24 /* line number if option set */
25 if (opt->line_numbers) {
26 fputs(current ? "\033[1m" : "\033[1;30m", stderr);
28 fprintf(stderr, "%7d\033[0m ", line->number);
30 n += 8;
33 /* highlight current line */
34 if (current)
35 fputs("\033[1;33m", stderr);
37 /* print content without overflowing terminal width */
38 for (i = 0; i < strlen(content) && n < cols; n++, i++)
39 fputc(content[i], stderr);
41 /* print spaces without overflowing terminal width */
42 if (!line->header) {
43 for (i = n; i <= 40 && n < cols; n++, i++)
44 fputc(' ', stderr);
47 /* comments in grey */
48 fputs("\033[1;30m", stderr);
50 /* print comment without overflowing terminal width */
51 for (i = 0; i < strlen(comment) && n < cols; n++, i++)
52 fputc(comment[i], stderr);
54 fputs("\033[0m\n", stderr);
56 free(content);
57 free(comment);
62 * Print all the lines from an array of pointer to lines.
64 * The total number oflines printed shall not excess 'count'.
66 void
67 draw_lines(Buffer *buffer, int count, int offset, int cols, Opt *opt)
69 Line *line = buffer->current;
70 int i = 0;
71 int j = 0;
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;
76 line = line->prev;
78 line = line ? line : buffer->first;
80 /* print up to count lines that match the input */
81 while (line && j < count) {
82 if (line->matches) {
83 draw_line(line, line == buffer->current, cols, opt);
84 j++;
87 line = line->next;
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.
101 void
102 draw_screen(Buffer *buffer, int offset, int tty_fd, Opt *opt)
104 struct winsize w;
105 int count;
107 ioctl(tty_fd, TIOCGWINSZ, &w);
108 count = MIN(opt->lines, w.ws_row - 2);
110 fputs("\n", stderr);
111 draw_lines(buffer, count, offset, 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);
119 void draw_clear(int lines)
121 int i;
123 for (i = 0; i < lines + 1; i++)
124 fputs("\r\033[K\n", stderr);
125 fprintf(stderr, "\033[%dA", lines + 1);
130 * Print the prompt, before the input, with the number of candidates that
131 * match.
133 void
134 draw_prompt(Buffer *buffer, int cols, Opt *opt)
136 size_t i;
137 int matching = buffer->matching;
138 int total = buffer->total;
139 char *input = expand_tabs(buffer->input);
140 char *suggest = expand_tabs(buffer->current->content);
142 /* for the '/' separator between the numbers */
143 cols--;
145 /* number of digits */
146 for (i = matching; i; i /= 10, cols--)
148 for (i = total; i; i /= 10, cols--)
151 /* 0 also has one digit*/
152 cols -= !matching ? 1 : 0;
154 /* actual prompt */
155 fprintf(stderr, "\r%s\033[K> ", opt->prompt);
156 cols -= 2 + strlen(opt->prompt);
158 /* input without overflowing terminal width */
159 for (i = 0; i < strlen(input) && cols > 0; cols--, i++)
160 fputc(input[i], stderr);
162 /* save the cursor position at the end of the input */
163 fputs("\033[s", stderr);
165 /* grey */
166 fputs("\033[1;30m", stderr);
168 /* suggest without overflowing terminal width */
169 if (opt->complete) {
170 for (; i < strlen(suggest) && cols > 0; cols--, i++)
171 fputc(suggest[i], stderr);
174 /* go to the end of the line */
175 for (i = 0; cols > 0; cols--, i++)
176 fputc(' ', stderr);
178 /* total match and line count at the end of the line */
179 fprintf(stderr, "%d/%d", matching, total);
181 /* restore cursor position at the end of the input */
182 fputs("\033[0m\033[u", stderr);
184 free(input);
185 free(suggest);