From 9dbd7ecab8772ccb79a6148fb54e9c656882260e Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Mon, 12 May 2025 22:58:55 -0400 Subject: [PATCH] We should get someone from Purdue to do this. They are the boilerplaters. --- interactive/model.go | 23 +++++--- interactive/params.go | 84 --------------------------- interactive/params/enlarge.go | 51 ++++++++++++++++ interactive/params/height.go | 41 +++++++++++++ interactive/params/min-height.go | 42 ++++++++++++++ interactive/params/min-width.go | 42 ++++++++++++++ interactive/params/resize.go | 99 ++++++++++++++++++++++++++++++++ interactive/params/width.go | 41 +++++++++++++ interactive/params/zoom.go | 41 +++++++++++++ interactive/update.go | 24 +++++--- interactive/view.go | 8 ++- 11 files changed, 393 insertions(+), 103 deletions(-) delete mode 100644 interactive/params.go create mode 100644 interactive/params/enlarge.go create mode 100644 interactive/params/height.go create mode 100644 interactive/params/min-height.go create mode 100644 interactive/params/min-width.go create mode 100644 interactive/params/resize.go create mode 100644 interactive/params/width.go create mode 100644 interactive/params/zoom.go diff --git a/interactive/model.go b/interactive/model.go index 03086c6..872f283 100644 --- a/interactive/model.go +++ b/interactive/model.go @@ -8,6 +8,7 @@ import ( "github.com/charmbracelet/huh" "github.com/siteworxpro/img-proxy-url-generator/config" "github.com/siteworxpro/img-proxy-url-generator/generator" + "github.com/siteworxpro/img-proxy-url-generator/interactive/params" "github.com/urfave/cli/v2" ) @@ -23,10 +24,10 @@ type Model struct { } type UrlParam interface { - value() string - display() string - key() string - Input() huh.Field + Value() string + Display() string + Key() string + Input() []huh.Field } func InitialModel(c *cli.Context) Model { @@ -68,13 +69,18 @@ func (m Model) initialFields() []huh.Field { fields := make([]huh.Field, 0) options := []UrlParam{ - NewHeight(), - NewWidth(), + params.NewHeight(), + params.NewWidth(), + params.NewResize(), + params.NewMinWidth(), + params.NewMinHeight(), + params.NewZoom(), + params.NewEnlarge(), } var huhOptions []huh.Option[UrlParam] 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, @@ -105,6 +111,9 @@ func (m Model) initialFields() []huh.Field { huh.NewOption[generator.Format]("JPEG", generator.JPG), huh.NewOption[generator.Format]("PNG", generator.PNG), 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), ). Key("format"). diff --git a/interactive/params.go b/interactive/params.go deleted file mode 100644 index e1f701f..0000000 --- a/interactive/params.go +++ /dev/null @@ -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 -} diff --git a/interactive/params/enlarge.go b/interactive/params/enlarge.go new file mode 100644 index 0000000..79ac3ae --- /dev/null +++ b/interactive/params/enlarge.go @@ -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} +} diff --git a/interactive/params/height.go b/interactive/params/height.go new file mode 100644 index 0000000..91c9cdb --- /dev/null +++ b/interactive/params/height.go @@ -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} +} diff --git a/interactive/params/min-height.go b/interactive/params/min-height.go new file mode 100644 index 0000000..79084a0 --- /dev/null +++ b/interactive/params/min-height.go @@ -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} +} diff --git a/interactive/params/min-width.go b/interactive/params/min-width.go new file mode 100644 index 0000000..8cc7baf --- /dev/null +++ b/interactive/params/min-width.go @@ -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} +} diff --git a/interactive/params/resize.go b/interactive/params/resize.go new file mode 100644 index 0000000..eed6570 --- /dev/null +++ b/interactive/params/resize.go @@ -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 +} diff --git a/interactive/params/width.go b/interactive/params/width.go new file mode 100644 index 0000000..fc16a93 --- /dev/null +++ b/interactive/params/width.go @@ -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} +} diff --git a/interactive/params/zoom.go b/interactive/params/zoom.go new file mode 100644 index 0000000..6be9903 --- /dev/null +++ b/interactive/params/zoom.go @@ -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} +} diff --git a/interactive/update.go b/interactive/update.go index 1b1ee08..fa2584a 100644 --- a/interactive/update.go +++ b/interactive/update.go @@ -16,12 +16,19 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg.String() { case "tab": if m.focusField != nil { - // get the index of the current field + index := -1 + selectedParamFields := make([]huh.Field, 0) + for _, field := range *m.selectedParams { + for _, f := range field.Input() { + selectedParamFields = append(selectedParamFields, f) + } + } + if m.inParamsFields { - for i, field := range *m.selectedParams { - if field.Input() == m.focusField { + for i, field := range selectedParamFields { + if field == m.focusField { index = i 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 !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.inParamsFields = true - paramsFields := *m.selectedParams - m.focusField = paramsFields[0].Input() + paramsFields := selectedParamFields + m.focusField = paramsFields[0] m.focusField.Focus() // 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.inParamsFields = false 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 m.focusField.Blur() if m.inParamsFields { - paramsFields := *m.selectedParams - m.focusField = paramsFields[index+1].Input() + m.focusField = selectedParamFields[index+1] } else { m.focusField = m.Fields[index+1] } diff --git a/interactive/view.go b/interactive/view.go index 8dbd2d8..8713b1c 100644 --- a/interactive/view.go +++ b/interactive/view.go @@ -22,7 +22,9 @@ func (m Model) View() string { } for _, field := range *m.selectedParams { - output += field.Input().View() + "\n\n" + for _, f := range field.Input() { + output += f.View() + "\n\n" + } } if *m.url == "" { @@ -31,8 +33,8 @@ func (m Model) View() string { params := make([]string, 0) for _, field := range *m.selectedParams { - if field.value() != "" { - params = append(params, field.key()+":"+field.value()) + if field.Value() != "" { + params = append(params, field.Key()+":"+field.Value()) } }