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

Commit

Permalink
auto resize view
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 8, 2022
1 parent cda81d4 commit c1ee4ce
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 27 deletions.
2 changes: 1 addition & 1 deletion gohost/sys_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
func SysHost() Host {
sysHostOnce.Do(func() {
sysHostInstance = &sysHost{
name: "System Host",
name: "System Host wrf asdfasdf",
desc: cfg.SysHostFile,
}
})
Expand Down
7 changes: 4 additions & 3 deletions tui/editor_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"gohost/gohost"
)

Expand Down Expand Up @@ -58,8 +57,9 @@ func (v *EditorView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch m := msg.(type) {
case tea.WindowSizeMsg:
v.hostEditor.SetHeight(m.Height)
v.hostEditor.SetHeight(m.Height - 2)
v.hostEditor.SetWidth(m.Width)
//v.model.log(fmt.Sprintf("editor view: w %d h %d", v.hostEditor.Width(), v.hostEditor.Height()))
case tea.KeyMsg:
if v.model.state == editorViewState {
switch {
Expand Down Expand Up @@ -91,7 +91,8 @@ func (v *EditorView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (v *EditorView) View() string {
return lipgloss.JoinVertical(lipgloss.Top, v.hostEditor.View(), v.statusLine)
//return lipgloss.JoinVertical(lipgloss.Top, v.hostEditor.View(), v.statusLine)
return v.hostEditor.View()
}

func (v *EditorView) Focus() {
Expand Down
26 changes: 20 additions & 6 deletions tui/log_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"io"
"time"
)

func NewLogView(model *Model) *LogView {
m := list.New([]list.Item{&LogItem{"Start recording log"}}, &LogItemDelegate{}, 0, 0)
m.SetShowStatusBar(false)
m := list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0)
m.Title = "Logs"
m.SetShowStatusBar(true)
m.SetShowHelp(false)
m.SetShowTitle(false)
m.SetShowTitle(true)
m.SetShowPagination(false)
return &LogView{
main: model,
Expand Down Expand Up @@ -45,18 +47,30 @@ func (l *LogView) View() string {

func (l *LogView) InsertLog(msg string) {
last := len(l.model.Items())
l.model.InsertItem(last, &LogItem{msg})
l.model.InsertItem(last, &LogItem{
log: msg,
time: time.Now().String(),
})
l.model.Select(last)
}

type LogItem struct {
log string
log string
time string
}

func (l LogItem) FilterValue() string {
return l.log
}

func (l LogItem) Title() string {
return l.log
}

func (l LogItem) Description() string {
return l.time
}

type LogItemDelegate struct {
}

Expand All @@ -76,7 +90,7 @@ func (d *LogItemDelegate) Height() int {
}

func (d *LogItemDelegate) Spacing() int {
return 0
return 1
}

func (d *LogItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
Expand Down
24 changes: 14 additions & 10 deletions tui/main_view.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tui

import (
"fmt"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -36,7 +37,7 @@ type Model struct {
func NewModel() (*Model, error) {
model := &Model{
state: treeViewState,
reservedHeight: 10,
reservedHeight: 1,
}
model.logView = NewLogView(model)
model.helpView = NewHelpView(model)
Expand All @@ -47,6 +48,7 @@ func NewModel() (*Model, error) {
}

func (m *Model) Init() tea.Cmd {
m.log(fmt.Sprintf("view h %d w %d", styles.DefaultView.GetHeight(), styles.DefaultView.GetWidth()))
return tea.Batch(
m.treeView.Init(),
m.editorView.Init(),
Expand All @@ -65,7 +67,6 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...)

case tea.KeyMsg:
m.log("hit key " + msg.String())
switch {
case key.Matches(msg, keys.Switch):
m.switchNextState()
Expand Down Expand Up @@ -96,27 +97,27 @@ func (m *Model) View() string {
case treeViewState:
v = lipgloss.JoinVertical(lipgloss.Left,
lipgloss.JoinHorizontal(lipgloss.Top,
styles.DefaultView.Render(m.logView.View()),
styles.FocusedView.Render(m.treeView.View()),
styles.DefaultView.Render(m.editorView.View()),
m.logView.View(),
),
m.helpView.View())

case editorViewState:
v = lipgloss.JoinVertical(lipgloss.Left,
lipgloss.JoinHorizontal(lipgloss.Top,
styles.DefaultView.Render(m.logView.View()),
styles.DefaultView.Render(m.treeView.View()),
styles.FocusedView.Render(m.editorView.View()),
m.logView.View(),
),
m.helpView.View())

case nodeViewState:
v = lipgloss.JoinVertical(lipgloss.Left,
lipgloss.JoinHorizontal(lipgloss.Top,
styles.DefaultView.Render(m.logView.View()),
styles.DefaultView.Render(m.treeView.View()),
styles.FocusedView.Render(m.nodeView.View()),
m.logView.View(),
),
m.helpView.View(),
)
Expand Down Expand Up @@ -161,10 +162,13 @@ func (m *Model) setFullHelp(state sessionState, kb [][]key.Binding) {
}

func (m *Model) resizeViews(sizeMsg tea.WindowSizeMsg, cmds *[]tea.Cmd) {
m.updateView(tea.WindowSizeMsg{Width: sizeMsg.Width / 3, Height: sizeMsg.Height - m.reservedHeight}, cmds, m.treeView)
m.updateView(tea.WindowSizeMsg{Width: sizeMsg.Width / 3, Height: sizeMsg.Height - m.reservedHeight}, cmds, m.editorView)
m.updateView(tea.WindowSizeMsg{Width: sizeMsg.Width / 3, Height: sizeMsg.Height - m.reservedHeight}, cmds, m.nodeView)
m.updateView(tea.WindowSizeMsg{Width: sizeMsg.Width / 3, Height: sizeMsg.Height - m.reservedHeight}, cmds, m.logView)
m.updateView(tea.WindowSizeMsg{Width: sizeMsg.Width, Height: sizeMsg.Height}, cmds, m.helpView)
m.log(fmt.Sprintf("window w %d h %d", sizeMsg.Width, sizeMsg.Height))
width := sizeMsg.Width / 3
height := sizeMsg.Height - m.reservedHeight
m.updateView(tea.WindowSizeMsg{Width: width, Height: height}, cmds, m.treeView)
m.updateView(tea.WindowSizeMsg{Width: width, Height: height}, cmds, m.editorView)
m.updateView(tea.WindowSizeMsg{Width: width, Height: height}, cmds, m.nodeView)
m.updateView(tea.WindowSizeMsg{Width: width, Height: height}, cmds, m.logView)
m.updateView(tea.WindowSizeMsg{Width: sizeMsg.Width, Height: 1}, cmds, m.helpView)
//m.log(fmt.Sprintf("w: %d h: %d", sizeMsg.Width, sizeMsg.Height))
}
26 changes: 20 additions & 6 deletions tui/styles/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@ package styles

import "github.com/charmbracelet/lipgloss"

const padding = 3
const padding = 0

var (
DefaultView = lipgloss.NewStyle().PaddingLeft(padding).PaddingRight(padding).BorderStyle(lipgloss.HiddenBorder())
WidgetView = lipgloss.NewStyle().PaddingBottom(padding).BorderStyle(lipgloss.HiddenBorder())
FocusedView = lipgloss.NewStyle().PaddingLeft(padding).PaddingRight(padding).BorderStyle(lipgloss.NormalBorder()).BorderForeground(lipgloss.Color("69"))
FocusedWidget = lipgloss.NewStyle().Foreground(lipgloss.Color("69"))
None = lipgloss.NewStyle()
None = lipgloss.NewStyle()
DefaultView = None
FocusedView = None
FocusedWidget = None

//DefaultView = lipgloss.NewStyle().
// PaddingLeft(padding).
// PaddingRight(padding).
// BorderStyle(lipgloss.NormalBorder()).
// BorderForeground(lipgloss.Color("70"))
//
//FocusedView = lipgloss.NewStyle().
// PaddingLeft(padding).
// PaddingRight(padding).
// BorderStyle(lipgloss.NormalBorder()).
// BorderForeground(lipgloss.Color("69"))
//
//FocusedWidget = lipgloss.NewStyle().
// Foreground(lipgloss.Color("69"))
)
2 changes: 1 addition & 1 deletion tui/tree_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (v *TreeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.WindowSizeMsg:
v.nodeList.SetHeight(m.Height)
v.nodeList.SetWidth(m.Width)
v.model.log(fmt.Sprintf("tree view w: %d h: %d", m.Width, m.Height))
//v.model.log(fmt.Sprintf("tree view w: %d h: %d", v.nodeList.Width(), v.nodeList.Height()))
case tea.KeyMsg:
if v.model.state == treeViewState {
switch {
Expand Down

0 comments on commit c1ee4ce

Please sign in to comment.