Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
Eun committed Mar 9, 2023
1 parent e1bbe64 commit e14e86f
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 100 deletions.
13 changes: 8 additions & 5 deletions button/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ type View struct {
}

func (m *View) Init() tea.Cmd {
return nil
return tea.ClearScreen
}

func (m *View) Update(msg tea.Msg) tea.Cmd {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter, tea.KeyEsc:
return m.respond()
case tea.KeyEnter:
return m.respond(nil)
case tea.KeyEsc:
return m.respond(bubbleviews.EscPressedError{})
}
case tea.WindowSizeMsg:
m.width = msg.Width
Expand Down Expand Up @@ -89,10 +91,11 @@ func (m *View) FocusStyle() lipgloss.Style {
return m.focusStyle
}

func (m *View) respond() func() tea.Msg {
func (m *View) respond(err error) func() tea.Msg {
return func() tea.Msg {
return &Response{
model: m,
view: m,
Error: err,
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions button/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

var _ bubbleviews.ResponseMessage = &Response{}

type Response struct {
model *View
view *View
Error error
}

func (r *Response) View() bubbleviews.View {
return r.model
return r.view
}

func (r *Response) OnResponse(msg bubbleviews.ResponseMessage) tea.Cmd {
response, ok := msg.(*Response)
if !ok || r.model.OnResponse == nil {
if !ok || r.view.OnResponse == nil {
return nil
}
return r.model.OnResponse(response)
return r.view.OnResponse(response)
}
24 changes: 13 additions & 11 deletions entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,43 @@ type View struct {

ext.PrefixExt
ext.SuffixExt
textinput.Model
TextInput textinput.Model
}

func (m *View) Init() tea.Cmd {
m.SetValue("")
return textinput.Blink
return tea.Batch(
tea.ClearScreen,
textinput.Blink,
)
}

func (m *View) Update(msg tea.Msg) tea.Cmd {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.Model.Width = msg.Width
m.TextInput.Width = msg.Width
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEsc:
return m.respond(nil, nil)
return m.respond(nil, bubbleviews.EscPressedError{})
case tea.KeyEnter:
v := m.Model.Value()
v := m.TextInput.Value()
return m.respond(&v, nil)
}
}

var cmd tea.Cmd
m.Model, cmd = m.Model.Update(msg)
m.TextInput, cmd = m.TextInput.Update(msg)
return cmd
}

func (m *View) View() string {
return m.RenderPrefix(m.Model.Width) + m.Model.View() + m.RenderSuffix(m.Model.Width)
return m.RenderPrefix(m.TextInput.Width) + m.TextInput.View() + m.RenderSuffix(m.TextInput.Width)
}

func (m *View) respond(text *string, err error) func() tea.Msg {
return func() tea.Msg {
return &Response{
model: m,
view: m,
Text: text,
Error: err,
}
Expand All @@ -57,7 +59,7 @@ func (m *View) respond(text *string, err error) func() tea.Msg {

func New() *View {
var m View
m.Model = textinput.New()
m.Model.Focus()
m.TextInput = textinput.New()
m.TextInput.Focus()
return &m
}
10 changes: 6 additions & 4 deletions entry/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

var _ bubbleviews.ResponseMessage = &Response{}

type Response struct {
model *View
view *View
Text *string
Error error
}

func (r *Response) View() bubbleviews.View {
return r.model
return r.view
}

func (r *Response) OnResponse(msg bubbleviews.ResponseMessage) tea.Cmd {
response, ok := msg.(*Response)
if !ok || r.model.OnResponse == nil {
if !ok || r.view.OnResponse == nil {
return nil
}
return r.model.OnResponse(response)
return r.view.OnResponse(response)
}
5 changes: 5 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bubbleviews

type EscPressedError struct{}

func (EscPressedError) Error() string { return "esc was pressed" }
27 changes: 14 additions & 13 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/Eun/bubbleviews/example/views/selectview"
"github.com/Eun/bubbleviews/loginform"
"github.com/Eun/bubbleviews/message"
"github.com/Eun/bubbleviews/spinner"
"github.com/Eun/bubbleviews/spinnerv"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/sanity-io/litter"
Expand All @@ -35,9 +35,8 @@ type TUI struct {
currentModel bubbleviews.View
quitting bool

selectView *selectview.View
width int
height int
width int
height int
}

func (tui *TUI) Init() tea.Cmd {
Expand Down Expand Up @@ -96,14 +95,12 @@ func (tui *TUI) handleResponse(response interface{}) tea.Cmd {
msg.SetSuffixStyle(escStyle)
msg.SetSuffix("(esc to go back)")
msg.OnResponse = func(response *message.Response) tea.Cmd {
return tui.showView(tui.selectView)
return tui.showView(tui.rootView())
}
return tui.showView(msg)
}

func NewTUI() (*TUI, error) { //nolint: unparam // allow nil error
var tui TUI

func (tui *TUI) rootView() bubbleviews.View {
// message view
msgView := message.New("")
data, _ := f.ReadFile("main.go")
Expand Down Expand Up @@ -148,7 +145,7 @@ func NewTUI() (*TUI, error) { //nolint: unparam // allow nil error
}

// spinner view
spinnerView := spinner.New(" Loading...", func(ctx context.Context, spinner *spinner.View) error {
spinnerView := spinnerv.New(" Loading...", func(ctx context.Context, spinner *spinnerv.View) error {
style := lipgloss.NewStyle().Foreground(lipgloss.Color("170"))
for i := 5; i >= 0; i-- {
select {
Expand All @@ -166,25 +163,29 @@ func NewTUI() (*TUI, error) { //nolint: unparam // allow nil error
spinnerView.SetSuffixStyle(escStyle)
spinnerView.SetSpinnerStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("170")))
spinnerView.SetAllowEscapeKey(true)
spinnerView.OnResponse = func(response *spinner.Response) tea.Cmd {
spinnerView.OnResponse = func(response *spinnerv.Response) tea.Cmd {
return tui.handleResponse(response)
}

tui.selectView = selectview.New(
rootView := selectview.New(
msgView,
buttonView,
entryView,
loginFormView,
spinnerView,
)
tui.selectView.OnResponse = func(response *selectview.Response) tea.Cmd {
rootView.OnResponse = func(response *selectview.Response) tea.Cmd {
if response.SelectedView == nil {
return nil
}
return tui.showView(response.SelectedView)
}
tui.currentModel = tui.selectView
return rootView
}

func NewTUI() (*TUI, error) { //nolint: unparam // allow nil error
var tui TUI
tui.showView(tui.rootView())
return &tui, nil
}

Expand Down
52 changes: 24 additions & 28 deletions loginform/login_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,8 @@ type View struct {
}

func (m *View) Init() tea.Cmd {
// set focus
m.currentFocus = m.EntryUsername
m.EntryUsername.Focus()
m.EntryPassword.Blur()
m.BtnOk.Blur()
m.BtnCancel.Blur()

// reset values
m.EntryUsername.SetValue("")
m.EntryPassword.SetValue("")

return tea.Batch(
tea.ClearScreen,
m.EntryUsername.Init(),
m.EntryPassword.Init(),
m.BtnOk.Init(),
Expand All @@ -61,11 +51,11 @@ func (m *View) Update(msg tea.Msg) tea.Cmd {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEsc:
return m.respond(nil, nil, nil)
return m.respond(nil, nil, bubbleviews.EscPressedError{})
case tea.KeyEnter:
if m.currentFocus == m.BtnOk || (m.currentFocus == m.EntryPassword && !m.showOK) {
user := m.EntryUsername.Value()
pass := m.EntryPassword.Value()
user := m.EntryUsername.TextInput.Value()
pass := m.EntryPassword.TextInput.Value()
return m.respond(&user, &pass, nil)
}
if m.currentFocus == m.BtnCancel {
Expand Down Expand Up @@ -139,7 +129,7 @@ func (m *View) ShowCancel() bool {
func (m *View) respond(username, password *string, err error) func() tea.Msg {
return func() tea.Msg {
return &Response{
model: m,
view: m,
Username: username,
Password: password,
Error: err,
Expand All @@ -150,11 +140,11 @@ func (m *View) respond(username, password *string, err error) func() tea.Msg {
func (m *View) focusNext() {
switch m.currentFocus {
case m.EntryUsername:
m.EntryUsername.Blur()
m.EntryPassword.Focus()
m.EntryUsername.TextInput.Blur()
m.EntryPassword.TextInput.Focus()
m.currentFocus = m.EntryPassword
case m.EntryPassword:
m.EntryPassword.Blur()
m.EntryPassword.TextInput.Blur()
if m.showOK {
m.BtnOk.Focus()
m.currentFocus = m.BtnOk
Expand All @@ -165,7 +155,7 @@ func (m *View) focusNext() {
m.currentFocus = m.BtnCancel
break
}
m.EntryUsername.Focus()
m.EntryUsername.TextInput.Focus()
m.currentFocus = m.EntryUsername
case m.BtnOk:
m.BtnOk.Blur()
Expand All @@ -174,19 +164,19 @@ func (m *View) focusNext() {
m.currentFocus = m.BtnCancel
break
}
m.EntryUsername.Focus()
m.EntryUsername.TextInput.Focus()
m.currentFocus = m.EntryUsername
case m.BtnCancel:
m.BtnCancel.Blur()
m.EntryUsername.Focus()
m.EntryUsername.TextInput.Focus()
m.currentFocus = m.EntryUsername
}
}

func (m *View) focusPrevious() {
switch m.currentFocus {
case m.EntryUsername:
m.EntryUsername.Blur()
m.EntryUsername.TextInput.Blur()
if m.showCancel {
m.BtnCancel.Focus()
m.currentFocus = m.BtnCancel
Expand All @@ -197,15 +187,15 @@ func (m *View) focusPrevious() {
m.currentFocus = m.BtnOk
break
}
m.EntryPassword.Focus()
m.EntryPassword.TextInput.Focus()
m.currentFocus = m.EntryPassword
case m.EntryPassword:
m.EntryPassword.Blur()
m.EntryUsername.Focus()
m.EntryPassword.TextInput.Blur()
m.EntryUsername.TextInput.Focus()
m.currentFocus = m.EntryUsername
case m.BtnOk:
m.BtnOk.Blur()
m.EntryPassword.Focus()
m.EntryPassword.TextInput.Focus()
m.currentFocus = m.EntryPassword
case m.BtnCancel:
m.BtnCancel.Blur()
Expand All @@ -214,7 +204,7 @@ func (m *View) focusPrevious() {
m.currentFocus = m.BtnOk
break
}
m.EntryPassword.Focus()
m.EntryPassword.TextInput.Focus()
m.currentFocus = m.EntryPassword
}
}
Expand All @@ -226,10 +216,16 @@ func New() *View {
m.EntryUsername.SetPrefix("Username")
m.EntryPassword = entry.New()
m.EntryPassword.SetPrefix("Password")
m.EntryPassword.EchoMode = textinput.EchoPassword
m.EntryPassword.TextInput.EchoMode = textinput.EchoPassword

m.BtnOk = button.New("OK")
m.BtnCancel = button.New("Cancel")

m.currentFocus = m.EntryUsername
m.EntryUsername.TextInput.Focus()
m.EntryPassword.TextInput.Blur()
m.BtnOk.Blur()
m.BtnCancel.Blur()

return &m
}
Loading

0 comments on commit e14e86f

Please sign in to comment.