-
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
16 changed files
with
738 additions
and
274 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
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) { | ||
//nolint:gocritic | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.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 @@ | ||
package chezmoibubbles |
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,81 @@ | ||
package chezmoibubbles | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
|
||
"github.com/twpayne/chezmoi/v2/pkg/chezmoi" | ||
) | ||
|
||
type ChoiceInputModel struct { | ||
textInput textinput.Model | ||
abbreviations map[string]string | ||
defaultValue *string | ||
canceled bool | ||
} | ||
|
||
func NewChoiceInputModel(prompt string, choices []string, defaultValue *string) ChoiceInputModel { | ||
textInput := textinput.New() | ||
textInput.Prompt = prompt + "? " | ||
textInput.Placeholder = strings.Join(choices, "/") | ||
if defaultValue != nil { | ||
textInput.Placeholder += ", default " + *defaultValue | ||
} | ||
abbreviations := chezmoi.UniqueAbbreviations(choices) | ||
textInput.Validate = func(s string) error { | ||
if s == "" && defaultValue != nil { | ||
return nil | ||
} | ||
if _, ok := abbreviations[s]; ok { | ||
return nil | ||
} | ||
return errors.New("unknown or ambiguous choice") | ||
} | ||
textInput.Focus() | ||
return ChoiceInputModel{ | ||
textInput: textInput, | ||
abbreviations: abbreviations, | ||
defaultValue: defaultValue, | ||
} | ||
} | ||
|
||
func (m ChoiceInputModel) Canceled() bool { | ||
return m.canceled | ||
} | ||
|
||
func (m ChoiceInputModel) Init() tea.Cmd { | ||
return textinput.Blink | ||
} | ||
|
||
func (m ChoiceInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
//nolint:gocritic | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyCtrlC, tea.KeyEsc: | ||
m.canceled = true | ||
return m, tea.Quit | ||
} | ||
} | ||
var cmd tea.Cmd | ||
m.textInput, cmd = m.textInput.Update(msg) | ||
if _, ok := m.abbreviations[m.textInput.Value()]; ok { | ||
return m, tea.Quit | ||
} | ||
return m, cmd | ||
} | ||
|
||
func (m ChoiceInputModel) Value() string { | ||
value := m.textInput.Value() | ||
if value == "" && m.defaultValue != nil { | ||
return *m.defaultValue | ||
} | ||
return m.abbreviations[value] | ||
} | ||
|
||
func (m ChoiceInputModel) 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,76 @@ | ||
package chezmoibubbles | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
type IntInputModel struct { | ||
textInput textinput.Model | ||
defaultValue *int64 | ||
canceled bool | ||
} | ||
|
||
func NewIntInputModel(prompt string, defaultValue *int64) IntInputModel { | ||
textInput := textinput.New() | ||
textInput.Prompt = prompt + "? " | ||
textInput.Placeholder = "int" | ||
if defaultValue != nil { | ||
textInput.Placeholder += ", default " + strconv.FormatInt(*defaultValue, 10) | ||
} | ||
textInput.Validate = func(value string) error { | ||
if value == "" && defaultValue != nil { | ||
return nil | ||
} | ||
_, err := strconv.ParseInt(value, 10, 64) | ||
return err | ||
} | ||
textInput.Focus() | ||
return IntInputModel{ | ||
textInput: textInput, | ||
defaultValue: defaultValue, | ||
} | ||
} | ||
|
||
func (m IntInputModel) Canceled() bool { | ||
return m.canceled | ||
} | ||
|
||
func (m IntInputModel) Init() tea.Cmd { | ||
return textinput.Blink | ||
} | ||
|
||
func (m IntInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
//nolint:gocritic | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyCtrlC, tea.KeyEsc: | ||
m.canceled = true | ||
return m, tea.Quit | ||
case tea.KeyEnter: | ||
if m.defaultValue != nil { | ||
m.textInput.SetValue(strconv.FormatInt(*m.defaultValue, 10)) | ||
return m, tea.Quit | ||
} | ||
} | ||
} | ||
var cmd tea.Cmd | ||
m.textInput, cmd = m.textInput.Update(msg) | ||
return m, cmd | ||
} | ||
|
||
func (m IntInputModel) Value() int64 { | ||
valueStr := m.textInput.Value() | ||
if valueStr == "" && m.defaultValue != nil { | ||
return *m.defaultValue | ||
} | ||
value, _ := strconv.ParseInt(valueStr, 10, 64) | ||
return value | ||
} | ||
|
||
func (m IntInputModel) 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,55 @@ | ||
package chezmoibubbles | ||
|
||
import ( | ||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
type PasswordInputModel struct { | ||
textInput textinput.Model | ||
canceled bool | ||
} | ||
|
||
func NewPasswordInputModel(prompt string) PasswordInputModel { | ||
textInput := textinput.New() | ||
textInput.Prompt = prompt | ||
textInput.Placeholder = "password" | ||
textInput.EchoMode = textinput.EchoNone | ||
textInput.Focus() | ||
return PasswordInputModel{ | ||
textInput: textInput, | ||
} | ||
} | ||
|
||
func (m PasswordInputModel) Canceled() bool { | ||
return m.canceled | ||
} | ||
|
||
func (m PasswordInputModel) Init() tea.Cmd { | ||
return textinput.Blink | ||
} | ||
|
||
func (m PasswordInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
//nolint:gocritic | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyEnter: | ||
return m, tea.Quit | ||
case tea.KeyCtrlC, tea.KeyEsc: | ||
m.canceled = true | ||
return m, tea.Quit | ||
} | ||
} | ||
var cmd tea.Cmd | ||
m.textInput, cmd = m.textInput.Update(msg) | ||
return m, cmd | ||
} | ||
|
||
func (m PasswordInputModel) Value() string { | ||
return m.textInput.Value() | ||
} | ||
|
||
func (m PasswordInputModel) View() string { | ||
return m.textInput.View() | ||
} |
Oops, something went wrong.