Skip to content

Commit

Permalink
Added multiple parameter support via two character separators
Browse files Browse the repository at this point in the history
  • Loading branch information
RamiAwar committed Feb 28, 2024
1 parent a1ba29e commit 232ea0b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 12 deletions.
6 changes: 3 additions & 3 deletions dialog/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ var (
// This matches most encountered patterns
// Skips match if there is a whitespace at the end ex. <param='my >
// Ignores <, > characters since they're used to match the pattern
patternRegex = `<([^<>]*[^\s])>`
parameterStringRegex = `<([^<>]*[^\s])>`
)

func insertParams(command string, filledInParams map[string]string) string {
r := regexp.MustCompile(patternRegex)
r := regexp.MustCompile(parameterStringRegex)

matches := r.FindAllStringSubmatch(command, -1)
if len(matches) == 0 {
Expand All @@ -48,7 +48,7 @@ func insertParams(command string, filledInParams map[string]string) string {

// SearchForParams returns variables from a command
func SearchForParams(command string) [][2]string {
r := regexp.MustCompile(patternRegex)
r := regexp.MustCompile(parameterStringRegex)

params := r.FindAllStringSubmatch(command, -1)
if len(params) == 0 {
Expand Down
97 changes: 88 additions & 9 deletions dialog/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ package dialog
import (
"fmt"
"log"
"regexp"

"github.com/awesome-gocui/gocui"
)

var (
layoutStep = 3
curView = -1

// This is for matching multiple default values in parameters
parameterMultipleValueRegex = `(\|_.*?_\|)`
)

func generateView(g *gocui.Gui, desc string, fill string, coords []int, editable bool) error {
func generateSingleParameterView(g *gocui.Gui, desc string, defaultParam string, coords []int, editable bool) error {
if StringInSlice(desc, views) {
return nil
}
if v, err := g.SetView(desc, coords[0], coords[1], coords[2], coords[3], 0); err != nil {
if err != gocui.ErrUnknownView {
return err
}
fmt.Fprint(v, fill)
fmt.Fprint(v, defaultParam)
}
view, _ := g.View(desc)
view.Title = desc
Expand All @@ -33,6 +37,61 @@ func generateView(g *gocui.Gui, desc string, fill string, coords []int, editable
return nil
}

func generateMultipleParameterView(g *gocui.Gui, desc string, defaultParams []string, coords []int, editable bool) error {
if StringInSlice(desc, views) {
return nil
}

currentOpt := 0
maxOpt := len(defaultParams)

if v, err := g.SetView(desc, coords[0], coords[1], coords[2], coords[3], 0); err != nil {
if err != gocui.ErrUnknownView {
return err
}
g.SetKeybinding(v.Name(), gocui.KeyArrowDown, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
if maxOpt == 0 {
return nil
}
next := currentOpt + 1
if next >= maxOpt {
next = currentOpt
}
v.Clear()
fmt.Fprint(v, defaultParams[next])
currentOpt = next
return nil
})
g.SetKeybinding(v.Name(), gocui.KeyArrowUp, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
if maxOpt == 0 {
return nil
}
prev := currentOpt - 1
if prev < 0 {
prev = currentOpt
}
v.Clear()
fmt.Fprint(v, defaultParams[prev])
currentOpt = prev
return nil
})
g.SetKeybinding(v.Name(), gocui.KeyCtrlK, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
v.Clear()
return nil
})

fmt.Fprint(v, defaultParams[currentOpt])
}

view, _ := g.View(desc)
view.Title = desc
view.Wrap = false
view.Autoscroll = true
view.Editable = editable
views = append(views, desc)
return nil
}

// GenerateParamsLayout generates CUI to receive params
func GenerateParamsLayout(params [][2]string, command string) {
g, err := gocui.NewGui(gocui.OutputNormal, false)
Expand All @@ -51,20 +110,40 @@ func GenerateParamsLayout(params [][2]string, command string) {
leftX := (maxX / 2) - (maxX / 3)
rightX := (maxX / 2) + (maxX / 3)

generateView(g, "Command(TAB => Select next, ENTER => Execute command):",
generateSingleParameterView(g, "Command(TAB => Select next, ENTER => Execute command):",
command, []int{leftX, maxY / 10, rightX, maxY/10 + 5}, false)
idx := 0

// Create a view for each param
for _, pair := range params {
// Unpack parameter key and value
k, v := pair[0], pair[1]
generateView(g, k, v,
[]int{leftX,
(maxY / 4) + (idx+1)*layoutStep,
rightX,
(maxY / 4) + 2 + (idx+1)*layoutStep},
true)

// Handle multiple default values
r := regexp.MustCompile(parameterMultipleValueRegex)
matches := r.FindAllStringSubmatch(command, -1)
if len(matches) > 0 {
parameters := []string{}
for _, p := range matches {
_, matchedGroup := p[0], p[1]
// Remove the separators
matchedGroup = matchedGroup[2 : len(matchedGroup)-2]
parameters = append(parameters, matchedGroup)
}
generateMultipleParameterView(
g, k, parameters, []int{leftX,
(maxY / 4) + (idx+1)*layoutStep,
rightX,
(maxY / 4) + 2 + (idx+1)*layoutStep},
true)
} else {
generateSingleParameterView(g, k, v,
[]int{leftX,
(maxY / 4) + (idx+1)*layoutStep,
rightX,
(maxY / 4) + 2 + (idx+1)*layoutStep},
true)
}
idx++
}

Expand Down

0 comments on commit 232ea0b

Please sign in to comment.