split profiles and options

This commit is contained in:
Celogeek 2023-04-02 21:14:14 +02:00
parent 49056e71d9
commit 3fd1529bb1
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc
4 changed files with 91 additions and 86 deletions

View File

@ -9,6 +9,8 @@ import (
"reflect"
"runtime"
"strings"
"github.com/celogeek/go-comic-converter/internal/converter/options"
)
type converterOrder struct {
@ -20,7 +22,7 @@ type converterOrder struct {
}
type Converter struct {
Options *Options
Options *options.Options
Cmd *flag.FlagSet
order []converterOrder
@ -28,7 +30,7 @@ type Converter struct {
}
func New() *Converter {
options := NewOptions()
options := options.New()
cmd := flag.NewFlagSet("go-comic-converter", flag.ExitOnError)
conv := &Converter{
Options: options,
@ -83,7 +85,7 @@ func (c *Converter) InitParse() {
c.AddBoolParam(&c.Options.Dry, "dry", false, "Dry run to show all options")
c.AddSection("Config")
c.AddStringParam(&c.Options.Profile, "profile", c.Options.Profile, fmt.Sprintf("Profile to use: \n%s", c.Options.profiles))
c.AddStringParam(&c.Options.Profile, "profile", c.Options.Profile, fmt.Sprintf("Profile to use: \n%s", c.Options.AvailableProfiles()))
c.AddIntParam(&c.Options.Quality, "quality", c.Options.Quality, "Quality of the image")
c.AddBoolParam(&c.Options.Crop, "crop", c.Options.Crop, "Crop images")
c.AddIntParam(&c.Options.Brightness, "brightness", c.Options.Brightness, "Brightness readjustement: between -100 and 100, > 0 lighter, < 0 darker")
@ -238,7 +240,7 @@ func (c *Converter) Validate() error {
return errors.New("profile missing")
}
if _, ok := c.Options.profiles[c.Options.Profile]; !ok {
if p := c.Options.GetProfile(); p == nil {
return fmt.Errorf("profile %q doesn't exists", c.Options.Profile)
}

View File

@ -1,10 +1,11 @@
package converter
package options
import (
"fmt"
"os"
"path/filepath"
"github.com/celogeek/go-comic-converter/internal/converter/profiles"
"gopkg.in/yaml.v3"
)
@ -32,10 +33,10 @@ type Options struct {
AddPanelView bool `yaml:"add_panel_view"`
LimitMb int `yaml:"limit_mb"`
profiles Profiles
profiles profiles.Profiles
}
func NewOptions() *Options {
func New() *Options {
return &Options{
Profile: "",
Quality: 85,
@ -49,8 +50,7 @@ func NewOptions() *Options {
HasCover: true,
AddPanelView: false,
LimitMb: 0,
profiles: NewProfile(),
profiles: profiles.New(),
}
}
@ -96,14 +96,6 @@ func (o *Options) LoadDefault() error {
return nil
}
func (o *Options) GetProfile() *Profile {
if profile, ok := o.profiles[o.Profile]; ok {
return &profile
} else {
return nil
}
}
func (o *Options) ShowDefault() string {
var profileDesc string
profile := o.GetProfile()
@ -158,3 +150,11 @@ func (o *Options) SaveDefault() error {
defer f.Close()
return yaml.NewEncoder(f).Encode(o)
}
func (o *Options) GetProfile() *profiles.Profile {
return o.profiles.Get(o.Profile)
}
func (o *Options) AvailableProfiles() string {
return o.profiles.String()
}

View File

@ -1,69 +0,0 @@
package converter
import (
"fmt"
"image/color"
"strings"
"github.com/celogeek/go-comic-converter/internal/epub"
)
type Profile struct {
Code string
Description string
Width int
Height int
Palette color.Palette
}
type Profiles map[string]Profile
var profiles = []Profile{
{"KS", "Kindle 1", 600, 670, epub.PALETTE_4},
{"K11", "Kindle 11", 1072, 1448, epub.PALETTE_16},
{"K2", "Kindle 2", 600, 670, epub.PALETTE_15},
{"K34", "Kindle Keyboard/Touch", 600, 800, epub.PALETTE_16},
{"K578", "Kindle", 600, 800, epub.PALETTE_16},
{"KDX", "Kindle DX/DXG", 824, 1000, epub.PALETTE_16},
{"KPW", "Kindle Paperwhite 1/2", 758, 1024, epub.PALETTE_16},
{"KV", "Kindle Paperwhite 3/4/Voyage/Oasis", 1072, 1448, epub.PALETTE_16},
{"KPW5", "Kindle Paperwhite 5/Signature Edition", 1236, 1648, epub.PALETTE_16},
{"KO", "Kindle Oasis 2/3", 1264, 1680, epub.PALETTE_16},
{"KS", "Kindle Scribe", 1860, 2480, epub.PALETTE_16},
// Kobo
{"KoMT", "Kobo Mini/Touch", 600, 800, epub.PALETTE_16},
{"KoG", "Kobo Glo", 768, 1024, epub.PALETTE_16},
{"KoGHD", "Kobo Glo HD", 1072, 1448, epub.PALETTE_16},
{"KoA", "Kobo Aura", 758, 1024, epub.PALETTE_16},
{"KoAHD", "Kobo Aura HD", 1080, 1440, epub.PALETTE_16},
{"KoAH2O", "Kobo Aura H2O", 1080, 1430, epub.PALETTE_16},
{"KoAO", "Kobo Aura ONE", 1404, 1872, epub.PALETTE_16},
{"KoN", "Kobo Nia", 758, 1024, epub.PALETTE_16},
{"KoC", "Kobo Clara HD/Kobo Clara 2E", 1072, 1448, epub.PALETTE_16},
{"KoL", "Kobo Libra H2O/Kobo Libra 2", 1264, 1680, epub.PALETTE_16},
{"KoF", "Kobo Forma", 1440, 1920, epub.PALETTE_16},
{"KoS", "Kobo Sage", 1440, 1920, epub.PALETTE_16},
{"KoE", "Kobo Elipsa", 1404, 1872, epub.PALETTE_16},
}
func NewProfile() Profiles {
r := Profiles{}
for _, profile := range profiles {
r[profile.Code] = profile
}
return r
}
func (p Profiles) String() string {
s := make([]string, 0)
for _, v := range profiles {
s = append(s, fmt.Sprintf(
" - %-7s ( %9s ) - %2d levels of gray - %s",
v.Code,
fmt.Sprintf("%dx%d", v.Width, v.Height),
len(v.Palette),
v.Description,
))
}
return strings.Join(s, "\n")
}

View File

@ -0,0 +1,72 @@
package profiles
import (
"fmt"
"image/color"
"strings"
"github.com/celogeek/go-comic-converter/internal/epub"
)
type Profile struct {
Code string
Description string
Width int
Height int
Palette color.Palette
}
type Profiles []Profile
func New() Profiles {
return []Profile{
{"KS", "Kindle 1", 600, 670, epub.PALETTE_4},
{"K11", "Kindle 11", 1072, 1448, epub.PALETTE_16},
{"K2", "Kindle 2", 600, 670, epub.PALETTE_15},
{"K34", "Kindle Keyboard/Touch", 600, 800, epub.PALETTE_16},
{"K578", "Kindle", 600, 800, epub.PALETTE_16},
{"KDX", "Kindle DX/DXG", 824, 1000, epub.PALETTE_16},
{"KPW", "Kindle Paperwhite 1/2", 758, 1024, epub.PALETTE_16},
{"KV", "Kindle Paperwhite 3/4/Voyage/Oasis", 1072, 1448, epub.PALETTE_16},
{"KPW5", "Kindle Paperwhite 5/Signature Edition", 1236, 1648, epub.PALETTE_16},
{"KO", "Kindle Oasis 2/3", 1264, 1680, epub.PALETTE_16},
{"KS", "Kindle Scribe", 1860, 2480, epub.PALETTE_16},
// Kobo
{"KoMT", "Kobo Mini/Touch", 600, 800, epub.PALETTE_16},
{"KoG", "Kobo Glo", 768, 1024, epub.PALETTE_16},
{"KoGHD", "Kobo Glo HD", 1072, 1448, epub.PALETTE_16},
{"KoA", "Kobo Aura", 758, 1024, epub.PALETTE_16},
{"KoAHD", "Kobo Aura HD", 1080, 1440, epub.PALETTE_16},
{"KoAH2O", "Kobo Aura H2O", 1080, 1430, epub.PALETTE_16},
{"KoAO", "Kobo Aura ONE", 1404, 1872, epub.PALETTE_16},
{"KoN", "Kobo Nia", 758, 1024, epub.PALETTE_16},
{"KoC", "Kobo Clara HD/Kobo Clara 2E", 1072, 1448, epub.PALETTE_16},
{"KoL", "Kobo Libra H2O/Kobo Libra 2", 1264, 1680, epub.PALETTE_16},
{"KoF", "Kobo Forma", 1440, 1920, epub.PALETTE_16},
{"KoS", "Kobo Sage", 1440, 1920, epub.PALETTE_16},
{"KoE", "Kobo Elipsa", 1404, 1872, epub.PALETTE_16},
}
}
func (p Profiles) String() string {
s := make([]string, 0)
for _, v := range p {
s = append(s, fmt.Sprintf(
" - %-7s ( %9s ) - %2d levels of gray - %s",
v.Code,
fmt.Sprintf("%dx%d", v.Width, v.Height),
len(v.Palette),
v.Description,
))
}
return strings.Join(s, "\n")
}
func (p Profiles) Get(name string) *Profile {
for _, profile := range p {
if profile.Code == name {
return &profile
}
}
return nil
}