mirror of
https://github.com/Belphemur/CBZOptimizer.git
synced 2025-10-14 12:38:50 +02:00
feat: add format setting for completion
This commit is contained in:
@@ -6,31 +6,57 @@ import (
|
|||||||
"CBZOptimizer/converter/constant"
|
"CBZOptimizer/converter/constant"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/thediveo/enumflag/v2"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var converterType constant.ConversionFormat
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
command := &cobra.Command{
|
command := &cobra.Command{
|
||||||
Use: "convert",
|
Use: "optimize [folder]",
|
||||||
Short: "Convert CBZ files using a specified converter",
|
Short: "Optimize all CBZ files in a folder recursively",
|
||||||
|
Long: "Optimize all CBZ files in a folder recursively.\nIt will take all the different pages in the CBZ files and convert them to the given format.\nThe original CBZ files will be kept intact depending if you choose to override or not.",
|
||||||
RunE: ConvertCbzCommand,
|
RunE: ConvertCbzCommand,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
}
|
}
|
||||||
|
formatFlag := enumflag.New(&converterType, "format", constant.CommandValue, enumflag.EnumCaseInsensitive)
|
||||||
|
_ = formatFlag.RegisterCompletion(command, "format", constant.HelpText)
|
||||||
|
|
||||||
command.Flags().Uint8P("quality", "q", 85, "Quality for conversion (0-100)")
|
command.Flags().Uint8P("quality", "q", 85, "Quality for conversion (0-100)")
|
||||||
command.Flags().IntP("parallelism", "n", 2, "Number of chapters to convert in parallel")
|
command.Flags().IntP("parallelism", "n", 2, "Number of chapters to convert in parallel")
|
||||||
command.Flags().BoolP("override", "o", false, "Override the original CBZ files")
|
command.Flags().BoolP("override", "o", false, "Override the original CBZ files")
|
||||||
|
command.PersistentFlags().VarP(
|
||||||
|
formatFlag,
|
||||||
|
"format", "f",
|
||||||
|
fmt.Sprintf("Format to convert the images to: %s", constant.ListAll()))
|
||||||
|
command.PersistentFlags().Lookup("format").NoOptDefVal = constant.DefaultConversion.String()
|
||||||
|
|
||||||
AddCommand(command)
|
AddCommand(command)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isValidFolder checks if the provided path is a valid directory
|
||||||
|
func isValidFolder(path string) bool {
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return info.IsDir()
|
||||||
|
}
|
||||||
|
|
||||||
func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
|
func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
|
||||||
path := args[0]
|
path := args[0]
|
||||||
if path == "" {
|
if path == "" {
|
||||||
return fmt.Errorf("path is required")
|
return fmt.Errorf("path is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !isValidFolder(path) {
|
||||||
|
return fmt.Errorf("the path needs to be a folder")
|
||||||
|
}
|
||||||
|
|
||||||
quality, err := cmd.Flags().GetUint8("quality")
|
quality, err := cmd.Flags().GetUint8("quality")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("invalid quality value")
|
return fmt.Errorf("invalid quality value")
|
||||||
@@ -46,7 +72,7 @@ func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
|
|||||||
return fmt.Errorf("invalid parallelism value")
|
return fmt.Errorf("invalid parallelism value")
|
||||||
}
|
}
|
||||||
|
|
||||||
chapterConverter, err := converter.Get(constant.ImageFormatWebP)
|
chapterConverter, err := converter.Get(converterType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get chapterConverter: %v", err)
|
return fmt.Errorf("failed to get chapterConverter: %v", err)
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,31 @@
|
|||||||
package constant
|
package constant
|
||||||
|
|
||||||
type ConversionFormat string
|
import "github.com/thediveo/enumflag/v2"
|
||||||
|
|
||||||
|
type ConversionFormat enumflag.Flag
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ImageFormatWebP ConversionFormat = "webp"
|
WebP ConversionFormat = iota
|
||||||
ImageFormatUnknown ConversionFormat = ""
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var CommandValue = map[ConversionFormat][]string{
|
||||||
|
WebP: {"webp"},
|
||||||
|
}
|
||||||
|
|
||||||
|
var HelpText = enumflag.Help[ConversionFormat]{
|
||||||
|
WebP: "WebP Image Format",
|
||||||
|
}
|
||||||
|
|
||||||
|
var DefaultConversion = WebP
|
||||||
|
|
||||||
|
func (c ConversionFormat) String() string {
|
||||||
|
return CommandValue[c][0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListAll() []string {
|
||||||
|
var formats []string
|
||||||
|
for _, names := range CommandValue {
|
||||||
|
formats = append(formats, names[0])
|
||||||
|
}
|
||||||
|
return formats
|
||||||
|
}
|
||||||
|
@@ -17,7 +17,7 @@ type Converter interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var converters = map[constant.ConversionFormat]Converter{
|
var converters = map[constant.ConversionFormat]Converter{
|
||||||
constant.ImageFormatWebP: webp.New(),
|
constant.WebP: webp.New(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Available returns a list of available converters.
|
// Available returns a list of available converters.
|
||||||
@@ -28,11 +28,11 @@ func Available() []constant.ConversionFormat {
|
|||||||
// Get returns a packer by name.
|
// Get returns a packer by name.
|
||||||
// If the packer is not available, an error is returned.
|
// If the packer is not available, an error is returned.
|
||||||
func Get(name constant.ConversionFormat) (Converter, error) {
|
func Get(name constant.ConversionFormat) (Converter, error) {
|
||||||
if packer, ok := converters[name]; ok {
|
if converter, ok := converters[name]; ok {
|
||||||
return packer, nil
|
return converter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("unkown converter \"%s\", available options are %s", name, strings.Join(lo.Map(Available(), func(item constant.ConversionFormat, index int) string {
|
return nil, fmt.Errorf("unkown converter \"%s\", available options are %s", name, strings.Join(lo.Map(Available(), func(item constant.ConversionFormat, index int) string {
|
||||||
return string(item)
|
return item.String()
|
||||||
}), ", "))
|
}), ", "))
|
||||||
}
|
}
|
||||||
|
@@ -25,7 +25,7 @@ type Converter struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (converter *Converter) Format() (format constant.ConversionFormat) {
|
func (converter *Converter) Format() (format constant.ConversionFormat) {
|
||||||
return constant.ImageFormatWebP
|
return constant.WebP
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *Converter {
|
func New() *Converter {
|
||||||
@@ -203,8 +203,8 @@ func (converter *Converter) convertPage(container *packer2.PageContainer, qualit
|
|||||||
return container, nil
|
return container, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert converts an image to the ImageFormatWebP format. It decodes the image from the input buffer,
|
// convert converts an image to the WebP format. It decodes the image from the input buffer,
|
||||||
// encodes it as a ImageFormatWebP file using the webp.Encode() function, and returns the resulting ImageFormatWebP
|
// encodes it as a WebP file using the webp.Encode() function, and returns the resulting WebP
|
||||||
// file as a bytes.Buffer.
|
// file as a bytes.Buffer.
|
||||||
func (converter *Converter) convert(image image.Image, quality uint) (*bytes.Buffer, error) {
|
func (converter *Converter) convert(image image.Image, quality uint) (*bytes.Buffer, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
1
go.mod
1
go.mod
@@ -25,6 +25,7 @@ require (
|
|||||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
github.com/stretchr/testify v1.9.0 // indirect
|
github.com/stretchr/testify v1.9.0 // indirect
|
||||||
|
github.com/thediveo/enumflag/v2 v2.0.5 // indirect
|
||||||
github.com/ulikunitz/xz v0.5.12 // indirect
|
github.com/ulikunitz/xz v0.5.12 // indirect
|
||||||
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
|
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
|
||||||
golang.org/x/text v0.17.0 // indirect
|
golang.org/x/text v0.17.0 // indirect
|
||||||
|
2
go.sum
2
go.sum
@@ -56,6 +56,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
|||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
github.com/thediveo/enumflag/v2 v2.0.5 h1:VJjvlAqUb6m6mxOrB/0tfBJI0Kvi9wJ8ulh38xK87i8=
|
||||||
|
github.com/thediveo/enumflag/v2 v2.0.5/go.mod h1:0NcG67nYgwwFsAvoQCmezG0J0KaIxZ0f7skg9eLq1DA=
|
||||||
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
|
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
|
||||||
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
||||||
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
|
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
|
||||||
|
Reference in New Issue
Block a user