mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-09-11 13:01:28 +02:00
zsh-workers/9945
This commit is contained in:
parent
dc9b0504d4
commit
04039e40ce
1 changed files with 80 additions and 18 deletions
80
Src/prompt.c
80
Src/prompt.c
|
@ -684,7 +684,8 @@ putstr(int d)
|
||||||
|
|
||||||
/* Count height etc. of a prompt string returned by promptexpand(). *
|
/* Count height etc. of a prompt string returned by promptexpand(). *
|
||||||
* This depends on the current terminal width, and tabs and *
|
* This depends on the current terminal width, and tabs and *
|
||||||
* newlines require nontrivial processing. */
|
* newlines require nontrivial processing. *
|
||||||
|
* Passing `overf' as -1 means to ignore columns (absolute width). */
|
||||||
|
|
||||||
/**/
|
/**/
|
||||||
mod_export void
|
mod_export void
|
||||||
|
@ -693,7 +694,7 @@ countprompt(char *str, int *wp, int *hp, int overf)
|
||||||
int w = 0, h = 1;
|
int w = 0, h = 1;
|
||||||
int s = 1;
|
int s = 1;
|
||||||
for(; *str; str++) {
|
for(; *str; str++) {
|
||||||
if(w >= columns) {
|
if(w >= columns && overf >= 0) {
|
||||||
w = 0;
|
w = 0;
|
||||||
h++;
|
h++;
|
||||||
}
|
}
|
||||||
|
@ -715,7 +716,7 @@ countprompt(char *str, int *wp, int *hp, int overf)
|
||||||
w++;
|
w++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(w >= columns) {
|
if(w >= columns && overf >= 0) {
|
||||||
if (!overf || w > columns) {
|
if (!overf || w > columns) {
|
||||||
w = 0;
|
w = 0;
|
||||||
h++;
|
h++;
|
||||||
|
@ -734,6 +735,7 @@ prompttrunc(int arg, int truncchar, int doprint, int endchar)
|
||||||
if (arg) {
|
if (arg) {
|
||||||
char ch = *fm, *ptr = bp, *truncstr;
|
char ch = *fm, *ptr = bp, *truncstr;
|
||||||
int truncatleft = ch == '<';
|
int truncatleft = ch == '<';
|
||||||
|
int w;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If there is already a truncation active, return so that
|
* If there is already a truncation active, return so that
|
||||||
|
@ -768,7 +770,8 @@ prompttrunc(int arg, int truncchar, int doprint, int endchar)
|
||||||
fm++;
|
fm++;
|
||||||
putpromptchar(doprint, endchar);
|
putpromptchar(doprint, endchar);
|
||||||
*bp = '\0';
|
*bp = '\0';
|
||||||
if (bp - ptr > trunclen) {
|
countprompt(ptr, &w, 0, -1);
|
||||||
|
if (w > trunclen) {
|
||||||
/*
|
/*
|
||||||
* We need to truncate. t points to the truncation string -- *
|
* We need to truncate. t points to the truncation string -- *
|
||||||
* which is inserted literally, without nice representation. *
|
* which is inserted literally, without nice representation. *
|
||||||
|
@ -780,24 +783,83 @@ prompttrunc(int arg, int truncchar, int doprint, int endchar)
|
||||||
char *t = truncstr;
|
char *t = truncstr;
|
||||||
int fullen = bp - ptr;
|
int fullen = bp - ptr;
|
||||||
int tlen = ztrlen(t), maxlen;
|
int tlen = ztrlen(t), maxlen;
|
||||||
|
maxlen = tlen < trunclen ? trunclen - tlen : 0;
|
||||||
|
if (w < fullen) {
|
||||||
|
/* Invisible substrings, lots of shuffling. */
|
||||||
|
int n = strlen(t);
|
||||||
|
addbufspc(n);
|
||||||
|
|
||||||
|
if (truncatleft) {
|
||||||
|
char *p = ptr + n, *q = p;
|
||||||
|
|
||||||
|
n = fullen - w;
|
||||||
|
|
||||||
|
/* Shift the whole string right, then *
|
||||||
|
* selectively copy to the left. */
|
||||||
|
memmove(p, ptr, fullen);
|
||||||
|
while (w > 0 || n > 0) {
|
||||||
|
if (*p == Inpar)
|
||||||
|
do {
|
||||||
|
*q++ = *p;
|
||||||
|
--n;
|
||||||
|
} while (*p++ != Outpar && *p && n);
|
||||||
|
else if (w) {
|
||||||
|
if (--w < maxlen)
|
||||||
|
*q++ = *p;
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bp = q;
|
||||||
|
} else {
|
||||||
|
/* Truncate on the right, selectively */
|
||||||
|
char *q = ptr + fullen;
|
||||||
|
|
||||||
|
/* First skip over as much as will "fit". */
|
||||||
|
while (w > 0 && maxlen > 0) {
|
||||||
|
if (*ptr == Inpar)
|
||||||
|
while (*ptr++ != Outpar && *ptr) {;}
|
||||||
|
else
|
||||||
|
++ptr, --w, --maxlen;
|
||||||
|
}
|
||||||
|
if (ptr < q) {
|
||||||
|
/* We didn't reach the end of the string. *
|
||||||
|
* In case there are more invisible bits, *
|
||||||
|
* insert the truncstr and keep looking. */
|
||||||
|
memmove(ptr + n, ptr, q - ptr);
|
||||||
|
q = ptr + n;
|
||||||
|
while (*t)
|
||||||
|
*ptr++ = *t++;
|
||||||
|
while (*q) {
|
||||||
|
if (*q == Inpar)
|
||||||
|
do {
|
||||||
|
*ptr++ = *q;
|
||||||
|
} while (*q++ != Outpar && *q);
|
||||||
|
else
|
||||||
|
++q;
|
||||||
|
}
|
||||||
|
bp = ptr;
|
||||||
|
*bp = 0;
|
||||||
|
} else
|
||||||
|
bp = ptr + n;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* No invisible substrings. */
|
||||||
if (tlen > fullen) {
|
if (tlen > fullen) {
|
||||||
addbufspc(tlen - fullen);
|
addbufspc(tlen - fullen);
|
||||||
bp += tlen - fullen;
|
bp += tlen - fullen;
|
||||||
} else
|
} else
|
||||||
bp -= fullen - trunclen;
|
bp -= fullen - trunclen;
|
||||||
maxlen = tlen < trunclen ? trunclen - tlen : 0;
|
|
||||||
if (truncatleft) {
|
if (truncatleft) {
|
||||||
if (maxlen)
|
if (maxlen)
|
||||||
memmove(ptr + strlen(t), ptr + fullen - maxlen,
|
memmove(ptr + strlen(t), ptr + fullen - maxlen,
|
||||||
maxlen);
|
maxlen);
|
||||||
while (*t)
|
} else
|
||||||
*ptr++ = *t++;
|
|
||||||
} else {
|
|
||||||
ptr += maxlen;
|
ptr += maxlen;
|
||||||
|
}
|
||||||
|
/* Finally, copy the truncstr into place. */
|
||||||
while (*t)
|
while (*t)
|
||||||
*ptr++ = *t++;
|
*ptr++ = *t++;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
zsfree(truncstr);
|
zsfree(truncstr);
|
||||||
trunclen = 0;
|
trunclen = 0;
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in a new issue