Render stdin text as Gemtext (#205) (#242)

This commit is contained in:
David Jimenez 2021-06-25 04:59:14 +01:00 committed by GitHub
parent d0998128f9
commit 4480e2d540
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -2,7 +2,9 @@ package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/makeworld-the-better-one/amfora/bookmarks"
"github.com/makeworld-the-better-one/amfora/client"
@ -68,6 +70,8 @@ func main() {
display.NewTab()
if len(os.Args[1:]) > 0 {
display.URL(os.Args[1])
} else if !isStdinEmpty() {
renderFromStdin()
}
// Start
@ -75,3 +79,20 @@ func main() {
panic(err)
}
}
func isStdinEmpty() bool {
stat, _ := os.Stdin.Stat()
return (stat.Mode() & os.ModeCharDevice) != 0
}
func renderFromStdin() {
stdinTextBuilder := new(strings.Builder)
_, err := io.Copy(stdinTextBuilder, os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading from standard input: %v\n", err)
os.Exit(1)
}
stdinText := stdinTextBuilder.String()
display.RenderFromString(stdinText)
}

View File

@ -529,6 +529,25 @@ func URL(u string) {
go goURL(t, fixUserURL(u))
}
func RenderFromString(str string) {
t := tabs[curTab]
page, _ := renderPageFromString(str)
setPage(t, page)
}
func renderPageFromString(str string) (*structs.Page, bool) {
rendered, links := renderer.RenderGemini(str, textWidth(), false)
page := &structs.Page{
Mediatype: structs.TextGemini,
Raw: str,
Content: rendered,
Links: links,
TermWidth: termW,
}
return page, true
}
func NumTabs() int {
return len(tabs)
}