-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from kiyonlin/master
🚨 Change project name
- Loading branch information
Showing
11 changed files
with
261 additions
and
16 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,114 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
|
||
input "github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/containerd/console" | ||
) | ||
|
||
type errMsg error | ||
|
||
type Prompt struct { | ||
p *tea.Program | ||
textInput input.Model | ||
err error | ||
title string | ||
answer string | ||
} | ||
|
||
func NewPrompt(title string, placeholder ...string) *Prompt { | ||
p := &Prompt{ | ||
title: title, | ||
textInput: input.NewModel(), | ||
} | ||
|
||
if len(placeholder) > 0 { | ||
p.textInput.Placeholder = placeholder[0] | ||
} | ||
|
||
p.p = tea.NewProgram(p) | ||
|
||
return p | ||
} | ||
|
||
func (p *Prompt) YesOrNo() (bool, error) { | ||
answer, err := p.Answer() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return parseBool(answer), nil | ||
} | ||
|
||
func parseBool(str string) bool { | ||
switch str { | ||
case "1", "t", "T", "true", "TRUE", "True", "y", "Y", "yes", "Yes": | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (p *Prompt) Answer() (result string, err error) { | ||
if err = checkConsole(); err != nil { | ||
return | ||
} | ||
|
||
if err := p.p.Start(); err != nil { | ||
return "", err | ||
} | ||
return p.answer, nil | ||
} | ||
|
||
func (p *Prompt) Init() tea.Cmd { | ||
p.textInput.Focus() | ||
|
||
return input.Blink(p.textInput) | ||
} | ||
|
||
func (p *Prompt) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
|
||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyCtrlC: | ||
fallthrough | ||
case tea.KeyEsc: | ||
fallthrough | ||
case tea.KeyEnter: | ||
p.answer = p.textInput.Value() | ||
return p, tea.Quit | ||
} | ||
|
||
// We handle errors just like any other message | ||
case errMsg: | ||
p.err = msg | ||
return p, nil | ||
} | ||
|
||
p.textInput, cmd = input.Update(msg, p.textInput) | ||
return p, cmd | ||
} | ||
|
||
func (p *Prompt) View() string { | ||
return fmt.Sprintf( | ||
"%s\n\n%s\n\n%s\n\n", | ||
p.title, | ||
input.View(p.textInput), | ||
"(esc to quit)", | ||
) | ||
} | ||
|
||
func checkConsole() (err error) { | ||
defer func() { | ||
if e := recover(); e != nil { | ||
err = fmt.Errorf("%v", e) | ||
} | ||
}() | ||
|
||
console.Current() | ||
|
||
return | ||
} |
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,92 @@ | ||
package internal | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
input "github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Prompt_New(t *testing.T) { | ||
t.Parallel() | ||
|
||
at := assert.New(t) | ||
|
||
p := NewPrompt("title", "placeholder") | ||
|
||
at.NotNil(p) | ||
at.Equal("title", p.title) | ||
at.Equal("placeholder", p.textInput.Placeholder) | ||
at.NotNil(p.p) | ||
} | ||
|
||
func Test_Prompt_Answer(t *testing.T) { | ||
t.Parallel() | ||
|
||
p := NewPrompt("") | ||
_, err := p.Answer() | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func Test_Prompt_YesOrNo(t *testing.T) { | ||
t.Parallel() | ||
|
||
p := NewPrompt("") | ||
_, err := p.YesOrNo() | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func Test_Prompt_ParseBool(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.True(t, parseBool("y")) | ||
assert.False(t, parseBool("")) | ||
} | ||
|
||
func Test_Prompt_Initialize(t *testing.T) { | ||
t.Parallel() | ||
|
||
at := assert.New(t) | ||
|
||
p := NewPrompt("") | ||
cmd := p.Init() | ||
|
||
switch cmd().(type) { | ||
case input.BlinkMsg: | ||
default: | ||
at.Fail("msg should be input.BlankMsg") | ||
} | ||
} | ||
|
||
func Test_Prompt_Update(t *testing.T) { | ||
t.Parallel() | ||
|
||
at := assert.New(t) | ||
|
||
p := NewPrompt("") | ||
|
||
var cmd tea.Cmd | ||
_, cmd = p.Update(nil) | ||
at.Nil(cmd) | ||
|
||
_, cmd = p.Update(errMsg(errors.New("fake error"))) | ||
at.NotNil(p.err) | ||
at.Nil(cmd) | ||
|
||
_, cmd = p.Update(tea.KeyMsg{Type: tea.KeyRune, Rune: 'a'}) | ||
at.Nil(cmd) | ||
|
||
_, cmd = p.Update(tea.KeyMsg{Type: tea.KeyCtrlC}) | ||
at.NotNil(cmd) | ||
} | ||
|
||
func Test_Prompt_View(t *testing.T) { | ||
t.Parallel() | ||
|
||
at := assert.New(t) | ||
p := NewPrompt("") | ||
|
||
at.Contains(p.View(), "esc") | ||
} |
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
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,16 +1,20 @@ | ||
module github.com/gofiber/fiber-cli | ||
module github.com/gofiber/cli | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/charmbracelet/bubbles v0.7.0 | ||
github.com/charmbracelet/bubbletea v0.12.0 | ||
github.com/containerd/console v1.0.1 | ||
github.com/fsnotify/fsnotify v1.4.7 | ||
github.com/jarcoal/httpmock v1.0.6 | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/muesli/termenv v0.7.4 | ||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect | ||
github.com/spf13/cobra v1.0.0 | ||
github.com/stretchr/testify v1.4.0 | ||
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 // indirect | ||
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect | ||
golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7 // indirect | ||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect | ||
gopkg.in/yaml.v2 v2.2.4 // indirect | ||
) |
Oops, something went wrong.