2024-09-15 09:04:32 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
2024-09-17 00:43:36 +02:00
|
|
|
"git.sentinel65x.com/dave/gary/debugger/memory"
|
2024-09-15 09:04:32 +02:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
lip "github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/creachadair/mds/slice"
|
|
|
|
)
|
|
|
|
|
2024-09-17 00:43:36 +02:00
|
|
|
type HexViewSetHeight struct {
|
|
|
|
Height int
|
2024-09-15 09:04:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type HexView struct {
|
|
|
|
AddrStyle lip.Style
|
|
|
|
ZeroStyle lip.Style
|
|
|
|
HexStyle lip.Style
|
|
|
|
|
2024-09-17 00:43:36 +02:00
|
|
|
mem *memory.Memory
|
|
|
|
commit func() tea.Cmd
|
|
|
|
height int
|
2024-09-15 09:04:32 +02:00
|
|
|
firstAddr int // top of viewport
|
|
|
|
selectedAddr int
|
|
|
|
editing bool
|
|
|
|
editNibble int // 0 or 1
|
|
|
|
}
|
|
|
|
|
2024-09-17 00:43:36 +02:00
|
|
|
func NewHexView(mem *memory.Memory, commit func() tea.Cmd) HexView {
|
2024-09-15 09:04:32 +02:00
|
|
|
st := lip.NewStyle()
|
|
|
|
ret := HexView{
|
|
|
|
AddrStyle: st,
|
|
|
|
ZeroStyle: st,
|
|
|
|
HexStyle: st,
|
2024-09-17 00:43:36 +02:00
|
|
|
mem: mem,
|
|
|
|
commit: commit,
|
2024-09-15 09:04:32 +02:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m HexView) Width() int {
|
|
|
|
hexdumpWidth := 16*3 + 2 // including spaces between dquads
|
|
|
|
addrWidth := m.addrNibbles() // Hex nibbles needed to represent all addrs
|
|
|
|
return addrWidth + 4 + hexdumpWidth
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m HexView) SelectedAddr() int {
|
|
|
|
return m.selectedAddr
|
|
|
|
}
|
|
|
|
|
2024-09-17 00:43:36 +02:00
|
|
|
func (m *HexView) moveSelection(delta int) bool {
|
|
|
|
newAddr := m.selectedAddr + delta
|
|
|
|
if newAddr >= 0 && newAddr < m.mem.Len() {
|
|
|
|
m.selectedAddr = newAddr
|
2024-09-17 01:10:17 +02:00
|
|
|
start, end := m.VisibleBytes()
|
|
|
|
newFirstLine := newAddr / 16
|
|
|
|
if newAddr < start {
|
|
|
|
newFirstLine = newFirstLine - (m.height * 4 / 5)
|
|
|
|
newFirstLine = max(newFirstLine, 0)
|
|
|
|
m.firstAddr = newFirstLine * 16
|
|
|
|
} else if newAddr >= end {
|
|
|
|
newFirstLine = newFirstLine - (m.height * 1 / 5)
|
|
|
|
newFirstLine = min(newFirstLine, (m.mem.Len()/16)-m.height)
|
|
|
|
m.firstAddr = newFirstLine * 16
|
|
|
|
}
|
2024-09-17 00:43:36 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m HexView) VisibleBytes() (start, end int) {
|
|
|
|
return m.firstAddr, m.firstAddr + (m.height * 16)
|
|
|
|
}
|
|
|
|
|
2024-09-15 09:04:32 +02:00
|
|
|
func (m HexView) addrFormat() string {
|
|
|
|
return fmt.Sprintf("%%%dx", m.addrNibbles())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m HexView) addrNibbles() int {
|
2024-09-17 00:43:36 +02:00
|
|
|
return int(math.Ceil(math.Log2(float64(m.mem.Len())*8))) / 4
|
2024-09-15 09:04:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m HexView) Update(msg tea.Msg) (HexView, tea.Cmd) {
|
|
|
|
if m.editing {
|
|
|
|
return m.updateEditMode(msg)
|
|
|
|
} else {
|
|
|
|
return m.updateViewMode(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m HexView) updateEditMode(msg tea.Msg) (HexView, tea.Cmd) {
|
|
|
|
switch msg := msg.(type) {
|
2024-09-17 00:43:36 +02:00
|
|
|
case HexViewSetHeight:
|
|
|
|
m.height = msg.Height
|
2024-09-15 09:04:32 +02:00
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.Type {
|
|
|
|
case tea.KeyLeft:
|
|
|
|
if m.editNibble == 1 {
|
|
|
|
m.editNibble = 0
|
2024-09-17 00:43:36 +02:00
|
|
|
} else if m.moveSelection(-1) {
|
|
|
|
m.editNibble = 1
|
2024-09-15 09:04:32 +02:00
|
|
|
}
|
|
|
|
case tea.KeyRight:
|
|
|
|
if m.editNibble == 0 {
|
|
|
|
m.editNibble = 1
|
2024-09-17 00:43:36 +02:00
|
|
|
} else if m.moveSelection(1) {
|
|
|
|
m.editNibble = 0
|
2024-09-15 09:04:32 +02:00
|
|
|
}
|
2024-09-17 00:43:36 +02:00
|
|
|
case tea.KeyUp:
|
|
|
|
m.moveSelection(-16)
|
|
|
|
case tea.KeyDown:
|
|
|
|
m.moveSelection(16)
|
2024-09-15 09:04:32 +02:00
|
|
|
case tea.KeyEsc:
|
|
|
|
m.editing = false
|
|
|
|
case tea.KeyEnter:
|
|
|
|
m.editing = false
|
2024-09-17 00:43:36 +02:00
|
|
|
return m, m.commit()
|
2024-09-15 09:04:32 +02:00
|
|
|
case tea.KeyRunes:
|
|
|
|
if unicode.In(msg.Runes[0], unicode.ASCII_Hex_Digit) {
|
|
|
|
nibble := hexToNibble(msg.Runes[0])
|
2024-09-17 00:43:36 +02:00
|
|
|
prev := m.mem.At(m.selectedAddr).Value
|
2024-09-15 09:04:32 +02:00
|
|
|
if m.editNibble == 0 {
|
2024-09-17 00:43:36 +02:00
|
|
|
m.mem.Write(m.selectedAddr, (nibble<<4)+(prev&0xF))
|
2024-09-15 09:04:32 +02:00
|
|
|
m.editNibble++
|
|
|
|
} else {
|
2024-09-17 00:43:36 +02:00
|
|
|
m.mem.Write(m.selectedAddr, (prev&0xF0)+nibble)
|
|
|
|
if m.moveSelection(1) {
|
|
|
|
m.editNibble = 0
|
|
|
|
}
|
2024-09-15 09:04:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hexToNibble(r rune) byte {
|
|
|
|
switch {
|
|
|
|
case r >= '0' && r <= '9':
|
|
|
|
return byte(r - '0')
|
|
|
|
case r >= 'a' && r <= 'f':
|
|
|
|
return byte(r - 'a' + 10)
|
|
|
|
case r >= 'A' && r <= 'F':
|
|
|
|
return byte(r - 'A' + 10)
|
|
|
|
}
|
|
|
|
panic("invalid hex rune")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m HexView) updateViewMode(msg tea.Msg) (HexView, tea.Cmd) {
|
|
|
|
switch msg := msg.(type) {
|
2024-09-17 00:43:36 +02:00
|
|
|
case HexViewSetHeight:
|
|
|
|
m.height = msg.Height
|
2024-09-15 09:04:32 +02:00
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
|
|
|
case "up":
|
2024-09-17 00:43:36 +02:00
|
|
|
m.moveSelection(-16)
|
2024-09-15 09:04:32 +02:00
|
|
|
case "down":
|
2024-09-17 00:43:36 +02:00
|
|
|
m.moveSelection(16)
|
2024-09-15 09:04:32 +02:00
|
|
|
case "left":
|
2024-09-17 00:43:36 +02:00
|
|
|
m.moveSelection(-1)
|
2024-09-15 09:04:32 +02:00
|
|
|
case "right":
|
2024-09-17 00:43:36 +02:00
|
|
|
m.moveSelection(1)
|
2024-09-15 09:04:32 +02:00
|
|
|
case "pgdown":
|
2024-09-17 00:43:36 +02:00
|
|
|
m.moveSelection(m.height * 16)
|
2024-09-15 09:04:32 +02:00
|
|
|
case "pgup":
|
2024-09-17 00:43:36 +02:00
|
|
|
m.moveSelection(-m.height * 16)
|
2024-09-15 09:04:32 +02:00
|
|
|
case "w":
|
|
|
|
m.editing = true
|
|
|
|
m.editNibble = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2024-09-17 01:10:17 +02:00
|
|
|
func (m HexView) View() string {
|
2024-09-17 00:43:36 +02:00
|
|
|
startAddr, endAddr := m.VisibleBytes()
|
|
|
|
bytes := m.mem.Slice(startAddr, endAddr)
|
|
|
|
|
2024-09-15 09:04:32 +02:00
|
|
|
var ret strings.Builder
|
|
|
|
addrFormat := m.addrFormat()
|
2024-09-17 00:43:36 +02:00
|
|
|
for line, bytes := range slice.Chunks(bytes, 16) {
|
2024-09-15 09:04:32 +02:00
|
|
|
lineAddr := m.firstAddr + (16 * line)
|
|
|
|
ret.WriteString(m.AddrStyle.Render(fmt.Sprintf(addrFormat, lineAddr)))
|
|
|
|
ret.WriteString(" ")
|
|
|
|
for i, b := range bytes {
|
|
|
|
byteAddr := lineAddr + i
|
|
|
|
if i == 8 {
|
|
|
|
ret.WriteString(" ")
|
|
|
|
} else if i != 0 {
|
|
|
|
ret.WriteByte(' ')
|
|
|
|
}
|
|
|
|
if m.editing && byteAddr == m.selectedAddr {
|
2024-09-17 00:43:36 +02:00
|
|
|
st := m.HexStyle.Bold(true)
|
2024-09-15 09:04:32 +02:00
|
|
|
s1, s2 := st.Reverse(true), st
|
|
|
|
if m.editNibble == 1 {
|
|
|
|
s1, s2 = s2, s1
|
|
|
|
}
|
2024-09-17 00:43:36 +02:00
|
|
|
ret.WriteString(s1.Render(fmt.Sprintf("%01x", b.Value>>4)))
|
|
|
|
ret.WriteString(s2.Render(fmt.Sprintf("%01x", b.Value&0xF)))
|
2024-09-15 09:04:32 +02:00
|
|
|
} else {
|
|
|
|
st := m.HexStyle
|
|
|
|
if m.selectedAddr == byteAddr {
|
|
|
|
st = m.HexStyle.Reverse(true)
|
2024-09-17 00:43:36 +02:00
|
|
|
} else if !b.Valid || b.Value == 0 {
|
2024-09-15 09:04:32 +02:00
|
|
|
st = m.ZeroStyle
|
|
|
|
}
|
2024-09-17 00:43:36 +02:00
|
|
|
if b.Changed {
|
|
|
|
st = st.Underline(true).Bold(true)
|
|
|
|
}
|
|
|
|
ret.WriteString(st.Render(b.Hex()))
|
2024-09-15 09:04:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.WriteByte('\n')
|
|
|
|
}
|
|
|
|
return strings.TrimRight(ret.String(), "\n")
|
|
|
|
}
|