]> granicus.if.org Git - git/commit
graph: automatically track display width of graph lines
authorJames Coglan <jcoglan@gmail.com>
Tue, 15 Oct 2019 23:47:47 +0000 (23:47 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 16 Oct 2019 02:11:24 +0000 (11:11 +0900)
commitfbccf255f9449c2f617d875ebf78b9f1730fae5d
tree3b6c5f94bd9adc0c6f995306ac87f03a20706963
parent108b97dc372828f0e72e56bbb40cae8e1e83ece6
graph: automatically track display width of graph lines

All the output functions called by `graph_next_line()` currently keep
track of how many printable chars they've written to the buffer, before
calling `graph_pad_horizontally()` to pad the line with spaces. Some
functions do this by incrementing a counter whenever they write to the
buffer, and others do it by encoding an assumption about how many chars
are written, as in:

    graph_pad_horizontally(graph, sb, graph->num_columns * 2);

This adds a fair amount of noise to the functions' logic and is easily
broken if one forgets to increment the right counter or update the
calculations used for padding.

To make this easier to use, I'm introducing a new struct called
`graph_line` that wraps a `strbuf` and keeps count of its display width
implicitly. `graph_next_line()` wraps this around the `struct strbuf *`
it's given and passes a `struct graph_line *` to the output functions,
which use its interface.

The `graph_line` interface wraps the `strbuf_addch()`,
`strbuf_addchars()` and `strbuf_addstr()` functions, and adds the
`graph_line_write_column()` function for adding a single character with
color formatting. The `graph_pad_horizontally()` function can then use
the `width` field from the struct rather than taking a character count
as a parameter.

Signed-off-by: James Coglan <jcoglan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
graph.c