Skip to content

Commit

Permalink
fix: 🐛 Fixed #150
Browse files Browse the repository at this point in the history
Parameters were getting extracted from the command as well as the description or tags. Changed this to only extract parameters out of the command.

150
  • Loading branch information
RamiAwar committed Feb 17, 2024
1 parent dbc472b commit f0a32cc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 48 deletions.
10 changes: 9 additions & 1 deletion cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ func filter(options []string, tag string) (commands []string, err error) {
}

lines := strings.Split(strings.TrimSuffix(buf.String(), "\n"), "\n")
var params [][2]string

// If only one line is selected, search for params in the command
if len(lines) == 1 {
snippetInfo := snippetTexts[lines[0]]
params = dialog.SearchForParams(snippetInfo.Command)
} else {
params = nil
}

params := dialog.SearchForParams(lines)
if params != nil {
snippetInfo := snippetTexts[lines[0]]
dialog.CurrentCommand = snippetInfo.Command
Expand Down
61 changes: 29 additions & 32 deletions dialog/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,44 +39,41 @@ func insertParams(command string, params map[string]string) string {
}

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

params := r.FindAllStringSubmatch(lines[0], -1)
if len(params) == 0 {
return nil
}
params := r.FindAllStringSubmatch(command, -1)
if len(params) == 0 {
return nil
}

extracted := map[string]string{}
ordered_params := [][2]string{}
for _, p := range params {
splitted := strings.Split(p[1], "=")
key := splitted[0]
_, param_exists := extracted[key]

// Set to empty if no value is provided and param is not already set
if len(splitted) == 1 && !param_exists {
extracted[key] = ""
} else if len(splitted) > 1 {
// Set the value instead if it is provided
extracted[key] = splitted[1]
}

// Fill in the keys only if seen for the first time to track order
if !param_exists {
ordered_params = append(ordered_params, [2]string{key, ""})
}
extracted := map[string]string{}
ordered_params := [][2]string{}
for _, p := range params {
splitted := strings.Split(p[1], "=")
key := splitted[0]
_, param_exists := extracted[key]

// Set to empty if no value is provided and param is not already set
if len(splitted) == 1 && !param_exists {
extracted[key] = ""
} else if len(splitted) > 1 {
// Set the value instead if it is provided
extracted[key] = splitted[1]
}

// Fill in the values
for i, param := range ordered_params {
pair := [2]string{param[0], extracted[param[0]]}
ordered_params[i] = pair
// Fill in the keys only if seen for the first time to track order
if !param_exists {
ordered_params = append(ordered_params, [2]string{key, ""})
}
return ordered_params
}
return nil

// Fill in the values
for i, param := range ordered_params {
pair := [2]string{param[0], extracted[param[0]]}
ordered_params[i] = pair
}
return ordered_params
}

func evaluateParams(g *gocui.Gui, _ *gocui.View) error {
Expand Down
30 changes: 15 additions & 15 deletions dialog/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestSearchForParams(t *testing.T) {
{"b", ""},
}

got := SearchForParams([]string{command})
got := SearchForParams(command)

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
Expand All @@ -24,7 +24,7 @@ func TestSearchForParams(t *testing.T) {
func TestSearchForParams_WithNoParams(t *testing.T) {
command := "no params"

got := SearchForParams([]string{command})
got := SearchForParams(command)

if got != nil {
t.Fatalf("wanted nil, got '%v'", got)
Expand All @@ -40,7 +40,7 @@ func TestSearchForParams_WithMultipleParams(t *testing.T) {
{"c", "3"},
}

got := SearchForParams([]string{command})
got := SearchForParams(command)

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
Expand All @@ -50,7 +50,7 @@ func TestSearchForParams_WithMultipleParams(t *testing.T) {
func TestSearchForParams_WithEmptyCommand(t *testing.T) {
command := ""

got := SearchForParams([]string{command})
got := SearchForParams(command)

if got != nil {
t.Fatalf("wanted nil, got '%v'", got)
Expand All @@ -66,7 +66,7 @@ func TestSearchForParams_WithNewline(t *testing.T) {
{"c", "3"},
}

got := SearchForParams([]string{command})
got := SearchForParams(command)

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
Expand All @@ -80,7 +80,7 @@ func TestSearchForParams_ValueWithSpaces(t *testing.T) {
{"param", "Lots of Bananas"},
}

got := SearchForParams([]string{command})
got := SearchForParams(command)

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
Expand All @@ -92,7 +92,7 @@ func TestSearchForParams_InvalidParamFormat(t *testing.T) {
want := [][2]string{
{"b", ""},
}
got := SearchForParams([]string{command})
got := SearchForParams(command)

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
Expand All @@ -104,7 +104,7 @@ func TestSearchForParams_InvalidParamFormatWithoutSpaces(t *testing.T) {
want := [][2]string{
{"b", ""},
}
got := SearchForParams([]string{command})
got := SearchForParams(command)

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
Expand All @@ -116,7 +116,7 @@ func TestSearchForParams_ConfusingBrackets(t *testing.T) {
want := [][2]string{
{"file", "path/to/file"},
}
got := SearchForParams([]string{command})
got := SearchForParams(command)
if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
}
Expand All @@ -127,7 +127,7 @@ func TestSearchForParams_MultipleParamsSameKey(t *testing.T) {
want := [][2]string{
{"a", "3"},
}
got := SearchForParams([]string{command})
got := SearchForParams(command)

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
Expand All @@ -139,7 +139,7 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues(t *testing.T) {
want := [][2]string{
{"a", "3"},
}
got := SearchForParams([]string{command})
got := SearchForParams(command)

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
Expand All @@ -152,7 +152,7 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues_MultipleLines(t *t
{"a", "3"},
{"b", "4"},
}
got := SearchForParams([]string{command})
got := SearchForParams(command)

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
Expand All @@ -164,7 +164,7 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat(t *t
want := [][2]string{
{"a", "3"},
}
got := SearchForParams([]string{command})
got := SearchForParams(command)

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
Expand All @@ -178,7 +178,7 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_Mult
{"b", "4"},
}

got := SearchForParams([]string{command})
got := SearchForParams(command)

if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
Expand All @@ -191,7 +191,7 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_Mult
{"a", "3"},
}

got := SearchForParams([]string{command})
got := SearchForParams(command)

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

0 comments on commit f0a32cc

Please sign in to comment.