Re: [hackers] Re: [dmenu][patch] Improve speed of drw_text when provided with large strings
On Mon, Aug 09, 2021 at 06:35:57PM +0200, Hiltjo Posthuma wrote: > On Sat, Aug 07, 2021 at 09:39:44PM -0400, Miles Alan wrote: > > On Sat, Aug 7, 2021, at 9:34 PM, m...@milesalan.com wrote: > > > - for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > > > > w; len--) > > > - drw_font_getexts(usedfont, utf8str, len, , > > > NULL); > > > + if (ew > w) > > > + for (ew = 0, len = 0; ew < w - lpad * 2 && len > > > < MIN(utf8strlen, > > > sizeof(buf) - 1); len++) > > > + drw_font_getexts(usedfont, utf8str, > > > len, , NULL); > > > + else > > > + len = MIN(utf8strlen, sizeof(buf) - 1); > > > > To test, try selecting previous/next items with the following (adjust -c as > > needed): > > for i in $(seq 20); do > > cat /dev/urandom | base64 | tr -d '\n' | head -c 100 > > done | ./dmenu -l 10 > > > > Hi Miles, > > Thanks for the patch. It uses indeed less resources for very long lines and it > makes more sense to do it this way. > > A small change for the test script, you probably want to add a newline (echo) > between it: > > for i in $(seq 20); do > cat /dev/urandom | base64 | tr -d '\n' | head -c 100 > echo > done | ./dmenu -l 10 > > I synced the code to dwm and the libsl repository too. > > -- > Kind regards, > Hiltjo > Hi, This patch had issues so it was reverted for now. Anyone have time to work on this during the holidays maybe? The drw.c drw_text() text truncation code using "..." is also incorrect currently for UTF-8 and when multiple fallback fonts are used. -- Kind regards, Hiltjo
Re: [hackers][st][PATCH] Add support for OSC color sequences.
On Sun, Dec 12, 2021 at 01:29:32AM -0500, Raheman Vaiya wrote: > > -static unsigned int defaultcs = 256; > >> +unsigned int defaultfg = 255; > >> +unsigned int defaultbg = 254; > >> +unsigned int defaultcs = 256; > > > The values in the config are changed here. Is it intended? If so, why? > > The colours 0 and 7 are distinct and can be set independently from the > foreground and background colours in most terminals. I modified defaultbg > and > defaultfg to reflect this. In retrospect, I probably should have made them > > 255 to avoid conflicting with legitimate palette colours. I have done so in > the > amended patch. > > >> + n = sprintf(buf, "\033]4;%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", > num, r, r, g, g, b, b); > > > I prefer snprintf here, just in case. The arguments for sprintf are too > long and > > should be wrapped. > > Ok. > > >> + else if (xsetcolorname(defaultbg, p)) > >> + fprintf(stderr, "erresc: invalid > background color %d\n", p); > > > The argument p is not int. The compiler (clang) also gives a warning > about it. > > Oops. I'm not sure why my gcc didn't produce a warning. > > > Case 10 should be ordered before case 11. > > Ok. > > > case 104: /* color reset, here p = NULL */ > > j = (narg > 1) ? atoi(strescseq.args[1]) : -1; > > > The variable j should be checked and also the xgetcolor and/or functions > using > > it should have range checks. It seems dc.col can be out-of-bounds with > some > > input. I also noticed a crash here while testing. > > Ok. > > >> +void xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char > *b); > > > xgetcolor should be below xstrdup and as a separate line. > > Will do. > > Thanks for reviewing the patch :). I have attached an amended copy below. > Let > me know if anything else requires attention. > > Regards, > Raheman > > On Sat, Dec 11, 2021 at 7:01 AM Hiltjo Posthuma > wrote: > > > Hi, > > > > Some comments below: > > > > On Fri, Dec 10, 2021 at 05:53:40PM -0500, Raheman Vaiya wrote: > > > The attached patch adds full support for the OSC 10/11/12/4 control > > > sequences. > > > These are used by programs like vim and theme.sh to query and set > > terminal > > > colours and are supported by most major terminals (e.g alacrittty, kitty, > > > libvte). I submitted a patch to the wiki some time ago, but it didn't > > > > xterm supports it too. > > > > > include > > > support for query sequences. I am posting it here because I feel it would > > > be a > > > good candidate for inclusion into st itself. > > > > > > You can test it using https://github.com/lemnos/theme.sh by running > > > > > > theme.sh zenburn > > > theme.sh -p > > > > > > Don't hesitate to let me know if I have made any grievous errors :P. > > > > > > Regards, > > > Raheman > > > > > diff --git a/config.def.h b/config.def.h > > > index 6f05dce..3932e83 100644 > > > --- a/config.def.h > > > +++ b/config.def.h > > > @@ -127,9 +127,9 @@ static const char *colorname[] = { > > > * Default colors (colorname index) > > > * foreground, background, cursor, reverse cursor > > > */ > > > -unsigned int defaultfg = 7; > > > -unsigned int defaultbg = 0; > > > -static unsigned int defaultcs = 256; > > > +unsigned int defaultfg = 255; > > > +unsigned int defaultbg = 254; > > > +unsigned int defaultcs = 256; > > > static unsigned int defaultrcs = 257; > > > > The values in the config are changed here. Is it intended? If so, why? > > > > > > > > /* > > > diff --git a/st.c b/st.c > > > index a9338e1..a469512 100644 > > > --- a/st.c > > > +++ b/st.c > > > @@ -1842,6 +1842,32 @@ csireset(void) > > > memset(, 0, sizeof(csiescseq)); > > > } > > > > > > +void > > > +osc4_color_response(int num) > > > +{ > > > + int n; > > > + char buf[32]; > > > + unsigned char r,g,b; > > > + > > > + xgetcolor(num, ,,); > > > + n = sprintf(buf, "\033]4;%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", > > num, r, r, g, g, b, b); > > > + > > > > I prefer snprintf here, just in case. The arguments for sprintf are too > > long and > > should be wrapped. > > > > > + ttywrite(buf, n, 1); > > > +} > > > + > > > +void > > > +osc_color_response(int index, int num) > > > +{ > > > + int n; > > > + char buf[32]; > > > + unsigned char r,g,b; > > > + > > > + xgetcolor(index, ,,); > > > + n = sprintf(buf, "\033]%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", > > num, r, r, g, g, b, b); > > > + > > > + ttywrite(buf, n, 1); > > > +} > > > + > > > void > > > strhandle(void) > > > { > > > @@ -1880,6 +1906,45 @@ strhandle(void) > > > } > > > } > > > return; > > > + case 11: > > > + if (narg < 2) > > > + break; > > > + > > > + p = strescseq.args[1]; > > > + > > > + if(!strcmp(p, "?")) > > > +
[hackers] [st] follow-up fix for OSC color sequences, return || Hiltjo Posthuma
commit 273db5ceaf392e68c2faf8f7dec14ea2e25e980d Author: Hiltjo Posthuma AuthorDate: Sun Dec 26 19:00:41 2021 +0100 Commit: Hiltjo Posthuma CommitDate: Sun Dec 26 19:00:41 2021 +0100 follow-up fix for OSC color sequences, return Otherwise the message "erresc: unknown str" is printed. diff --git a/st.c b/st.c index 781dbf2..6783c2b 100644 --- a/st.c +++ b/st.c @@ -1928,7 +1928,7 @@ strhandle(void) fprintf(stderr, "erresc: invalid foreground color: %s\n", p); else redraw(); - break; + return; case 11: if (narg < 2) break; @@ -1941,7 +1941,7 @@ strhandle(void) fprintf(stderr, "erresc: invalid background color: %s\n", p); else redraw(); - break; + return; case 12: if (narg < 2) break; @@ -1954,7 +1954,7 @@ strhandle(void) fprintf(stderr, "erresc: invalid cursor color: %s\n", p); else redraw(); - break; + return; case 4: /* color set */ if (narg < 3) break;
[hackers] [st] Add support for OSC color sequences || Raheman Vaiya
commit 8e310303903792c010d03c046ba75f8b18f7d3a7 Author: Raheman Vaiya AuthorDate: Sun Dec 26 18:57:04 2021 +0100 Commit: Hiltjo Posthuma CommitDate: Sun Dec 26 18:57:44 2021 +0100 Add support for OSC color sequences diff --git a/config.def.h b/config.def.h index 6f05dce..91ab8ca 100644 --- a/config.def.h +++ b/config.def.h @@ -120,6 +120,8 @@ static const char *colorname[] = { /* more colors can be added after 255 to use with DefaultXX */ "#cc", "#55", + "gray90", /* default foreground colour */ + "black", /* default background colour */ }; @@ -127,9 +129,9 @@ static const char *colorname[] = { * Default colors (colorname index) * foreground, background, cursor, reverse cursor */ -unsigned int defaultfg = 7; -unsigned int defaultbg = 0; -static unsigned int defaultcs = 256; +unsigned int defaultfg = 258; +unsigned int defaultbg = 259; +unsigned int defaultcs = 256; static unsigned int defaultrcs = 257; /* diff --git a/st.c b/st.c index a9338e1..781dbf2 100644 --- a/st.c +++ b/st.c @@ -1842,6 +1842,42 @@ csireset(void) memset(, 0, sizeof(csiescseq)); } +void +osc4_color_response(int num) +{ + int n; + char buf[32]; + unsigned char r, g, b; + + if (xgetcolor(num, , , )) { + fprintf(stderr, "erresc: failed to fetch osc4 color %d\n", num); + return; + } + + n = snprintf(buf, sizeof buf, "\033]4;%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", +num, r, r, g, g, b, b); + + ttywrite(buf, n, 1); +} + +void +osc_color_response(int index, int num) +{ + int n; + char buf[32]; + unsigned char r, g, b; + + if (xgetcolor(index, , , )) { + fprintf(stderr, "erresc: failed to fetch osc color %d\n", index); + return; + } + + n = snprintf(buf, sizeof buf, "\033]%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", +num, r, r, g, g, b, b); + + ttywrite(buf, n, 1); +} + void strhandle(void) { @@ -1880,6 +1916,45 @@ strhandle(void) } } return; + case 10: + if (narg < 2) + break; + + p = strescseq.args[1]; + + if (!strcmp(p, "?")) + osc_color_response(defaultfg, 10); + else if (xsetcolorname(defaultfg, p)) + fprintf(stderr, "erresc: invalid foreground color: %s\n", p); + else + redraw(); + break; + case 11: + if (narg < 2) + break; + + p = strescseq.args[1]; + + if (!strcmp(p, "?")) + osc_color_response(defaultbg, 11); + else if (xsetcolorname(defaultbg, p)) + fprintf(stderr, "erresc: invalid background color: %s\n", p); + else + redraw(); + break; + case 12: + if (narg < 2) + break; + + p = strescseq.args[1]; + + if (!strcmp(p, "?")) + osc_color_response(defaultcs, 12); + else if (xsetcolorname(defaultcs, p)) + fprintf(stderr, "erresc: invalid cursor color: %s\n", p); + else + redraw(); + break; case 4: /* color set */ if (narg < 3) break; @@ -1887,7 +1962,10 @@ strhandle(void) /* FALLTHROUGH */ case 104: /* color reset, here p = NULL */ j = (narg > 1) ? atoi(strescseq.args[1]) : -1; - if (xsetcolorname(j, p)) { + + if (!strcmp(p, "?")) + osc4_color_response(j); + else if (xsetcolorname(j, p)) { if (par == 104 && narg <= 1) return; /* color reset without parameter */ fprintf(stderr, "erresc: invalid color j=%d, p=%s\n", diff --git a/st.h b/st.h index fa2eddf..519b9bd 100644 --- a/st.h +++ b/st.h @@ -111,6 +111,8 @@ void *xmalloc(size_t); void *xrealloc(void *, size_t); char *xstrdup(const char *); +int xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b); + /* config.h globals */ extern char *utmp; extern char *scroll; @@ -123,3 +125,4 @@ extern char *termname; extern unsigned int tabspaces; extern unsigned int defaultfg; extern unsigned int defaultbg; +extern