derp, helper method rename

This commit is contained in:
2025-05-12 21:58:17 -04:00
parent 0aa8065640
commit 48d7730a5c
4 changed files with 202 additions and 93 deletions

View File

@@ -3,6 +3,7 @@ package interactive
import ( import (
"fmt" "fmt"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh" "github.com/charmbracelet/huh"
"github.com/siteworxpro/img-proxy-url-generator/config" "github.com/siteworxpro/img-proxy-url-generator/config"
@@ -11,18 +12,21 @@ import (
) )
type Model struct { type Model struct {
Form *huh.Form Fields []huh.Field
generator *generator.Generator generator *generator.Generator
url *string url *string
format *generator.Format format *generator.Format
selectedParams *[]UrlParam selectedParams *[]UrlParam
err error err error
focusField huh.Field
inParamsFields bool
} }
type UrlParam interface { type UrlParam interface {
value() string value() string
display() string
key() string key() string
Input() *huh.Input Input() huh.Field
} }
func InitialModel(c *cli.Context) Model { func InitialModel(c *cli.Context) Model {
@@ -33,49 +37,10 @@ func InitialModel(c *cli.Context) Model {
format: generator.ToPtr(generator.DEF), format: generator.ToPtr(generator.DEF),
} }
options := []UrlParam{ fields := make([]huh.Field, 0)
Height{ fields = append(fields, m.initialFields()...)
paramValue: "40",
},
Width{
paramValue: "40",
},
}
var huhOptions []huh.Option[UrlParam] m.Fields = fields
for _, option := range options {
huhOptions = append(huhOptions, huh.NewOption[UrlParam](option.value(), option))
}
form := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Key("imgUrl").
Description("The URL of the image to generate a proxy for.").
Title("Image URL").
Value(m.url).
Prompt("Enter the image URL:"),
huh.NewMultiSelect[UrlParam]().
Description("Params to add to the URL.").
Options(huhOptions...).
Value(m.selectedParams),
huh.NewSelect[generator.Format]().
Description("Convert the image format.").
Options(
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]("Default", generator.DEF),
).
Key("format").
Title("Format").
Value(m.format),
),
m.selectedOptionFields(),
).WithShowHelp(false)
m.Form = form
cfg, _ := config.NewConfig(c.String("config")) cfg, _ := config.NewConfig(c.String("config"))
@@ -98,20 +63,62 @@ func InitialModel(c *cli.Context) Model {
return m return m
} }
func (m Model) selectedOptionFields() *huh.Group { func (m Model) initialFields() []huh.Field {
fields := make([]huh.Field, 0) fields := make([]huh.Field, 0)
if m.selectedParams == nil || len(*m.selectedParams) == 0 { options := []UrlParam{
return huh.NewGroup( NewHeight(),
huh.NewText().Description("No params selected.").Title("Params").CharLimit(0), NewWidth(),
)
} }
for _, param := range *m.selectedParams { var huhOptions []huh.Option[UrlParam]
fields = append(fields, param.Input()) for _, option := range options {
huhOptions = append(huhOptions, huh.NewOption[UrlParam](option.display(), option))
} }
return huh.NewGroup(fields...) fields = append(fields,
huh.NewInput().
Key("imgUrl").
Description("The URL of the image to generate a proxy for.").
Title("Image URL").
Value(m.url).
Prompt("Enter the image URL:"),
)
fields = append(fields,
huh.NewMultiSelect[UrlParam]().
Description("Params to add to the URL.").
Options(huhOptions...).
Value(m.selectedParams).
WithKeyMap(&huh.KeyMap{
MultiSelect: huh.MultiSelectKeyMap{
Up: key.NewBinding(key.WithKeys("up"), key.WithHelp("up", "up")),
Down: key.NewBinding(key.WithKeys("down"), key.WithHelp("down", "down")),
Toggle: key.NewBinding(key.WithKeys(" "), key.WithHelp(" ", "toggle")),
},
}),
)
fields = append(fields, huh.NewSelect[generator.Format]().
Description("Convert the image format.").
Options(
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]("Default", generator.DEF),
).
Key("format").
Title("Format").
Value(m.format).
WithKeyMap(&huh.KeyMap{
Select: huh.SelectKeyMap{
Up: key.NewBinding(key.WithKeys("up"), key.WithHelp("up", "up")),
Down: key.NewBinding(key.WithKeys("down"), key.WithHelp("down", "down")),
},
}),
)
return fields
} }
func (m Model) Init() tea.Cmd { func (m Model) Init() tea.Cmd {

View File

@@ -1,45 +1,84 @@
package interactive package interactive
import "github.com/charmbracelet/huh" import (
"github.com/aws/aws-sdk-go/aws"
"github.com/charmbracelet/huh"
)
/**
* Height
*/
type Height struct { type Height struct {
UrlParam paramValue *string
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 { func (h Height) value() string {
return "height" return *h.paramValue
} }
func (h Height) key() string { func (h Height) key() string {
return "h" return "h"
} }
func (h Height) Input() *huh.Input { func (h Height) Input() huh.Field {
return huh.NewInput(). return h.field
Key(h.key()).
Description("The height of the image.").
Title("Height").
Value(&h.paramValue)
} }
type Width struct { type Width struct {
UrlParam paramValue *string
paramValue string field *huh.Input
} }
func (h Width) value() string { /**
* 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" return "width"
} }
func (h Width) key() string { func (h Width) value() string {
return "w" return *h.paramValue
} }
func (h Width) Input() *huh.Input { func (h Width) key() string {
return huh.NewInput(). return "h"
Key(h.key()). }
Description("The width of the image.").
Title("Width"). func (h Width) Input() huh.Field {
Value(&h.paramValue) return h.field
} }

View File

@@ -7,31 +7,85 @@ import (
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.focusField == nil {
m.Fields[0].Focus()
m.focusField = m.Fields[0]
}
if msg, ok := msg.(tea.KeyMsg); ok { if msg, ok := msg.(tea.KeyMsg); ok {
switch msg.String() { switch msg.String() {
case "tab": case "tab":
if m.Form.GetFocusedField().GetKey() == "format" { if m.focusField != nil {
return m, m.Form.NextGroup() // get the index of the current field
} index := -1
c := m.Form.NextField() if m.inParamsFields {
return m, c for i, field := range *m.selectedParams {
case "shift+tab": if field.Input() == m.focusField {
if m.Form.GetFocusedField().GetKey() == "imgUrl" { index = i
return m, nil break
} }
}
} else {
for i, field := range m.Fields {
if field == m.focusField {
index = i
break
}
}
}
c := m.Form.PrevField() // if the field is not found, return
return m, c if index == -1 {
return m, nil
}
// 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 {
m.focusField.Blur()
m.inParamsFields = true
paramsFields := *m.selectedParams
m.focusField = paramsFields[0].Input()
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 {
m.focusField.Blur()
m.inParamsFields = false
m.focusField = m.Fields[0]
m.focusField.Focus()
// if not in the params fields and the field is the last one, go to the first one
} else if index == len(m.Fields)-1 && !m.inParamsFields {
m.focusField.Blur()
m.focusField = m.Fields[0]
m.focusField.Focus()
} else {
// otherwise, go to the next field
m.focusField.Blur()
if m.inParamsFields {
paramsFields := *m.selectedParams
m.focusField = paramsFields[index+1].Input()
} else {
m.focusField = m.Fields[index+1]
}
m.focusField.Focus()
}
}
case "ctrl+c", "esc": case "ctrl+c", "esc":
return m, tea.Quit return m, tea.Quit
case "enter": case "enter":
return m, nil return m, nil
default: default:
form, cmd := m.Form.Update(msg) if m.focusField != nil {
m.Form = form.(*huh.Form) md, cmd := m.focusField.(huh.Field).Update(msg)
return m, cmd if md != nil {
m.focusField = md.(huh.Field)
}
return m, cmd
}
} }
} }

View File

@@ -13,21 +13,30 @@ func (m Model) View() string {
Underline(true). Underline(true).
Render("Welcome to the img-proxy URL Generator!") + "\n\n" Render("Welcome to the img-proxy URL Generator!") + "\n\n"
if m.Form == nil {
return ""
}
if m.err != nil { if m.err != nil {
return output + "Error: " + m.err.Error() + "\n" + "Press Ctrl+C to exit.\n" return output + "Error: " + m.err.Error() + "\n" + "Press Ctrl+C to exit.\n"
} }
output = output + m.Form.View() + "\n" for _, field := range m.Fields {
output += field.View() + "\n\n"
}
for _, field := range *m.selectedParams {
output += field.Input().View() + "\n\n"
}
if *m.url == "" { if *m.url == "" {
return output + help() return output + help()
} }
url, _ := m.generator.GenerateUrl(*m.url, []string{}, *m.format) params := make([]string, 0)
for _, field := range *m.selectedParams {
if field.value() != "" {
params = append(params, field.key()+":"+field.value())
}
}
url, _ := m.generator.GenerateUrl(*m.url, params, *m.format)
output += fmt.Sprintf("\nGenerated URL: %s\n\n", lipgloss.NewStyle(). output += fmt.Sprintf("\nGenerated URL: %s\n\n", lipgloss.NewStyle().
Bold(true). Bold(true).