Permitting file as argument
[iomenu.git] / draw.c
blobcb2962ae10ef9775e728a787bd300e551dadd14e
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <sys/ioctl.h>
6 #include "main.h"
9 /*
10 * Print a line to stderr.
12 void
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";
18 int n = 0;
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);
23 } else {
24 strcat(output, current ? "\033[1;31m > " : " ");
26 n += 8;
29 /* highlight current line */
30 if (current)
31 strcat(output, "\033[1;33m");
33 /* content */
34 strncat(output, content, cols - n);
35 n += strlen(content);
37 /* align comment */
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",
41 MAX(1, 40 - n));
42 n += MAX(1, 40 - n);
43 } else if (line->header)
45 /* comment */
46 strcat(output, "\033[1;30m");
47 strncat(output, comment, cols - n);
48 n += strlen(comment);
50 strcat(output, "\033[m\n");
52 fputs(output, stderr);
54 free(content);
55 free(comment);
60 * Print all the lines from an array of pointer to lines.
62 * The total number oflines printed shall not excess 'count'.
64 void
65 draw_lines(Buffer *buffer, int count, int cols, Opt *opt)
67 Line *line = buffer->current;
68 int i = 0;
69 int j = 0;
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;
74 line = line->prev;
76 line = line ? line : buffer->first;
78 /* print up to count lines that match the input */
79 while (line && j < count) {
80 if (line->matches) {
81 draw_line(line, line == buffer->current, cols, opt);
82 j++;
85 line = line->next;
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.
99 void
100 draw_screen(Buffer *buffer, int tty_fd, Opt *opt)
102 struct winsize w;
103 int count;
105 if (ioctl(tty_fd, TIOCGWINSZ, &w) < 0)
106 die("could not get terminal size");
108 count = MIN(opt->lines, w.ws_row - 2);
110 fputs("\n", stderr);
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);
119 void
120 draw_clear(int lines)
122 int i;
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
132 * match.
134 void
135 draw_prompt(Buffer *buffer, int cols, Opt *opt)
137 size_t i;
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 */
144 cols--;
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*/
151 /* actual prompt */
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);
162 /* grey */
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);
174 free(input);
175 free(suggest);