Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add selection prompt #33

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/atotto/clipboard v0.1.2
github.com/charmbracelet/bubbletea v0.12.2
github.com/mattn/go-runewidth v0.0.9
github.com/muesli/reflow v0.2.1-0.20201126184510-3bcb929042f2
github.com/muesli/termenv v0.7.4
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/muesli/reflow v0.2.1-0.20201126184510-3bcb929042f2 h1:+cpkcmASpeBn4qXz2tU+x+njyKe91NoHXqrJdhoDnZo=
github.com/muesli/reflow v0.2.1-0.20201126184510-3bcb929042f2/go.mod h1:qT22vjVmM9MIUeLgsVYe/Ye7eZlbv9dZjL3dVhUqLX8=
github.com/muesli/termenv v0.7.2 h1:r1raklL3uKE7rOvWgSenmEm2px+dnc33OTisZ8YR1fw=
github.com/muesli/termenv v0.7.2/go.mod h1:ct2L5N2lmix82RaY3bMWwVu/jUFc9Ule0KGDCiKYPh8=
github.com/muesli/termenv v0.7.4 h1:/pBqvU5CpkY53tU0vVn+xgs2ZTX63aH5nY+SSps5Xa8=
Expand Down
60 changes: 60 additions & 0 deletions selection/choice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package selection

import (
"fmt"
"reflect"
)

// Choice represents a single choice. This type used as an input
// for the selection prompt, for filtering and as a result value.
// The index is populated by the prompt itself and is exported
// to be accessed when filtering.
type Choice struct {
Index int
String string
Value interface{}
}

// NewChoice creates a new choice for a given input and chooses
// a suitable string representation. The index is left at 0 to
// be populated by the selection prompt later on.
func NewChoice(item interface{}) *Choice {
choice := &Choice{Index: 0, Value: item}

switch i := item.(type) {
case Choice:
choice.String = i.String
choice.Value = i.Value
case *Choice:
choice.String = i.String
choice.Value = i.Value
case string:
choice.String = i
case fmt.Stringer:
choice.String = i.String()
default:
choice.String = fmt.Sprintf("%+v", i)
}

return choice
}

// Choices converts a slice of anything to a slice of choices.
// Choices panics if the input is not a slice.
func Choices(sliceChoices interface{}) []*Choice {
switch reflect.TypeOf(sliceChoices).Kind() {
case reflect.Slice:
slice := reflect.ValueOf(sliceChoices)

choices := make([]*Choice, 0, slice.Len())

for i := 0; i < slice.Len(); i++ {
value := slice.Index(i).Interface()
choices = append(choices, NewChoice(value))
}

return choices
default:
panic("SliceChoices argument is not a slice")
}
}
53 changes: 53 additions & 0 deletions selection/keymap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package selection

// DefaultKeyMap is a KeyMap with sensible default key mappings
// that can also be used as a starting point for customization.
var DefaultKeyMap = KeyMap{
Down: []string{"down"},
Up: []string{"up"},
Select: []string{"enter"},
Abort: []string{"ctrl+c"},
ClearFilter: []string{"esc"},
ScrollDown: []string{"pgdown", "right"},
ScrollUp: []string{"pgup", "left"},
}

// KeyMap defines the keys that trigger certain actions.
type KeyMap struct {
Down []string
Up []string
Select []string
Abort []string
ClearFilter []string
ScrollDown []string
ScrollUp []string
}

func keyMatches(key string, mapping []string) bool {
for _, m := range mapping {
if m == key {
return true
}
}

return false
}

// validateKeyMap returns true if the given key map contains at
// least the bare minimum set of key bindings for the functional
// prompt and false otherwise.
func validateKeyMap(km KeyMap) bool {
if len(km.Up) == 0 {
return false
}

if len(km.Down) == 0 {
return false
}

if len(km.Select) == 0 {
return false
}

return true
}
Loading