We should get someone from Purdue to do this. They are the boilerplaters.

This commit is contained in:
2025-05-12 22:58:55 -04:00
parent 48d7730a5c
commit 9dbd7ecab8
11 changed files with 393 additions and 103 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/charmbracelet/huh" "github.com/charmbracelet/huh"
"github.com/siteworxpro/img-proxy-url-generator/config" "github.com/siteworxpro/img-proxy-url-generator/config"
"github.com/siteworxpro/img-proxy-url-generator/generator" "github.com/siteworxpro/img-proxy-url-generator/generator"
"github.com/siteworxpro/img-proxy-url-generator/interactive/params"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@@ -23,10 +24,10 @@ type Model struct {
} }
type UrlParam interface { type UrlParam interface {
value() string Value() string
display() string Display() string
key() string Key() string
Input() huh.Field Input() []huh.Field
} }
func InitialModel(c *cli.Context) Model { func InitialModel(c *cli.Context) Model {
@@ -68,13 +69,18 @@ func (m Model) initialFields() []huh.Field {
fields := make([]huh.Field, 0) fields := make([]huh.Field, 0)
options := []UrlParam{ options := []UrlParam{
NewHeight(), params.NewHeight(),
NewWidth(), params.NewWidth(),
params.NewResize(),
params.NewMinWidth(),
params.NewMinHeight(),
params.NewZoom(),
params.NewEnlarge(),
} }
var huhOptions []huh.Option[UrlParam] var huhOptions []huh.Option[UrlParam]
for _, option := range options { for _, option := range options {
huhOptions = append(huhOptions, huh.NewOption[UrlParam](option.display(), option)) huhOptions = append(huhOptions, huh.NewOption[UrlParam](option.Display(), option))
} }
fields = append(fields, fields = append(fields,
@@ -105,6 +111,9 @@ func (m Model) initialFields() []huh.Field {
huh.NewOption[generator.Format]("JPEG", generator.JPG), huh.NewOption[generator.Format]("JPEG", generator.JPG),
huh.NewOption[generator.Format]("PNG", generator.PNG), huh.NewOption[generator.Format]("PNG", generator.PNG),
huh.NewOption[generator.Format]("BMP", generator.BMP), huh.NewOption[generator.Format]("BMP", generator.BMP),
huh.NewOption[generator.Format]("WEBP", generator.WEBP),
huh.NewOption[generator.Format]("GIF", generator.GIF),
huh.NewOption[generator.Format]("ICO", generator.ICO),
huh.NewOption[generator.Format]("Default", generator.DEF), huh.NewOption[generator.Format]("Default", generator.DEF),
). ).
Key("format"). Key("format").

View File

@@ -1,84 +0,0 @@
package interactive
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/charmbracelet/huh"
)
/**
* Height
*/
type Height struct {
paramValue *string
field *huh.Input
}
func NewHeight() *Height {
h := &Height{
paramValue: aws.String(""),
field: huh.NewInput().
Key("h").
Description("The height of the image.").
Title("Height"),
}
h.field.Value(h.paramValue)
return h
}
func (h Height) display() string {
return "height"
}
func (h Height) value() string {
return *h.paramValue
}
func (h Height) key() string {
return "h"
}
func (h Height) Input() huh.Field {
return h.field
}
type Width struct {
paramValue *string
field *huh.Input
}
/**
* Width
*/
func NewWidth() *Width {
w := &Width{
paramValue: aws.String(""),
field: huh.NewInput().
Key("h").
Description("The width of the image.").
Title("With"),
}
w.field.Value(w.paramValue)
return w
}
func (h Width) display() string {
return "width"
}
func (h Width) value() string {
return *h.paramValue
}
func (h Width) key() string {
return "h"
}
func (h Width) Input() huh.Field {
return h.field
}

View File

@@ -0,0 +1,51 @@
package params
import (
"github.com/charmbracelet/huh"
)
type Enlarge struct {
enlarge *[]string
field huh.Field
}
func NewEnlarge() *Enlarge {
e := &Enlarge{
enlarge: &[]string{},
field: huh.NewMultiSelect[string]().
Options(
huh.NewOption("true", "true"),
).
Description("Whether to enlarge the image.").
WithKeyMap(
huh.NewDefaultKeyMap(),
),
}
e.enlarge = &[]string{}
e.field.(*huh.MultiSelect[string]).Value(e.enlarge)
return e
}
func (e Enlarge) Display() string {
return "enlarge"
}
func (e Enlarge) Value() string {
value := "0"
if len(*e.enlarge) > 0 {
value = "1"
}
return value
}
func (e Enlarge) Key() string {
return "el"
}
func (e Enlarge) Input() []huh.Field {
return []huh.Field{e.field}
}

View File

@@ -0,0 +1,41 @@
package params
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/charmbracelet/huh"
)
type Height struct {
paramValue *string
field *huh.Input
}
func NewHeight() *Height {
h := &Height{
paramValue: aws.String(""),
field: huh.NewInput().
Key("h").
Description("The height of the image.").
Title("Height"),
}
h.field.Value(h.paramValue)
return h
}
func (h Height) Display() string {
return "height"
}
func (h Height) Value() string {
return *h.paramValue
}
func (h Height) Key() string {
return "h"
}
func (h Height) Input() []huh.Field {
return []huh.Field{h.field}
}

View File

@@ -0,0 +1,42 @@
package params
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/charmbracelet/huh"
)
type MinHeight struct {
paramValue *string
field *huh.Input
}
func NewMinHeight() *MinHeight {
mh := &MinHeight{
paramValue: nil,
field: huh.NewInput().
Key("min-height").
Description("The minimum height of the image.").
Title("Min Height"),
}
mh.paramValue = aws.String("")
mh.field.Value(mh.paramValue)
return mh
}
func (mh MinHeight) Display() string {
return "min-height"
}
func (mh MinHeight) Value() string {
return *mh.paramValue
}
func (mh MinHeight) Key() string {
return "mh"
}
func (mh MinHeight) Input() []huh.Field {
return []huh.Field{mh.field}
}

View File

@@ -0,0 +1,42 @@
package params
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/charmbracelet/huh"
)
type MinWidth struct {
paramValue *string
field *huh.Input
}
func NewMinWidth() *MinWidth {
mw := &MinWidth{
paramValue: nil,
field: huh.NewInput().
Key("min-width").
Description("The minimum width of the image.").
Title("Min Width"),
}
mw.paramValue = aws.String("")
mw.field.Value(mw.paramValue)
return mw
}
func (mw MinWidth) Display() string {
return "min-width"
}
func (mw MinWidth) Value() string {
return *mw.paramValue
}
func (mw MinWidth) Key() string {
return "mw"
}
func (mw MinWidth) Input() []huh.Field {
return []huh.Field{mw.field}
}

