From 700a3014688ff11a75e33cba9be43b8e20b59e4d Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 16 Sep 2024 16:10:17 -0700 Subject: [PATCH] debugger: more UI tweaks and playing with layout --- .gitignore | 1 + debugger/hexview.go | 2 +- debugger/ui.go | 77 +++++++++++++++++++++++++++------------------ 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 935164a..023bdc0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .envrc out/ +log.txt diff --git a/debugger/hexview.go b/debugger/hexview.go index 5c264a7..a284176 100644 --- a/debugger/hexview.go +++ b/debugger/hexview.go @@ -176,7 +176,7 @@ func (m HexView) updateViewMode(msg tea.Msg) (HexView, tea.Cmd) { return m, nil } -func (m HexView) View(height int) string { +func (m HexView) View() string { startAddr, endAddr := m.VisibleBytes() bytes := m.mem.Slice(startAddr, endAddr) diff --git a/debugger/ui.go b/debugger/ui.go index d0c20d1..2101e02 100644 --- a/debugger/ui.go +++ b/debugger/ui.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "log" "git.sentinel65x.com/dave/gary/debugger/memory" tea "github.com/charmbracelet/bubbletea" @@ -14,7 +13,7 @@ func UI(mem *memory.Memory) error { width: 0, height: 0, mem: mem, - bottomMsg: "Loading...", + statusMsg: "Loading...", } initial.hex = NewHexView(mem, initial.commitWrites) initial.hex.AddrStyle = text @@ -29,13 +28,21 @@ func UI(mem *memory.Memory) error { } var ( - amber = lip.Color("#ffb00") - slate = lip.Color("235") - text = lip.NewStyle().Foreground(amber).Background(slate) + amber = lip.Color("#ffb00") + slate = lip.Color("235") + text = lip.NewStyle(). + Foreground(amber). + Background(slate). + BorderForeground(amber). + BorderBackground(slate). + Border(lip.NormalBorder(), false, false, false, false) faintText = text.Faint(true) - box = text.Border(lip.NormalBorder(), true, true, true, true). - BorderForeground(amber). - BorderBackground(slate) + + box = lip.NewStyle(). + Border(lip.NormalBorder()). + BorderForeground(amber). + BorderBackground(slate). + Padding(1, 2) ) type msgMemoryChanged struct { @@ -56,22 +63,29 @@ type debugger struct { mem *memory.Memory hex HexView lastErr error - bottomMsg string + statusMsg string } func (m debugger) Init() tea.Cmd { - return tea.Sequence( - m.readFullMemory(), - staticMsg(msgUpdateStatus{"Initial load complete."}), - ) + loads := m.readFullMemoryCmds() + ret := make([]tea.Cmd, 0, (len(loads)*2)+1) + for i, c := range loads { + ret = append(ret, staticMsg(msgUpdateStatus{fmt.Sprintf("Loading block %d/%d", i+1, len(loads))}), c) + } + ret = append(ret, staticMsg(msgUpdateStatus{"Load complete."})) + return tea.Sequence(ret...) } func (m debugger) readFullMemory() tea.Cmd { + return tea.Sequence(m.readFullMemoryCmds()...) +} + +func (m debugger) readFullMemoryCmds() []tea.Cmd { var ret []tea.Cmd for i := 0; i < m.mem.Len(); i += 128 { ret = append(ret, m.readMemory(i, 128)) } - return tea.Sequence(ret...) + return ret } func (m debugger) readMemory(start, count int) tea.Cmd { @@ -103,13 +117,12 @@ func (m debugger) commitWrites() tea.Cmd { } func (m debugger) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - log.Printf("msv: %#v", msg) switch msg := msg.(type) { case tea.WindowSizeMsg: m.width = msg.Width m.height = msg.Height var cmd tea.Cmd - m.hex, cmd = m.hex.Update(HexViewSetHeight{m.height - 6}) // TODO: make less shit + m.hex, cmd = m.hex.Update(HexViewSetHeight{m.height - box.GetVerticalFrameSize() - 1}) return m, cmd case tea.KeyMsg: switch msg.String() { @@ -120,11 +133,10 @@ func (m debugger) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, m.readMemory(start, end-start) } case msgErr: - log.Print(msg.err) m.lastErr = msg.err return m, nil case msgUpdateStatus: - m.bottomMsg = msg.status + m.statusMsg = msg.status } var cmd tea.Cmd m.hex, cmd = m.hex.Update(msg) @@ -158,20 +170,23 @@ func (m debugger) View() string { return lip.Place(m.width, m.height, lip.Center, lip.Center, "Please embiggen your terminal") } - statusBar := text.Width(m.width).Height(1).Align(lip.Center) - topSegment := statusBar.Width(m.width / 3).Reverse(true) + hex := box.Render(m.hex.View()) - hexBox := lip.NewStyle().Border(lip.NormalBorder()).BorderForeground(amber).BorderBackground(slate).Padding(1, 2) - hexHeight := m.height - 2 - hexBox.GetVerticalFrameSize() - hex := hexBox.Render(m.hex.View(hexHeight)) + barText := text.Reverse(true) //.Height(1) - topLeft := text.Reverse(true).Bold(true).Render("Addr: ") + text.Reverse(true).Render(fmt.Sprintf("0x%-5x", m.hex.SelectedAddr())) - topLeft = topSegment.Padding(0, 1).Align(lip.Left).Render(topLeft) - topMid := topSegment.Bold(true).Render("GARY Debugger") - topRight := topSegment.Render("") - topStatus := lip.JoinHorizontal(lip.Top, topLeft, topMid, topRight) - bottomStatus := statusBar.Render(m.bottomMsg) + topLeft := barText.Bold(true).PaddingLeft(1).Render("Addr: ") + topLeft += barText.Render(fmt.Sprintf("0x%x (%d)", m.hex.SelectedAddr(), m.hex.SelectedAddr())) + topRight := barText.PaddingRight(1).Render(m.statusMsg) + title := barText.Padding(0, 1).Bold(true).Render("GARY Debugger") - top := lip.JoinVertical(lip.Center, topStatus, hex, bottomStatus) - return top + sideWidth := max(lip.Width(topLeft), lip.Width(topRight)) + titleWidth := m.width - 2*sideWidth + topLeft = barText.Width(sideWidth).Align(lip.Left).Render(topLeft) + topRight = barText.Width(sideWidth).Align(lip.Right).Render(topRight) + title = barText.Width(titleWidth).Align(lip.Center).Render(title) + + status := lip.JoinHorizontal(lip.Top, topLeft, title, topRight) + + all := lip.JoinVertical(lip.Center, status, hex) + return all }