Gentoo Archives: gentoo-commits

From: Sven Eden <sven.eden@×××.de>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/ufed:master commit in: /
Date: Fri, 01 Feb 2013 10:50:07
Message-Id: 1359656382.b07cb5ebe2e03e4be359e898ffcc2a2d0bd1d6dc.yamakuzure@gentoo
1 commit: b07cb5ebe2e03e4be359e898ffcc2a2d0bd1d6dc
2 Author: Sven Eden <sven.eden <AT> gmx <DOT> de>
3 AuthorDate: Thu Jan 31 18:19:42 2013 +0000
4 Commit: Sven Eden <sven.eden <AT> gmx <DOT> de>
5 CommitDate: Thu Jan 31 18:19:42 2013 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/ufed.git;a=commit;h=b07cb5eb
7
8 Changed the help display to use the new flag type. I have used
9 the oportunity to clean up the line initialization which was
10 unneccessarily complicated.
11 Further added a redraw to the borders to not show the new
12 status separators when the help text is to be displayed.
13
14 ---
15 ufed-curses-help.c | 154 ++++++++++++++++++++++++++--------------------------
16 ufed-curses-help.h | 8 +++-
17 2 files changed, 84 insertions(+), 78 deletions(-)
18
19 diff --git a/ufed-curses-help.c b/ufed-curses-help.c
20 index ba71d04..76825d2 100644
21 --- a/ufed-curses-help.c
22 +++ b/ufed-curses-help.c
23 @@ -1,5 +1,4 @@
24 #include "ufed-curses-help.h"
25 -
26 #include "ufed-curses.h"
27
28 #include <ctype.h>
29 @@ -12,13 +11,15 @@
30 #include <unistd.h>
31
32 /* internal types */
33 -static struct line {
34 - struct item item;
35 - char *text;
36 -} *lines;
37 +// Do not use an own struct, just use sFlag
38 +//static struct line {
39 +// struct item item;
40 +// char *text;
41 +//} *lines;
42 +static sFlag* lines = NULL;
43
44 /* internal members */
45 -static int helpheight, helpwidth;
46 +static size_t helpheight, helpwidth;
47
48 /* external members */
49
50 @@ -107,117 +108,116 @@ static void init_lines(void) {
51 "Copyright 1999-2005 Gentoo Foundation",
52 "Distributed under the terms of the GNU General Public License v2"
53 };
54 - struct line *line;
55 - const char * const *paragraph = &help[0], *word = &help[0][0];
56 - int n, y=0;
57 + sFlag* line = NULL;
58 + size_t lineCount = sizeof(help) / sizeof(*help);
59 + size_t currLine = 0;
60 + size_t n = 0;
61 + int y = 0;
62 + const char* word = help[currLine];
63
64 helpheight = wHeight(List);
65 - helpwidth = wWidth(List);
66 + helpwidth = wWidth(List);
67 + char buf[helpwidth + 1];
68 + memset(buf, 0, (helpwidth + 1) * sizeof(char));
69
70 atexit(&free_lines);
71 - for(;;) {
72 - line = malloc(sizeof *line);
73 - if(line==NULL)
74 - ERROR_EXIT(-1, "Can not allocate %lu bytes for help line struct\n", sizeof(*line));
75 - if(lines==NULL) {
76 - line->item.prev = (struct item *) line;
77 - line->item.next = (struct item *) line;
78 - lines = line;
79 - } else {
80 - line->item.next = (struct item *) lines;
81 - line->item.prev = lines->item.prev;
82 - lines->item.prev->next = (struct item *) line;
83 - lines->item.prev = (struct item *) line;
84 - }
85 + while( currLine < lineCount ) {
86 + line = addFlag(&lines, "help", y++, 1, " ");
87
88 - line->item.currline = 0;
89 - line->item.isMasked = false;
90 - line->item.isGlobal = true;
91 - line->item.listline = y++;
92 - line->item.ndescr = 1;
93 + // Find the last space character in the string
94 + // if it is too long to display.
95 n = strlen(word);
96 - if(n > helpwidth-1) {
97 - for(n = helpwidth-1; word[n]!=' '; n--) {
98 - if(n==0) {
99 - n = helpwidth;
100 - break;
101 - }
102 - }
103 + if(n > helpwidth - 1) {
104 + for(n = helpwidth-1; (n > 0) && (word[n] != ' '); --n) ;
105 + if(n==0)
106 + n = helpwidth;
107 }
108 - line->text = calloc((n+1), sizeof(char));
109 - if(line->text==NULL)
110 - ERROR_EXIT(-1, "Can not allocate %lu bytes for help line\n", (n+1) * sizeof(char));
111 - memcpy(line->text, word, n);
112 - while(word[n]==' ')
113 +
114 + // copy the text if there is any
115 + if (n) {
116 + memcpy(buf, word, n);
117 + buf[n++] = '\0';
118 + addFlagDesc(line, NULL, buf, "+ ");
119 + } else
120 + addFlagDesc(line, NULL, " ", "+ ");
121 +
122 + // Advance behind current spaces
123 + while (word[n] == ' ')
124 n++;
125 - word += n;
126 - if(word[0]=='\0') {
127 - paragraph++;
128 - if(paragraph == &help[sizeof help / sizeof *help])
129 - break;
130 - word = &(*paragraph)[0];
131 - }
132 +
133 + // See whether there is text left...
134 + if (strlen(word) > n)
135 + word += n;
136 + else if (++currLine < lineCount)
137 + // ...or advance one line
138 + word = help[currLine];
139 }
140 }
141
142 static void free_lines(void) {
143 - struct line *line = lines;
144 - if(line!=NULL) {
145 - line->item.prev->next = NULL;
146 - do {
147 - void *p = line;
148 - free(line->text);
149 - line = (struct line *) line->item.next;
150 - free(p);
151 - } while(line!=NULL);
152 - lines = NULL;
153 + sFlag* line = lines->prev;
154 +
155 + // Clear all lines
156 + while (lines) {
157 + if (line)
158 + destroyFlag(&lines, &line);
159 + else
160 + destroyFlag(&lines, &lines);
161 + line = lines ? lines->prev ? lines->prev : lines : NULL;
162 }
163 }
164
165 -static const struct key keys[] = {
166 +static const sKey keys[] = {
167 #define key(x) x, sizeof(x)-1
168 { '\033', key("Back (Esc)") },
169 { '\0', key("") }
170 #undef key
171 };
172
173 -static int drawline(struct item *item, bool highlight) {
174 - struct line *line = (struct line *) item;
175 +static int drawline(sFlag* line, bool highlight) {
176 char buf[wWidth(List)+1];
177 - sprintf(buf, "%-*.*s", wWidth(List), wWidth(List), line->text);
178 +
179 + sprintf(buf, "%-*.*s", wWidth(List), wWidth(List), line->desc[0].desc);
180 if(!highlight)
181 wattrset(win(List), COLOR_PAIR(3));
182 else
183 wattrset(win(List), COLOR_PAIR(3) | A_BOLD | A_REVERSE);
184 - mvwaddstr(win(List), line->item.currline, 0, buf);
185 + mvwaddstr(win(List), line->currline, 0, buf);
186 if(highlight)
187 - wmove(win(List), line->item.currline, 0);
188 + wmove(win(List), line->currline, 0);
189 wnoutrefresh(win(List));
190 return 1;
191 }
192
193 -static int callback(struct item **currentitem, int key) {
194 +static int callback(sFlag** curr, int key) {
195 switch(key) {
196 - case 'Q': case 'q':
197 - case '\033':
198 - return 0;
199 + case 'Q': case 'q':
200 + case '\033':
201 + return 0;
202 #ifdef KEY_RESIZE
203 - case KEY_RESIZE:
204 - free_lines();
205 - init_lines();
206 - *currentitem = (struct item *) lines;
207 - return -2;
208 + case KEY_RESIZE:
209 + free_lines();
210 + init_lines();
211 + *curr = lines;
212 + return -2;
213 #endif
214 - default:
215 - return -1;
216 + default:
217 + return -1;
218 }
219 }
220
221 void help(void) {
222 - if(helpheight!=wHeight(List) || helpwidth!=wWidth(List)) {
223 + if ( ((int)helpheight != wHeight(List))
224 + || ((int)helpwidth != wWidth(List)) ) {
225 if(lines!=NULL)
226 free_lines();
227 init_lines();
228 }
229 - maineventloop("", &callback, &drawline, (struct item *) lines, keys);
230 +
231 + maineventloop("", &callback, &drawline, lines, keys, false);
232 +
233 + // Re-draw separators:
234 + drawTop(true);
235 + drawBottom(true);
236 + drawStatus(true);
237 }
238
239 diff --git a/ufed-curses-help.h b/ufed-curses-help.h
240 index 5c66482..4cc0df0 100644
241 --- a/ufed-curses-help.h
242 +++ b/ufed-curses-help.h
243 @@ -1 +1,7 @@
244 -extern void help(void);
245 +#pragma once
246 +#ifndef UFED_CURSES_HELP_H_INCLUDED
247 +#define UFED_CURSES_HELP_H_INCLUDED
248 +
249 +void help(void);
250 +
251 +#endif /* UFED_CURSES_HELP_H_INCLUDED */