Fossil SCM

Factor out the common terminal width detection code for comment printing into a new function.

mistachkin 2014-07-24 05:05 trunk
Commit 351de029f3b5a009446dc4ab4737d34bf995e845
1 file changed +55 -60
+55 -60
--- src/comformat.c
+++ src/comformat.c
@@ -52,10 +52,51 @@
5252
** This is the number of spaces to print when a tab character is seen.
5353
*/
5454
#ifndef COMMENT_TAB_WIDTH
5555
# define COMMENT_TAB_WIDTH (8)
5656
#endif
57
+
58
+/*
59
+** This function sets the maximum number of characters to print per line
60
+** based on the detected terminal line width, if available; otherwise, it
61
+** uses the legacy default terminal line width minus the amount to indent.
62
+**
63
+** Zero is returned to indicate any failure. One is returned to indicate
64
+** the successful detection of the terminal line width. Negative one is
65
+** returned to indicate the terminal line width is using the hard-coded
66
+** legacy default value.
67
+*/
68
+static int comment_set_maxchars(
69
+ int indent,
70
+ int *pMaxChars
71
+){
72
+#if defined(_WIN32)
73
+ CONSOLE_SCREEN_BUFFER_INFO csbi;
74
+ memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
75
+ if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){
76
+ *pMaxChars = csbi.srWindow.Right - csbi.srWindow.Left - indent;
77
+ return 1;
78
+ }
79
+ return 0;
80
+#elif defined(TIOCGWINSZ)
81
+ struct winsize w;
82
+ memset(&w, 0, sizeof(struct winsize));
83
+ if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){
84
+ *pMaxChars = w.ws_col - indent;
85
+ return 1;
86
+ }
87
+ return 0;
88
+#else
89
+ /*
90
+ ** Fallback to using more-or-less the "legacy semantics" of hard-coding
91
+ ** the maximum line length to a value reasonable for the vast majority
92
+ ** of supported systems.
93
+ */
94
+ *pMaxChars = COMMENT_LEGACY_LINE_LENGTH - indent;
95
+ return -1;
96
+#endif
97
+}
5798
5899
/*
59100
** This function checks the current line being printed against the original
60101
** comment text. Upon matching, it emits a new line and updates the provided
61102
** character and line counts, if applicable.
@@ -208,49 +249,26 @@
208249
static int comment_print_legacy(
209250
const char *zText, /* The comment text to be printed. */
210251
int indent, /* Number of spaces to indent each non-initial line. */
211252
int width /* Maximum number of characters per line. */
212253
){
213
- int tlen = width - indent;
254
+ int maxChars = width - indent;
214255
int si, sk, i, k;
215256
int doIndent = 0;
216257
char *zBuf;
217258
char zBuffer[400];
218259
int lineCnt = 0;
219260
220
-#if defined(_WIN32)
221
- if( width<0 ){
222
- CONSOLE_SCREEN_BUFFER_INFO csbi;
223
- memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
224
- if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){
225
- tlen = csbi.srWindow.Right - csbi.srWindow.Left - indent;
226
- }
227
- }
228
-#elif defined(TIOCGWINSZ)
229
- if( width<0 ){
230
- struct winsize w;
231
- memset(&w, 0, sizeof(struct winsize));
232
- if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){
233
- tlen = w.ws_col - indent;
234
- }
235
- }
236
-#else
237
- if( width<0 ){
238
- /*
239
- ** Fallback to using more-or-less the "legacy semantics" of hard-coding
240
- ** the maximum line length to a value reasonable for the vast majority
241
- ** of supported systems.
242
- */
243
- tlen = COMMENT_LEGACY_LINE_LENGTH - indent;
244
- }
245
-#endif
246
- if( zText==0 ) zText = "(NULL)";
247
- if( tlen<=0 ){
248
- tlen = strlen(zText);
249
- }
250
- if( tlen >= (sizeof(zBuffer)) ){
251
- zBuf = fossil_malloc(tlen+1);
261
+ if( width<0 ){
262
+ comment_set_maxchars(indent, &maxChars);
263
+ }
264
+ if( zText==0 ) zText = "(NULL)";
265
+ if( maxChars<=0 ){
266
+ maxChars = strlen(zText);
267
+ }
268
+ if( maxChars >= (sizeof(zBuffer)) ){
269
+ zBuf = fossil_malloc(maxChars+1);
252270
}else{
253271
zBuf = zBuffer;
254272
}
255273
for(;;){
256274
while( fossil_isspace(zText[0]) ){ zText++; }
@@ -260,11 +278,11 @@
260278
lineCnt = 1;
261279
}
262280
if( zBuf!=zBuffer) fossil_free(zBuf);
263281
return lineCnt;
264282
}
265
- for(sk=si=i=k=0; zText[i] && k<tlen; i++){
283
+ for(sk=si=i=k=0; zText[i] && k<maxChars; i++){
266284
char c = zText[i];
267285
if( fossil_isspace(c) ){
268286
si = i;
269287
sk = k;
270288
if( k==0 || zBuf[k-1]!=' ' ){
@@ -348,36 +366,13 @@
348366
const char *zLine;
349367
350368
if( legacy ){
351369
return comment_print_legacy(zText, indent, width);
352370
}
353
-#if defined(_WIN32)
354
- if( width<0 ){
355
- CONSOLE_SCREEN_BUFFER_INFO csbi;
356
- memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
357
- if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){
358
- maxChars = csbi.srWindow.Right - csbi.srWindow.Left - indent;
359
- }
360
- }
361
-#elif defined(TIOCGWINSZ)
362
- if( width<0 ){
363
- struct winsize w;
364
- memset(&w, 0, sizeof(struct winsize));
365
- if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){
366
- maxChars = w.ws_col - indent;
367
- }
368
- }
369
-#else
370
- if( width<0 ){
371
- /*
372
- ** Fallback to using more-or-less the "legacy semantics" of hard-coding
373
- ** the maximum line length to a value reasonable for the vast majority
374
- ** of supported systems.
375
- */
376
- maxChars = COMMENT_LEGACY_LINE_LENGTH - indent;
377
- }
378
-#endif
371
+ if( width<0 ){
372
+ comment_set_maxchars(indent, &maxChars);
373
+ }
379374
if( zText==0 ) zText = "(NULL)";
380375
if( maxChars<=0 ){
381376
maxChars = strlen(zText);
382377
}
383378
if( trimSpace ){
384379
--- src/comformat.c
+++ src/comformat.c
@@ -52,10 +52,51 @@
52 ** This is the number of spaces to print when a tab character is seen.
53 */
54 #ifndef COMMENT_TAB_WIDTH
55 # define COMMENT_TAB_WIDTH (8)
56 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
58 /*
59 ** This function checks the current line being printed against the original
60 ** comment text. Upon matching, it emits a new line and updates the provided
61 ** character and line counts, if applicable.
@@ -208,49 +249,26 @@
208 static int comment_print_legacy(
209 const char *zText, /* The comment text to be printed. */
210 int indent, /* Number of spaces to indent each non-initial line. */
211 int width /* Maximum number of characters per line. */
212 ){
213 int tlen = width - indent;
214 int si, sk, i, k;
215 int doIndent = 0;
216 char *zBuf;
217 char zBuffer[400];
218 int lineCnt = 0;
219
220 #if defined(_WIN32)
221 if( width<0 ){
222 CONSOLE_SCREEN_BUFFER_INFO csbi;
223 memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
224 if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){
225 tlen = csbi.srWindow.Right - csbi.srWindow.Left - indent;
226 }
227 }
228 #elif defined(TIOCGWINSZ)
229 if( width<0 ){
230 struct winsize w;
231 memset(&w, 0, sizeof(struct winsize));
232 if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){
233 tlen = w.ws_col - indent;
234 }
235 }
236 #else
237 if( width<0 ){
238 /*
239 ** Fallback to using more-or-less the "legacy semantics" of hard-coding
240 ** the maximum line length to a value reasonable for the vast majority
241 ** of supported systems.
242 */
243 tlen = COMMENT_LEGACY_LINE_LENGTH - indent;
244 }
245 #endif
246 if( zText==0 ) zText = "(NULL)";
247 if( tlen<=0 ){
248 tlen = strlen(zText);
249 }
250 if( tlen >= (sizeof(zBuffer)) ){
251 zBuf = fossil_malloc(tlen+1);
252 }else{
253 zBuf = zBuffer;
254 }
255 for(;;){
256 while( fossil_isspace(zText[0]) ){ zText++; }
@@ -260,11 +278,11 @@
260 lineCnt = 1;
261 }
262 if( zBuf!=zBuffer) fossil_free(zBuf);
263 return lineCnt;
264 }
265 for(sk=si=i=k=0; zText[i] && k<tlen; i++){
266 char c = zText[i];
267 if( fossil_isspace(c) ){
268 si = i;
269 sk = k;
270 if( k==0 || zBuf[k-1]!=' ' ){
@@ -348,36 +366,13 @@
348 const char *zLine;
349
350 if( legacy ){
351 return comment_print_legacy(zText, indent, width);
352 }
353 #if defined(_WIN32)
354 if( width<0 ){
355 CONSOLE_SCREEN_BUFFER_INFO csbi;
356 memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
357 if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){
358 maxChars = csbi.srWindow.Right - csbi.srWindow.Left - indent;
359 }
360 }
361 #elif defined(TIOCGWINSZ)
362 if( width<0 ){
363 struct winsize w;
364 memset(&w, 0, sizeof(struct winsize));
365 if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){
366 maxChars = w.ws_col - indent;
367 }
368 }
369 #else
370 if( width<0 ){
371 /*
372 ** Fallback to using more-or-less the "legacy semantics" of hard-coding
373 ** the maximum line length to a value reasonable for the vast majority
374 ** of supported systems.
375 */
376 maxChars = COMMENT_LEGACY_LINE_LENGTH - indent;
377 }
378 #endif
379 if( zText==0 ) zText = "(NULL)";
380 if( maxChars<=0 ){
381 maxChars = strlen(zText);
382 }
383 if( trimSpace ){
384
--- src/comformat.c
+++ src/comformat.c
@@ -52,10 +52,51 @@
52 ** This is the number of spaces to print when a tab character is seen.
53 */
54 #ifndef COMMENT_TAB_WIDTH
55 # define COMMENT_TAB_WIDTH (8)
56 #endif
57
58 /*
59 ** This function sets the maximum number of characters to print per line
60 ** based on the detected terminal line width, if available; otherwise, it
61 ** uses the legacy default terminal line width minus the amount to indent.
62 **
63 ** Zero is returned to indicate any failure. One is returned to indicate
64 ** the successful detection of the terminal line width. Negative one is
65 ** returned to indicate the terminal line width is using the hard-coded
66 ** legacy default value.
67 */
68 static int comment_set_maxchars(
69 int indent,
70 int *pMaxChars
71 ){
72 #if defined(_WIN32)
73 CONSOLE_SCREEN_BUFFER_INFO csbi;
74 memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
75 if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){
76 *pMaxChars = csbi.srWindow.Right - csbi.srWindow.Left - indent;
77 return 1;
78 }
79 return 0;
80 #elif defined(TIOCGWINSZ)
81 struct winsize w;
82 memset(&w, 0, sizeof(struct winsize));
83 if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){
84 *pMaxChars = w.ws_col - indent;
85 return 1;
86 }
87 return 0;
88 #else
89 /*
90 ** Fallback to using more-or-less the "legacy semantics" of hard-coding
91 ** the maximum line length to a value reasonable for the vast majority
92 ** of supported systems.
93 */
94 *pMaxChars = COMMENT_LEGACY_LINE_LENGTH - indent;
95 return -1;
96 #endif
97 }
98
99 /*
100 ** This function checks the current line being printed against the original
101 ** comment text. Upon matching, it emits a new line and updates the provided
102 ** character and line counts, if applicable.
@@ -208,49 +249,26 @@
249 static int comment_print_legacy(
250 const char *zText, /* The comment text to be printed. */
251 int indent, /* Number of spaces to indent each non-initial line. */
252 int width /* Maximum number of characters per line. */
253 ){
254 int maxChars = width - indent;
255 int si, sk, i, k;
256 int doIndent = 0;
257 char *zBuf;
258 char zBuffer[400];
259 int lineCnt = 0;
260
261 if( width<0 ){
262 comment_set_maxchars(indent, &maxChars);
263 }
264 if( zText==0 ) zText = "(NULL)";
265 if( maxChars<=0 ){
266 maxChars = strlen(zText);
267 }
268 if( maxChars >= (sizeof(zBuffer)) ){
269 zBuf = fossil_malloc(maxChars+1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270 }else{
271 zBuf = zBuffer;
272 }
273 for(;;){
274 while( fossil_isspace(zText[0]) ){ zText++; }
@@ -260,11 +278,11 @@
278 lineCnt = 1;
279 }
280 if( zBuf!=zBuffer) fossil_free(zBuf);
281 return lineCnt;
282 }
283 for(sk=si=i=k=0; zText[i] && k<maxChars; i++){
284 char c = zText[i];
285 if( fossil_isspace(c) ){
286 si = i;
287 sk = k;
288 if( k==0 || zBuf[k-1]!=' ' ){
@@ -348,36 +366,13 @@
366 const char *zLine;
367
368 if( legacy ){
369 return comment_print_legacy(zText, indent, width);
370 }
371 if( width<0 ){
372 comment_set_maxchars(indent, &maxChars);
373 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374 if( zText==0 ) zText = "(NULL)";
375 if( maxChars<=0 ){
376 maxChars = strlen(zText);
377 }
378 if( trimSpace ){
379

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button