~mariusor/motley

bb7abf43ff490e913e633302429a5218ac88b294 — Marius Orcsik 2 months ago c698275
Dispatch paint messages to statusbar and pager
3 files changed, 51 insertions(+), 24 deletions(-)

M pager.go
M statusbar.go
M ui.go
M pager.go => pager.go +5 -1
@@ 202,7 202,7 @@ func (p *pagerModel) writeItem(s io.Writer, it pub.Item) error {
	return fmt.Errorf("unknown activitypub object of type %T", it)
}

func (p *pagerModel) showItem(it pub.Item) error {
func (p *pagerModel) showItem(it pub.Item) tea.Cmd {
	s := strings.Builder{}
	if err := p.writeItem(&s, it); err != nil {
		p.logFn("err: %s", err)


@@ 227,6 227,10 @@ func (p *pagerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	)

	switch msg := msg.(type) {
	case paintMsg:
		if n := msg.n; n != nil {
			cmds = append(cmds, p.showItem(n.Item))
		}
	case tea.KeyMsg:
		switch msg.String() {
		case "home", "g":

M statusbar.go => statusbar.go +37 -0
@@ 10,6 10,7 @@ import (
	"github.com/charmbracelet/bubbles/spinner"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	pub "github.com/go-ap/activitypub"
	rw "github.com/mattn/go-runewidth"
	"github.com/muesli/reflow/margin"
	"github.com/muesli/reflow/truncate"


@@ 120,6 121,38 @@ func (s *statusModel) spin(msg tea.Msg) tea.Cmd {
	return tick
}

type statusNode struct {
	*n
}

func (a statusNode) View() string {
	s := strings.Builder{}
	switch it := a.Item; it.(type) {
	case pub.ItemCollection, pub.IRI:
		fmt.Fprintf(&s, "Collection %s: %d items", a.n.n, len(a.n.c))
	case pub.Item:
		switch typ := a.GetType(); {
		case pub.IntransitiveActivityTypes.Contains(typ) || pub.ActivityTypes.Contains(typ):
			pub.OnActivity(a.Item, func(act *pub.Activity) error {
				ob := node(act.Object)
				fmt.Fprintf(&s, "%s >> %s", typ, statusNode{ob}.View())
				return nil
			})
		case pub.ActorTypes.Contains(typ) || pub.ObjectTypes.Contains(typ):
			pub.OnObject(a.Item, func(ob *pub.Object) error {
				fmt.Fprintf(&s, "%s: %s", typ, ob.Name.First().String())
				return nil
			})
		case typ == "":
			pub.OnObject(a.Item, func(ob *pub.Object) error {
				fmt.Fprintf(&s, "%s", ob.Name.First().String())
				return nil
			})
		}
	}
	return s.String()
}

func (s *statusModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	var cmd tea.Cmd



@@ 130,6 163,10 @@ func (s *statusModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
		if s.state.Is(statusBusy) {
			cmd = s.spin(msg)
		}
	case paintMsg:
		if n := msg.n; n != nil {
			cmd = s.showStatusMessage(statusNode{n}.View())
		}
	case statusState:
		if !msg.Is(statusError) && s.state.Is(statusError) {
			s.state ^= statusError

M ui.go => ui.go +9 -23
@@ 170,27 170,26 @@ func (m *model) setSize(w, h int) {
	}
}

type paintMsg struct{}
type paintMsg struct {
	*n
}

func paintCmd() tea.Msg {
	return paintMsg{}
func paintCmd(n *n) tea.Cmd {
	return func() tea.Msg {
		return paintMsg{n}
	}
}

func (m *model) update(msg tea.Msg) tea.Cmd {
	cmds := make([]tea.Cmd, 0)

	switch msg := msg.(type) {
	case paintMsg:
		if m.currentNode != nil {
			m.logFn("current node: %s", m.currentNode.n)
			m.displayItem(m.currentNode)
		}
	case *n:
		m.currentNodePosition = m.tree.list.Cursor()
		m.currentNode = msg
		ctx, _ := context.WithTimeout(context.Background(), time.Millisecond*300)
		cmd := m.loadDepsForNode(ctx, m.currentNode)
		cmds = append(cmds, paintCmd, cmd)
		cmds = append(cmds, paintCmd(m.currentNode), cmd)
	case advanceMsg:
		cmds = append(cmds, m.Advance(msg))
	case tea.KeyMsg:


@@ 227,7 226,7 @@ func (m *model) update(msg tea.Msg) tea.Cmd {
		}
	}
	if m.status.state.Is(statusBusy) && !m.tree.IsSyncing() {
		cmds = append(cmds, m.status.stoppedLoading, paintCmd)
		cmds = append(cmds, m.status.stoppedLoading, paintCmd(m.currentNode))
	}
	cmds = append(cmds, m.updatePager(msg))
	cmds = append(cmds, m.updateStatusBar(msg))


@@ 359,19 358,6 @@ type advanceMsg struct {
	*n
}

func (m *model) displayItem(n *n) tea.Cmd {
	it := n.Item
	switch it.(type) {
	case pub.ItemCollection, pub.IRI:
		m.pager.showItem(it)
		return m.status.showStatusMessage(fmt.Sprintf("Collection %s: %d items", n.n, len(n.c)))
	case pub.Item:
		m.pager.showItem(it)
		return m.status.showStatusMessage(fmt.Sprintf("%s: %s", it.GetType(), it.GetLink()))
	}
	return nil
}

func renderWithBorder(s string, focused bool) string {
	borderColour := hintColor
	if !focused {