ref: 1be7f16d6216762401da82b50a1b1606f5ac9667
parent: 6d1b825eaca4da51d7b9860e3ff6a0e432c7f925
author: spew <spew@palas>
date: Fri Jan 31 18:33:15 EST 2025
some more
--- a/guitest.c
+++ b/guitest.c
@@ -78,8 +78,10 @@
draw(t->screen, t->scrollr, t->cols[BORD], nil, ZP);
scrpos = t->scrollr;
- scrpos.min.y = scrpos.min.y+max(0, Dy(scrpos))*t->topline/t->nlines;
- scrpos.max.y = scrpos.min.y+Dy(t->scrollr)*t->Frame.maxlines/t->nlines;
+ if(t->nlines > 0){
+ scrpos.min.y = scrpos.min.y+max(0, Dy(scrpos))*t->topline/t->nlines;
+ scrpos.max.y = scrpos.min.y+Dy(t->scrollr)*t->Frame.maxlines/t->nlines;
+ }
scrpos = insetrect(scrpos, 1);
draw(screen, scrpos, t->cols[BACK], nil, ZP);
@@ -88,21 +90,8 @@
}
void
-textaddline(Text *t, uint sol)
-{
- if(t->nlines == t->cap){
- t->cap *= 2;
- t->lines = erealloc(t->lines, t->cap*sizeof(*t->lines));
- }
- t->lines[t->nlines++] = sol;
-}
-
-void
textinit(Text *t)
{
- Rune *rp;
- uint sol;
-
t->scrollr = t->screen->r;
t->scrollr.max.x = t->screen->r.min.x + Scrollwid + 1;
@@ -110,14 +99,13 @@
t->bodyr.min.y = t->screen->r.min.y;
t->bodyr.max = t->screen->r.max;
- t->cap = 1024;
- t->lines = emallocz(t->cap*sizeof(*t->lines), 1);
- t->nlines = sol = 0;
- for(rp = t->text; *rp != L'\0'; rp++)
- if(*rp == L'\n'){
- textaddline(t, sol);
- sol = rp + 1 - t->text;
- }
+ t->text = emallocz(8192*sizeof(*t->text), 1);
+ t->textcap = 8192;
+ t->textlen = 0;
+
+ t->linescap = 1024;
+ t->lines = emallocz(t->linescap*sizeof(*t->lines), 1);
+ t->nlines = 0;
t->topline = 0;
frclear(t, 0);
@@ -125,9 +113,40 @@
}
void
+textsetline(Text *t, uint sol)
+{
+ if(t->nlines == t->linescap){
+ t->linescap *= 2;
+ t->lines = erealloc(t->lines, t->linescap*sizeof(*t->lines));
+ }
+ t->lines[t->nlines++] = sol;
+}
+
+void
+textaddlines(Text *t, Rune *s)
+{
+ Rune *rp;
+ uint sol;
+ int len;
+
+ len = runestrlen(s);
+ while(len-1 > t->textcap-t->textlen){
+ t->textcap *= 2;
+ t->text = erealloc(t->text, t->textcap*sizeof(*t->text));
+ }
+ runestrecpy(t->text + t->textlen, t->text+t->textcap, s);
+ sol = t->textlen;
+ for(rp = s; rp = runestrchr(rp, L'\n'); rp++){
+ textsetline(t, sol);
+ sol = rp + 1 - s + t->textlen;
+ }
+ t->textlen += len;
+}
+
+void
texttopline(Text *t, int y)
{
- t->topline = min(t->nlines-1, max(0, y-t->scrollr.min.y)*t->nlines/Dy(t->scrollr));
+ t->topline = min(max(0, y-t->scrollr.min.y)*t->nlines/Dy(t->scrollr), t->nlines-1);
}
void
@@ -136,9 +155,11 @@
Rune r, *rs;
Mouse m;
Chan *chan;
- char *s, *wsys;
+ char *s, *wsys, *mntstr;
int len, fd;
+ ARGBEGIN{}ARGEND
+
len = readn(0, buf, sizeof(buf));
buf[len] = '\0';
for(s = buf, rs = lorem; *s != '\0'; s+=len, rs++)
@@ -154,8 +175,6 @@
chan = emallocz(sizeof(*chan), 1);
- chan->body.text = lorem;
- chan->body.textlen = runestrlen(chan->body.text);
chan->body.cols[BACK] = allocimagemix(display, DPurpleblue, DWhite);
chan->body.cols[HIGH] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DDarkyellow);
chan->body.cols[BORD] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPurpleblue);
@@ -163,12 +182,14 @@
chan->body.cols[HTEXT] = display->black;
chan->body.screen = screen;
textinit(&chan->body);
+ textaddlines(&chan->body, lorem);
if((wsys = getenv("wsys")) == nil)
sysfatal("cannot find $wsys: %r");
if((fd = open(wsys, ORDWR)) < 0)
sysfatal("cannot open $wsys: %r");
- if(mount(fd, -1, "/mnt/wsysnicks", MREPL, "new") < 0)
+ mntstr = smprint("new -r %d %d %d %d", screen->r.min.x - Dx(screen->r)/3, screen->r.min.y-4, screen->r.min.x-4, screen->r.max.y+4);
+ if(mount(fd, -1, "/mnt/wsysnicks", MREPL, mntstr) < 0)
sysfatal("cannot create new window: %r");
if(gengetwindow(display, "/mnt/wsysnicks/winname", &chan->nicks.screen, &chan->nicks._screen, Refnone) < 0)
sysfatal("cannot get window: %r");
--- a/mez.h
+++ b/mez.h
@@ -10,7 +10,7 @@
struct Text {
Frame;
Rune *text;
- uint textlen, topline, nlines, cap, *lines;
+ uint textlen, topline, nlines, linescap, textcap, *lines;
Rectangle scrollr, bodyr;
Image *screen, *cols[NCOL];
Screen *_screen;
--
⑨