Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
add tip for more form item
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 11, 2022
1 parent feea2fd commit ef1d460
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 86 deletions.
60 changes: 60 additions & 0 deletions tui/form/button.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package form

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"gohost/tui/styles"
)

func NewButton(text string) *Button {
return &Button{
Text: text,
focused: false,
focusedStyle: styles.None,
unfocusedStyle: styles.None,
}
}

type Button struct {
Text string
focused bool
focusedStyle lipgloss.Style
unfocusedStyle lipgloss.Style
}

func (b *Button) Init() tea.Cmd {
return nil
}

func (b *Button) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return b, nil
}

func (b *Button) View() string {
if b.focused {
return b.focusedStyle.Render(b.Text)
}
return b.unfocusedStyle.Render(b.Text)
}

func (b *Button) Focus(mode FocusMode) tea.Cmd {
b.focused = true
return nil
}

func (b *Button) Unfocus() tea.Cmd {
b.focused = false
return nil
}

func (b *Button) InterceptKey(m tea.KeyMsg) bool {
return false
}

func (b *Button) SetFocusedStyle(style lipgloss.Style) {
b.focusedStyle = style
}

func (b *Button) SetUnfocusedStyle(style lipgloss.Style) {
b.unfocusedStyle = style
}
32 changes: 2 additions & 30 deletions tui/form/choice.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ var _ Item = (*Choices)(nil)
func NewChoice(items []list.DefaultItem) *Choices {
return &Choices{
items: items,
SelectedPrefix: "[v]",
UnselectedPrefix: "[ ]",
SelectedPrefix: "(v) ",
UnselectedPrefix: "( ) ",
MorePlaceHold: "...",
ShowMorePlaceHold: true,
width: 0,
height: 0,
Spacing: 1,
focused: false,
cursorIndex: -1,
Expand All @@ -45,7 +43,6 @@ type Choices struct {
focused bool
focusedStyle lipgloss.Style
unfocusedStyle lipgloss.Style
width, height int
Spacing int
cursorIndex int
selectedIndex int
Expand Down Expand Up @@ -78,14 +75,6 @@ func (c *Choices) View() string {
return b.String()
}

func (c *Choices) Width() int {
return c.width
}

func (c *Choices) Height() int {
return c.height
}

func (c *Choices) Init() tea.Cmd {
return nil
}
Expand All @@ -94,9 +83,6 @@ func (c *Choices) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch m := msg.(type) {
case tea.WindowSizeMsg:
c.SetHeight(m.Height)
c.SetWidth(m.Width)
case tea.KeyMsg:
switch {
case key.Matches(m, keys.Up):
Expand Down Expand Up @@ -149,20 +135,6 @@ func (c *Choices) InterceptKey(keyMsg tea.KeyMsg) bool {
return false
}

func (c *Choices) SetWidth(width int) {
c.width = width
}

func (c *Choices) SetHeight(height int) {
c.height = height
ah := len(c.items) + len(c.items)*c.Spacing - 1
if ah < height {
c.height = ah
} else {
c.height = height
}
}

func (c *Choices) itemTitle(idx int) string {
if idx == c.selectedIndex {
return c.SelectedPrefix + c.items[idx].Title()
Expand Down
35 changes: 22 additions & 13 deletions tui/form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ import (
)

func New() *Form {
vp := viewport.New(0, 0)
return &Form{
Items: make([]Item, 0),
ItemFocusedStyle: styles.None,
ItemUnfocusedStyle: styles.None,
MorePlaceHold: "...",
Spacing: 0,
viewport: viewport.New(0, 0),
viewport: vp,
preFocus: 0,
focus: 0,
width: 0,
height: 0,
}
}

Expand All @@ -32,8 +35,7 @@ type Form struct {
viewport viewport.Model
preFocus int
focus int
width int
height int
width, height int
}

func (v *Form) Init() tea.Cmd {
Expand All @@ -45,7 +47,10 @@ func (v *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch m := msg.(type) {
case tea.WindowSizeMsg:
v.SetSize(m.Width, m.Height)
v.width = m.Width
v.height = m.Height
v.viewport.Width = v.width
v.viewport.Height = v.height - 1
return v, nil
case tea.KeyMsg:
focusedItem := v.Items[v.focus]
Expand Down Expand Up @@ -93,17 +98,21 @@ func (v *Form) View() string {
}
}
v.viewport.SetContent(b.String())
return v.viewport.View()
}

func (v *Form) SetSize(width, height int) {
v.width = width
v.height = height
for _, item := range v.Items {
item.SetWidth(width)
v.viewport.Width = width
v.viewport.Height = height
b = strings.Builder{}
if !v.viewport.AtBottom() {
b.WriteString(lipgloss.NewStyle().Width(v.width).Height(v.height - 1).Render(v.viewport.View()))
b.WriteString(cfg.LineBreak)
if len(v.MorePlaceHold) > v.width {
b.WriteString(v.MorePlaceHold[:v.width])
} else {
b.WriteString(v.MorePlaceHold)
b.WriteString(strings.Repeat(" ", v.width-len(v.MorePlaceHold)))
}
} else {
b.WriteString(lipgloss.NewStyle().Width(v.width).Height(v.height).Render(v.viewport.View()))
}
return b.String()
}

func (v *Form) AddItem(widget Item) {
Expand Down
4 changes: 0 additions & 4 deletions tui/form/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ type Item interface {
tea.Model
Focus(mode FocusMode) tea.Cmd
Unfocus() tea.Cmd
SetWidth(width int)
SetHeight(height int)
Width() int
Height() int
InterceptKey(m tea.KeyMsg) bool
SetFocusedStyle(style lipgloss.Style)
SetUnfocusedStyle(style lipgloss.Style)
Expand Down
30 changes: 0 additions & 30 deletions tui/form/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ var _ Item = (*TextInput)(nil)
func NewTextInput() *TextInput {
t := &TextInput{
Model: textinput.New(),
width: 0,
height: 1,
focusedStyle: styles.None,
unfocusedStyle: styles.None,
}
Expand All @@ -23,7 +21,6 @@ func NewTextInput() *TextInput {

type TextInput struct {
textinput.Model
width, height int
focused bool
focusedStyle lipgloss.Style
unfocusedStyle lipgloss.Style
Expand All @@ -37,30 +34,6 @@ func (t *TextInput) SetUnfocusedStyle(style lipgloss.Style) {
t.unfocusedStyle = style
}

func (t *TextInput) Width() int {
return t.Model.Width
}

func (t *TextInput) Height() int {
if t.focused {
return 1 + t.focusedStyle.GetHeight()
}
return 1 + t.unfocusedStyle.GetHeight()
}

func (t *TextInput) SetWidth(width int) {
t.Model.Width = width - len(t.Prompt) - 1
t.width = width
}

func (t *TextInput) SetHeight(height int) {
if height > 0 {
t.height = 1
} else {
t.height = 0
}
}

func (t *TextInput) Init() tea.Cmd {
return nil
}
Expand Down Expand Up @@ -91,9 +64,6 @@ func (t *TextInput) InterceptKey(keyMsg tea.KeyMsg) bool {
}

func (t *TextInput) View() string {
if t.height <= 0 {
return ""
}
if t.focused {
return t.focusedStyle.Render(t.Model.View())
}
Expand Down
28 changes: 19 additions & 9 deletions tui/node_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ import (
type NodeView struct {
model *Model
*form.Form
//nodeTypeChoices *form.Choices
nameTextInput *form.TextInput
descTextInput *form.TextInput
urlTextInput *form.TextInput
typeChoices *form.Choices
}

func NewNodeView(model *Model) *NodeView {
// Text inputs
nodeNameTextInput := form.NewTextInput()
nodeNameTextInput.Prompt = "Name: "
nodeNameTextInput.Focus(form.FocusFirstMode)
nameTextInput := form.NewTextInput()
nameTextInput.Prompt = "Name: "
nameTextInput.Focus(form.FocusFirstMode)

descTextInput := form.NewTextInput()
descTextInput.Prompt = "Description: "
Expand All @@ -33,18 +36,25 @@ func NewNodeView(model *Model) *NodeView {
nodeTypeChoices.Spacing = 1
nodeTypeChoices.ShowMorePlaceHold = false

// Confirm button
confirmButton := form.NewButton("[ Confirm ] ")

nodeForm := &NodeView{
model: model,
Form: form.New(),
//nodeTypeChoices: nodeTypeChoices,
model: model,
Form: form.New(),
nameTextInput: nameTextInput,
descTextInput: descTextInput,
urlTextInput: urlTextInput,
typeChoices: nodeTypeChoices,
}
nodeForm.Spacing = 1
nodeForm.SetItemFocusedStyle(styles.FocusedFormItem)
nodeForm.SetItemUnfocusedStyle(styles.UnfocusedFormItem)
nodeForm.AddItem(nodeNameTextInput)
nodeForm.AddItem(nameTextInput)
nodeForm.AddItem(descTextInput)
nodeForm.AddItem(urlTextInput)
nodeForm.AddItem(nodeTypeChoices)
nodeForm.AddItem(confirmButton)

return nodeForm
}
Expand All @@ -59,7 +69,7 @@ func (v *NodeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch m := msg.(type) {
case tea.WindowSizeMsg:
v.SetSize(m.Width, m.Height)
_, cmd = v.Form.Update(msg)
log.Debug(fmt.Sprintf("node view w %d h %d", m.Width, m.Height))
case tea.KeyMsg:
if v.model.state == nodeViewState {
Expand Down

0 comments on commit ef1d460

Please sign in to comment.