feat: add format setting for completion

This commit is contained in:
Antoine Aflalo
2024-08-27 08:51:35 -04:00
parent eec6fe91dd
commit 01e5c78369
6 changed files with 65 additions and 13 deletions

View File

@@ -1,8 +1,31 @@
package constant
type ConversionFormat string
import "github.com/thediveo/enumflag/v2"
type ConversionFormat enumflag.Flag
const (
ImageFormatWebP ConversionFormat = "webp"
ImageFormatUnknown ConversionFormat = ""
WebP ConversionFormat = iota
)
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
}

View File

@@ -17,7 +17,7 @@ type Converter interface {
}
var converters = map[constant.ConversionFormat]Converter{
constant.ImageFormatWebP: webp.New(),
constant.WebP: webp.New(),
}
// Available returns a list of available converters.
@@ -28,11 +28,11 @@ func Available() []constant.ConversionFormat {
// Get returns a packer by name.
// If the packer is not available, an error is returned.
func Get(name constant.ConversionFormat) (Converter, error) {
if packer, ok := converters[name]; ok {
return packer, nil
if converter, ok := converters[name]; ok {
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 string(item)
return item.String()
}), ", "))
}

View File

@@ -25,7 +25,7 @@ type Converter struct {
}
func (converter *Converter) Format() (format constant.ConversionFormat) {
return constant.ImageFormatWebP
return constant.WebP
}
func New() *Converter {
@@ -203,8 +203,8 @@ func (converter *Converter) convertPage(container *packer2.PageContainer, qualit
return container, nil
}
// convert converts an image to the ImageFormatWebP 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
// convert converts an image to the WebP format. It decodes the image from the input buffer,
// encodes it as a WebP file using the webp.Encode() function, and returns the resulting WebP
// file as a bytes.Buffer.
func (converter *Converter) convert(image image.Image, quality uint) (*bytes.Buffer, error) {
var buf bytes.Buffer