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

Commit

Permalink
fix textinput
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 16, 2022
1 parent b390c74 commit 9658402
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 36 deletions.
3 changes: 0 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
)

func main() {
//if err := cmd.Execute(); err != nil {
// panic(err)
//}
if err := log.New("debug.log"); err != nil {
panic(err)
}
Expand Down
30 changes: 7 additions & 23 deletions tui/form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package form

import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"gohost/config"
Expand All @@ -23,14 +22,12 @@ const (
type HideCondition func() bool

func New() *Form {
vp := viewport.New(0, 0)
return &Form{
Items: make([]ItemModel, 0),
ItemFocusedStyle: styles.None,
ItemUnfocusedStyle: styles.None,
MorePlaceHold: "...",
Spacing: 0,
viewport: vp,
preFocus: 0,
focus: 0,
width: 0,
Expand All @@ -44,14 +41,13 @@ type Form struct {
ItemUnfocusedStyle lipgloss.Style
MorePlaceHold string
Spacing int
viewport viewport.Model
preFocus int
focus int
width, height int
}

func (v *Form) Init() tea.Cmd {
return v.viewport.Init()
return nil
}

func (v *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand All @@ -61,8 +57,7 @@ func (v *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.WindowSizeMsg:
v.width = m.Width
v.height = m.Height
v.viewport.Width = v.width
v.viewport.Height = v.height - 1
// TODO add layout for items to adjust size dynamically
return v, nil
case tea.KeyMsg:
focusedItem := v.Items[v.focus]
Expand Down Expand Up @@ -113,22 +108,11 @@ func (v *Form) View() string {
b.WriteString(strings.Repeat(cfg.LineBreak, v.Spacing+1))
}
}
v.viewport.SetContent(b.String())

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()
return lipgloss.NewStyle().
Width(v.width).
Height(v.height).
Align(lipgloss.Top).
Render(b.String())
}

func (v *Form) AddItem(widget ItemModel) {
Expand Down
11 changes: 2 additions & 9 deletions tui/form/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ package form
import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"gohost/tui/styles"
)

var _ ItemModel = (*TextInput)(nil)

func NewTextInput() *TextInput {
t := &TextInput{
CommonItem: NewCommonItem(),
Model: textinput.New(),
focusedStyle: styles.None,
unfocusedStyle: styles.None,
CommonItem: NewCommonItem(),
Model: textinput.New(),
}
t.Unfocus()
return t
Expand All @@ -23,9 +19,6 @@ func NewTextInput() *TextInput {
type TextInput struct {
*CommonItem
textinput.Model
focused bool
focusedStyle lipgloss.Style
unfocusedStyle lipgloss.Style
}

func (t *TextInput) Init() tea.Cmd {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tui/node_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func NewNodeView(model *Model) *NodeView {
// Text inputs
nameTextInput := form.NewTextInput()
nameTextInput.Prompt = "Name: "
nameTextInput.Focus(form.FocusFirstMode)

descTextInput := form.NewTextInput()
descTextInput.Prompt = "Description: "
Expand Down Expand Up @@ -122,6 +121,7 @@ func NewNodeView(model *Model) *NodeView {
nodeForm.AddItem(nodeTypeChoices)
nodeForm.AddItem(urlTextInput)
nodeForm.AddItem(confirmButton)
nodeForm.FocusAvailableFirstItem()

return nodeForm
}
Expand Down

0 comments on commit 9658402

Please sign in to comment.