Skip to content

Commit 181a5c6

Browse files
committed
update
1 parent 91a6faa commit 181a5c6

File tree

8 files changed

+1157
-24
lines changed

8 files changed

+1157
-24
lines changed

confirmation/model_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"testing"
77

88
tea "github.com/charmbracelet/bubbletea"
9-
_ "github.com/cnlubo/myssh/common"
109
"github.com/cnlubo/myssh/confirmation"
1110
"github.com/cnlubo/myssh/test"
1211
"github.com/muesli/termenv"

confirmation/templates.go

+11-13
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,39 @@ package confirmation
44
// arrow.
55

66
const TemplateArrow = `
7-
{{- Bold .Prompt -}}
7+
{{- .Prompt | bold | cyan -}}
88
{{ if .YesSelected -}}
9-
{{- print (Bold " ▸Yes ") " No" -}}
9+
{{- print (bold (yellow " ▸Yes ")) (cyan " No") -}}
1010
{{- else if .NoSelected -}}
11-
{{- print " Yes " (Bold "▸No") -}}
11+
{{- print (cyan " Yes ") (bold (yellow "▸No")) -}}
1212
{{- else -}}
1313
{{- " Yes No" -}}
1414
{{- end -}}
1515
`
16-
17-
// ResultTemplateArrow is the ResultTemplate that matches TemplateArrow.
1816
const ResultTemplateArrow = `
1917
{{- print .Prompt " " -}}
2018
{{- if .FinalValue -}}
21-
{{- Foreground "32" "Yes" -}}
19+
{{- print | cyan (bold "Yes") -}}
2220
{{- else -}}
23-
{{- Foreground "32" "No" -}}
21+
{{- print | cyan (bold "No") -}}
2422
{{- end }}
2523
`
2624

2725
const TemplateYN = `
28-
{{- .Prompt | bold | red -}}
26+
{{- .Prompt | bold | cyan -}}
2927
{{ if .YesSelected -}}
30-
{{- print | red (bold " [Y/") red ("n") red (bold "]") -}}
28+
{{- print (cyan " [") (bold (yellow "Y")) (cyan "/n]") -}}
3129
{{- else if .NoSelected -}}
32-
{{- print | red (bold " [y/N]") -}}
30+
{{- print (cyan " [y/") (bold (yellow "N")) (cyan "]") -}}
3331
{{- else -}}
3432
{{- " [y/n]" -}}
3533
{{- end -}}
3634
`
3735
const ResultTemplateYN = `
38-
{{- .Prompt | bold | green -}}
36+
{{- .Prompt | bold | cyan -}}
3937
{{ if .FinalValue -}}
40-
{{- print | green (bold " [Y/n]") -}}
38+
{{- print | cyan (bold " [Y/n]") -}}
4139
{{- else -}}
42-
{{- print | green (bold " [y/N]") -}}
40+
{{- print | cyan (bold " [y/N]") -}}
4341
{{- end }}
4442
`

