Skip to content

Commit

Permalink
feat: support multiple default values
Browse files Browse the repository at this point in the history
This CL supports multiple default values like <proxy=cfg1|cfg2|cfg3>,
this parameter will rendered like this in `generateView`:

```
--proxy=cfg1|cfg2|cfg3----------------------------------
|cfg1                                                  |
|cfg2                                                  |
|cfg3                                                  |
|------------------------------------------------------
```

then we can use up/down arrow to select the wanted item, after press
`enter` evaluateResult will be executed.

This CL also let the parameter view's title use larger width.

close knqyf263#192
  • Loading branch information
hitzhangjie committed May 23, 2022
1 parent e7a5e03 commit c656f58
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
22 changes: 16 additions & 6 deletions dialog/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

var (
views = []string{}
layoutStep = 3
curView = -1
idxView = 0
views = []string{}
viewsUseMultiValues = map[string]bool{}
layoutStep = 3
curView = -1
idxView = 0

//CurrentCommand is the command before assigning to variables
CurrentCommand string
Expand Down Expand Up @@ -57,8 +58,17 @@ func evaluateParams(g *gocui.Gui, _ *gocui.View) error {
for _, v := range views {
view, _ := g.View(v)
res := view.Buffer()
res = strings.Replace(res, "\n", "", -1)
paramsFilled[v] = strings.TrimSpace(res)
if _, ok := viewsUseMultiValues[v]; !ok {
res = strings.Replace(res, "\n", "", -1)
paramsFilled[v] = strings.TrimSpace(res)
} else {
_, cy := view.Cursor()
l, err := view.Line(cy)
if err != nil {
return err
}
paramsFilled[v] = strings.TrimSpace(l)
}
}
FinalCommand = insertParams(CurrentCommand, paramsFilled)
return gocui.ErrQuit
Expand Down
27 changes: 23 additions & 4 deletions dialog/view.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package dialog

import (
"bytes"
"fmt"
"log"
"strings"

"github.com/jroimartin/gocui"
)

func generateView(g *gocui.Gui, desc string, fill string, coords []int, editable bool) error {
func generateView(g *gocui.Gui, desc string, fill string, coords []int, editable, listStyle bool) error {
if StringInSlice(desc, views) {
return nil
}
Expand All @@ -23,6 +25,12 @@ func generateView(g *gocui.Gui, desc string, fill string, coords []int, editable
view.Autoscroll = true
view.Editable = editable

if listStyle {
view.Highlight = true
view.SelBgColor = gocui.ColorGreen
view.SelFgColor = gocui.ColorRed
}

views = append(views, desc)
idxView++

Expand All @@ -45,11 +53,22 @@ func GenerateParamsLayout(params map[string]string, command string) {

maxX, maxY := g.Size()
generateView(g, "Command(TAB => Select next, ENTER => Execute command):",
command, []int{maxX / 10, maxY / 10, (maxX / 2) + (maxX / 3), maxY/10 + 5}, false)
command, []int{maxX / 10, maxY / 10, (maxX / 2) + (maxX / 3), maxY/10 + 5}, false, false)
idx := 0
for k, v := range params {
generateView(g, k, v, []int{maxX / 10, (maxY / 4) + (idx+1)*layoutStep,
maxX/10 + 20, (maxY / 4) + 2 + (idx+1)*layoutStep}, true)
defaultValues := strings.Split(v, "|")
if len(defaultValues) <= 1 {
generateView(g, k, v, []int{maxX / 10, (maxY / 4) + (idx+1)*layoutStep,
maxX/2 + maxX/3, (maxY / 4) + 2 + (idx+1)*layoutStep}, true, false)
} else {
b := bytes.Buffer{}
for _, value := range defaultValues {
fmt.Fprintf(&b, "%s\n", value)
}
generateView(g, k, b.String(), []int{maxX / 10, (maxY / 4) + (idx+1)*layoutStep,
maxX/2 + maxX/3, (maxY / 4) + 2 + (idx+1)*layoutStep + len(defaultValues)}, true, true)
viewsUseMultiValues[k] = true
}
idx++
}

Expand Down

1 comment on commit c656f58

@1426536231
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, here is a bug that when multiple "multiple default values " ,for ensample, "cp <proxy=cfg1|cfg2|cfg3> <proxy1=cfg1|cfg2|cfg3>". it can just show two line for proxy. can you fix it~

--proxy=cfg1|cfg2|cfg3----------------------------------
|cfg1                                                  |
|cfg2                                                  |
|------------------------------------------------------
--proxy1=cfg1|cfg2|cfg3----------------------------------
|cfg1                                                  |
|cfg2                                                  |
|cfg3                                                  |
|------------------------------------------------------

Please sign in to comment.