shithub: mycel

Download patch

ref: 3dfe00dc2f63fe2d93dc977a1973ab03a74b93bc
parent: cdae93dae8b27ebf26cca981be01088e01e9f865
author: Philip Silva <[email protected]>
date: Tue Dec 29 06:33:58 EST 2020

Downloading

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -17,6 +17,7 @@
 	"opossum/logger"
 	"opossum/nodes"
 	"opossum/style"
+	"os"
 	"strings"
 
 	"github.com/mjl-/duit"
@@ -1132,6 +1133,7 @@
 	StatusBar *duit.Label
 	LocationField *duit.Field
 	client    *http.Client
+	Download func(done chan int) chan string
 }
 
 func NewBrowser(_dui *duit.DUI, initUrl string) (b *Browser) {
@@ -1276,7 +1278,23 @@
 	if contentType.IsHTML() || contentType.IsPlain() {
 		b.render(buf)
 	} else {
-		log.Errorf("unhandled content type: %v", contentType)
+		done := make(chan int)
+		res := b.Download(done)
+
+		log.Infof("Download unhandled content type: %v", contentType)
+
+		go func() {
+			fn := <-res
+
+			if fn != "" {
+				log.Infof("Download to %v", fn)
+				f, _ := os.Create(fn)
+				f.Write(buf)
+				f.Close()
+			}
+
+			done <- 1
+		}()
 	}
 	return duit.Event{
 		Consumed:   true,
--- a/cmd/browse/main.go
+++ b/cmd/browse/main.go
@@ -37,24 +37,7 @@
 	logger.Quiet = flag.Bool("quiet", defaultQuietActive, "don't print info messages and non-fatal errors")
 }
 
-func Main() (err error) {
-	dui, err = duit.NewDUI("opossum", nil) // TODO: rm global var
-	if err != nil {
-		return fmt.Errorf("new dui: %w", err)
-	}
-
-	style.Init(dui, log)
-
-	w := dui.Display.Windows.Bounds().Dx()
-	log.Printf("w=%v", w)
-	log.Printf("w'=%v", dui.Scale(w))
-	log.Printf("kid=%v", dui.Top.R)
-	browser.SetLogger(log)
-	img.SetLogger(log)
-	opossum.SetLogger(log)
-	nodes.SetLogger(log)
-	b := browser.NewBrowser(dui, *startPage)
-
+func render(b *browser.Browser) {
 	dui.Top.UI = &duit.Box{
 		Kids: duit.NewKids(
 			&duit.Grid{
@@ -81,8 +64,88 @@
 	}
 	browser.PrintTree(b.Website.UI)
 	log.Printf("Render.....")
+	dui.MarkLayout(dui.Top.UI)
+	dui.MarkDraw(dui.Top.UI)
 	dui.Render()
 	log.Printf("Rendering done")
+}
+
+func confirm(b *browser.Browser, text, value string) chan string {
+	res := make(chan string)
+	
+	f := &duit.Field{
+		Text: value,
+	}
+
+	dui.Top.UI = &duit.Box{
+		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,
+			},
+		),
+	}
+	log.Printf("Render.....")
+	dui.MarkLayout(dui.Top.UI)
+	dui.MarkDraw(dui.Top.UI)
+	dui.Render()
+	log.Printf("Rendering done")
+
+	return res
+}
+
+func Main() (err error) {
+	dui, err = duit.NewDUI("opossum", nil) // TODO: rm global var
+	if err != nil {
+		return fmt.Errorf("new dui: %w", err)
+	}
+
+	style.Init(dui, log)
+
+	w := dui.Display.Windows.Bounds().Dx()
+	log.Printf("w=%v", w)
+	log.Printf("w'=%v", dui.Scale(w))
+	log.Printf("kid=%v", dui.Top.R)
+	browser.SetLogger(log)
+	img.SetLogger(log)
+	opossum.SetLogger(log)
+	nodes.SetLogger(log)
+	b := browser.NewBrowser(dui, *startPage)
+	b.Download = func(done chan int) chan string {
+		go func() {
+			<-done
+			render(b)
+		}()
+		return confirm(b, fmt.Sprintf("Download %v", b.URL()), "/download.file")
+	}
+
+	render(b)
 
 	for {
 		select {