myssh/keymg.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,13 @@ func createDefaultSSHKey(env *Environment) error {
105105

106106
input := confirmation.New("Do you want to create default SSHKey",
107107
confirmation.NewValue(true))
108-
input.Template = confirmation.TemplateYN
109-
input.ResultTemplate = confirmation.ResultTemplateYN
110-
input.KeyMap.SelectYes = append(input.KeyMap.SelectYes, "+")
111-
input.KeyMap.SelectNo = append(input.KeyMap.SelectNo, "-")
112-
//input.ExtendedTemplateFuncs = common.ClorFuncMap
108+
//input.Template = confirmation.TemplateYN
109+
//input.ResultTemplate = confirmation.ResultTemplateYN
110+
//input.KeyMap.SelectYes = append(input.KeyMap.SelectYes, "+")
111+
//input.KeyMap.SelectNo = append(input.KeyMap.SelectNo, "-")
113112

114113
confirm, err = input.RunPrompt()
115-
if err != nil {
116-
fmt.Printf("Error: %v\n", err)
117-
os.Exit(1)
118-
}
119-
fmt.Println(confirm)
114+
utils.CheckAndExit(err)
120115
if confirm {
121116
// Create default alias directory
122117
err := os.Mkdir(filepath.Join(keyStorePath, DefaultKey), 0755)
@@ -171,6 +166,8 @@ func createDefaultSSHKey(env *Environment) error {
171166
runHook(ag.Alias, env)
172167
utils.PrintN(utils.Info, fmt.Sprintf("Now using SSH key: [%s]", ag.Alias))
173168

169+
} else {
170+
return errors.New("Exit...")
174171
}
175172
}
176173
return nil

selection/choice.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package selection
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
)
7+
8+
// Choice represents a single choice. This type used as an input
9+
// for the selection prompt, for filtering and as a result value.
10+
// The index is populated by the prompt itself and is exported
11+
// to be accessed when filtering.
12+
type Choice struct {
13+
Index int
14+
String string
15+
Value interface{}
16+
}
17+
18+
// NewChoice creates a new choice for a given input and chooses
19+
// a suitable string representation. The index is left at 0 to
20+
// be populated by the selection prompt later on.
21+
func NewChoice(item interface{}) *Choice {
22+
choice := &Choice{Index: 0, Value: item}
23+
24+
switch i := item.(type) {
25+
case Choice:
26+
choice.String = i.String
27+
choice.Value = i.Value
28+
case *Choice:
29+
choice.String = i.String
30+
choice.Value = i.Value
31+
case string:
32+
choice.String = i
33+
case fmt.Stringer:
34+
choice.String = i.String()
35+
default:
36+
choice.String = fmt.Sprintf("%+v", i)
37+
}
38+
39+
return choice
40+
}
41+
42+
// Choices converts a slice of anything to a slice of choices.
43+
// Choices panics if the input is not a slice.
44+
func Choices(sliceChoices interface{}) []*Choice {
45+
switch reflect.TypeOf(sliceChoices).Kind() {
46+
case reflect.Slice:
47+
slice := reflect.ValueOf(sliceChoices)
48+
49+
choices := make([]*Choice, 0, slice.Len())
50+
51+
for i := 0; i < slice.Len(); i++ {
52+
value := slice.Index(i).Interface()
53+
choices = append(choices, NewChoice(value))
54+
}
55+
56+
return choices
57+
default:
58+
panic("SliceChoices argument is not a slice")
59+
}
60+
}

selection/keymap.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package selection
2+
3+
import (
4+
"fmt"
5+
6+
tea "github.com/charmbracelet/bubbletea"
7+
)
8+
9+
// NewDefaultKeyMap returns a KeyMap with sensible default key mappings that can
10+
// also be used as a starting point for customization.
11+
func NewDefaultKeyMap() *KeyMap {
12+
return &KeyMap{
13+
Down: []string{"down"},
14+
Up: []string{"up"},
15+
Select: []string{"enter"},
16+
Abort: []string{"ctrl+c"},
17+
ClearFilter: []string{"esc"},
18+
ScrollDown: []string{"pgdown"},
19+
ScrollUp: []string{"pgup"},
20+
}
21+
}
22+
23+
// KeyMap defines the keys that trigger certain actions.
24+
type KeyMap struct {
25+
Down []string
26+
Up []string
27+
Select []string
28+
Abort []string
29+
ClearFilter []string
30+
ScrollDown []string
31+
ScrollUp []string
32+
}
33+
34+
func keyMatches(key tea.KeyMsg, mapping []string) bool {
35+
for _, m := range mapping {
36+
if m == key.String() {
37+
return true
38+
}
39+
}
40+
41+
return false
42+
}
43+
44+
// validateKeyMap returns true if the given key map contains at
45+
// least the bare minimum set of key bindings for the functional
46+
// prompt and false otherwise.
47+
func validateKeyMap(km *KeyMap) error {
48+
if len(km.Up) == 0 {
49+
return fmt.Errorf("no up key")
50+
}
51+
52+
if len(km.Down) == 0 {
53+
return fmt.Errorf("no down key")
54+
}
55+
56+
if len(km.Select) == 0 {
57+
return fmt.Errorf("no select key")
58+
}
59+
60+
if len(km.Abort) == 0 {
61+
return fmt.Errorf("no abort key")
62+
}
63+
64+
return nil
65+
}

0 commit comments

Comments
 (0)