Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor with highlighting pattern. #65

Merged
merged 10 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module github.com/chapar-rest/chapar
go 1.22.1

require (
gioui.org v0.7.0
gioui.org/x v0.7.0
gioui.org v0.7.1
gioui.org/x v0.7.1
github.com/PaesslerAG/jsonpath v0.1.1
github.com/alecthomas/chroma/v2 v2.14.0
github.com/dustin/go-humanize v1.0.1
github.com/google/uuid v1.6.0
github.com/jhump/protoreflect v1.16.0
github.com/oligo/gioview v0.4.1-0.20240715092046-997b87ed0255
github.com/oligo/gioview v0.5.1-0.20240805165652-db7ac64b4905
golang.org/x/exp/shiny v0.0.0-20240707233637-46b078467d37
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.33.1-0.20240408130810-98873a205002
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.1
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -30,9 +30,9 @@ require (
github.com/shopspring/decimal v1.3.1 // indirect
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 // indirect
golang.org/x/image v0.18.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
)
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
eliasnaur.com/font v0.0.0-20230308162249-dd43949cb42d h1:ARo7NCVvN2NdhLlJE9xAbKweuI9L6UgfTbYb0YwPacY=
eliasnaur.com/font v0.0.0-20230308162249-dd43949cb42d/go.mod h1:OYVuxibdk9OSLX8vAqydtRPP87PyTFcT9uH3MlEGBQA=
gioui.org v0.7.0 h1:5I+7Uu2yjTu7W5p7HWQrgsDPO3vex+8T1DsvCLGBfuI=
gioui.org v0.7.0/go.mod h1:19wZxaNP+eHN4H2YdZwEfbkAAgoYB5rcIbDHo4BqUl4=
gioui.org v0.7.1 h1:l7OVj47n1z8acaszQ6Wlu+Rxme+HqF3q8b+Fs68+x3w=
gioui.org v0.7.1/go.mod h1:5Kw/q7R1BWc5MKStuTNvhCgSrRqbfHc9Dzfjs4IGgZo=
gioui.org/cpu v0.0.0-20210808092351-bfe733dd3334/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ=
gioui.org/cpu v0.0.0-20220412190645-f1e9e8c3b1f7 h1:tNJdnP5CgM39PRc+KWmBRRYX/zJ+rd5XaYxY5d5veqA=
gioui.org/cpu v0.0.0-20220412190645-f1e9e8c3b1f7/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ=
gioui.org/shader v1.0.8 h1:6ks0o/A+b0ne7RzEqRZK5f4Gboz2CfG+mVliciy6+qA=
gioui.org/shader v1.0.8/go.mod h1:mWdiME581d/kV7/iEhLmUgUK5iZ09XR5XpduXzbePVM=
gioui.org/x v0.7.0 h1:8rkLfy4MAHM+MgEv8IUwRcmB9rxJqYnRzbuOJqXzG6Y=
gioui.org/x v0.7.0/go.mod h1:zJdmUbD5PuGHL8fzX0kehLTZ6TmIvfo6mg10sW54dsU=
gioui.org/x v0.7.1 h1:7bnQHsV7qB36tIUit2WDcUx4Cnmo+6T9I38B9brLQ7o=
gioui.org/x v0.7.1/go.mod h1:5CzZ64oFpOaqb2kaMvj+QEr5T3nVuLKD0LizLH32ii0=
git.wow.st/gmp/jni v0.0.0-20210610011705-34026c7e22d0 h1:bGG/g4ypjrCJoSvFrP5hafr9PPB5aw8SjcOWWila7ZI=
git.wow.st/gmp/jni v0.0.0-20210610011705-34026c7e22d0/go.mod h1:+axXBRUTIDlCeE73IKeD/os7LoEnTKdkp8/gQOFjqyo=
github.com/PaesslerAG/gval v1.0.0/go.mod h1:y/nm5yEyTeX6av0OfKJNp9rBNj2XrGhAf5+v24IBN1I=
Expand Down Expand Up @@ -47,8 +47,8 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/jhump/protoreflect v1.16.0 h1:54fZg+49widqXYQ0b+usAFHbMkBGR4PpXrsHc8+TBDg=
github.com/jhump/protoreflect v1.16.0/go.mod h1:oYPd7nPvcBw/5wlDfm/AVmU9zH9BgqGCI469pGxfj/8=
github.com/oligo/gioview v0.4.1-0.20240715092046-997b87ed0255 h1:ao+r5gHJoc+A0CA0fAXFyc/RURb8lvIeWuuRHj70QKY=
github.com/oligo/gioview v0.4.1-0.20240715092046-997b87ed0255/go.mod h1:WEa28vrpnPfZnhYL14tAWdsM4kvcW7NUlYLTE/Xbefo=
github.com/oligo/gioview v0.5.1-0.20240805165652-db7ac64b4905 h1:iBiM6OWNacjpEy5qwflkvZC5pp6XJ3Fz5azIdEO7tmE=
github.com/oligo/gioview v0.5.1-0.20240805165652-db7ac64b4905/go.mod h1:5wxSoxxJ7FOVPnEYhqlUrT451rOmL8DQZCTeVlbVkoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
Expand All @@ -61,20 +61,20 @@ golang.org/x/exp/shiny v0.0.0-20240707233637-46b078467d37 h1:SOSg7+sueresE4IbmmG
golang.org/x/exp/shiny v0.0.0-20240707233637-46b078467d37/go.mod h1:3F+MieQB7dRYLTmnncoFbb1crS5lfQoTfDgQy6K4N0o=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 h1:NnYq6UN9ReLM9/Y01KWNOWyI5xQ9kbIms5GGJVwS/Yc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/protobuf v1.33.1-0.20240408130810-98873a205002 h1:V7Da7qt0MkY3noVANIMVBk28nOnijADeOR3i5Hcvpj4=
google.golang.org/protobuf v1.33.1-0.20240408130810-98873a205002/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 h1:Zy9XzmMEflZ/MAaA7vNcoebnRAld7FsPW1EeBB7V0m8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
33 changes: 5 additions & 28 deletions ui/pages/requests/component/address_bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type AddressBar struct {
url *widget.Editor
url *widgets.PatternEditor

lastSelectedMethod string
methodDropDown *widgets.DropDown
Expand All @@ -27,7 +27,7 @@ type AddressBar struct {

func NewAddressBar(theme *chapartheme.Theme, address, method string) *AddressBar {
a := &AddressBar{
url: &widget.Editor{},
url: widgets.NewPatternEditor(),
methodDropDown: widgets.NewDropDownWithoutBorder(theme),
lastSelectedMethod: method,
}
Expand Down Expand Up @@ -55,6 +55,7 @@ func (a *AddressBar) SetSelectedMethod(method string) {

func (a *AddressBar) SetOnURLChanged(onURLChanged func(url string)) {
a.onURLChanged = onURLChanged
a.url.SetOnChanged(onURLChanged)
}

func (a *AddressBar) SetOnMethodChanged(onMethodChanged func(method string)) {
Expand All @@ -66,6 +67,7 @@ func (a *AddressBar) SetOnMethodChanged(onMethodChanged func(method string)) {

func (a *AddressBar) SetOnSubmit(onSubmit func()) {
a.onSubmit = onSubmit
a.url.SetOnSubmit(onSubmit)
}

func (a *AddressBar) SetURL(url string) {
Expand All @@ -84,27 +86,6 @@ func (a *AddressBar) Layout(gtx layout.Context, theme *chapartheme.Theme) layout
CornerRadius: unit.Dp(4),
}

for {
event, ok := a.url.Update(gtx)
if !ok {
break
}

switch event.(type) {
// on carriage return event
case widget.SubmitEvent:
if a.onSubmit != nil {
// goroutine to prevent blocking the ui update
go a.onSubmit()
}
// on change event
case widget.ChangeEvent:
if a.onURLChanged != nil {
a.onURLChanged(a.url.Text())
}
}
}

if a.methodDropDown.GetSelected().Text != a.lastSelectedMethod {
a.lastSelectedMethod = a.methodDropDown.GetSelected().Text
if a.onMethodChanged != nil {
Expand All @@ -131,11 +112,7 @@ func (a *AddressBar) Layout(gtx layout.Context, theme *chapartheme.Theme) layout
widgets.DrawLineFlex(theme.SeparatorColor, unit.Dp(20), unit.Dp(1)),
layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Left: unit.Dp(10), Right: unit.Dp(5)}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
gtx.Constraints.Min.Y = gtx.Dp(20)
editor := material.Editor(theme.Material(), a.url, "https://example.com")
editor.SelectionColor = theme.TextSelectionColor
editor.TextSize = unit.Sp(14)
return editor.Layout(gtx)
return a.url.Layout(gtx, theme, "https://example.com")
})
}),
)
Expand Down
25 changes: 13 additions & 12 deletions ui/pages/requests/component/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package component
import (
"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"

"github.com/chapar-rest/chapar/ui/chapartheme"
"github.com/chapar-rest/chapar/ui/keys"
"github.com/chapar-rest/chapar/ui/widgets"
)

Expand All @@ -20,7 +18,7 @@ type Field struct {
Label string
Value string

Editor *widget.Editor
Editor *widgets.PatternEditor
}

func NewForm(fields []*Field) *Form {
Expand All @@ -37,18 +35,26 @@ func (f *Form) GetValues() map[string]string {
values := make(map[string]string)
for _, field := range f.Fields {
if field.Editor == nil {
field.Editor = new(widget.Editor)
field.Editor = widgets.NewPatternEditor()
field.Editor.SetOnChanged(f.onDataChange)
}

values[field.Label] = field.Editor.Text()
}
return values
}

func (f *Form) onDataChange(_ string) {
if f.onChange != nil {
f.onChange(f.GetValues())
}
}

func (f *Form) SetValues(values map[string]string) {
for _, field := range f.Fields {
if field.Editor == nil {
field.Editor = new(widget.Editor)
field.Editor = widgets.NewPatternEditor()
field.Editor.SetOnChanged(f.onDataChange)
}

field.Editor.SetText(values[field.Label])
Expand All @@ -61,15 +67,10 @@ func (f *Form) Layout(gtx layout.Context, theme *chapartheme.Theme) layout.Dimen
field := field
childs = append(childs, layout.Rigid(func(gtx layout.Context) layout.Dimensions {
if field.Editor == nil {
field.Editor = new(widget.Editor)
field.Editor = widgets.NewPatternEditor()
field.Editor.SetOnChanged(f.onDataChange)
}

keys.OnEditorChange(gtx, field.Editor, func() {
if f.onChange != nil {
f.onChange(f.GetValues())
}
})

lb := &widgets.LabeledInput{
Label: field.Label,
SpaceBetween: 5,
Expand Down
85 changes: 41 additions & 44 deletions ui/pages/requests/component/pre_post_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (

"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"

"github.com/chapar-rest/chapar/internal/domain"
"github.com/chapar-rest/chapar/ui/chapartheme"
"github.com/chapar-rest/chapar/ui/keys"
"github.com/chapar-rest/chapar/ui/widgets"
)

Expand All @@ -28,9 +26,9 @@ type PrePostRequest struct {
}

type SetEnvForm struct {
statusCodeEditor widget.Editor
targetEditor widget.Editor
fromEditor widget.Editor
statusCodeEditor *widgets.LabeledInput
targetEditor *widgets.LabeledInput
fromEditor *widgets.LabeledInput
fromDropDown *widgets.DropDown
preview string
}
Expand Down Expand Up @@ -62,6 +60,28 @@ func NewPrePostRequest(options []Option, theme *chapartheme.Theme) *PrePostReque
widgets.NewDropDownOption("From Header").WithValue(domain.PostRequestSetFromResponseHeader),
widgets.NewDropDownOption("From Cookie").WithValue(domain.PostRequestSetFromResponseCookie),
),
statusCodeEditor: &widgets.LabeledInput{
Label: "Status Code",
SpaceBetween: 5,
MinEditorWidth: unit.Dp(150),
MinLabelWidth: unit.Dp(80),
Editor: widgets.NewPatternEditor(),
},
targetEditor: &widgets.LabeledInput{
Label: "Target Key",
SpaceBetween: 5,
MinEditorWidth: unit.Dp(150),
MinLabelWidth: unit.Dp(80),
Editor: widgets.NewPatternEditor(),
},
fromEditor: &widgets.LabeledInput{
Label: "Key",
SpaceBetween: 5,
MinEditorWidth: unit.Dp(150),
MinLabelWidth: unit.Dp(80),
Editor: widgets.NewPatternEditor(),
Hint: "e.g. name",
},
},
}
p.setEnvForm.fromDropDown.MaxWidth = unit.Dp(150)
Expand Down Expand Up @@ -111,6 +131,16 @@ func (p *PrePostRequest) SetOnPostRequestSetChanged(f func(statusCode int, item,
statusCode, _ := strconv.Atoi(p.setEnvForm.statusCodeEditor.Text())
p.onSetEnvFormChanged(statusCode, p.setEnvForm.targetEditor.Text(), selected, p.setEnvForm.fromEditor.Text())
})
p.setEnvForm.statusCodeEditor.SetOnChanged(func(text string) {
p.enforceNumericEditor(p.setEnvForm.statusCodeEditor.Editor)
p.handleDataChange()
})
p.setEnvForm.targetEditor.SetOnChanged(func(_ string) {
p.handleDataChange()
})
p.setEnvForm.fromEditor.SetOnChanged(func(_ string) {
p.handleDataChange()
})
}

func (p *PrePostRequest) Layout(gtx layout.Context, theme *chapartheme.Theme) layout.Dimensions {
Expand Down Expand Up @@ -153,7 +183,7 @@ func (p *PrePostRequest) handleDataChange() {
}
}

func (p *PrePostRequest) enforceNumericEditor(editor *widget.Editor) {
func (p *PrePostRequest) enforceNumericEditor(editor *widgets.PatternEditor) {
if _, err := strconv.Atoi(editor.Text()); err != nil {
editor.SetText("0")
}
Expand All @@ -162,42 +192,15 @@ func (p *PrePostRequest) enforceNumericEditor(editor *widget.Editor) {
func (p *PrePostRequest) SetEnvForm(gtx layout.Context, theme *chapartheme.Theme) layout.Dimensions {
topButtonInset := layout.Inset{Top: unit.Dp(8), Bottom: unit.Dp(4)}

keys.OnEditorChange(gtx, &p.setEnvForm.statusCodeEditor, func() {
p.enforceNumericEditor(&p.setEnvForm.statusCodeEditor)
p.handleDataChange()
})

keys.OnEditorChange(gtx, &p.setEnvForm.targetEditor, func() {
p.handleDataChange()
})

keys.OnEditorChange(gtx, &p.setEnvForm.fromEditor, func() {
p.handleDataChange()
})

return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return topButtonInset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
lb := &widgets.LabeledInput{
Label: "Target Key",
SpaceBetween: 5,
MinEditorWidth: unit.Dp(150),
MinLabelWidth: unit.Dp(80),
Editor: &p.setEnvForm.targetEditor,
}
return lb.Layout(gtx, theme)
return p.setEnvForm.targetEditor.Layout(gtx, theme)
})
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return topButtonInset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
lb := &widgets.LabeledInput{
Label: "Status Code",
SpaceBetween: 5,
MinEditorWidth: unit.Dp(150),
MinLabelWidth: unit.Dp(80),
Editor: &p.setEnvForm.statusCodeEditor,
}
return lb.Layout(gtx, theme)
return p.setEnvForm.statusCodeEditor.Layout(gtx, theme)
})
}),

Expand Down Expand Up @@ -227,15 +230,9 @@ func (p *PrePostRequest) SetEnvForm(gtx layout.Context, theme *chapartheme.Theme
hint = "e.g. $.data[0].name"
}

lb := &widgets.LabeledInput{
Label: label,
SpaceBetween: 5,
MinEditorWidth: unit.Dp(150),
MinLabelWidth: unit.Dp(80),
Editor: &p.setEnvForm.fromEditor,
Hint: hint,
}
return lb.Layout(gtx, theme)
p.setEnvForm.fromEditor.SetHint(hint)
p.setEnvForm.fromEditor.SetLabel(label)
return p.setEnvForm.fromEditor.Layout(gtx, theme)
})
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
Expand Down
Loading
Loading