From 060ab01648a7b3b6ba1eca9b1978a84bf34216f4 Mon Sep 17 00:00:00 2001 From: pablu Date: Sun, 29 Mar 2026 18:46:31 +0200 Subject: [PATCH] Add simple variables viewer, that doesnt cut of line --- ui/codeviewer/model.go | 1 + ui/model.go | 5 ++-- ui/update.go | 2 +- ui/variablesviewer/model.go | 53 +++++++++++++++++++++++++++++++++++++ ui/view.go | 2 +- 5 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 ui/variablesviewer/model.go diff --git a/ui/codeviewer/model.go b/ui/codeviewer/model.go index ce5eb66..02352c1 100644 --- a/ui/codeviewer/model.go +++ b/ui/codeviewer/model.go @@ -113,6 +113,7 @@ func (c CodeViewer) View() string { fmt.Fprintf(&out, "%-4d%s%s%s %s\n", lineNumber, breakpoint, executor, cursor, line) } + // Count long lines, and add here if they are out of view, this is a bit tricky I guess if len(lines)-c.offset < c.Height { for range c.Height - len(lines) { fmt.Fprintf(&out, "\n") diff --git a/ui/model.go b/ui/model.go index 126f3e4..ba23018 100644 --- a/ui/model.go +++ b/ui/model.go @@ -3,6 +3,7 @@ package ui import ( "git.pablu.de/pablu/pybug/internal/bridge" "git.pablu.de/pablu/pybug/ui/codeviewer" + "git.pablu.de/pablu/pybug/ui/variablesviewer" "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" ) @@ -22,7 +23,7 @@ type Model struct { messages []string stdoutOutput viewport.Model codeViewer codeviewer.CodeViewer - localsViewer viewport.Model + localsViewer variablesviewer.VariableViewer breakpoints map[string][]int } @@ -33,7 +34,7 @@ func NewModel(b *bridge.Bridge, file string, text string) Model { stdoutOutput := viewport.New(0, 0) codeViewer := codeviewer.NewCodeViewer(text) - localsViewer := viewport.New(0, 0) + localsViewer := variablesviewer.NewVariableViewer(map[string]any{}) return Model{ currentFile: file, diff --git a/ui/update.go b/ui/update.go index 9416025..4cbf1f5 100644 --- a/ui/update.go +++ b/ui/update.go @@ -26,7 +26,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Batch(ListenBridgeExecutionsStopped(m.listenBridgeExecutionsStopped), m.GetLocals()) case LocalsMsg: m.currLocals = map[string]any(msg) - m.localsViewer.SetContent(strings.Join(flattenDict(m.currLocals, 0), "\n")) + m.localsViewer.SetNewVariables(m.currLocals) } return m, cmd diff --git a/ui/variablesviewer/model.go b/ui/variablesviewer/model.go new file mode 100644 index 0000000..85bc0c4 --- /dev/null +++ b/ui/variablesviewer/model.go @@ -0,0 +1,53 @@ +package variablesviewer + +import ( + "fmt" + "slices" + "strings" + + tea "github.com/charmbracelet/bubbletea" +) + +type VariableViewer struct { + variables map[string]any + + Width, Height int +} + +func NewVariableViewer(variables map[string]any) VariableViewer { + return VariableViewer{ + variables: variables, + Width: 0, + Height: 0, + } +} + +func (v *VariableViewer) SetNewVariables(variables map[string]any) { + v.variables = variables +} + +func (v VariableViewer) Init() tea.Cmd { + return nil +} + +func (v VariableViewer) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + return v, nil +} + +func (v VariableViewer) View() string { + out := strings.Builder{} + + keys := make([]string, 0, len(v.variables)) + for k := range v.variables { + keys = append(keys, k) + } + slices.Sort(keys) + + // TODO make this listen to Height correctly + for _, key := range keys { + value := v.variables[key] + fmt.Fprintf(&out, "> %s: %s\n", key, value) + } + + return out.String() +} diff --git a/ui/view.go b/ui/view.go index 7abade9..2b94cb6 100644 --- a/ui/view.go +++ b/ui/view.go @@ -14,7 +14,7 @@ func flattenDict(m map[string]interface{}, indent int) []string { prefix := strings.Repeat(" ", indent) for k, v := range m { switch val := v.(type) { - case map[string]interface{}: + case map[string]any: lines = append(lines, fmt.Sprintf("%s%s:", prefix, k)) lines = append(lines, flattenDict(val, indent+1)...) default: