Permalink
Browse files

Give owl_text_indent an indent_first_line parameter

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...
1 parent d8f22b6 commit 14be3a516baee0a39a99eada3fd50d39455b4724 @JasonGross JasonGross committed Jul 20, 2011
Showing with 10 additions and 6 deletions.
  1. +2 −2 cmd.c
  2. +1 −1 style.c
  3. +7 −3 text.c
View
4 cmd.c
@@ -259,7 +259,7 @@ void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {
if (cmd->usage && *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_normal(fm, indent);
owl_fmtext_append_normal(fm, "\n");
@@ -273,7 +273,7 @@ void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {
if (cmd->description && *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_normal(fm, indent);
owl_fmtext_append_normal(fm, "\n");
View
@@ -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 = owl_text_indent(body, OWL_TAB);
+ indent = owl_text_indent(body, OWL_TAB, true);
curlen = strlen(indent);
if (curlen == 0 || indent[curlen-1] != '\n') {
char *tmp = indent;
View
10 text.c
@@ -6,18 +6,22 @@
/* Returns a copy of 'in' with each line indented 'n'
* 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;
GString *out = g_string_new("");
int i;
+ bool indent_this_line = indent_first_line;
last=in+strlen(in)-1;
ptr1=in;
while (ptr1<=last) {
- for (i=0; i<n; i++) {
- g_string_append_c(out, ' ');
+ if (indent_this_line) {
+ for (i = 0; i < n; i++) {
+ g_string_append_c(out, ' ');
+ }
}
+ indent_this_line = true;
ptr2=strchr(ptr1, '\n');
if (!ptr2) {
g_string_append(out, ptr1);

0 comments on commit 14be3a5

Please sign in to comment.