mirror of
https://codeberg.org/scip/epuppy.git
synced 2025-12-16 20:11:00 +01:00
initial commit
This commit is contained in:
72
cmd/config.go
Normal file
72
cmd/config.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/knadh/koanf/providers/posflag"
|
||||
"github.com/knadh/koanf/v2"
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
const (
|
||||
Version string = `v0.0.1`
|
||||
Usage string = `epuppy [-vd] <epub file>`
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Showversion bool `koanf:"version"` // -v
|
||||
Debug bool `koanf:"debug"` // -d
|
||||
StoreProgress bool `koanf:"store-progress"` // -s
|
||||
Document string
|
||||
InitialProgress int // lines
|
||||
}
|
||||
|
||||
func InitConfig(output io.Writer) (*Config, error) {
|
||||
var kloader = koanf.New(".")
|
||||
|
||||
// setup custom usage
|
||||
flagset := flag.NewFlagSet("config", flag.ContinueOnError)
|
||||
flagset.Usage = func() {
|
||||
_, err := fmt.Fprintln(output, Usage)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to print to output: %s", err)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// parse commandline flags
|
||||
flagset.BoolP("version", "v", false, "show program version")
|
||||
flagset.BoolP("debug", "d", false, "enable debugging")
|
||||
flagset.BoolP("store-progress", "s", false, "store reading progress")
|
||||
|
||||
if err := flagset.Parse(os.Args[1:]); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse program arguments: %w", err)
|
||||
}
|
||||
|
||||
// command line setup
|
||||
if err := kloader.Load(posflag.Provider(flagset, ".", kloader), nil); err != nil {
|
||||
return nil, fmt.Errorf("error loading flags: %w", err)
|
||||
}
|
||||
|
||||
// fetch values
|
||||
conf := &Config{}
|
||||
if err := kloader.Unmarshal("", &conf); err != nil {
|
||||
return nil, fmt.Errorf("error unmarshalling: %w", err)
|
||||
}
|
||||
|
||||
// arg is the epub file
|
||||
if len(flagset.Args()) > 0 {
|
||||
conf.Document = flagset.Args()[0]
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
func (c *Config) GetConfigDir() string {
|
||||
home, _ := os.UserHomeDir()
|
||||
return filepath.Join(home, ".config", "epuppy")
|
||||
}
|
||||
166
cmd/pager.go
Normal file
166
cmd/pager.go
Normal file
@@ -0,0 +1,166 @@
|
||||
package cmd
|
||||
|
||||
// pager setup using bubbletea
|
||||
// file shamlelessly copied from:
|
||||
// https://github.com/charmbracelet/bubbletea/tree/main/examples/pager
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/viewport"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/muesli/reflow/wordwrap"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
var (
|
||||
titleStyle = func() lipgloss.Style {
|
||||
b := lipgloss.RoundedBorder()
|
||||
b.Right = "├"
|
||||
return lipgloss.NewStyle().BorderStyle(b).Padding(0, 1)
|
||||
}()
|
||||
|
||||
infoStyle = func() lipgloss.Style {
|
||||
b := lipgloss.RoundedBorder()
|
||||
b.Left = "┤"
|
||||
return titleStyle.BorderStyle(b)
|
||||
}()
|
||||
|
||||
viewstyle = lipgloss.NewStyle()
|
||||
)
|
||||
|
||||
type Meta struct {
|
||||
lines int
|
||||
currentline int
|
||||
initialprogress int
|
||||
document string
|
||||
}
|
||||
|
||||
type Doc struct {
|
||||
content string
|
||||
title string
|
||||
ready bool
|
||||
viewport viewport.Model
|
||||
initialwidth int
|
||||
meta *Meta
|
||||
}
|
||||
|
||||
func (m Doc) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m Doc) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
var (
|
||||
cmd tea.Cmd
|
||||
cmds []tea.Cmd
|
||||
)
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" {
|
||||
return m, tea.Quit
|
||||
}
|
||||
|
||||
case tea.WindowSizeMsg:
|
||||
headerHeight := lipgloss.Height(m.headerView())
|
||||
footerHeight := lipgloss.Height(m.footerView())
|
||||
verticalMarginHeight := headerHeight + footerHeight
|
||||
|
||||
if !m.ready {
|
||||
// Since this program is using the full size of the viewport we
|
||||
// need to wait until we've received the window dimensions before
|
||||
// we can initialize the viewport. The initial dimensions come in
|
||||
// quickly, though asynchronously, which is why we wait for them
|
||||
// here.
|
||||
m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight)
|
||||
m.viewport.YPosition = headerHeight
|
||||
|
||||
m.viewport.SetContent(wordwrap.String(m.content, m.initialwidth))
|
||||
m.viewport.ScrollDown(m.meta.initialprogress)
|
||||
|
||||
m.ready = true
|
||||
} else {
|
||||
m.viewport.Width = msg.Width
|
||||
m.viewport.Height = msg.Height - verticalMarginHeight
|
||||
m.viewport.SetContent(wordwrap.String(m.content, msg.Width))
|
||||
}
|
||||
}
|
||||
|
||||
// Handle keyboard and mouse events in the viewport
|
||||
m.viewport, cmd = m.viewport.Update(msg)
|
||||
cmds = append(cmds, cmd)
|
||||
|
||||
return m, tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
func (m Doc) View() string {
|
||||
if !m.ready {
|
||||
return "\n Initializing..."
|
||||
}
|
||||
|
||||
// update current line for later saving
|
||||
// FIXME: doesn't work correctly yet
|
||||
m.meta.currentline = int(float64(m.meta.lines) * m.viewport.ScrollPercent())
|
||||
|
||||
return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView())
|
||||
}
|
||||
|
||||
func (m Doc) headerView() string {
|
||||
title := titleStyle.Render(m.title)
|
||||
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title)))
|
||||
return lipgloss.JoinHorizontal(lipgloss.Center, title, line)
|
||||
}
|
||||
|
||||
func (m Doc) footerView() string {
|
||||
info := infoStyle.Render(fmt.Sprintf("%3.f%%", m.viewport.ScrollPercent()*100))
|
||||
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(info)))
|
||||
return lipgloss.JoinHorizontal(lipgloss.Center, line, info)
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func Pager(conf *Config, title, message string) (int, error) {
|
||||
width := 80
|
||||
scrollto := 0
|
||||
|
||||
if term.IsTerminal(int(os.Stdout.Fd())) {
|
||||
w, _, err := term.GetSize(0)
|
||||
if err == nil {
|
||||
width = w
|
||||
}
|
||||
}
|
||||
|
||||
if conf.StoreProgress {
|
||||
scrollto = conf.InitialProgress
|
||||
}
|
||||
|
||||
meta := Meta{
|
||||
initialprogress: scrollto,
|
||||
lines: len(strings.Split(message, "\r\n")),
|
||||
}
|
||||
|
||||
p := tea.NewProgram(
|
||||
Doc{content: message, title: title, initialwidth: width, meta: &meta},
|
||||
tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer"
|
||||
tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel
|
||||
)
|
||||
|
||||
if _, err := p.Run(); err != nil {
|
||||
return 0, fmt.Errorf("could not run pager:", err)
|
||||
}
|
||||
|
||||
if conf.Debug {
|
||||
fmt.Printf("scrollto: %d, last: %d, diff: %d\n",
|
||||
scrollto, meta.currentline, scrollto-meta.currentline)
|
||||
}
|
||||
|
||||
return meta.currentline, nil
|
||||
}
|
||||
49
cmd/root.go
Normal file
49
cmd/root.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
||||
func Die(err error) int {
|
||||
log.Fatal("Error: ", err.Error())
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
func Execute(output io.Writer) int {
|
||||
conf, err := InitConfig(output)
|
||||
if err != nil {
|
||||
return Die(err)
|
||||
}
|
||||
|
||||
if conf.Showversion {
|
||||
_, err := fmt.Fprintf(output, "This is epuppy version %s\n", Version)
|
||||
if err != nil {
|
||||
return Die(fmt.Errorf("failed to print to output: %s", err))
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
if conf.StoreProgress {
|
||||
progress, err := GetProgress(conf)
|
||||
if err == nil {
|
||||
conf.InitialProgress = int(progress)
|
||||
}
|
||||
}
|
||||
|
||||
progress, err := View(conf)
|
||||
if err != nil {
|
||||
return Die(err)
|
||||
}
|
||||
|
||||
if conf.StoreProgress {
|
||||
if err := StoreProgress(conf, progress); err != nil {
|
||||
return Die(err)
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
81
cmd/store.go
Normal file
81
cmd/store.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
nonprintable = regexp.MustCompile(`[^a-zA-Z0-9\-\._]+`)
|
||||
slugify = regexp.MustCompile(`[/\\]`)
|
||||
suffix = regexp.MustCompile(`\.[a-z]+$`)
|
||||
)
|
||||
|
||||
func StoreProgress(conf *Config, progress int) error {
|
||||
cfgpath := conf.GetConfigDir()
|
||||
|
||||
if err := Mkdir(cfgpath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
filename := filepath.Join(cfgpath, Slug(conf.Document))
|
||||
|
||||
fd, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
_, err = fd.WriteString(fmt.Sprintf("%d\n", progress))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetProgress(conf *Config) (int64, error) {
|
||||
cfgpath := conf.GetConfigDir()
|
||||
|
||||
filename := filepath.Join(cfgpath, Slug(conf.Document))
|
||||
|
||||
fd, err := os.OpenFile(filename, os.O_RDONLY, 0644)
|
||||
if err != nil {
|
||||
return 0, nil // ignore errors and return no progress
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
scanner := bufio.NewScanner(fd)
|
||||
var line string
|
||||
|
||||
for scanner.Scan() {
|
||||
line = scanner.Text()
|
||||
break
|
||||
}
|
||||
|
||||
return strconv.ParseInt(strings.TrimSpace(line), 10, 64)
|
||||
}
|
||||
|
||||
func Mkdir(dir string) error {
|
||||
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
|
||||
err := os.MkdirAll(dir, os.ModePerm)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create directory %s: %w", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// FIXME: check https://github.com/gosimple/slug
|
||||
func Slug(input string) string {
|
||||
slug := slugify.ReplaceAllString(input, "-")
|
||||
slug = suffix.ReplaceAllString(slug, "")
|
||||
return nonprintable.ReplaceAllString(slug, "")
|
||||
}
|
||||
41
cmd/view.go
Normal file
41
cmd/view.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"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(" ")
|
||||
}
|
||||
|
||||
for _, point := range book.Ncx.Points {
|
||||
if len(point.Content.Body) > 0 {
|
||||
buf.WriteString("### " + point.Content.Title)
|
||||
buf.WriteString("\r\n\r\n")
|
||||
buf.WriteString(point.Content.Body)
|
||||
buf.WriteString("\r\n\r\n\r\n\r\n")
|
||||
}
|
||||
}
|
||||
|
||||
return Pager(conf, head.String(), buf.String())
|
||||
}
|
||||
Reference in New Issue
Block a user