View File

@@ -0,0 +1,99 @@
package params
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/charmbracelet/huh"
)
type Resize struct {
fields []huh.Field
height *string
width *string
resizeType *string
enlarge *[]string
extent *[]string
}
func NewResize() *Resize {
rs := &Resize{
fields: []huh.Field{
huh.NewInput().
Key("resize.height").
Description("The height of the image.").
Title("Height"),
huh.NewInput().
Key("resize.width").
Description("The width of the image.").
Title("Width"),
huh.NewSelect[string]().
Title("Resize Type").
Options(
huh.NewOption("fit", "fit"),
huh.NewOption("fill", "fill"),
huh.NewOption("fill-down", "fill-down"),
huh.NewOption("force", "force"),
huh.NewOption("auto", "auto"),
).WithKeyMap(huh.NewDefaultKeyMap()),
huh.NewMultiSelect[string]().
Options(
huh.NewOption("true", "true"),
).
Description("Whether to enlarge the image.").
WithKeyMap(
huh.NewDefaultKeyMap(),
),
huh.NewMultiSelect[string]().
Options(
huh.NewOption("true", "true"),
).
Description("Whether to extend the image.").
WithKeyMap(
huh.NewDefaultKeyMap(),
),
},
}
rs.height = aws.String("")
rs.width = aws.String("")
rs.enlarge = &[]string{}
rs.extent = &[]string{}
rs.resizeType = aws.String("auto")
rs.fields[0].(*huh.Input).Value(rs.height)
rs.fields[1].(*huh.Input).Value(rs.width)
rs.fields[2].(*huh.Select[string]).Value(rs.resizeType)
rs.fields[3].(*huh.MultiSelect[string]).Value(rs.enlarge)
rs.fields[4].(*huh.MultiSelect[string]).Value(rs.extent)
return rs
}
func (r Resize) Display() string {
return "resize"
}
func (r Resize) Value() string {
if *r.height == "" || *r.width == "" {
return ""
}
resize := "0"
if *r.enlarge != nil && len(*r.enlarge) > 0 {
resize = "1"
}
extent := "0"
if *r.extent != nil && len(*r.extent) > 0 {
extent = "1"
}
return *r.resizeType + ":" + *r.height + ":" + *r.width + ":" + resize + ":" + extent
}
func (r Resize) Key() string {
return "rs"
}
func (r Resize) Input() []huh.Field {
return r.fields
}

