MCPcopy Create free account
hub / github.com/GenericMappingTools/gmt / gmtapi_wrap_the_line

Function gmtapi_wrap_the_line

src/gmt_api.c:14166–14228  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14164}
14165
14166GMT_LOCAL void gmtapi_wrap_the_line (struct GMTAPI_CTRL *API, int level, FILE *fp, const char *in_line) {
14167 /* Break the in_ine across multiple lines determined by the terminal line width API->terminal_width */
14168 bool keep_same_indent = (level < 0), go = true, force = false;
14169 int k, j, next_level, current_width = 0, try = 0;
14170 unsigned int width;
14171 static int gmtapi_indent[8] = {0, 2, 5, 7, 10, 13, 15, 0}; /* Last one is for custom negative values exceeding 6 */
14172 struct GMT_WORD *W = gmtapi_split_words (in_line); /* Create array of words */
14173 char message[GMT_MSGSIZ] = {""};
14174
14175#ifdef DEBUG_USAGE_OPTIONS
14176 { /* Breaks usage string into one word per line. See gmt_usage_messages.sh in the sandbox/guru repo for what to do next */
14177 strcpy (message, in_line);
14178 gmt_strrepc (message, ' ', '\n');
14179 API->print_func (fp, message); /* Do the printing */
14180 return;
14181 }
14182 #endif
14183
14184 /* Start with any fixed indent */
14185 level = abs (level); /* In case it was negative */
14186 if (level > 6) { /* Place custom level in last entry. This is to help g**math issue wrapped operator messages */
14187 gmtapi_indent[7] = level;
14188 level = 7;
14189 go = false; /* Already indented and ready to go */
14190 }
14191 next_level = (keep_same_indent) ? level : level + 1; /* Do we indent when we wrap or not */
14192 for (j = 0; go && j < gmtapi_indent[level]; j++) strcat (message, " "); /* Starting spaces */
14193 current_width = gmtapi_indent[level];
14194 for (k = 0; W[k].word; k++) { /* As long as there are more words... */
14195 width = (gmtapi_space (W[k+1].space)) ? API->terminal_width : API->terminal_width - 1; /* May need one space for ellipsis at end */
14196 if (force || (current_width + strlen (W[k].word) + W[k].space) < width) { /* Word will fit on current line */
14197 if (gmtapi_space (W[k].space)) /* This word requires a leading space */
14198 strcat (message, " ");
14199 strcat (message, W[k].word);
14200 current_width += (int)(strlen (W[k].word) + gmtapi_space (W[k].space)); /* Update line width so far */
14201 free (W[k].word); /* Free the word we are done with */
14202 if (W[k+1].word == NULL) /* Finalize the last line */
14203 strcat (message, "\n");
14204 force = false; /* In case it was set */
14205 }
14206 else { /* Must split at the current break point and continue on next line */
14207 if (W[k].space) { /* No break character needed since space separation is expected */
14208 strcat (message, "\n"); /* Move to new line */
14209 for (j = 0; j < gmtapi_indent[next_level]; j++) strcat (message, " "); /* Initial indent plus possibly next indent*/
14210 current_width = gmtapi_indent[next_level]; /* Indent plus next indent */
14211 }
14212 else { /* Split in the middle of an option so append breakline and start new line with ellipsis after indent */
14213 strcat (message, GMT_LINE_BREAK);
14214 strcat (message, "\n");
14215 for (j = 0; j < gmtapi_indent[next_level]; j++) strcat (message, " "); /* Initial indent plus possibly next indent */
14216 strcat (message, GMT_LINE_CONT); /* And the ellipsis */
14217 current_width = gmtapi_indent[next_level]; /* Indent plus the next indent */
14218 }
14219 W[k].space = 0; /* Can be no leading space if starting a the line */
14220 k--; /* Since k will be incremented by loop and we did not write this word yet */
14221 try++;
14222 if (try == 2) /* Word is longer than effective terminal width - must force output (even if too long) to get past this stalemate */
14223 force = true, try = 0;

Callers 1

GMT_UsageFunction · 0.85

Calls 3

gmtapi_split_wordsFunction · 0.85
gmt_strrepcFunction · 0.85
gmtapi_spaceFunction · 0.85

Tested by

no test coverage detected