Files
epuppy/cmd/view.go
T.v.Dein 08f470e0d5 Ui features (#6)
* fix #3: added h ui command to toggle ui
* fix #4: added -N flag to disable colors
* fixed styling of line numbers (-n)
* fix #5 add usage section
* add pkg/epub module readme
* add license terms to all file headers
* fixed usage printing
* added basic unit tests
* run the tests only on linux due to the use of base64 utitlity
2025-10-17 14:10:45 +02:00

111 lines
2.4 KiB
Go

/*
Copyright © 2025 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/tlinden/epuppy/pkg/epub"
)
func View(conf *Config) (int, error) {
switch filepath.Ext(conf.Document) {
case ".epub":
return ViewEpub(conf)
default:
return ViewText(conf)
}
}
func ViewText(conf *Config) (int, error) {
data, err := os.ReadFile(conf.Document)
if err != nil {
return 0, err
}
if conf.Dump {
return fmt.Println(string(data))
}
return Pager(conf, conf.Document, string(data))
}
func ViewEpub(conf *Config) (int, error) {
book, err := epub.Open(conf.Document, conf.XML)
if err != nil {
return 0, err
}
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(" ")
}
fetchByContent(conf, &buf, book)
if conf.Dump {
return fmt.Println(buf.String())
}
return Pager(conf, head.String(), buf.String())
}
// FIXME: since the switch to book.Files() in epub.Open() this
// returns invalid chapter numbering
func fetchByContent(conf *Config, buf *strings.Builder, book *epub.Book) bool {
chapter := 1
var gotbody bool
for _, content := range book.Content {
if len(content.Body) > 0 {
if content.Title != "" {
buf.WriteString(conf.Colors.Chapter.
Render(fmt.Sprintf("──────┤ %s ├──────", content.Title)))
}
buf.WriteString("\r\n\r\n")
if conf.Dump {
// avoid excess whitespaces when printing to stdout
buf.WriteString(content.Body)
} else {
buf.WriteString(conf.Colors.Body.Render(content.Body))
}
buf.WriteString("\r\n\r\n\r\n\r\n")
chapter++
gotbody = true
}
}
return gotbody
}