-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,118 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package chezmoibubbles | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
|
||
"github.com/twpayne/chezmoi/v2/pkg/chezmoi" | ||
) | ||
|
||
type BoolInputModel struct { | ||
textInput textinput.Model | ||
defaultValue *bool | ||
canceled bool | ||
} | ||
|
||
func NewBoolInputModel(prompt string, defaultValue *bool) BoolInputModel { | ||
textInput := textinput.New() | ||
textInput.Prompt = prompt + "? " | ||
textInput.Placeholder = "bool" | ||
if defaultValue != nil { | ||
textInput.Placeholder += ", default " + strconv.FormatBool(*defaultValue) | ||
} | ||
textInput.Validate = func(value string) error { | ||
if value == "" && defaultValue != nil { | ||
return nil | ||
} | ||
_, err := chezmoi.ParseBool(value) | ||
return err | ||
} | ||
textInput.Focus() | ||
return BoolInputModel{ | ||
textInput: textInput, | ||
defaultValue: defaultValue, | ||
} | ||
} | ||
|
||
func (m BoolInputModel) Canceled() bool { | ||
return m.canceled | ||
} | ||
|
||
func (m BoolInputModel) Init() tea.Cmd { | ||
return textinput.Blink | ||
} | ||
|
||
func (m BoolInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
if keyMsg, ok := msg.(tea.KeyMsg); ok { | ||
switch keyMsg.Type { | ||
case tea.KeyCtrlC, tea.KeyEsc: | ||
m.canceled = true | ||
return m, tea.Quit | ||
case tea.KeyEnter: | ||
if m.defaultValue != nil { | ||
m.textInput.SetValue(strconv.FormatBool(*m.defaultValue)) | ||
return m, tea.Quit | ||
} | ||
} | ||
} | ||
var cmd tea.Cmd | ||
m.textInput, cmd = m.textInput.Update(msg) | ||
if _, err := chezmoi.ParseBool(m.textInput.Value()); err == nil { | ||
return m, tea.Quit | ||
} | ||
return m, cmd | ||
} | ||
|
||
func (m BoolInputModel) Value() bool { | ||
valueStr := m.textInput.Value() | ||
if valueStr == "" && m.defaultValue != nil { | ||
return *m.defaultValue | ||
} | ||
value, _ := chezmoi.ParseBool(valueStr) | ||
return value | ||
} | ||
|
||
func (m BoolInputModel) View() string { | ||
return m.textInput.View() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package chezmoibubbles | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBoolInputModel(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
defaultValue *bool | ||
input string | ||
expectedCanceled bool | ||
expectedValue bool | ||
}{ | ||
{ | ||
name: "empty_with_default", | ||
defaultValue: boolPtr(true), | ||
input: "\r", | ||
expectedValue: true, | ||
}, | ||
{ | ||
name: "cancel_ctrlc", | ||
input: "\x03", | ||
expectedCanceled: true, | ||
}, | ||
{ | ||
name: "cancel_esc", | ||
input: "\x1b", | ||
expectedCanceled: true, | ||
}, | ||
{ | ||
name: "true", | ||
input: "t", | ||
expectedValue: true, | ||
}, | ||
{ | ||
name: "false", | ||
input: "f", | ||
expectedValue: false, | ||
}, | ||
{ | ||
name: "yes", | ||
input: "y", | ||
expectedValue: true, | ||
}, | ||
{ | ||
name: "no", | ||
input: "n", | ||
expectedValue: false, | ||
}, | ||
{ | ||
name: "one", | ||
input: "1", | ||
expectedValue: true, | ||
}, | ||
{ | ||
name: "zero", | ||
input: "0", | ||
expectedValue: false, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
actualModel := testRunModelWithInput(t, NewBoolInputModel("prompt", tc.defaultValue), tc.input) | ||
assert.Equal(t, tc.expectedCanceled, actualModel.Canceled()) | ||
assert.Equal(t, tc.expectedValue, actualModel.Value()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package chezmoibubbles | ||
|
||
import ( | ||
"testing" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var keyTypes = map[tea.KeyType]struct{}{ | ||
tea.KeyCtrlC: {}, | ||
tea.KeyEnter: {}, | ||
tea.KeyEsc: {}, | ||
} | ||
|
||
//nolint:ireturn,nolintlint | ||
func makeKeyMsg(r rune) tea.Msg { | ||
key := tea.Key{ | ||
Type: tea.KeyRunes, | ||
Runes: []rune{r}, | ||
} | ||
if _, ok := keyTypes[tea.KeyType(r)]; ok { | ||
key = tea.Key{ | ||
Type: tea.KeyType(r), | ||
} | ||
} | ||
return tea.KeyMsg(key) | ||
} | ||
|
||
//nolint:ireturn,nolintlint | ||
func makeKeyMsgs(s string) []tea.Msg { | ||
msgs := make([]tea.Msg, 0, len(s)) | ||
for _, r := range s { | ||
msgs = append(msgs, makeKeyMsg(r)) | ||
} | ||
return msgs | ||
} | ||
|
||
//nolint:ireturn,nolintlint | ||
func testRunModelWithInput[M tea.Model](t *testing.T, model M, input string) M { | ||
t.Helper() | ||
for _, msg := range makeKeyMsgs(input) { | ||
m, _ := model.Update(msg) | ||
var ok bool | ||
model, ok = m.(M) | ||
require.True(t, ok) | ||
} | ||
return model | ||
} | ||
|
||
func boolPtr(b bool) *bool { return &b } | ||
func int64Ptr(i int64) *int64 { return &i } | ||
func stringPtr(s string) *string { return &s } |
Oops, something went wrong.