You've already forked img-proxy-url-generator
Fixed some shit
This commit is contained in:
119
interactive/model.go
Normal file
119
interactive/model.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package interactive
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/huh"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/config"
|
||||
"github.com/siteworxpro/img-proxy-url-generator/generator"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
Form *huh.Form
|
||||
generator *generator.Generator
|
||||
url *string
|
||||
format *generator.Format
|
||||
selectedParams *[]UrlParam
|
||||
err error
|
||||
}
|
||||
|
||||
type UrlParam interface {
|
||||
value() string
|
||||
key() string
|
||||
Input() *huh.Input
|
||||
}
|
||||
|
||||
func InitialModel(c *cli.Context) Model {
|
||||
|
||||
m := Model{
|
||||
url: aws.String(""),
|
||||
selectedParams: &[]UrlParam{},
|
||||
format: generator.ToPtr(generator.DEF),
|
||||
}
|
||||
|
||||
options := []UrlParam{
|
||||
Height{
|
||||
paramValue: "40",
|
||||
},
|
||||
Width{
|
||||
paramValue: "40",
|
||||
},
|
||||
}
|
||||
|
||||
var huhOptions []huh.Option[UrlParam]
|
||||
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"))
|
||||
|
||||
if cfg == nil {
|
||||
err := fmt.Errorf("config not loaded")
|
||||
m.err = err
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
g, err := generator.NewGenerator(cfg)
|
||||
|
||||
if err != nil {
|
||||
m.err = err
|
||||
return m
|
||||
}
|
||||
|
||||
m.generator = g
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m Model) selectedOptionFields() *huh.Group {
|
||||
fields := make([]huh.Field, 0)
|
||||
|
||||
if m.selectedParams == nil || len(*m.selectedParams) == 0 {
|
||||
return huh.NewGroup(
|
||||
huh.NewText().Description("No params selected.").Title("Params").CharLimit(0),
|
||||
)
|
||||
}
|
||||
|
||||
for _, param := range *m.selectedParams {
|
||||
fields = append(fields, param.Input())
|
||||
}
|
||||
|
||||
return huh.NewGroup(fields...)
|
||||
}
|
||||
|
||||
func (m Model) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
45
interactive/params.go
Normal file
45
interactive/params.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package interactive
|
||||
|
||||
import "github.com/charmbracelet/huh"
|
||||
|
||||
type Height struct {
|
||||
UrlParam
|
||||
paramValue string
|
||||
}
|
||||
|
||||
func (h Height) value() string {
|
||||
return "height"
|
||||
}
|
||||
|
||||
func (h Height) key() string {
|
||||
return "h"
|
||||
}
|
||||
|
||||
func (h Height) Input() *huh.Input {
|
||||
return huh.NewInput().
|
||||
Key(h.key()).
|
||||
Description("The height of the image.").
|
||||
Title("Height").
|
||||
Value(&h.paramValue)
|
||||
}
|
||||
|
||||
type Width struct {
|
||||
UrlParam
|
||||
paramValue string
|
||||
}
|
||||
|
||||
func (h Width) value() string {
|
||||
return "width"
|
||||
}
|
||||
|
||||
func (h Width) key() string {
|
||||
return "w"
|
||||
}
|
||||
|
||||
func (h Width) Input() *huh.Input {
|
||||
return huh.NewInput().
|
||||
Key(h.key()).
|
||||
Description("The width of the image.").
|
||||
Title("Width").
|
||||
Value(&h.paramValue)
|
||||
}
|
||||
39
interactive/update.go
Normal file
39
interactive/update.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package interactive
|
||||
|
||||
import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/huh"
|
||||
)
|
||||
|
||||
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
||||
if msg, ok := msg.(tea.KeyMsg); ok {
|
||||
switch msg.String() {
|
||||
case "tab":
|
||||
if m.Form.GetFocusedField().GetKey() == "format" {
|
||||
return m, m.Form.NextGroup()
|
||||
}
|
||||
|
||||
c := m.Form.NextField()
|
||||
return m, c
|
||||
case "shift+tab":
|
||||
if m.Form.GetFocusedField().GetKey() == "imgUrl" {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
c := m.Form.PrevField()
|
||||
return m, c
|
||||
case "ctrl+c", "esc":
|
||||
return m, tea.Quit
|
||||
case "enter":
|
||||
return m, nil
|
||||
default:
|
||||
form, cmd := m.Form.Update(msg)
|
||||
m.Form = form.(*huh.Form)
|
||||
|
||||
return m, cmd
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
48
interactive/view.go
Normal file
48
interactive/view.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package interactive
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
func (m Model) View() string {
|
||||
output := lipgloss.NewStyle().
|
||||
Foreground(lipgloss.Color("5")).
|
||||
Background(lipgloss.Color("0")).
|
||||
Bold(true).
|
||||
Underline(true).
|
||||
Render("Welcome to the img-proxy URL Generator!") + "\n\n"
|
||||
|
||||
if m.Form == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if m.err != nil {
|
||||
return output + "Error: " + m.err.Error() + "\n" + "Press Ctrl+C to exit.\n"
|
||||
}
|
||||
|
||||
output = output + m.Form.View() + "\n"
|
||||
|
||||
if *m.url == "" {
|
||||
return output + help()
|
||||
}
|
||||
|
||||
url, _ := m.generator.GenerateUrl(*m.url, []string{}, *m.format)
|
||||
|
||||
output += fmt.Sprintf("\nGenerated URL: %s\n\n", lipgloss.NewStyle().
|
||||
Bold(true).
|
||||
Foreground(lipgloss.Color("#0000ff")).
|
||||
Render(url))
|
||||
|
||||
output += help()
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func help() string {
|
||||
return lipgloss.
|
||||
NewStyle().
|
||||
Inline(true).
|
||||
Foreground(lipgloss.Color("#123456")).
|
||||
Render("Ctrl+C | Esc: exit * Tab: Next Field * Sft+Tab: Prev Field * Space: Select\n")
|
||||
}
|
||||
Reference in New Issue
Block a user