You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fx/new/main.go

259 lines
4.6 KiB
Go

10 months ago
package main
import (
"fmt"
"io"
"os"
"runtime/pprof"
10 months ago
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
10 months ago
)
func main() {
10 months ago
if _, ok := os.LookupEnv("FX_PPROF"); ok {
f, err := os.Create("cpu.prof")
if err != nil {
panic(err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
panic(err)
}
defer pprof.StopCPUProfile()
memProf, err := os.Create("mem.prof")
if err != nil {
panic(err)
}
defer pprof.WriteHeapProfile(memProf)
10 months ago
}
10 months ago
data, err := io.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
head, err := parse(data)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
return
}
m := &model{
head: head,
10 months ago
top: head,
10 months ago
}
p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
_, err = p.Run()
if err != nil {
panic(err)
}
}
type model struct {
10 months ago
termWidth, termHeight int
10 months ago
head, top *node
10 months ago
cursor int // cursor position [0, termHeight)
10 months ago
}
func (m *model) Init() tea.Cmd {
return nil
}
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
10 months ago
m.termWidth = msg.Width
m.termHeight = msg.Height
10 months ago
case tea.MouseMsg:
switch msg.Type {
case tea.MouseWheelUp:
10 months ago
m.up()
10 months ago
case tea.MouseWheelDown:
10 months ago
m.down()
10 months ago
}
case tea.KeyMsg:
return m.handleKey(msg)
}
return m, nil
}
func (m *model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch {
case key.Matches(msg, keyMap.Quit):
return m, tea.Quit
case key.Matches(msg, keyMap.Up):
10 months ago
m.up()
10 months ago
case key.Matches(msg, keyMap.Down):
10 months ago
m.down()
case key.Matches(msg, keyMap.PageUp):
m.cursor = 0
for i := 0; i < m.viewHeight(); i++ {
m.up()
}
case key.Matches(msg, keyMap.PageDown):
m.cursor = m.viewHeight() - 1
for i := 0; i < m.viewHeight(); i++ {
m.down()
}
case key.Matches(msg, keyMap.HalfPageUp):
m.cursor = 0
for i := 0; i < m.viewHeight()/2; i++ {
m.up()
}
case key.Matches(msg, keyMap.HalfPageDown):
m.cursor = m.viewHeight() - 1
for i := 0; i < m.viewHeight()/2; i++ {
m.down()
10 months ago
}
10 months ago
case key.Matches(msg, keyMap.GotoTop):
m.cursor = 0
m.head = m.top
case key.Matches(msg, keyMap.GotoBottom):
m.cursor = 0
m.head = m.findBottom()
m.scrollIntoView()
m.cursor = m.visibleLines() - 1
case key.Matches(msg, keyMap.Collapse):
m.cursorPointsTo().collapse()
m.scrollIntoView()
10 months ago
}
return m, nil
}
10 months ago
func (m *model) up() {
m.cursor--
if m.cursor < 0 {
m.cursor = 0
if m.head.prev != nil {
m.head = m.head.prev
}
}
}
func (m *model) down() {
m.cursor++
n := m.cursorPointsTo()
if n == nil {
m.cursor--
return
}
if m.cursor >= m.viewHeight() {
m.cursor = m.viewHeight() - 1
if m.head.next != nil && !n.atEnd() {
m.head = m.head.next
}
}
}
func (m *model) visibleLines() int {
visibleLines := 0
n := m.head
for n != nil && visibleLines < m.viewHeight() {
visibleLines++
n = n.next
}
return visibleLines
}
func (m *model) scrollIntoView() string {
visibleLines := m.visibleLines()
for visibleLines < m.viewHeight() && m.head.prev != nil {
visibleLines++
m.cursor++
m.head = m.head.prev
}
return fmt.Sprintf("visible lines: %d", visibleLines)
}
10 months ago
func (m *model) View() string {
var screen []byte
head := m.head
10 months ago
10 months ago
// Prerender syntax
colon := currentTheme.Syntax([]byte{':', ' '})
comma := currentTheme.Syntax([]byte{','})
for i := 0; i < m.viewHeight(); i++ {
10 months ago
if head == nil {
break
}
for ident := 0; ident < int(head.depth); ident++ {
screen = append(screen, ' ', ' ')
}
if head.key != nil {
10 months ago
keyColor := currentTheme.Key
if m.cursor == i {
keyColor = currentTheme.Cursor
}
screen = append(screen, keyColor(head.key)...)
screen = append(screen, colon...)
screen = append(screen, colorForValue(head.value)(head.value)...)
} else {
colorize := colorForValue(head.value)
if m.cursor == i {
colorize = currentTheme.Cursor
}
screen = append(screen, colorize(head.value)...)
10 months ago
}
if head.comma {
10 months ago
screen = append(screen, comma...)
10 months ago
}
10 months ago
10 months ago
screen = append(screen, '\n')
head = head.next
}
10 months ago
10 months ago
n := m.cursorPointsTo()
if n == nil {
screen = append(screen, '-')
} else {
screen = append(screen, currentTheme.StatusBar(n.value)...)
10 months ago
}
10 months ago
screen = append(screen, m.scrollIntoView()...)
10 months ago
return string(screen)
}
10 months ago
func (m *model) viewHeight() int {
10 months ago
return m.termHeight - 1
10 months ago
}
func (m *model) cursorPointsTo() *node {
head := m.head
for i := 0; i < m.cursor; i++ {
if head == nil {
10 months ago
break
10 months ago
}
head = head.next
}
return head
}
10 months ago
func (m *model) findBottom() *node {
n := m.head
for n.next != nil {
if n.end != nil {
n = n.end
} else {
n = n.next
}
}
return n
10 months ago
}