Add simple variables viewer, that doesnt cut of line
This commit is contained in:
@@ -113,6 +113,7 @@ func (c CodeViewer) View() string {
|
|||||||
|
|
||||||
fmt.Fprintf(&out, "%-4d%s%s%s %s\n", lineNumber, breakpoint, executor, cursor, line)
|
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 {
|
if len(lines)-c.offset < c.Height {
|
||||||
for range c.Height - len(lines) {
|
for range c.Height - len(lines) {
|
||||||
fmt.Fprintf(&out, "\n")
|
fmt.Fprintf(&out, "\n")
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package ui
|
|||||||
import (
|
import (
|
||||||
"git.pablu.de/pablu/pybug/internal/bridge"
|
"git.pablu.de/pablu/pybug/internal/bridge"
|
||||||
"git.pablu.de/pablu/pybug/ui/codeviewer"
|
"git.pablu.de/pablu/pybug/ui/codeviewer"
|
||||||
|
"git.pablu.de/pablu/pybug/ui/variablesviewer"
|
||||||
"github.com/charmbracelet/bubbles/viewport"
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
)
|
)
|
||||||
@@ -22,7 +23,7 @@ type Model struct {
|
|||||||
messages []string
|
messages []string
|
||||||
stdoutOutput viewport.Model
|
stdoutOutput viewport.Model
|
||||||
codeViewer codeviewer.CodeViewer
|
codeViewer codeviewer.CodeViewer
|
||||||
localsViewer viewport.Model
|
localsViewer variablesviewer.VariableViewer
|
||||||
|
|
||||||
breakpoints map[string][]int
|
breakpoints map[string][]int
|
||||||
}
|
}
|
||||||
@@ -33,7 +34,7 @@ func NewModel(b *bridge.Bridge, file string, text string) Model {
|
|||||||
|
|
||||||
stdoutOutput := viewport.New(0, 0)
|
stdoutOutput := viewport.New(0, 0)
|
||||||
codeViewer := codeviewer.NewCodeViewer(text)
|
codeViewer := codeviewer.NewCodeViewer(text)
|
||||||
localsViewer := viewport.New(0, 0)
|
localsViewer := variablesviewer.NewVariableViewer(map[string]any{})
|
||||||
|
|
||||||
return Model{
|
return Model{
|
||||||
currentFile: file,
|
currentFile: 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())
|
return m, tea.Batch(ListenBridgeExecutionsStopped(m.listenBridgeExecutionsStopped), m.GetLocals())
|
||||||
case LocalsMsg:
|
case LocalsMsg:
|
||||||
m.currLocals = map[string]any(msg)
|
m.currLocals = map[string]any(msg)
|
||||||
m.localsViewer.SetContent(strings.Join(flattenDict(m.currLocals, 0), "\n"))
|
m.localsViewer.SetNewVariables(m.currLocals)
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, cmd
|
return m, cmd
|
||||||
|
|||||||
53
ui/variablesviewer/model.go
Normal file
53
ui/variablesviewer/model.go
Normal 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()
|
||||||
|
}
|
||||||
@@ -14,7 +14,7 @@ func flattenDict(m map[string]interface{}, indent int) []string {
|
|||||||
prefix := strings.Repeat(" ", indent)
|
prefix := strings.Repeat(" ", indent)
|
||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]any:
|
||||||
lines = append(lines, fmt.Sprintf("%s%s:", prefix, k))
|
lines = append(lines, fmt.Sprintf("%s%s:", prefix, k))
|
||||||
lines = append(lines, flattenDict(val, indent+1)...)
|
lines = append(lines, flattenDict(val, indent+1)...)
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user