Skip to content

Commit

Permalink
Give owl_text_indent an indent_first_line parameter
Browse files Browse the repository at this point in the history
This is useful if you need to indent text that isn't broken into chunks
that end with newlines.

This is primarily in preparation for the next commit, where we need to
replace the indent on the first line by a prefix.  The other
(reasonable) option is to make owl_text_indent always prefix the string
it's given with an indent, even when it's given the empty string.  This
would break the nice property that indent(A + B) = indent(A) + indent(B)
whenever A ended with a newline.  After some discussion on zephyr and on
the github pull request, I decided to go with this option.
  • Loading branch information
JasonGross committed Jul 21, 2011
1 parent d8f22b6 commit 14be3a5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cmd.c
Expand Up @@ -259,7 +259,7 @@ void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {


if (cmd->usage && *cmd->usage) { if (cmd->usage && *cmd->usage) {
s = cmd->usage; s = cmd->usage;
indent = owl_text_indent(s, OWL_TAB); indent = owl_text_indent(s, OWL_TAB, true);
owl_fmtext_append_bold(fm, "\nSYNOPSIS\n"); owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
owl_fmtext_append_normal(fm, indent); owl_fmtext_append_normal(fm, indent);
owl_fmtext_append_normal(fm, "\n"); owl_fmtext_append_normal(fm, "\n");
Expand All @@ -273,7 +273,7 @@ void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {


if (cmd->description && *cmd->description) { if (cmd->description && *cmd->description) {
s = cmd->description; s = cmd->description;
indent = owl_text_indent(s, OWL_TAB); indent = owl_text_indent(s, OWL_TAB, true);
owl_fmtext_append_bold(fm, "\nDESCRIPTION\n"); owl_fmtext_append_bold(fm, "\nDESCRIPTION\n");
owl_fmtext_append_normal(fm, indent); owl_fmtext_append_normal(fm, indent);
owl_fmtext_append_normal(fm, "\n"); owl_fmtext_append_normal(fm, "\n");
Expand Down
2 changes: 1 addition & 1 deletion style.c
Expand Up @@ -64,7 +64,7 @@ void owl_style_get_formattext(const owl_style *s, owl_fmtext *fm, const owl_mess
} }


/* indent and ensure ends with a newline */ /* indent and ensure ends with a newline */
indent = owl_text_indent(body, OWL_TAB); indent = owl_text_indent(body, OWL_TAB, true);
curlen = strlen(indent); curlen = strlen(indent);
if (curlen == 0 || indent[curlen-1] != '\n') { if (curlen == 0 || indent[curlen-1] != '\n') {
char *tmp = indent; char *tmp = indent;
Expand Down
10 changes: 7 additions & 3 deletions text.c
Expand Up @@ -6,18 +6,22 @@


/* Returns a copy of 'in' with each line indented 'n' /* Returns a copy of 'in' with each line indented 'n'
* characters. Result must be freed with g_free. */ * characters. Result must be freed with g_free. */
CALLER_OWN char *owl_text_indent(const char *in, int n) CALLER_OWN char *owl_text_indent(const char *in, int n, bool indent_first_line)
{ {
const char *ptr1, *ptr2, *last; const char *ptr1, *ptr2, *last;
GString *out = g_string_new(""); GString *out = g_string_new("");
int i; int i;
bool indent_this_line = indent_first_line;


last=in+strlen(in)-1; last=in+strlen(in)-1;
ptr1=in; ptr1=in;
while (ptr1<=last) { while (ptr1<=last) {
for (i=0; i<n; i++) { if (indent_this_line) {
g_string_append_c(out, ' '); for (i = 0; i < n; i++) {
g_string_append_c(out, ' ');
}
} }
indent_this_line = true;
ptr2=strchr(ptr1, '\n'); ptr2=strchr(ptr1, '\n');
if (!ptr2) { if (!ptr2) {
g_string_append(out, ptr1); g_string_append(out, ptr1);
Expand Down

0 comments on commit 14be3a5

Please sign in to comment.