View File

@@ -0,0 +1,41 @@
package params
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/charmbracelet/huh"
)
type Width struct {
paramValue *string
field *huh.Input
}
func NewWidth() *Width {
w := &Width{
paramValue: aws.String(""),
field: huh.NewInput().
Key("h").
Description("The width of the image.").
Title("Width"),
}
w.field.Value(w.paramValue)
return w
}
func (h Width) Display() string {
return "width"
}
func (h Width) Value() string {
return *h.paramValue
}
func (h Width) Key() string {
return "h"
}
func (h Width) Input() []huh.Field {
return []huh.Field{h.field}
}

View File

@@ -0,0 +1,41 @@
package params
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/charmbracelet/huh"
)
type Zoom struct {
zoom *string
field huh.Field
}
func NewZoom() *Zoom {
z := &Zoom{
zoom: aws.String(""),
field: huh.NewInput().
Key("z").
Description("Percentage to zoom the image (1.4 == 140%) .").
Title("Zoom"),
}
z.field.(*huh.Input).Value(z.zoom)
return z
}
func (z *Zoom) Value() string {
return *z.zoom
}
func (z *Zoom) Display() string {
return "zoom"
}
func (z *Zoom) Key() string {
return "z"
}
func (z *Zoom) Input() []huh.Field {
return []huh.Field{z.field}
}

View File

@@ -16,12 +16,19 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.String() { switch msg.String() {
case "tab": case "tab":
if m.focusField != nil { if m.focusField != nil {
// get the index of the current field
index := -1 index := -1
selectedParamFields := make([]huh.Field, 0)
for _, field := range *m.selectedParams {
for _, f := range field.Input() {
selectedParamFields = append(selectedParamFields, f)
}
}
if m.inParamsFields { if m.inParamsFields {
for i, field := range *m.selectedParams { for i, field := range selectedParamFields {
if field.Input() == m.focusField { if field == m.focusField {
index = i index = i
break break
} }
@@ -41,15 +48,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
// if the field is the last one, and we have params selected go to the param fields // if the field is the last one, and we have params selected go to the param fields
if !m.inParamsFields && index == len(m.Fields)-1 && len(*m.selectedParams) > 0 { if !m.inParamsFields && index == len(m.Fields)-1 && len(selectedParamFields) > 0 {
m.focusField.Blur() m.focusField.Blur()
m.inParamsFields = true m.inParamsFields = true
paramsFields := *m.selectedParams paramsFields := selectedParamFields
m.focusField = paramsFields[0].Input() m.focusField = paramsFields[0]
m.focusField.Focus() m.focusField.Focus()
// if the field is the last one, and we have params selected go to the first non params field // if the field is the last one, and we have params selected go to the first non params field
} else if m.inParamsFields && index == len(*m.selectedParams)-1 { } else if m.inParamsFields && index == len(selectedParamFields)-1 {
m.focusField.Blur() m.focusField.Blur()
m.inParamsFields = false m.inParamsFields = false
m.focusField = m.Fields[0] m.focusField = m.Fields[0]
@@ -64,8 +71,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// otherwise, go to the next field // otherwise, go to the next field
m.focusField.Blur() m.focusField.Blur()
if m.inParamsFields { if m.inParamsFields {
paramsFields := *m.selectedParams m.focusField = selectedParamFields[index+1]
m.focusField = paramsFields[index+1].Input()
} else { } else {
m.focusField = m.Fields[index+1] m.focusField = m.Fields[index+1]
} }

View File

@@ -22,7 +22,9 @@ func (m Model) View() string {
} }
for _, field := range *m.selectedParams { for _, field := range *m.selectedParams {
output += field.Input().View() + "\n\n" for _, f := range field.Input() {
output += f.View() + "\n\n"
}
} }
if *m.url == "" { if *m.url == "" {
@@ -31,8 +33,8 @@ func (m Model) View() string {
params := make([]string, 0) params := make([]string, 0)
for _, field := range *m.selectedParams { for _, field := range *m.selectedParams {
if field.value() != "" { if field.Value() != "" {
params = append(params, field.key()+":"+field.value()) params = append(params, field.Key()+":"+field.Value())
} }
} }