Files
epuppy/cmd/view.go
Thomas von Dein 7544f8877a lots of fixes and additions:
- add color and color config support
- fix epub parsing
- add variable margin (left and right arrow keys)
- add help screen (hit `?`)
- add config file support
2025-10-15 14:36:43 +02:00

49 lines
1010 B
Go

package cmd
import (
"fmt"
"strings"
"github.com/tlinden/epuppy/pkg/epub"
)
func View(conf *Config) (int, error) {
book, err := epub.Open(conf.Document)
if err != nil {
return 0, err
}
defer book.Close()
buf := strings.Builder{}
head := strings.Builder{}
for _, creator := range book.Opf.Metadata.Creator {
head.WriteString(creator.Data)
head.WriteString(" ")
}
head.WriteString("- ")
for _, title := range book.Opf.Metadata.Title {
head.WriteString(title)
head.WriteString(" ")
}
// FIXME: since the switch to book.Files() in epub.Open() this
// returns invalid chapter numbering
chapter := 1
for _, content := range book.Content {
if len(content.Body) > 0 {
buf.WriteString(conf.Colors.Chapter.
Render(fmt.Sprintf("Chapter %d: %s", chapter, content.Title)))
buf.WriteString("\r\n\r\n")
buf.WriteString(conf.Colors.Body.Render(content.Body))
buf.WriteString("\r\n\r\n\r\n\r\n")
chapter++
}
}
return Pager(conf, head.String(), buf.String())
}