Looking better and working now

This commit is contained in:
2026-03-28 16:43:09 +01:00
parent 7d94dee26b
commit 41d0dd3b14
3 changed files with 66 additions and 39 deletions

View File

@@ -10,17 +10,19 @@ import (
type Model struct { type Model struct {
currentFile string currentFile string
cursor int
text string text string
width int textLines int
height int
lineCount int
bridge *bridge.Bridge
width int
height int
bridge *bridge.Bridge
listenBridge chan string listenBridge chan string
messages []string messages []string
viewport viewport.Model stdoutOutput viewport.Model
codeViewer viewport.Model
cursor int
breakpoints map[string][]int breakpoints map[string][]int
} }
@@ -28,18 +30,23 @@ type Model struct {
func NewModel(b *bridge.Bridge, file string, text string) Model { func NewModel(b *bridge.Bridge, file string, text string) Model {
c := b.Subscribe() c := b.Subscribe()
vp := viewport.New(0, 0) stdoutOutput := viewport.New(0, 0)
codeViewer := viewport.New(0, 0)
return Model{ return Model{
currentFile: file, currentFile: file,
text: text, text: text,
cursor: 0, textLines: len(strings.Split(text, "\n")),
lineCount: len(strings.Split(text, "\n")),
bridge: b, bridge: b,
breakpoints: make(map[string][]int),
listenBridge: c, listenBridge: c,
messages: make([]string, 0),
viewport: vp, breakpoints: make(map[string][]int),
messages: make([]string, 0),
cursor: 0,
codeViewer: codeViewer,
stdoutOutput: stdoutOutput,
} }
} }

View File

@@ -1,6 +1,8 @@
package ui package ui
import ( import (
"strings"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
) )
@@ -9,31 +11,54 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg: case tea.KeyMsg:
return m.HandleKeyMsg(msg) return m.HandleKeyMsg(msg)
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
m.width = msg.Width return m.UpdateWindowSize(msg)
m.height = msg.Height
case StdoutMsg: case StdoutMsg:
m.messages = append(m.messages, string(msg)) m.messages = append(m.messages, string(msg))
m.stdoutOutput.SetContent(strings.Join(m.messages, ""))
m.stdoutOutput.GotoBottom()
return m, ListenBridge(m.listenBridge) return m, ListenBridge(m.listenBridge)
} }
return m, nil return m, nil
} }
func (m Model) UpdateWindowSize(msg tea.WindowSizeMsg) (tea.Model, tea.Cmd) {
m.width = msg.Width
m.height = msg.Height
editorHeight := msg.Height * 70 / 100
outputHeight := msg.Height - editorHeight - 4
m.codeViewer.Width = msg.Width
m.codeViewer.Height = editorHeight
m.stdoutOutput.Width = msg.Width
m.stdoutOutput.Height = outputHeight
m.stdoutOutput.SetContent(strings.Join(m.messages, ""))
return m, nil
}
func (m Model) HandleKeyMsg(key tea.KeyMsg) (tea.Model, tea.Cmd) { func (m Model) HandleKeyMsg(key tea.KeyMsg) (tea.Model, tea.Cmd) {
switch key.String() { switch key.String() {
case "q", "ctrl+c": case "q", "ctrl+c":
return m, tea.Quit return m, tea.Quit
case "k": case "k":
m.cursor = clamp(0, m.height, m.cursor-1) m.codeViewer.ScrollUp(1)
m.cursor = max(0, m.cursor-1)
case "j": case "j":
m.cursor = clamp(0, min(m.height, m.lineCount-1), m.cursor+1) m.codeViewer.ScrollUp(1)
m.cursor = min(m.textLines-1, m.cursor+1)
case "b": case "b":
m.bridge.Breakpoint(m.currentFile, m.cursor) lineNumber := m.cursor + 1
m.bridge.Breakpoint(m.currentFile, lineNumber)
if file, ok := m.breakpoints[m.currentFile]; ok { if file, ok := m.breakpoints[m.currentFile]; ok {
m.breakpoints[m.currentFile] = append(file, m.cursor+1) m.breakpoints[m.currentFile] = append(file, lineNumber)
} else { } else {
m.breakpoints[m.currentFile] = []int{ m.breakpoints[m.currentFile] = []int{
m.cursor + 1, lineNumber,
} }
} }
case "c": case "c":
@@ -42,9 +67,3 @@ func (m Model) HandleKeyMsg(key tea.KeyMsg) (tea.Model, tea.Cmd) {
return m, nil return m, nil
} }
func clamp(minimum, maximum, val int) int {
val = max(minimum, val)
val = min(maximum, val)
return val
}

View File

@@ -12,7 +12,7 @@ import (
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
) )
var panelStyle = lipgloss.NewStyle().Border(lipgloss.NormalBorder()) var panelStyle = lipgloss.NewStyle().Border(lipgloss.NormalBorder()).Padding(0, 1)
func (m Model) View() string { func (m Model) View() string {
var buf bytes.Buffer var buf bytes.Buffer
@@ -27,7 +27,6 @@ func (m Model) View() string {
lines := strings.Split(buf.String(), "\n") lines := strings.Split(buf.String(), "\n")
breakpoints := m.breakpoints[m.currentFile] breakpoints := m.breakpoints[m.currentFile]
for i, line := range lines { for i, line := range lines {
breakpoint := " " breakpoint := " "
cursor := " " cursor := " "
@@ -42,15 +41,17 @@ func (m Model) View() string {
fmt.Fprintf(&out, "%-4d%s%s %s\n", i+1, breakpoint, cursor, line) fmt.Fprintf(&out, "%-4d%s%s %s\n", i+1, breakpoint, cursor, line)
} }
frameW, frameH := panelStyle.GetFrameSize() m.codeViewer.SetContent(out.String())
topHeight := m.height * 70 / 100
code := panelStyle.Width(m.width - frameW).Height(topHeight - frameH).Render(out.String()) hFrame, wFrame := panelStyle.GetFrameSize()
m.viewport.Height = m.height - (topHeight + frameH) topPanel := panelStyle.
m.viewport.Width = m.width - frameW Height(m.codeViewer.Height - hFrame).
m.viewport.SetContent(strings.Join(m.messages, "")) Width(m.codeViewer.Width - wFrame).
m.viewport.GotoBottom() Render(m.codeViewer.View())
output := panelStyle.Width(m.viewport.Width).Height(m.viewport.Height).Render(m.viewport.View())
return lipgloss.JoinVertical(lipgloss.Top, code, output) bottomPanel := panelStyle.Height(m.stdoutOutput.Height - hFrame).
Width(m.stdoutOutput.Width - wFrame).
Render(m.stdoutOutput.View())
return lipgloss.JoinVertical(lipgloss.Top, topPanel, bottomPanel)
} }