/** * Writes help message for this option out to given stream. * @param out stream to write message to * @return output stream */ std::ostream& write_help(std::ostream& out) const { std::string help_str; help_str += " "; if (this->short_flag_.size() > 0) { help_str += this->short_flag_; if (!this->is_switch_) { help_str += " "; if (this->meta_var_.size() == 0) { help_str += "VALUE"; } else { help_str += this->meta_var_; } } if (this->long_flag_.size() > 0) { help_str += ", "; } } if (this->long_flag_.size() > 0) { help_str += this->long_flag_; if (!this->is_switch_) { help_str += "="; if (this->meta_var_.size() == 0) { help_str += "VALUE"; } else { help_str += this->meta_var_; } } } if (this->help_.size() > 0) { if (help_str.size() > CMDOPTS_OPTION_COL_WIDTH-2) { help_str += "\n"; } else { while (help_str.size() < CMDOPTS_OPTION_COL_WIDTH) { help_str += " "; } } std::string help_msg = this->help_; std::string::size_type defval = help_msg.find("%default"); std::string replace_val = this->current_value_as_string(); while (defval != std::string::npos) { help_msg.replace(defval, 8, replace_val.c_str()); defval = help_msg.find("%default"); } help_str += help_msg; std::string help_desc = textwrap(help_str, CMDOPTS_LINE_WIDTH, 0, CMDOPTS_OPTION_COL_WIDTH); help_str = help_desc; } out << help_str; return out; }
int Console::write(std::string str) { return writeDelayed(textwrap(str, lineWidth)); }
int Console::write(const char *ptr) { return writeDelayed(textwrap((std::string) ptr, lineWidth)); }
int strwrap(const char *str, const int width, char *lines[], int maxlines) { #ifdef HAVE_LIBTEXTWRAP textwrap_t p; int j; char *s; char *s0; char *t; textwrap_init(&p); textwrap_columns(&p, width); s0 = s = textwrap(&p, str); for (j=0; j<maxlines; j++) { t = strchr(s, '\n'); if (t == NULL) { lines[j] = (char *)malloc(strlen(s) + 1); strcpy(lines[j], s); free(s0); return j + 1; } else { lines[j] = (char *)malloc(t - s + 1); strncpy(lines[j], s, t-s); lines[j][t-s] = 0; s = t + 1; } } return j; #else /* "Simple" greedy line-wrapper */ int len = STRLEN(str); int l = 0; const char *s, *e, *end, *lb; if (str == 0) return 0; /* * s = first character of current line, * e = last character of current line * end = last character of the input string (the trailing \0) */ s = e = str; end = str + len; while (len > 0) { /* try to fit the most characters */ e = s + width - 1; /* simple case: we got everything */ if (e >= end) { e = end; } else { /* find a breaking point */ while (e > s && !isspace(*e) && *e != '.' && *e != ',') e--; } /* no word-break point found, so just unconditionally break * the line */ if (e == s) e = s + width; /* if there's an explicit linebreak, honor it */ lb = strchr(s, '\n'); if (lb != NULL && lb < e) e = lb; lines[l] = (char *)malloc(e-s+2); strncpy(lines[l], s, e-s+1); lines[l][e-s+1] = 0; CHOMP(lines[l]); len -= (e-s+1); s = e + 1; if (++l >= maxlines) break; } return l; #endif /* HAVE_LIBTEXTWRAP */ }