Skip to content

Commit

Permalink
feat: ✨ Added order to parameters based on order of occurrence
Browse files Browse the repository at this point in the history
Closes #258.
  • Loading branch information
RamiAwar committed Feb 2, 2024
1 parent b5974c2 commit 2864c1e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 63 deletions.
17 changes: 15 additions & 2 deletions dialog/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func insertParams(command string, params map[string]string) string {
}

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

Expand All @@ -49,6 +49,7 @@ func SearchForParams(lines []string) map[string]string {
}

extracted := map[string]string{}
ordered_params := [][2]string{}
for _, p := range params {
splitted := strings.Split(p[1], "=")
key := splitted[0]
Expand All @@ -58,10 +59,22 @@ func SearchForParams(lines []string) map[string]string {
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, ""})
}
}

// Fill in the values
for i, param := range ordered_params {
pair := [2]string{param[0], extracted[param[0]]}
ordered_params[i] = pair
}
return extracted
return ordered_params
}
return nil
}
Expand Down
94 changes: 35 additions & 59 deletions dialog/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,15 @@ import (
func TestSearchForParams(t *testing.T) {
command := "<a=1> <b> hello"

params := map[string]string{
"a": "1",
"b": "",
want := [][2]string{
{"a", "1"},
{"b", ""},
}

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

for key, value := range params {
if got[key] != value {
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
}
}

for key, value := range got {
if params[key] != value {
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
}
if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
}
}

Expand All @@ -42,24 +34,16 @@ func TestSearchForParams_WithNoParams(t *testing.T) {
func TestSearchForParams_WithMultipleParams(t *testing.T) {
command := "<a=1> <b> <c=3>"

params := map[string]string{
"a": "1",
"b": "",
"c": "3",
want := [][2]string{
{"a", "1"},
{"b", ""},
{"c", "3"},
}

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

for key, value := range params {
if got[key] != value {
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
}
}

for key, value := range got {
if params[key] != value {
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
}
if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
}
}

Expand All @@ -76,31 +60,23 @@ func TestSearchForParams_WithEmptyCommand(t *testing.T) {
func TestSearchForParams_WithNewline(t *testing.T) {
command := "<a=1> <b> hello\n<c=3>"

params := map[string]string{
"a": "1",
"b": "",
"c": "3",
want := [][2]string{
{"a", "1"},
{"b", ""},
{"c", "3"},
}

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

for key, value := range params {
if got[key] != value {
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
}
}

for key, value := range got {
if params[key] != value {
t.Fatalf("wanted param '%s' to equal '%s', got '%s'", key, value, got[key])
}
if diff := deep.Equal(want, got); diff != nil {
t.Fatal(diff)
}
}

func TestSearchForParams_InvalidParamFormat(t *testing.T) {
command := "<a=1 <b> hello"
want := map[string]string{
"b": "",
want := [][2]string{
{"b", ""},
}
got := SearchForParams([]string{command})

Expand All @@ -111,8 +87,8 @@ func TestSearchForParams_InvalidParamFormat(t *testing.T) {

func TestSearchForParams_ConfusingBrackets(t *testing.T) {
command := "cat <<EOF > <file=path/to/file>\nEOF"
want := map[string]string{
"file": "path/to/file",
want := [][2]string{
{"file", "path/to/file"},
}
got := SearchForParams([]string{command})
if diff := deep.Equal(want, got); diff != nil {
Expand All @@ -122,8 +98,8 @@ func TestSearchForParams_ConfusingBrackets(t *testing.T) {

func TestSearchForParams_MultipleParamsSameKey(t *testing.T) {
command := "<a=1> <a=2> <a=3>"
want := map[string]string{
"a": "3",
want := [][2]string{
{"a", "3"},
}
got := SearchForParams([]string{command})

Expand All @@ -134,8 +110,8 @@ func TestSearchForParams_MultipleParamsSameKey(t *testing.T) {

func TestSearchForParams_MultipleParamsSameKeyDifferentValues(t *testing.T) {
command := "<a=1> <a=2> <a=3>"
want := map[string]string{
"a": "3",
want := [][2]string{
{"a", "3"},
}
got := SearchForParams([]string{command})

Expand All @@ -146,9 +122,9 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues(t *testing.T) {

func TestSearchForParams_MultipleParamsSameKeyDifferentValues_MultipleLines(t *testing.T) {
command := "<a=1> <a=2> <a=3>\n<b=4>"
want := map[string]string{
"a": "3",
"b": "4",
want := [][2]string{
{"a", "3"},
{"b", "4"},
}
got := SearchForParams([]string{command})

Expand All @@ -159,8 +135,8 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues_MultipleLines(t *t

func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat(t *testing.T) {
command := "<a=1> <a=2 <a=3>"
want := map[string]string{
"a": "3",
want := [][2]string{
{"a", "3"},
}
got := SearchForParams([]string{command})

Expand All @@ -171,9 +147,9 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat(t *t

func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_MultipleLines(t *testing.T) {
command := "<a=1> <a=2> <a=3 \n<b=4>"
want := map[string]string{
"a": "2",
"b": "4",
want := [][2]string{
{"a", "2"},
{"b", "4"},
}

got := SearchForParams([]string{command})
Expand All @@ -185,8 +161,8 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_Mult

func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_MultipleLines2(t *testing.T) {
command := "<a=1> <a=2> <a=3>\n<b=4"
want := map[string]string{
"a": "3",
want := [][2]string{
{"a", "3"},
}

got := SearchForParams([]string{command})
Expand Down
8 changes: 6 additions & 2 deletions dialog/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func generateView(g *gocui.Gui, desc string, fill string, coords []int, editable
}

// GenerateParamsLayout generates CUI to receive params
func GenerateParamsLayout(params map[string]string, command string) {
func GenerateParamsLayout(params [][2]string, command string) {
g, err := gocui.NewGui(gocui.OutputNormal, false)
if err != nil {
log.Panicln(err)
Expand All @@ -47,7 +47,11 @@ func GenerateParamsLayout(params map[string]string, command string) {
generateView(g, "Command(TAB => Select next, ENTER => Execute command):",
command, []int{maxX / 10, maxY / 10, (maxX / 2) + (maxX / 3), maxY/10 + 5}, false)
idx := 0
for k, v := range params {

// 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{maxX / 10, (maxY / 4) + (idx+1)*layoutStep,
maxX/10 + 20, (maxY / 4) + 2 + (idx+1)*layoutStep}, true)
idx++
Expand Down

0 comments on commit 2864c1e

Please sign in to comment.