package interactive
import (
"fmt"
"github.com/manifoldco/promptui"
"gitlab.com/pcanilho/go-jira"
"gitlab.com/pcanilho/go-jira-cli/cmd/helpers"
"gitlab.com/pcanilho/go-jira-cli/internal"
"strings"
)
type issueProject struct {
promptui.Select
next, previous Menu
controller internal.ProjectController
}
var issueProjectTemplate = &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "→ ({{ .Key | red }}) {{ .Name | cyan }}",
Inactive: " ({{ .Key | red }}) {{ .Name | cyan }}",
Selected: "→ {{ .Name | red | cyan }}",
Details: `{{ if not .Name }} {{ . }} {{ else }}
----------- Details ------------
{{ "Key:" | faint }} {{ .Key }}
{{ "Name:" | faint }} {{ .Name }} {{ end }}`,
FuncMap: helpers.GenerateTemplateFuncMap(promptui.FuncMap),
}
func NewIssueProjectMenu(projects []jira.Project, previous Menu) Menu {
values := []interface{}{PreviousMenu}
for _, p := range projects {
values = append(values, p)
}
return &issueProject{
previous: previous,
Select: promptui.Select{
HideSelected: true,
Label: "-------- Projects --------",
Templates: issueProjectTemplate,
Items: values,
Searcher: func(input string, index int) bool {
project := projects[index]
key := strings.TrimSpace(strings.ToLower(project.Key))
name := strings.TrimSpace(strings.ToLower(project.Name))
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(key, input) || strings.Contains(name, input)
},
},
}
}
func (mp *issueProject) AttachController(controller internal.Controller) {
mp.controller = controller
}
func (mp *issueProject) Render() (int, string, error) {
if mp.controller == nil {
return -1, "", fmt.Errorf("the menu [controller] cannot be nil")
}
i, _, err := mp.Run()
if err != nil {
return mp.previous.Render()
}
switch i {
case PreviousSelected:
mp.next = mp.previous
}
// @TODO - Search within
return mp.next.Render()
}
|
The pages are generated with Golds v0.3.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |