Skip to content

Commit

Permalink
fix: Fixed bug with multiple default values, added hint
Browse files Browse the repository at this point in the history
  • Loading branch information
RamiAwar committed Mar 5, 2024
1 parent 3cd490f commit 05981b7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ So I made it possible to register snippets with description and search them easi
# TOC

- [Main features](#main-features)
- [Parameters] (#parameters)
- [Examples](#examples)
- [Register the previous command easily](#register-the-previous-command-easily)
- [bash](#bash-prev-function)
Expand Down
15 changes: 15 additions & 0 deletions dialog/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@ func TestSearchForParams_EqualsInDefaultValueIgnored(t *testing.T) {
}
}

func TestSearchForParams_MultipleDefaultValuesDoNotBreakFunction(t *testing.T) {
command := "echo \"<param=|_Hello_||_Hello world_||_How are you?_|> <second=Hello>, <third>\""
want := [][2]string{
{"param", "|_Hello_||_Hello world_||_How are you?_|"},
{"second", "Hello"},
{"third", ""},
}

got := SearchForParams(command)

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
}
}

func TestInsertParams(t *testing.T) {
command := "<a=1> <a> <b> hello"

Expand Down
28 changes: 20 additions & 8 deletions dialog/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,16 @@ func generateMultipleParameterView(g *gocui.Gui, desc string, defaultParams []st

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

view, _ := g.View(desc)
view.Title = desc
viewTitle := desc
// Adjust view title to hint the user about the available
// options if there are more than one
if maxOpt > 1 {
viewTitle = desc + " (UP/DOWN => Select default value)"
}

view.Title = viewTitle
view.Wrap = false
view.Autoscroll = true
view.Editable = editable
Expand Down Expand Up @@ -117,12 +124,14 @@ func GenerateParamsLayout(params [][2]string, command string) {
// Create a view for each param
for _, pair := range params {
// Unpack parameter key and value
k, v := pair[0], pair[1]
parameterKey, parameterValue := pair[0], pair[1]

// Handle multiple default values
// Check value for multiple defaults
r := regexp.MustCompile(parameterMultipleValueRegex)
matches := r.FindAllStringSubmatch(command, -1)
matches := r.FindAllStringSubmatch(parameterValue, -1)

if len(matches) > 0 {
// Extract the default values and generate multiple params view
parameters := []string{}
for _, p := range matches {
_, matchedGroup := p[0], p[1]
Expand All @@ -131,14 +140,17 @@ func GenerateParamsLayout(params [][2]string, command string) {
parameters = append(parameters, matchedGroup)
}
generateMultipleParameterView(
g, k, parameters, []int{leftX,
g, parameterKey, parameters, []int{
leftX,
(maxY / 4) + (idx+1)*layoutStep,
rightX,
(maxY / 4) + 2 + (idx+1)*layoutStep},
true)
} else {
generateSingleParameterView(g, k, v,
[]int{leftX,
// Generate single param view using the single value
generateSingleParameterView(g, parameterKey, parameterValue,
[]int{
leftX,
(maxY / 4) + (idx+1)*layoutStep,
rightX,
(maxY / 4) + 2 + (idx+1)*layoutStep},
Expand Down

0 comments on commit 05981b7

Please sign in to comment.