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

Commit

Permalink
view auto size
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 9, 2022
1 parent 4a1c53c commit 4e5a91b
Show file tree
Hide file tree
Showing 8 changed files with 4,520 additions and 31 deletions.
4,481 changes: 4,481 additions & 0 deletions debug.log

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions tui/main_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewModel() (*Model, error) {
model := &Model{
//state: treeViewState,
state: nodeViewState,
styleWidth: styleWidth,
styleWidth: styleWidth * 2,
styleHeight: styleHeight,
shortHelperHeight: 1,
}
Expand Down Expand Up @@ -164,13 +164,12 @@ func (m *Model) setFullHelp(state sessionState, kb [][]key.Binding) {

func (m *Model) resizeViews(sizeMsg tea.WindowSizeMsg, cmds *[]tea.Cmd) {
log.Debug(fmt.Sprintf("window w %d h %d", sizeMsg.Width, sizeMsg.Height))
m.leftViewWidth = (sizeMsg.Width - m.styleWidth*2) / 3
m.rightViewWidth = sizeMsg.Width - m.leftViewWidth - m.styleWidth*2
m.leftViewWidth = (sizeMsg.Width - m.styleWidth) / 3
m.rightViewWidth = (sizeMsg.Width - m.styleWidth) - m.leftViewWidth
height := sizeMsg.Height - m.styleHeight - m.shortHelperHeight
log.Debug(fmt.Sprintf("left w %d right w %d h %d", m.leftViewWidth, m.rightViewWidth, height))
m.updateView(tea.WindowSizeMsg{Width: m.leftViewWidth, Height: height}, cmds, m.treeView)
m.updateView(tea.WindowSizeMsg{Width: m.rightViewWidth, Height: height}, cmds, m.editorView)
// FIXME why this need minus 1 ?
m.updateView(tea.WindowSizeMsg{Width: m.rightViewWidth - 1, Height: height - 1}, cmds, m.nodeView)
m.updateView(tea.WindowSizeMsg{Width: m.rightViewWidth, Height: height}, cmds, m.nodeView)
m.updateView(tea.WindowSizeMsg{Width: sizeMsg.Width, Height: 1}, cmds, m.helpView)
}
4 changes: 2 additions & 2 deletions tui/node_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"gohost/log"
"gohost/tui/styles"
"gohost/tui/view"
"gohost/tui/widget"
)
Expand Down Expand Up @@ -44,7 +44,7 @@ func NewNodeView(model *Model) *NodeView {
focusIdx: 0,
nodeTypes: nodeTypes,
}
nodeView.WidgetStyle = styles.DefaultView
nodeView.WidgetStyle = lipgloss.NewStyle().PaddingBottom(1)
nodeView.AddWidget(nodeNameTextInput)
nodeView.AddWidget(descTextInput)
nodeView.AddWidget(urlTextInput)
Expand Down
2 changes: 1 addition & 1 deletion tui/styles/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package styles

import "github.com/charmbracelet/lipgloss"

const padding = 1
const padding = 2

var (
None = lipgloss.NewStyle()
Expand Down
30 changes: 17 additions & 13 deletions tui/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ func (v *BaseView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch m := msg.(type) {
case tea.WindowSizeMsg:
for i := 0; i < len(v.Widgets); i++ {

}
v.SetSize(m.Width, m.Height)
return v, nil
case tea.KeyMsg:
switch m.String() {
case "up", "down":
Expand All @@ -66,31 +65,36 @@ func (v *BaseView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (v *BaseView) View() string {
var str string
for i := 0; i < len(v.Widgets); i++ {
str = lipgloss.JoinVertical(lipgloss.Left, str, v.Widgets[i].View())
if lipgloss.Height(str) > v.height {
break
w := v.Widgets[i]
if w.Height()+lipgloss.Height(str) > v.height {
return str
}
if i == 0 {
str = lipgloss.JoinVertical(lipgloss.Left, w.View())
continue
}
str = lipgloss.JoinVertical(lipgloss.Left, str, v.Widgets[i].View())
log.Debug(fmt.Sprintf("cur h %d, view h %d", lipgloss.Height(str), v.height))
}
return str
}
func (v *BaseView) SetSize(width, height int) {
v.width = width
v.height = height
remain := height
height = height / len(v.Widgets)
remain := v.height
height = v.height / len(v.Widgets)
for i := 0; i < len(v.Widgets); i++ {
w := v.Widgets[i]
w.SetWidth(width)
if i == len(v.Widgets)-1 {
w.SetHeight(remain)
log.Debug(fmt.Sprintf("base view w %d h %d", width, remain))
log.Debug(fmt.Sprintf("base view w %d h %d", width, w.Height()))
} else {
h := w.SetHeight(height)
remain -= h
log.Debug(fmt.Sprintf("base view w %d h %d", width, h))
w.SetHeight(height)
remain -= w.Height()
log.Debug(fmt.Sprintf("base view w %d h %d", width, w.Height()))
}
}

}

func (v *BaseView) AddWidget(widget widget.Widget) {
Expand Down
7 changes: 1 addition & 6 deletions tui/widget/choice.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package widget

import (
"fmt"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"gohost/log"
)

var _ Widget = (*Choice)(nil)
Expand All @@ -16,7 +14,6 @@ func NewChoice(items []list.Item) *Choice {
model.SetShowPagination(true)
model.SetShowStatusBar(false)
model.SetFilteringEnabled(false)
model.SetShowTitle(false)
return &Choice{
Model: model,
}
Expand All @@ -38,7 +35,6 @@ func (c *Choice) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.WindowSizeMsg:
c.SetHeight(m.Height)
c.SetWidth(m.Width)
log.Debug(fmt.Sprintf("choice w %d h %d", c.Model.Width(), c.Model.Height()))
}
c.Model, cmd = c.Model.Update(msg)
cmds = append(cmds, cmd)
Expand Down Expand Up @@ -67,7 +63,6 @@ func (c *Choice) SetWidth(width int) {
c.Model.SetWidth(width)
}

func (c *Choice) SetHeight(height int) int {
func (c *Choice) SetHeight(height int) {
c.Model.SetHeight(height)
return c.Model.Height()
}
14 changes: 11 additions & 3 deletions tui/widget/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ type TextInput struct {
textinput.Model
}

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

func (t *TextInput) SetHeight(height int) int {
func (t *TextInput) Height() int {
return 1
}

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

func (t *TextInput) SetHeight(height int) {
return
}

func (t *TextInput) Init() tea.Cmd {
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion tui/widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ type Widget interface {
HandleKeyUp() bool
HandleKeyDown() bool
SetWidth(width int)
SetHeight(height int) int
SetHeight(height int)
Width() int
Height() int
}

0 comments on commit 4e5a91b

Please sign in to comment.