]> Nishi Git Mirror - libw3.git/commitdiff
proper scroll
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Tue, 30 Jan 2024 02:02:11 +0000 (02:02 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Tue, 30 Jan 2024 02:02:11 +0000 (02:02 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@139 d27a3e52-49c5-7645-884c-6793ebffc270

Example/w3b/w3b.c

index 323326936f6924bd110b713b590e00233d7bda2c..2f3c4ba597db0d0a46e1b8347c3efd0f5c63260e 100644 (file)
@@ -58,6 +58,8 @@ char* databuf;
 int datalen;
 bool isredir;
 
+int x = 0;
+
 void status_handler(struct W3* w3, int st) {
        if(st >= 300 && st < 400) {
                isredir = true;
@@ -94,6 +96,7 @@ void access_site(const char* url) {
                        datalen = 0;
                        W3_Set_Method(w3, "GET");
                        W3_Set_Path(w3, u->path);
+                       W3_Set_Header(w3, "User-Agent", "LibW3/" LIBW3_VERSION " W3B/");
                        W3_HTTP_Enable_Redirect(w3);
                        W3_On(w3, "status", (void*)status_handler);
                        W3_On(w3, "data", (void*)data_handler);
@@ -120,6 +123,7 @@ int start = 0;
 
 void html_handler(char* tagname, char* attr) {
        if(nl - start > termh - 3) return;
+       int oldnl = nl;
        if(strcasecmp(tagname, "title") == 0) {
                title = true;
        } else if(strcasecmp(tagname, "/title") == 0) {
@@ -181,8 +185,10 @@ void html_handler(char* tagname, char* attr) {
                        if(alt != NULL) {
                                printf("[%s]", alt);
                                free(alt);
+                               x += 2 + strlen(alt);
                        } else {
                                printf("[IMG]");
+                               x += 5;
                        }
                }
        } else if(strcasecmp(tagname, "hr") == 0) {
@@ -196,6 +202,13 @@ void html_handler(char* tagname, char* attr) {
                nl++;
        }
        fflush(stdout);
+       if(x >= termw) {
+               if(nl >= start) {
+                       nl++;
+                       x = x % termw;
+               }
+       }
+       if(oldnl != nl) x = 0;
 }
 
 void text_handler(char* data) {
@@ -247,6 +260,13 @@ void text_handler(char* data) {
                }
                sprintf(text, "%s%s%s\x1b[m", italic ? "\x1b[3m" : "", bold ? "\x1b[1m" : "", fmt_data);
                if(nl >= start) printf("%s", text);
+               x += strlen(text);
+               if(x >= termw) {
+                       if(nl >= start) {
+                               nl++;
+                               x = x % termw;
+                       }
+               }
                fflush(stdout);
                free(text);
                free(fmt_data);