Skip to content

Commit 13e4760

Browse files
Johannes Sixtgitster
authored andcommitted
recv_sideband: Do not use ANSI escape sequence on dumb terminals.
The "clear to end of line" sequence is used to nicely output the progress indicator without leaving garbage on the terminal. However, this works only on ANSI capable terminals. We use the same check as in color.c to find out whether the terminal supports this feature and use a workaround (a few spaces in a row) if it does not. [jc: as an old fashoned git myself, and given the fact that the possible prefix and suffix are small number of short constant strings, I actually prefer a simpler-and-more-stupid approach. This is with Nico's clean-up.] Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f85fd3f commit 13e4760

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

sideband.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,27 @@
1313
*/
1414

1515
#define PREFIX "remote:"
16-
#define SUFFIX "\033[K" /* change to " " if ANSI sequences don't work */
16+
17+
#define ANSI_SUFFIX "\033[K"
18+
#define DUMB_SUFFIX " "
19+
20+
#define FIX_SIZE 10 /* large enough for any of the above */
1721

1822
int recv_sideband(const char *me, int in_stream, int out, int err)
1923
{
2024
unsigned pf = strlen(PREFIX);
21-
unsigned sf = strlen(SUFFIX);
22-
char buf[pf + LARGE_PACKET_MAX + sf + 1];
25+
unsigned sf;
26+
char buf[LARGE_PACKET_MAX + 2*FIX_SIZE];
27+
char *suffix, *term;
28+
2329
memcpy(buf, PREFIX, pf);
30+
term = getenv("TERM");
31+
if (term && strcmp(term, "dumb"))
32+
suffix = ANSI_SUFFIX;
33+
else
34+
suffix = DUMB_SUFFIX;
35+
sf = strlen(suffix);
36+
2437
while (1) {
2538
int band, len;
2639
len = packet_read_line(in_stream, buf + pf, LARGE_PACKET_MAX);
@@ -59,10 +72,10 @@ int recv_sideband(const char *me, int in_stream, int out, int err)
5972
* line data actually contains something.
6073
*/
6174
if (brk > pf+1 + 1) {
62-
char save[sf];
75+
char save[FIX_SIZE];
6376
memcpy(save, buf + brk, sf);
6477
buf[brk + sf - 1] = buf[brk - 1];
65-
memcpy(buf + brk - 1, SUFFIX, sf);
78+
memcpy(buf + brk - 1, suffix, sf);
6679
safe_write(err, buf, brk + sf);
6780
memcpy(buf + brk, save, sf);
6881
} else

0 commit comments

Comments
 (0)