move epubprogress

This commit is contained in:
Celogeek 2024-01-05 17:54:02 +01:00
parent 9601e0a9b4
commit 66b66a6b76
Signed by: celogeek
SSH Key Fingerprint: SHA256:DEDfxIK2nUWXbslbRkww3zsauDjhWHlTXar+ak4lDJ4
2 changed files with 10 additions and 14 deletions

View File

@ -4,6 +4,7 @@ create a progress bar with custom settings.
package epubprogress
import (
"encoding/json"
"fmt"
"os"
"time"
@ -20,18 +21,21 @@ type Options struct {
TotalJob int
}
type EpubProgress interface {
type epubProgress interface {
Add(num int) error
Close() error
}
func New(o Options) EpubProgress {
func New(o Options) epubProgress {
if o.Quiet {
return progressbar.DefaultSilent(int64(o.Max))
}
if o.Json {
return newEpubProgressJson(o)
return &epubProgressJson{
o: o,
e: json.NewEncoder(os.Stdout),
}
}
fmtJob := fmt.Sprintf("%%0%dd", len(fmt.Sprint(o.TotalJob)))

View File

@ -2,23 +2,15 @@ package epubprogress
import (
"encoding/json"
"os"
)
type EpubProgressJson struct {
type epubProgressJson struct {
o Options
e *json.Encoder
current int
}
func newEpubProgressJson(o Options) EpubProgress {
return &EpubProgressJson{
o: o,
e: json.NewEncoder(os.Stdout),
}
}
func (p *EpubProgressJson) Add(num int) error {
func (p *epubProgressJson) Add(num int) error {
p.current += num
p.e.Encode(map[string]any{
"type": "progress",
@ -37,6 +29,6 @@ func (p *EpubProgressJson) Add(num int) error {
return nil
}
func (p *EpubProgressJson) Close() error {
func (p *epubProgressJson) Close() error {
return nil
}