shithub: mycel

Download patch

ref: 35f58b7553f6ebeeaabcdba5b6950f312463e3b8
parent: 3260406659434893622caf40d0b6ef8884e7cc16
author: Philip Silva <philip.silva@protonmail.com>
date: Sat Sep 4 05:36:38 EDT 2021

Untangle main.go a bit

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -1394,7 +1394,7 @@
 	LocationField *duit.Field
 	loading bool
 	client    *http.Client
-	Download func(done chan int) chan string
+	Download func(res chan *string)
 }
 
 func NewBrowser(_dui *duit.DUI, initUrl string) (b *Browser) {
@@ -1560,21 +1560,20 @@
 	if contentType.IsHTML() || contentType.IsPlain() || contentType.IsEmpty() {
 		b.render(contentType, buf)
 	} else {
-		done := make(chan int)
-		res := b.Download(done)
+		res := make(chan *string, 1)
+		b.Download(res)
 
 		log.Infof("Download unhandled content type: %v", contentType)
 
 		fn := <-res
 
-		if fn != "" {
-			log.Infof("Download to %v", fn)
-			f, _ := os.Create(fn)
+		if fn != nil && *fn != "" {
+			log.Infof("Download to %v", *fn)
+			f, _ := os.Create(*fn)
 			f.Write(buf)
 			f.Close()
 		}
 		dui.Call <- func() {
-			done <- 1
 			b.loading = false
 		}
 	}
--- a/cmd/opossum/main.go
+++ b/cmd/opossum/main.go
@@ -16,17 +16,27 @@
 	"github.com/mjl-/duit"
 )
 
-var dui *duit.DUI
 
-var cpuprofile string
-var startPage string = "http://9p.io"
-var dbg bool
+var (
+	dui *duit.DUI
+	b *browser.Browser
+	cpuprofile string
+	startPage string = "http://9p.io"
+	dbg bool
+	v View
+)
 
 func init() {
 	browser.EnableNoScriptTag = true
 }
 
-func mainView(b *browser.Browser) []*duit.Kid {
+type View interface {
+	Render() []*duit.Kid
+}
+
+type Nav struct {}
+
+func (n *Nav) Render() []*duit.Kid {
 	return duit.NewKids(
 		&duit.Grid{
 			Columns: 2,
@@ -51,13 +61,73 @@
 	)
 }
 
-func render(b *browser.Browser, kids []*duit.Kid) {
+type Confirm struct {
+	text string
+	value string
+	res chan *string
+	done bool
+}
+
+func (c *Confirm) Render() []*duit.Kid {
+	f := &duit.Field{
+		Text: c.value,
+	}
+	return duit.NewKids(
+		&duit.Grid{
+			Columns: 3,
+			Padding: duit.NSpace(3, duit.SpaceXY(5, 3)),
+			Halign:  []duit.Halign{duit.HalignLeft, duit.HalignLeft, duit.HalignRight},
+			Valign:  []duit.Valign{duit.ValignMiddle, duit.ValignMiddle, duit.ValignMiddle},
+			Kids: duit.NewKids(
+				&duit.Button{
+					Text:  "Ok",
+					Font:  browser.Style.Font(),
+					Click: func() (e duit.Event) {
+						if c.done { return }
+						s := f.Text
+						c.res <- &s
+						c.done = true
+						e.Consumed = true
+						v = &Nav{}
+						render()
+						return
+					},
+				},
+				&duit.Button{
+					Text:  "Abort",
+					Font:  browser.Style.Font(),
+					Click: func() (e duit.Event) {
+						if c.done { return }
+						close(c.res)
+						c.done = true
+						e.Consumed = true
+						v = &Nav{}
+						render()
+						return
+					},
+				},
+				f,
+			),
+		},
+		&duit.Label{
+			Text: c.text,
+		},
+	)
+}
+
+type Loading struct {}
+
+func (l *Loading) Render() []*duit.Kid {
+	return nil
+}
+
+func render() {
 	white, err := dui.Display.AllocImage(image.Rect(0, 0, 10, 10), draw.ARGB32, true, 0xffffffff)
 	if err != nil {
 		log.Errorf("%v", err)
 	}
 	dui.Top.UI = &duit.Box{
-		Kids: kids,
+		Kids: v.Render(),
 		Background: white,
 	}
 	browser.PrintTree(b.Website.UI)
@@ -68,53 +138,6 @@
 	log.Printf("Rendering done")
 }
 
-func confirm(b *browser.Browser, text, value string) chan string {
-	res := make(chan string)
-
-	dui.Call <- func() {
-		f := &duit.Field{
-			Text: value,
-		}
-
-		kids := duit.NewKids(
-			&duit.Grid{
-				Columns: 3,
-				Padding: duit.NSpace(3, duit.SpaceXY(5, 3)),
-				Halign:  []duit.Halign{duit.HalignLeft, duit.HalignLeft, duit.HalignRight},
-				Valign:  []duit.Valign{duit.ValignMiddle, duit.ValignMiddle, duit.ValignMiddle},
-				Kids: duit.NewKids(
-					&duit.Button{
-						Text:  "Ok",
-						Font:  browser.Style.Font(),
-						Click: func() (e duit.Event) {
-							res <- f.Text
-							e.Consumed = true
-							return
-						},
-					},
-					&duit.Button{
-						Text:  "Abort",
-						Font:  browser.Style.Font(),
-						Click: func() (e duit.Event) {
-							res <- ""
-							e.Consumed = true
-							return
-						},
-					},
-					f,
-				),
-			},
-			&duit.Label{
-				Text: text,
-			},
-		)
-
-		render(b, kids)
-	}
-
-	return res
-}
-
 func Main() (err error) {
 	dui, err = duit.NewDUI("opossum", nil) // TODO: rm global var
 	if err != nil {
@@ -124,17 +147,18 @@
 
 	style.Init(dui)
 
-	b := browser.NewBrowser(dui, startPage)
-	b.Download = func(done chan int) chan string {
-		go func() {
-			<-done
-			dui.Call <- func() {
-				render(b, mainView(b))
-			}
-		}()
-		return confirm(b, fmt.Sprintf("Download %v", b.URL()), "/download.file")
+	b = browser.NewBrowser(dui, startPage)
+	b.Download = func(res chan *string) {
+		v = &Confirm{
+			text: fmt.Sprintf("Download %v", b.URL()),
+			value: "/download.file",
+			res: res,
+		}
+		render()
+		return
 	}
-	render(b, mainView(b))
+	v = &Nav{}
+	render()
 
 	for {
 		select {