-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
4a27669
commit d21994b
Showing
7 changed files
with
295 additions
and
127 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 |
---|---|---|
@@ -1,18 +1,27 @@ | ||
import { createSelection } from './src' | ||
import { createSelection, createPrompt } from './src' | ||
|
||
const result = createSelection([ | ||
{ text: 'feat', description: 'Introducing new features' }, | ||
{ text: 'fix', description: 'Bug fix' }, | ||
{ text: 'docs', description: 'Writing docs' }, | ||
{ text: 'style', description: 'Improving structure/format of the code' }, | ||
{ text: 'refactor', description: 'Refactoring code' }, | ||
{ text: 'test', description: 'Refactoring code' }, | ||
{ text: 'chore', description: 'When adding missing tests' }, | ||
{ text: 'perf', description: 'Improving performance' } | ||
], { | ||
headerText: 'Select Commit Type: ', | ||
perPage: 5, | ||
footerText: 'Footer here' | ||
}) | ||
// const result = createSelection([ | ||
// { text: 'feat', description: 'Introducing new features' }, | ||
// { text: 'fix', description: 'Bug fix' }, | ||
// { text: 'docs', description: 'Writing docs' }, | ||
// { text: 'style', description: 'Improving structure/format of the code' }, | ||
// { text: 'refactor', description: 'Refactoring code' }, | ||
// { text: 'test', description: 'Refactoring code' }, | ||
// { text: 'chore', description: 'When adding missing tests' }, | ||
// { text: 'perf', description: 'Improving performance' } | ||
// ], { | ||
// headerText: 'Select Commit Type: ', | ||
// perPage: 5, | ||
// footerText: 'Footer here' | ||
// }) | ||
|
||
// console.log(result) | ||
const result = createPrompt("Enter anything: ") | ||
|
||
console.log(result) | ||
|
||
const result2 = createPrompt("Enter password: ", { | ||
echoMode: 'none' | ||
}) | ||
|
||
console.log(result2) |
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,85 @@ | ||
package prompt | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/mritd/bubbles/common" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/mritd/bubbles/prompt" | ||
) | ||
|
||
type model struct { | ||
input *prompt.Model | ||
} | ||
|
||
func (m model) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
// By default, the prompt component will not return a "tea.Quit" | ||
// message unless Ctrl+C is pressed. | ||
// | ||
// If there is no error in the input, the prompt component returns | ||
// a "common.DONE" message when the Enter key is pressed. | ||
switch msg { | ||
case common.DONE: | ||
return m, tea.Quit | ||
} | ||
|
||
_, cmd := m.input.Update(msg) | ||
return m, cmd | ||
} | ||
|
||
func (m model) View() string { | ||
return m.input.View() | ||
} | ||
|
||
func (m model) Value() string { | ||
return m.input.Value() | ||
} | ||
|
||
type Result struct { | ||
Value string `json:"value"` | ||
Error string `json:"error"` | ||
} | ||
|
||
func Prompt(promptText, echoMode string, required bool, charLimit int) string { | ||
m := model{input: &prompt.Model{ | ||
ValidateFunc: prompt.VFNotBlank, | ||
Prompt: promptText, | ||
CharLimit: charLimit, | ||
EchoMode: prompt.EchoNormal, | ||
}} | ||
|
||
if echoMode == "none" { | ||
m.input.EchoMode = prompt.EchoNone | ||
} else if echoMode == "password" { | ||
m.input.EchoMode = prompt.EchoPassword | ||
} else { | ||
m.input.EchoMode = prompt.EchoNormal | ||
} | ||
|
||
if required { | ||
m.input.ValidateFunc = prompt.VFNotBlank | ||
} else { | ||
m.input.ValidateFunc = prompt.VFDoNothing | ||
} | ||
|
||
p := tea.NewProgram(&m) | ||
err := p.Start() | ||
if err != nil { | ||
result, _ := json.Marshal(&Result{ | ||
Value: m.Value(), | ||
Error: fmt.Sprintf("%s", err), | ||
}) | ||
return string(result) | ||
} | ||
result, _ := json.Marshal(&Result{ | ||
Value: m.Value(), | ||
Error: "", | ||
}) | ||
return string(result) | ||
} |
Oops, something went wrong.