25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# include <windows.h>
#else
# include <termios.h>
#endif
#if INTERFACE
#define COMMENT_PRINT_NONE ((u32)0x00000000) /* No flags. */
#define COMMENT_PRINT_WORD_BREAK ((u32)0x00000001) /* Break lines on words. */
#define COMMENT_PRINT_DEFAULT COMMENT_PRINT_NONE /* Default flags. */
#endif
/*
** This is the previous value used by most external callers when they
** needed to specify a default maximum line length to be used with the
** comment_print() function.
*/
#ifndef COMMENT_LEGACY_LINE_LENGTH
# define COMMENT_LEGACY_LINE_LENGTH (78)
#endif
/*
** This is the number of spaces to print when a tab character is seen.
*/
#ifndef COMMENT_TAB_WIDTH
# define COMMENT_TAB_WIDTH (8)
#endif
/*
** Given a comment string zText, format that string for printing
** on a TTY. Assume that the output cursors is indent spaces from
** the left margin and that a single line can contain no more than
** width characters. Indent all subsequent lines by indent.
**
** Return the number of newlines that are output.
*/
int comment_print(
const char *zText, /* The comment text to be printed. */
int indent, /* Number of spaces to indent each non-initial line. */
int width, /* Maximum number of characters per line. */
int flags /* Zero or more "COMMENT_PRINT_*" flags, see above. */
){
int maxChars = width - indent;
int lineCnt = 0;
const char *zLine;
#if defined(_WIN32)
if( width<0 ){
CONSOLE_SCREEN_BUFFER_INFO csbi;
memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){
maxChars = csbi.srWindow.Right - csbi.srWindow.Left - indent;
}
}
#elif defined(TIOCGWINSZ)
if( width<0 ){
struct winsize w;
memset(&w, 0, sizeof(struct winsize));
if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){
maxChars = w.ws_col - indent;
}
}
#else
if( width<0 ){
/*
** Fallback to using more-or-less the "legacy semantics" of hard-coding
** the maximum line length to a value reasonable for the vast majority
** of supported systems.
*/
maxChars = COMMENT_LEGACY_LINE_LENGTH - indent;
}
#endif
if( zText==0 ) zText = "(NULL)";
if( maxChars<=0 ){
maxChars = strlen(zText);
}
while( fossil_isspace(zText[0]) ){ zText++; }
if( zText[0]==0 ){
fossil_print("\n");
lineCnt++;
return lineCnt;
}
zLine = zText;
for(;;){
comment_print_line(zLine, zLine>zText ? indent : 0, maxChars,
flags & COMMENT_PRINT_WORD_BREAK, &lineCnt,
&zLine);
if( !zLine || !zLine[0] ) break;
}
return lineCnt;
}
/*
** This function prints one logical line of a comment, stopping when it hits
** a new line -OR- runs out of space on the logical line.
*/
void comment_print_line(
const char *zLine, /* [in] The comment line to print. */
int indent, /* [in] Number of spaces to indent, zero for none. */
int lineChars, /* [in] Maximum number of characters to print. */
int wordBreak, /* [in] Non-zero to try breaking on word boundaries. */
int *pLineCnt, /* [in/out] Pointer to the total line count. */
const char **pzLine /* [out] Pointer to the end of the logical line. */
){
int index = 0, charCnt = 0, lineCnt = 0, maxChars;
if( !zLine ) return;
if( lineChars<=0 ) return;
comment_print_indent(zLine, indent, &index);
maxChars = lineChars;
for(;;){
char c = zLine[index];
if( c==0 ){
break;
}else{
index++;
|
>
|
|
|
|
|
|
<
<
|
|
|
<
<
<
|
>
|
>
>
|
|
<
<
<
<
<
|
>
|
<
<
<
<
<
<
|
<
<
<
|
<
>
|
<
|
|
<
<
<
>
>
>
>
|
<
<
<
|
|
|
>
>
|
|
<
<
<
<
<
<
<
|
>
|
|
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# include <windows.h>
#else
# include <termios.h>
#endif
#if INTERFACE
#define COMMENT_PRINT_NONE ((u32)0x00000000) /* No flags. */
#define COMMENT_PRINT_TRIM_SPACE ((u32)0x00000001) /* Trim leading/trailing. */
#define COMMENT_PRINT_WORD_BREAK ((u32)0x00000002) /* Break lines on words. */
#define COMMENT_PRINT_DEFAULT (COMMENT_PRINT_TRIM_SPACE) /* Defaults. */
#endif
/*
** This is the previous value used by most external callers when they
** needed to specify a default maximum line length to be used with the
** comment_print() function.
*/
#ifndef COMMENT_LEGACY_LINE_LENGTH
# define COMMENT_LEGACY_LINE_LENGTH (78)
#endif
/*
** This is the number of spaces to print when a tab character is seen.
*/
#ifndef COMMENT_TAB_WIDTH
# define COMMENT_TAB_WIDTH (8)
#endif
/*
** This function scans the specified comment line starting just after the
** initial index and returns the index of the next spacing character -OR-
** zero if such a character cannot be found. For the purposes of this
** algorithm, the NUL character is treated the same as a spacing character.
*/
static int comment_next_space(
const char *zLine, /* [in] The comment line being printed. */
int index /* [in] The current character index being handled. */
){
int nextIndex = index + 1;
for(;;){
char c = zLine[nextIndex];
if( c==0 || fossil_isspace(c) ){
return nextIndex;
}
nextIndex++;
}
return 0; /* NOT REACHED */
}
/*
** This function is called when printing a logical comment line to perform
** the necessary indenting.
*/
static void comment_print_indent(
const char *zLine, /* [in] The comment line being printed. */
int indent, /* [in] Number of spaces to indent, zero for none. */
int trimSpace, /* [in] Non-zero to trim leading/trailing spaces. */
int *piIndex /* [in/out] Pointer to first non-space character. */
){
if( indent>0 ){
fossil_print("%*s", indent, "");
if( trimSpace && zLine && piIndex ){
int index = *piIndex;
while( fossil_isspace(zLine[index]) ){ index++; }
*piIndex = index;
}
}
}
/*
** This function prints one logical line of a comment, stopping when it hits
** a new line -OR- runs out of space on the logical line.
*/
static void comment_print_line(
const char *zLine, /* [in] The comment line to print. */
int indent, /* [in] Number of spaces to indent, zero for none. */
int lineChars, /* [in] Maximum number of characters to print. */
int trimSpace, /* [in] Non-zero to trim leading/trailing spaces. */
int wordBreak, /* [in] Non-zero to try breaking on word boundaries. */
int *pLineCnt, /* [in/out] Pointer to the total line count. */
const char **pzLine /* [out] Pointer to the end of the logical line. */
){
int index = 0, charCnt = 0, lineCnt = 0, maxChars;
if( !zLine ) return;
if( lineChars<=0 ) return;
comment_print_indent(zLine, indent, trimSpace, &index);
maxChars = lineChars;
for(;;){
char c = zLine[index];
if( c==0 ){
break;
}else{
index++;
|
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
}
if( pzLine ){
*pzLine = zLine + index;
}
}
/*
** This function is called when printing a logical comment line to perform
** the necessary indenting.
*/
void comment_print_indent(
const char *zLine, /* [in] The comment line being printed. */
int indent, /* [in] Number of spaces to indent, zero for none. */
int *piIndex /* [in/out] Pointer to first non-space character. */
){
if( indent>0 ){
fossil_print("%*s", indent, "");
if( zLine && piIndex ){
int index = *piIndex;
while( fossil_isspace(zLine[index]) ){ index++; }
*piIndex = index;
}
}
}
/*
** This function scans the specified comment line starting just after the
** initial index and returns the index of the next spacing character -OR-
** zero if such a character cannot be found. For the purposes of this
** algorithm, the NUL character is treated the same as a spacing character.
*/
int comment_next_space(
const char *zLine, /* [in] The comment line being printed. */
int index /* [in] The current character index being handled. */
){
int nextIndex = index + 1;
for(;;){
char c = zLine[nextIndex];
if( c==0 || fossil_isspace(c) ){
return nextIndex;
}
nextIndex++;
}
return 0; /* NOT REACHED */
}
/*
**
** COMMAND: test-comment-format
**
** Usage: %fossil test-comment-format ?OPTIONS? PREFIX TEXT ?WIDTH?
|
|
>
>
|
>
>
|
|
|
|
>
|
<
<
>
>
|
|
<
|
>
>
>
>
>
>
|
|
>
>
>
>
>
>
|
>
>
>
|
<
<
<
<
>
>
>
|
>
>
|
<
<
>
|
|
<
<
>
>
|
>
>
>
>
|
|
|
>
>
>
>
|
|
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
}
if( pzLine ){
*pzLine = zLine + index;
}
}
/*
** Given a comment string zText, format that string for printing
** on a TTY. Assume that the output cursors is indent spaces from
** the left margin and that a single line can contain no more than
** width characters. Indent all subsequent lines by indent.
**
** Return the number of newlines that are output.
*/
int comment_print(
const char *zText, /* The comment text to be printed. */
int indent, /* Number of spaces to indent each non-initial line. */
int width, /* Maximum number of characters per line. */
int flags /* Zero or more "COMMENT_PRINT_*" flags, see above. */
){
int maxChars = width - indent;
int trimSpace = flags & COMMENT_PRINT_TRIM_SPACE;
int wordBreak = flags & COMMENT_PRINT_WORD_BREAK;
int lineCnt = 0;
const char *zLine;
#if defined(_WIN32)
if( width<0 ){
CONSOLE_SCREEN_BUFFER_INFO csbi;
memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){
maxChars = csbi.srWindow.Right - csbi.srWindow.Left - indent;
}
}
#elif defined(TIOCGWINSZ)
if( width<0 ){
struct winsize w;
memset(&w, 0, sizeof(struct winsize));
if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){
maxChars = w.ws_col - indent;
}
}
#else
if( width<0 ){
/*
** Fallback to using more-or-less the "legacy semantics" of hard-coding
** the maximum line length to a value reasonable for the vast majority
** of supported systems.
*/
maxChars = COMMENT_LEGACY_LINE_LENGTH - indent;
}
#endif
if( zText==0 ) zText = "(NULL)";
if( maxChars<=0 ){
maxChars = strlen(zText);
}
if( trimSpace ){
while( fossil_isspace(zText[0]) ){ zText++; }
}
if( zText[0]==0 ){
fossil_print("\n");
lineCnt++;
return lineCnt;
}
zLine = zText;
for(;;){
comment_print_line(zLine, zLine>zText ? indent : 0, maxChars,
trimSpace, wordBreak, &lineCnt, &zLine);
if( !zLine || !zLine[0] ) break;
}
return lineCnt;
}
/*
**
** COMMAND: test-comment-format
**
** Usage: %fossil test-comment-format ?OPTIONS? PREFIX TEXT ?WIDTH?
|