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 package epubprogress
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"time" "time"
@ -20,18 +21,21 @@ type Options struct {
TotalJob int TotalJob int
} }
type EpubProgress interface { type epubProgress interface {
Add(num int) error Add(num int) error
Close() error Close() error
} }
func New(o Options) EpubProgress { func New(o Options) epubProgress {
if o.Quiet { if o.Quiet {
return progressbar.DefaultSilent(int64(o.Max)) return progressbar.DefaultSilent(int64(o.Max))
} }
if o.Json { 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))) fmtJob := fmt.Sprintf("%%0%dd", len(fmt.Sprint(o.TotalJob)))

View File

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