This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
243 additions
and
67 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,87 @@ | ||
package view | ||
|
||
import ( | ||
tea "github.com/charmbracelet/bubbletea" | ||
"gohost/tui/widget" | ||
"strings" | ||
) | ||
|
||
var _ View = (*BaseView)(nil) | ||
|
||
func New() *BaseView { | ||
return &BaseView{ | ||
widgets: make([]widget.Widget, 0), | ||
preFocus: 0, | ||
focus: 0, | ||
} | ||
} | ||
|
||
type BaseView struct { | ||
widgets []widget.Widget | ||
preFocus int | ||
focus int | ||
} | ||
|
||
func (v *BaseView) Init() tea.Cmd { | ||
panic("implement me") | ||
} | ||
|
||
func (v *BaseView) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
_, cmd := v.widgets[v.focus].Update(msg) | ||
return v, cmd | ||
} | ||
|
||
func (v *BaseView) View() string { | ||
var b strings.Builder | ||
for i := 0; i < len(v.widgets); i++ { | ||
b.WriteString(v.widgets[i].View()) | ||
b.WriteString("\n") | ||
} | ||
return b.String() | ||
} | ||
|
||
func (v *BaseView) AddWidget(widget widget.Widget) { | ||
if widget == nil { | ||
return | ||
} | ||
v.widgets = append(v.widgets, widget) | ||
} | ||
|
||
func (v *BaseView) FocusNextWidget() []tea.Cmd { | ||
return v.setFocusWidget(v.idxAfterFocusWidget()) | ||
} | ||
|
||
func (v *BaseView) FocusPreWidget() []tea.Cmd { | ||
return v.setFocusWidget(v.idxBeforeFocusWidget()) | ||
} | ||
|
||
func (v *BaseView) idxAfterFocusWidget() int { | ||
if v.widgets[v.focus].HasNext() { | ||
return v.focus | ||
} | ||
idx := v.focus + 1 | ||
if idx >= len(v.widgets) { | ||
idx = 0 | ||
} | ||
return idx | ||
} | ||
|
||
func (v *BaseView) idxBeforeFocusWidget() int { | ||
if v.widgets[v.focus].HasPre() { | ||
return v.focus | ||
} | ||
idx := v.focus - 1 | ||
if idx < 0 { | ||
idx = len(v.widgets) - 1 | ||
} | ||
return idx | ||
} | ||
|
||
func (v *BaseView) setFocusWidget(idx int) []tea.Cmd { | ||
v.preFocus = v.focus | ||
v.focus = idx | ||
return []tea.Cmd{ | ||
v.widgets[v.preFocus].Unfocus(), | ||
v.widgets[v.focus].Focus(), | ||
} | ||
} |
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,13 @@ | ||
package view | ||
|
||
import ( | ||
tea "github.com/charmbracelet/bubbletea" | ||
"gohost/tui/widget" | ||
) | ||
|
||
type View interface { | ||
tea.Model | ||
AddWidget(widget widget.Widget) | ||
FocusNextWidget() []tea.Cmd | ||
FocusPreWidget() []tea.Cmd | ||
} |
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,44 @@ | ||
package widget | ||
|
||
import ( | ||
"github.com/charmbracelet/bubbles/list" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
var _ Widget = (*List)(nil) | ||
|
||
func NewList(items []list.Item, delegate list.ItemDelegate, width, height int) *List { | ||
return &List{ | ||
Model: list.New(items, delegate, width, height), | ||
} | ||
} | ||
|
||
type List struct { | ||
list.Model | ||
} | ||
|
||
func (l *List) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (l *List) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
l.Model, cmd = l.Model.Update(msg) | ||
return l, cmd | ||
} | ||
|
||
func (l *List) Focus() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (l *List) Unfocus() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (l *List) HasNext() bool { | ||
return l.Model.Index() != len(l.Model.Items())-1 | ||
} | ||
|
||
func (l *List) HasPre() bool { | ||
return l.Model.Index() != 0 | ||
} |
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,52 @@ | ||
package widget | ||
|
||
import ( | ||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"gohost/tui/styles" | ||
) | ||
|
||
var _ Widget = (*TextInput)(nil) | ||
|
||
func NewTextInput() *TextInput { | ||
t := &TextInput{ | ||
Model: textinput.New(), | ||
} | ||
t.Unfocus() | ||
return t | ||
} | ||
|
||
type TextInput struct { | ||
textinput.Model | ||
} | ||
|
||
func (t *TextInput) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (t *TextInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
t.Model, cmd = t.Model.Update(msg) | ||
return t, cmd | ||
} | ||
|
||
func (t *TextInput) Focus() tea.Cmd { | ||
t.TextStyle = styles.FocusedWidget | ||
t.PromptStyle = styles.FocusedWidget | ||
return t.Model.Focus() | ||
} | ||
|
||
func (t *TextInput) Unfocus() tea.Cmd { | ||
t.TextStyle = styles.None | ||
t.PromptStyle = styles.None | ||
t.Model.Blur() | ||
return nil | ||
} | ||
|
||
func (t *TextInput) HasNext() bool { | ||
return false | ||
} | ||
|
||
func (t *TextInput) HasPre() bool { | ||
return false | ||
} |
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,11 @@ | ||
package widget | ||
|
||
import tea "github.com/charmbracelet/bubbletea" | ||
|
||
type Widget interface { | ||
tea.Model | ||
Focus() tea.Cmd | ||
Unfocus() tea.Cmd | ||
HasNext() bool | ||
HasPre() bool | ||
} |