/* * pp_handle_indent() prints out the intendation, and * the whole line (without the final newline), after * de-tabifying. */ static void pp_handle_indent(struct pretty_print_context *pp, struct strbuf *sb, int indent, const char *line, int linelen) { strbuf_addchars(sb, ' ', indent); if (pp->expand_tabs_in_log) strbuf_add_tabexpand(sb, pp->expand_tabs_in_log, line, linelen); else strbuf_add(sb, line, linelen); }
int main() { struct strbuf buffer; strbuf_init(&buffer, 0); strbuf_grow(&buffer, 100); print(buffer); strbuf_addchars(&buffer, 'a', 90); print(buffer); strbuf_insert(&buffer, 40, "prince dhaliwal", 15); strbuf_fread(&buffer, 100, stdin); print(buffer); printf("%zd", strbuf_avail(&buffer)); strbuf_release(&buffer); return 0; }
static void strbuf_add_indented_text(struct strbuf *buf, const char *text, int indent, int indent2) { if (indent < 0) indent = 0; while (*text) { const char *eol = strchrnul(text, '\n'); if (*eol == '\n') eol++; strbuf_addchars(buf, ' ', indent); strbuf_add(buf, text, eol - text); text = eol; indent = indent2; } }
static void graph_padding_line(struct git_graph *graph, struct strbuf *sb) { int i; if (graph->state != GRAPH_COMMIT) { graph_next_line(graph, sb); return; } /* * Output the row containing this commit * Iterate up to and including graph->num_columns, * since the current commit may not be in any of the existing * columns. (This happens when the current commit doesn't have any * children that we have already processed.) */ for (i = 0; i < graph->num_columns; i++) { struct column *col = &graph->columns[i]; struct commit *col_commit = col->commit; if (col_commit == graph->commit) { strbuf_write_column(sb, col, '|'); if (graph->num_parents < 3) strbuf_addch(sb, ' '); else { int num_spaces = ((graph->num_parents - 2) * 2); strbuf_addchars(sb, ' ', num_spaces); } } else { strbuf_write_column(sb, col, '|'); strbuf_addch(sb, ' '); } } graph_pad_horizontally(graph, sb, graph->num_columns); /* * Update graph->prev_state since we have output a padding line */ graph->prev_state = GRAPH_PADDING; }
static void rerere_strbuf_putconflict(struct strbuf *buf, int ch, size_t size) { strbuf_addchars(buf, ch, size); strbuf_addch(buf, '\n'); }
/* * Wrap the text, if necessary. The variable indent is the indent for the * first line, indent2 is the indent for all other lines. * If indent is negative, assume that already -indent columns have been * consumed (and no extra indent is necessary for the first line). */ int strbuf_add_wrapped_text(struct strbuf *buf, const char *text, int indent1, int indent2, int width) { int indent, w, assume_utf8 = 1; const char *bol, *space, *start = text; size_t orig_len = buf->len; if (width <= 0) { strbuf_add_indented_text(buf, text, indent1, indent2); return 1; } retry: bol = text; w = indent = indent1; space = NULL; if (indent < 0) { w = -indent; space = text; } for (;;) { char c; size_t skip; while ((skip = display_mode_esc_sequence_len(text))) text += skip; c = *text; if (!c || isspace(c)) { if (w < width || !space) { const char *start = bol; if (!c && text == start) return w; if (space) start = space; else strbuf_addchars(buf, ' ', indent); strbuf_add(buf, start, text - start); if (!c) return w; space = text; if (c == '\t') w |= 0x07; else if (c == '\n') { space++; if (*space == '\n') { strbuf_addch(buf, '\n'); goto new_line; } else if (!isalnum(*space)) goto new_line; else strbuf_addch(buf, ' '); } w++; text++; } else { new_line: strbuf_addch(buf, '\n'); text = bol = space + isspace(*space); space = NULL; w = indent = indent2; } continue; } if (assume_utf8) { w += utf8_width(&text, NULL); if (!text) { assume_utf8 = 0; text = start; strbuf_setlen(buf, orig_len); goto retry; } } else { w++; text++; } } }
static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */ const char *placeholder, struct format_commit_context *c) { struct strbuf local_sb = STRBUF_INIT; int total_consumed = 0, len, padding = c->padding; if (padding < 0) { const char *start = strrchr(sb->buf, '\n'); int occupied; if (!start) start = sb->buf; occupied = utf8_strnwidth(start, -1, 1); padding = (-padding) - occupied; } while (1) { int modifier = *placeholder == 'C'; int consumed = format_commit_one(&local_sb, placeholder, c); total_consumed += consumed; if (!modifier) break; placeholder += consumed; if (*placeholder != '%') break; placeholder++; total_consumed++; } len = utf8_strnwidth(local_sb.buf, -1, 1); if (c->flush_type == flush_left_and_steal) { const char *ch = sb->buf + sb->len - 1; while (len > padding && ch > sb->buf) { const char *p; if (*ch == ' ') { ch--; padding++; continue; } /* check for trailing ansi sequences */ if (*ch != 'm') break; p = ch - 1; while (ch - p < 10 && *p != '\033') p--; if (*p != '\033' || ch + 1 - p != display_mode_esc_sequence_len(p)) break; /* * got a good ansi sequence, put it back to * local_sb as we're cutting sb */ strbuf_insert(&local_sb, 0, p, ch + 1 - p); ch = p - 1; } strbuf_setlen(sb, ch + 1 - sb->buf); c->flush_type = flush_left; } if (len > padding) { switch (c->truncate) { case trunc_left: strbuf_utf8_replace(&local_sb, 0, len - (padding - 2), ".."); break; case trunc_middle: strbuf_utf8_replace(&local_sb, padding / 2 - 1, len - (padding - 2), ".."); break; case trunc_right: strbuf_utf8_replace(&local_sb, padding - 2, len - (padding - 2), ".."); break; case trunc_none: break; } strbuf_addbuf(sb, &local_sb); } else { int sb_len = sb->len, offset = 0; if (c->flush_type == flush_left) offset = padding - len; else if (c->flush_type == flush_both) offset = (padding - len) / 2; /* * we calculate padding in columns, now * convert it back to chars */ padding = padding - len + local_sb.len; strbuf_addchars(sb, ' ', padding); memcpy(sb->buf + sb_len + offset, local_sb.buf, local_sb.len); } strbuf_release(&local_sb); c->flush_type = no_flush; return total_consumed; }
static void output_pair_header(struct diff_options *diffopt, int patch_no_width, struct strbuf *buf, struct strbuf *dashes, struct patch_util *a_util, struct patch_util *b_util) { struct object_id *oid = a_util ? &a_util->oid : &b_util->oid; struct commit *commit; char status; const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET); const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD); const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW); const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT); const char *color; if (!dashes->len) strbuf_addchars(dashes, '-', strlen(find_unique_abbrev(oid, DEFAULT_ABBREV))); if (!b_util) { color = color_old; status = '<'; } else if (!a_util) { color = color_new; status = '>'; } else if (strcmp(a_util->patch, b_util->patch)) { color = color_commit; status = '!'; } else { color = color_commit; status = '='; } strbuf_reset(buf); strbuf_addstr(buf, status == '!' ? color_old : color); if (!a_util) strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf); else strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1, find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV)); if (status == '!') strbuf_addf(buf, "%s%s", color_reset, color); strbuf_addch(buf, status); if (status == '!') strbuf_addf(buf, "%s%s", color_reset, color_new); if (!b_util) strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf); else strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1, find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV)); commit = lookup_commit_reference(the_repository, oid); if (commit) { if (status == '!') strbuf_addf(buf, "%s%s", color_reset, color); strbuf_addch(buf, ' '); pp_commit_easy(CMIT_FMT_ONELINE, commit, buf); } strbuf_addf(buf, "%s\n", color_reset); fwrite(buf->buf, buf->len, 1, stdout); }