shithub: mycel

Download patch

ref: c70619eff54eb25a853b850ac7ccb2f447658519
parent: 35f58b7553f6ebeeaabcdba5b6950f312463e3b8
author: Philip Silva <philip.silva@protonmail.com>
date: Sun Sep 12 02:09:19 EDT 2021

Move LocationField into main.go

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -1391,10 +1391,10 @@
 	dui       *duit.DUI
 	Website   *Website
 	StatusBar *duit.Label
-	LocationField *duit.Field
 	loading bool
 	client    *http.Client
 	Download func(res chan *string)
+	LocCh chan string
 }
 
 func NewBrowser(_dui *duit.DUI, initUrl string) (b *Browser) {
@@ -1412,29 +1412,9 @@
 			Text: "",
 		},
 		Website: &Website{},
+		LocCh: make(chan string, 10),
 	}
 
-	b.LocationField = &duit.Field{
-		Text:    initUrl,
-		Font:    Style.Font(),
-		Keys:    func(k rune, m draw.Mouse) (e duit.Event) {
-			if k == EnterKey && !b.loading {
-				b.loading = true
-				a := b.LocationField.Text
-				if !strings.HasPrefix(strings.ToLower(a), "http") {
-					a = "http://" + a
-				}
-				u, err := url.Parse(a)
-				if err != nil {
-					log.Errorf("parse url: %v", err)
-					return
-				}
-				return b.LoadUrl(u)
-			}
-			return
-		},
-	}
-
 	u, err := url.Parse(initUrl)
 	if err != nil {
 		log.Fatalf("parse: %v", err)
@@ -1498,9 +1478,8 @@
 
 func (b *Browser) Back() (e duit.Event) {
 	if !b.loading {
-		b.loading = true
 		b.History.Back()
-		b.LocationField.Text = b.History.URL().String()
+		b.LocCh <- b.History.URL().String()
 		b.LoadUrl(b.History.URL())
 	}
 	e.Consumed = true
@@ -1517,8 +1496,7 @@
 		b.showBodyMessage("")
 
 		if !b.loading {
-			b.loading = true
-			b.LocationField.Text = u.String()
+			b.LocCh <- u.String()
 			b.LoadUrl(u)
 		}
 
@@ -1528,6 +1506,10 @@
 	}
 }
 
+func (b *Browser) Loading() bool {
+	return b.loading
+}
+
 func (b *Browser) showBodyMessage(msg string) {
 	b.Website.UI = &duit.Label{
 		Text: msg,
@@ -1540,6 +1522,7 @@
 
 // LoadUrl after from location field,
 func (b *Browser) LoadUrl(url *url.URL) (e duit.Event) {
+	b.loading = true
 	go b.loadUrl(url)
 	e.Consumed = true
 
@@ -1672,7 +1655,7 @@
 		}
 		b.History.Push(resp.Request.URL, of)
 		log.Printf("b.History is now %s", b.History.String())
-		b.LocationField.Text = b.URL().String()
+		b.LocCh <- b.URL().String()
 	}
 	return
 }
--- a/cmd/opossum/main.go
+++ b/cmd/opossum/main.go
@@ -10,8 +10,10 @@
 	"github.com/psilva261/opossum/js"
 	"github.com/psilva261/opossum/logger"
 	"github.com/psilva261/opossum/style"
+	"net/url"
 	"os/signal"
 	"runtime/pprof"
+	"strings"
 	"time"
 	"github.com/mjl-/duit"
 )
@@ -21,9 +23,10 @@
 	dui *duit.DUI
 	b *browser.Browser
 	cpuprofile string
-	startPage string = "http://9p.io"
+	loc string = "http://9p.io"
 	dbg bool
 	v View
+	Style = style.Map{}
 )
 
 func init() {
@@ -34,10 +37,37 @@
 	Render() []*duit.Kid
 }
 
-type Nav struct {}
+type Nav struct {
+	LocationField *duit.Field
+}
 
+func NewNav() (n *Nav) {
+	n = &Nav{
+		LocationField: &duit.Field{
+			Text:    loc,
+			Font:    Style.Font(),
+			Keys:    func(k rune, m draw.Mouse) (e duit.Event) {
+				if k == browser.EnterKey && !b.Loading() {
+					a := n.LocationField.Text
+					if !strings.HasPrefix(strings.ToLower(a), "http") {
+						a = "http://" + a
+					}
+					u, err := url.Parse(a)
+					if err != nil {
+						log.Errorf("parse url: %v", err)
+						return
+					}
+					return b.LoadUrl(u)
+				}
+				return
+			},
+		},
+	}
+	return
+}
+
 func (n *Nav) Render() []*duit.Kid {
-	return duit.NewKids(
+	uis := []duit.UI{
 		&duit.Grid{
 			Columns: 2,
 			Padding: duit.NSpace(2, duit.SpaceXY(5, 3)),
@@ -51,14 +81,16 @@
 				},
 				&duit.Box{
 					Kids: duit.NewKids(
-						b.LocationField,
+						n.LocationField,
 					),
 				},
 			),
 		},
-		b.StatusBar,
-		b.Website,
-	)
+	}
+	if b != nil {
+		uis = append(uis, b.StatusBar, b.Website)
+	}
+	return duit.NewKids(uis...)
 }
 
 type Confirm struct {
@@ -88,7 +120,7 @@
 						c.res <- &s
 						c.done = true
 						e.Consumed = true
-						v = &Nav{}
+						v = NewNav()
 						render()
 						return
 					},
@@ -101,7 +133,7 @@
 						close(c.res)
 						c.done = true
 						e.Consumed = true
-						v = &Nav{}
+						v = NewNav()
 						render()
 						return
 					},
@@ -130,7 +162,9 @@
 		Kids: v.Render(),
 		Background: white,
 	}
-	browser.PrintTree(b.Website.UI)
+	if b != nil {
+		browser.PrintTree(b.Website.UI)
+	}
 	log.Printf("Render.....")
 	dui.MarkLayout(dui.Top.UI)
 	dui.MarkDraw(dui.Top.UI)
@@ -146,8 +180,13 @@
 	dui.Debug = dbg
 
 	style.Init(dui)
+	v = NewNav()
+	render()
 
-	b = browser.NewBrowser(dui, startPage)
+	b = &browser.Browser{
+		LocCh: make(chan string, 10),
+	}
+	b = browser.NewBrowser(dui, loc)
 	b.Download = func(res chan *string) {
 		v = &Confirm{
 			text: fmt.Sprintf("Download %v", b.URL()),
@@ -157,15 +196,23 @@
 		render()
 		return
 	}
-	v = &Nav{}
+	v = NewNav()
 	render()
 
 	for {
 		select {
 		case e := <-dui.Inputs:
+			//log.Infof("e=%v", e)
 			dui.Input(e)
 
+		case loc = <-b.LocCh:
+			log.Infof("loc=%v", loc)
+			if nav, ok := v.(*Nav); ok {
+				nav.LocationField.Text = loc
+			}
+
 		case err, ok := <-dui.Error:
+			//log.Infof("err=%v", err)
 			if !ok {
 				return nil
 			}
@@ -203,7 +250,7 @@
 			if len(args) > 1 {
 				usage()
 			}
-			startPage, args = args[0], args[1:]
+			loc, args = args[0], args[1:]
 		}
 	}