diff --git a/cmd/config.go b/cmd/config.go index 113818d..a692cc3 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -17,7 +17,17 @@ import ( const ( Version string = `v0.0.2` - Usage string = `epuppy [-vd] ` + Usage string = `Usage epuppy [options] + +Options: +-D --dark enable dark mode +-s --store-progress store and use previous (experimental) +-n --line-numbers add line numbers +-c --config use config +-d --debug enable debugging +-h --help show help message +-v --version show program version +` ) type Config struct { @@ -25,6 +35,7 @@ type Config struct { Debug bool `koanf:"debug"` // -d StoreProgress bool `koanf:"store-progress"` // -s Darkmode bool `koanf:"dark"` // -D + LineNumbers bool `koanf:"line-numbers"` // -n Config string `koanf:"config"` // -c ColorDark ColorSetting `koanf:"colordark"` // comes from config file only ColorLight ColorSetting `koanf:"colorlight"` // comes from config file only @@ -52,6 +63,7 @@ func InitConfig(output io.Writer) (*Config, error) { flagset.BoolP("debug", "d", false, "enable debugging") flagset.BoolP("dark", "D", false, "enable dark mode") flagset.BoolP("store-progress", "s", false, "store reading progress") + flagset.BoolP("line-numbers", "n", false, "add line numbers") flagset.StringP("config", "c", "", "read config from file") if err := flagset.Parse(os.Args[1:]); err != nil { diff --git a/cmd/pager.go b/cmd/pager.go index e4018c2..b86db5f 100644 --- a/cmd/pager.go +++ b/cmd/pager.go @@ -189,7 +189,7 @@ func (m Doc) View() string { // update current line for later saving // FIXME: doesn't work correctly yet - m.meta.currentline = int(float64(m.meta.lines) * m.viewport.ScrollPercent()) + m.meta.currentline = int(float64(m.viewport.TotalLineCount()) * m.viewport.ScrollPercent()) var helpView string if m.help.ShowAll { @@ -234,13 +234,28 @@ func Pager(conf *Config, title, message string) (int, error) { scrollto = conf.InitialProgress } + if conf.LineNumbers { + catn := "" + for idx, line := range strings.Split(message, "\n") { + catn += fmt.Sprintf("%d: %s\n", idx, line) + } + message = catn + } + meta := Meta{ initialprogress: scrollto, - lines: len(strings.Split(message, "\r\n")), + lines: len(strings.Split(message, "\n")), } p := tea.NewProgram( - Doc{content: message, title: title, initialwidth: width, meta: &meta, config: conf, keys: keys}, + Doc{ + content: message, + title: title, + initialwidth: width, + meta: &meta, + config: conf, + keys: keys, + }, 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 ) diff --git a/cmd/view.go b/cmd/view.go index f5f59b3..826a75c 100644 --- a/cmd/view.go +++ b/cmd/view.go @@ -2,12 +2,32 @@ 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 + } + + return Pager(conf, conf.Document, string(data)) +} + +func ViewEpub(conf *Config) (int, error) { book, err := epub.Open(conf.Document) if err != nil { return 0, err @@ -35,8 +55,11 @@ func View(conf *Config) (int, error) { for _, content := range book.Content { if len(content.Body) > 0 { - buf.WriteString(conf.Colors.Chapter. - Render(fmt.Sprintf("Chapter %d: %s", chapter, content.Title))) + if content.Title != "" { + buf.WriteString(conf.Colors.Chapter. + Render(fmt.Sprintf("──────┤ %s ├──────", 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") diff --git a/pkg/epub/content.go b/pkg/epub/content.go index e062d31..dc187f4 100644 --- a/pkg/epub/content.go +++ b/pkg/epub/content.go @@ -3,9 +3,19 @@ package epub import ( "encoding/xml" "fmt" + "regexp" "strings" ) +var ( + cleantitle = regexp.MustCompile(`(?s).*`) + cleanmarkup = regexp.MustCompile(`<[^<>]+>`) + cleanentities = regexp.MustCompile(`&.+;`) + cleancomments = regexp.MustCompile(`/*.*/`) + cleanspace = regexp.MustCompile(`^\s*`) + cleanh1 = regexp.MustCompile(``) +) + // Content nav-point content type Content struct { Src string `xml:"src,attr" json:"src"` diff --git a/pkg/epub/ncx.go b/pkg/epub/ncx.go index dcc7bc6..78b4fb1 100644 --- a/pkg/epub/ncx.go +++ b/pkg/epub/ncx.go @@ -1,18 +1,5 @@ package epub -import ( - "regexp" -) - -var ( - cleantitle = regexp.MustCompile(`(?s).*`) - cleanmarkup = regexp.MustCompile(`<[^<>]+>`) - cleanentities = regexp.MustCompile(`&.+;`) - cleancomments = regexp.MustCompile(`/*.*/`) - cleanspace = regexp.MustCompile(`^\s*`) - cleanh1 = regexp.MustCompile(``) -) - // Ncx OPS/toc.ncx type Ncx struct { Points []*NavPoint `xml:"navMap>navPoint" json:"points"`