shithub: mycel

Download patch

ref: 29ad10125ebbee01ea914955a33c0be26ee1ae20
parent: b881ccc61dad23f9a33fd05c8aa00213d591adce
author: Philip Silva <philip.silva@protonmail.com>
date: Sat Jun 5 05:53:58 EDT 2021

Add rpc to query nodes

--- a/browser/fs/fs.go
+++ b/browser/fs/fs.go
@@ -1,11 +1,16 @@
 package fs
 
 import (
+	"bufio"
+	"encoding/json"
 	"fmt"
 	"github.com/knusbaum/go9p/fs"
 	"github.com/knusbaum/go9p/proto"
 	"github.com/psilva261/opossum/logger"
+	"github.com/psilva261/opossum/nodes"
+	"net"
 	"os/user"
+	"strings"
 	"sync"
 )
 
@@ -18,8 +23,13 @@
 	cssDir *fs.StaticDir
 	jsDir *fs.StaticDir
 	html string
+	DOM Queryable
 )
 
+type Queryable interface {
+	Query(q string) ([]*nodes.Node, error)
+}
+
 func SetLogger(l *logger.Logger) {
 	log = l
 }
@@ -29,13 +39,13 @@
 
 	u, err := user.Current()
 	if err != nil {
-		log.Errorf("get user: %w", err)
+		log.Errorf("get user: %v", err)
 		return
 	}
 	un = u.Username
 	gn, err = group(u)
 	if err != nil {
-		log.Errorf("get group: %w", err)
+		log.Errorf("get group: %v", err)
 		return
 	}
 	oFS, root = fs.NewFS(un, gn, 0500)
@@ -51,7 +61,7 @@
 	root.AddChild(h)
 	d, err := fs.CreateStaticDir(oFS, root, un, "css", 0500|proto.DMDIR, 0)
 	if err != nil {
-		log.Errorf("create static dir: %w", err)
+		log.Errorf("create static dir: %v", err)
 		return
 	}
 	cssDir = d.(*fs.StaticDir)
@@ -58,11 +68,52 @@
 	root.AddChild(cssDir)
 	d, err = fs.CreateStaticDir(oFS, root, un, "js", 0500|proto.DMDIR, 0)
 	if err != nil {
-		log.Errorf("create static dir: %w", err)
+		log.Errorf("create static dir: %v", err)
 		return
 	}
 	jsDir = d.(*fs.StaticDir)
 	root.AddChild(jsDir)
+	q := fs.NewListenFile(oFS.NewStat("query", un, gn, 0600))
+	root.AddChild(q)
+	lq := (*fs.ListenFileListener)(q)
+	go Query(lq)
+}
+
+func Query(lq *fs.ListenFileListener) {
+	for {
+		conn, err := lq.Accept()
+		if err != nil {
+			log.Errorf("accept: %v", err)
+			continue
+		}
+		go query(conn)
+	}
+}
+
+func query(conn net.Conn) {
+	r := bufio.NewReader(conn)
+	enc := json.NewEncoder(conn)
+	defer conn.Close()
+
+	l, err := r.ReadString('\n')
+	if err != nil {
+		log.Errorf("read string: %v", err)
+		return
+	}
+	l = strings.TrimSpace(l)
+
+	if DOM == nil {
+		return
+	}
+	nodes, err := DOM.Query(l)
+	if err != nil {
+		log.Errorf("query nodes: %v", err)
+		return
+	}
+	if err := enc.Encode(nodes); err != nil {
+		log.Errorf("encode: %v", err)
+		return
+	}
 }
 
 func Update(htm string, css []string, js []string) {
--- a/browser/fs/fs_plan9.go
+++ b/browser/fs/fs_plan9.go
@@ -21,7 +21,7 @@
 	go func() {
 		err = go9p.ServeReadWriter(f1, f1, srv)
 		if err != nil {
-			log.Fatalf("serve rw: %v", err)
+			log.Errorf("serve rw: %v", err)
 		}
 	}()
 
--- a/browser/website.go
+++ b/browser/website.go
@@ -133,8 +133,9 @@
 	body := grep(doc, "body")
 
 	log.Printf("Layout website...")
+	nt := nodes.NewNodeTree(body, style.Map{}, nodeMap, &nodes.Node{})
 	scroller = NewScroll(
-		NodeToBox(0, browser, nodes.NewNodeTree(body, style.Map{}, nodeMap, &nodes.Node{})),
+		NodeToBox(0, browser, nt),
 	)
 	numElements := 0
 	TraverseTree(scroller, func(ui duit.UI) {
@@ -144,13 +145,15 @@
 	log.Printf("Layouting done (%v elements created)", numElements)
 	if numElements < 10 {
 		log.Errorf("Less than 10 elements layouted, seems css processing failed. Will layout without css")
+		nt = nodes.NewNodeTree(body, style.Map{}, make(map[*html.Node]style.Map), nil)
 		scroller = NewScroll(
-			NodeToBox(0, browser, nodes.NewNodeTree(body, style.Map{}, make(map[*html.Node]style.Map), nil)),
+			NodeToBox(0, browser, nt),
 		)
 		w.UI = scroller
 	}
 
 	fs.Update(htm, csss, js)
+	fs.DOM = nt
 
 	log.Flush()
 }
--- a/cmd/browse/main.go
+++ b/cmd/browse/main.go
@@ -6,6 +6,7 @@
 	"fmt"
 	"image"
 	"os"
+	"github.com/knusbaum/go9p"
 	"github.com/psilva261/opossum"
 	"github.com/psilva261/opossum/browser"
 	"github.com/psilva261/opossum/browser/fs"
@@ -184,6 +185,7 @@
 
 	log = logger.Log
 	log.Debug = *dbg
+	go9p.Verbose = log.Debug
 
 	if err := Main(); err != nil {
 		log.Fatalf("Main: %v", err)
--- a/domino/jsfcall/jsfcall.go
+++ b/domino/jsfcall/jsfcall.go
@@ -7,8 +7,6 @@
 	Rinit
 	Tclick
 	Rclick
-	Tquery
-	Rquery
 )
 
 type Msg struct {