Skip to content

Commit

Permalink
Merge pull request #24 from orbatschow/feature/default-selection-current
Browse files Browse the repository at this point in the history
  • Loading branch information
orbatschow authored Apr 26, 2023
2 parents 25079ec + b5a4af1 commit d446469
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 19 deletions.
2 changes: 2 additions & 0 deletions example/kontext.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ group:
default: kind-kind
selection:
# make this context the default selected option within the interactive dialogue
# note: you can use "-", as a value, to use the active context as the new selection default
default: kind-kind
# sort the interactive selection
# possible values, defaults to asc:
Expand Down Expand Up @@ -75,6 +76,7 @@ group:
# interactive selection options for all groups
selection:
# make this group the default selected group
# note: you can use "-", as a value, to use the active group as the new selection default
default: dev
# sort the interactive selection
# possible values, defaults to asc:
Expand Down
25 changes: 15 additions & 10 deletions pkg/context/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,25 @@ func (c *Client) buildInteractiveSelectPrinter() (*pterm.InteractiveSelectPrinte
sort.Strings(keys)
}

// set the default selection option
if len(group.Context.Selection.Default) > 0 {
selector := pterm.DefaultInteractiveSelect.
WithMaxHeight(MaxSelectHeight).
WithOptions(keys)

// check if there are defaults for the selection and set them accordingly
switch group.Context.Selection.Default {
// if the default is empty, return without setting default option
case "":
return selector, nil
// if the default select is "-", set the current context as the default option
case "-":
return selector.WithDefaultOption(c.State.Context.Active), nil
// search for the given default selection context
default:
// get the default selection context
_, ok := c.APIConfig.Contexts[group.Context.Selection.Default]
if !ok {
return nil, fmt.Errorf("could not find default selection context: '%s'", group.Context.Selection.Default)
}

return pterm.DefaultInteractiveSelect.
WithMaxHeight(MaxSelectHeight).
WithOptions(keys).
WithDefaultOption(group.Context.Selection.Default), nil
return selector.WithDefaultOption(group.Context.Selection.Default), nil
}
return pterm.DefaultInteractiveSelect.
WithMaxHeight(MaxSelectHeight).
WithOptions(keys), nil
}
58 changes: 58 additions & 0 deletions pkg/context/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,64 @@ func Test_buildInteractiveSelectPrinter(t *testing.T) {
},
wantErr: false,
},
{
name: "should return a printer, that sets the default selection to the current context",
args: args{
APIConfig: &api.Config{
Contexts: map[string]*api.Context{
"kind-b": nil,
"kind-c": nil,
"kind-a": nil,
},
CurrentContext: "kind-b",
},
Config: &config.Config{
Group: config.Group{
Items: []config.GroupItem{
{
Name: "group-a",
Context: config.Context{
Selection: config.Selection{
Default: "-",
},
},
},
},
},
},
State: &state.State{
Group: state.Group{
Active: "group-a",
},
Context: state.Context{
Active: "kind-b",
},
},
},
want: &pterm.InteractiveSelectPrinter{
TextStyle: &pterm.Style{
pterm.FgLightCyan,
},
DefaultText: "Please select an option",
Options: []string{
"kind-a",
"kind-b",
"kind-c",
},
OptionStyle: &pterm.Style{
pterm.FgDefault,
pterm.BgDefault,
},
DefaultOption: "kind-b",
MaxHeight: MaxSelectHeight,
Selector: ">",
SelectorStyle: &pterm.Style{
pterm.FgLightMagenta,
},
},
wantErr: false,
},

{
name: "should return an error, as the default selection context does not exist",
args: args{
Expand Down
24 changes: 15 additions & 9 deletions pkg/group/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,20 @@ func (c *Client) buildInteractiveSelectPrinter() (*pterm.InteractiveSelectPrinte
sort.Sort(sort.Reverse(sort.StringSlice(keys)))
}

// set the default selection option
if len(c.Config.Group.Selection.Default) > 0 {
selector := pterm.DefaultInteractiveSelect.
WithMaxHeight(MaxSelectHeight).
WithOptions(keys)

// check if there are defaults for the selection and set them accordingly
switch c.Config.Group.Selection.Default {
// if the default is empty, return without setting default option
case "":
return selector, nil
// if the default select is "-", set the current group as the default selection option
case "-":
return selector.WithDefaultOption(c.State.Group.Active), nil
// search for the given default selection group
default:
// get the default selection group
_, ok := lo.Find(c.Config.Group.Items, func(item config.GroupItem) bool {
return item.Name == c.Config.Group.Selection.Default
Expand All @@ -35,12 +47,6 @@ func (c *Client) buildInteractiveSelectPrinter() (*pterm.InteractiveSelectPrinte
return nil, fmt.Errorf("could not find default selection group: '%s'", c.State.Group.Active)
}

return pterm.DefaultInteractiveSelect.
WithMaxHeight(MaxSelectHeight).
WithOptions(keys).
WithDefaultOption(c.Config.Group.Selection.Default), nil
return selector.WithDefaultOption(c.Config.Group.Selection.Default), nil
}
return pterm.DefaultInteractiveSelect.
WithMaxHeight(MaxSelectHeight).
WithOptions(keys), nil
}
50 changes: 50 additions & 0 deletions pkg/group/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,56 @@ func Test_buildInteractiveSelectPrinter(t *testing.T) {
},
wantErr: false,
},
{
name: "should return a printer, that sets the default selection to the active group",
args: args{
Config: &config.Config{
Group: config.Group{
Selection: config.Selection{
Default: "-",
},
Items: []config.GroupItem{
{
Name: "group-a",
},
{
Name: "group-b",
},
{
Name: "group-c",
},
},
},
},
State: &state.State{
Group: state.Group{
Active: "group-b",
},
},
},
want: &pterm.InteractiveSelectPrinter{
TextStyle: &pterm.Style{
pterm.FgLightCyan,
},
DefaultText: "Please select an option",
Options: []string{
"group-a",
"group-b",
"group-c",
},
OptionStyle: &pterm.Style{
pterm.FgDefault,
pterm.BgDefault,
},
DefaultOption: "group-b",
MaxHeight: MaxSelectHeight,
Selector: ">",
SelectorStyle: &pterm.Style{
pterm.FgLightMagenta,
},
},
wantErr: false,
},
{
name: "should throw an error, as the default selection group does not exist",
args: args{
Expand Down

0 comments on commit d446469

Please sign in to comment.