Add simple variables viewer, that doesnt cut of line

This commit is contained in:
2026-03-29 18:46:31 +02:00
parent bb829286d1
commit 060ab01648
5 changed files with 59 additions and 4 deletions

View File

@@ -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")

View File

@@ -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,

View File

@@ -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

View File

@@ -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()
}

View File

@@ -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: