Moved the "cli_parser_c::format_text()" function into a common file and out of the class and renamed it to "format_paragraph()".

This commit is contained in:
Moritz Bunkus 2009-08-12 14:27:18 +02:00
parent 52e730a501
commit 73167c6782
3 changed files with 83 additions and 66 deletions

View File

@ -18,12 +18,12 @@
#include "common/command_line.h"
#include "common/common.h"
#include "common/strings/editing.h"
#include "common/strings/formatting.h"
#include "common/translation.h"
#define INDENT_COLUMN_OPTION_NAME 2
#define INDENT_COLUMN_OPTION_DESCRIPTION 30
#define INDENT_COLUMN_SECTION_HEADER 1
#define WRAP_COLUMN 79
cli_parser_c::option_t::option_t()
: m_needs_arg(false)
@ -52,75 +52,15 @@ cli_parser_c::option_t::option_t(const std::string &spec,
std::string
cli_parser_c::option_t::format_text() {
std::string text;
std::string description = m_description.get_translated();
int indent_column = 0;
int current_column = 0;
if (cli_parser_c::option_t::ot_option == m_type) {
indent_column = INDENT_COLUMN_OPTION_DESCRIPTION;
text = std::string(INDENT_COLUMN_OPTION_NAME, ' ') + m_name;
current_column = text.length();
if (cli_parser_c::option_t::ot_option == m_type)
return format_paragraph(description, INDENT_COLUMN_OPTION_DESCRIPTION, std::string(INDENT_COLUMN_OPTION_NAME, ' ') + m_name);
} else if (cli_parser_c::option_t::ot_section_header == m_type) {
indent_column = INDENT_COLUMN_SECTION_HEADER;
text = "\n";
description += ":";
}
else if (cli_parser_c::option_t::ot_section_header == m_type)
return std::string("\n") + format_paragraph(description + ":", INDENT_COLUMN_SECTION_HEADER);
if ((0 != indent_column) && (indent_column <= current_column)) {
text += "\n";
current_column = 0;
}
std::string indent(indent_column, ' ');
text += std::string(indent_column - current_column, ' ');
current_column = utf8_strlen(text);
std::string::size_type current_pos = 0;
bool first_word_in_line = true;
bool needs_space = false;
const char * break_chars = " ,.)/:";
while (description.length() > current_pos) {
std::string::size_type word_start = description.find_first_not_of(" ", current_pos);
if (std::string::npos == word_start)
break;
std::string::size_type word_end = description.find_first_of(break_chars, word_start);
char next_needs_space = false;
if (std::string::npos == word_end)
word_end = description.length();
else if (description[word_end] != ' ')
++word_end;
else
next_needs_space = true;
std::string word = description.substr(word_start, word_end - word_start);
bool needs_space_now = needs_space && (0 != word.find_first_of(break_chars));
if (!first_word_in_line && ((current_column + (needs_space_now ? 0 : 1) + utf8_strlen(word)) >= WRAP_COLUMN)) {
text += "\n" + indent;
current_column = indent_column;
first_word_in_line = true;
}
if (!first_word_in_line && needs_space_now) {
text += " ";
++current_column;
}
text += word;
current_column += utf8_strlen(word);
current_pos = word_end;
first_word_in_line = false;
needs_space = next_needs_space;
}
text += "\n";
return text;
return format_paragraph(description);
}
// ------------------------------------------------------------

View File

@ -139,3 +139,71 @@ fix_format(const char *fmt,
#endif
}
std::string
format_paragraph(const std::string &text_to_wrap,
int indent_column,
const std::string &text_first_line,
int wrap_column,
const char *break_chars) {
wrap_column += 5;
std::string text = text_first_line;
int current_column = utf8_strlen(text);
if ((0 != indent_column) && (current_column >= indent_column)) {
text += "\n";
current_column = 0;
}
std::string indent(indent_column, ' ');
text += std::string(indent_column - current_column, ' ');
current_column = indent_column;
std::string::size_type current_pos = 0;
bool first_word_in_line = true;
bool needs_space = false;
while (text_to_wrap.length() > current_pos) {
std::string::size_type word_start = text_to_wrap.find_first_not_of(" ", current_pos);
if (std::string::npos == word_start)
break;
if (word_start != current_pos)
needs_space = true;
std::string::size_type word_end = text_to_wrap.find_first_of(break_chars, word_start);
char next_needs_space = false;
if (std::string::npos == word_end)
word_end = text_to_wrap.length();
else if (text_to_wrap[word_end] != ' ')
++word_end;
else
next_needs_space = true;
std::string word = text_to_wrap.substr(word_start, word_end - word_start);
size_t word_length = utf8_strlen(word);
bool needs_space_now = needs_space && (0 != word.find_first_of(break_chars));
if (!first_word_in_line && ((current_column + (needs_space_now ? 0 : 1) + word_length) >= wrap_column)) {
text += "\n" + indent;
current_column = indent_column;
first_word_in_line = true;
}
if (!first_word_in_line && needs_space_now) {
text += " ";
++current_column;
}
text += word;
current_column += word_length;
current_pos = word_end;
first_word_in_line = false;
needs_space = next_needs_space;
}
text += "\n";
return text;
}

View File

@ -18,6 +18,8 @@
#include <string>
#include "common/strings/editing.h"
#define FMT_TIMECODE "%02d:%02d:%02d.%03d"
#define ARG_TIMECODEINT(t) (int32_t)( (t) / 60 / 60 / 1000), \
(int32_t)(((t) / 60 / 1000) % 60), \
@ -32,7 +34,14 @@
(int32_t)( (t) % 1000000000)
#define ARG_TIMECODEN(t) ARG_TIMECODENINT((int64_t)(t))
#define WRAP_COLUMN 79
std::string MTX_DLL_API format_timecode(int64_t timecode, unsigned int precision = 9);
std::string MTX_DLL_API format_paragraph(const std::string &text_to_wrap,
int indent_column = 0,
const std::string &text_first_line = empty_string,
int wrap_column = WRAP_COLUMN,
const char *break_chars = " ,.)/:");
void MTX_DLL_API fix_format(const char *fmt, std::string &new_fmt);