~mariusor/motley

21161e8c448073bff78661774a4373e69541bb3c — Marius Orcsik 4 months ago d4e0ebe
Improved logo rendering
3 files changed, 48 insertions(+), 11 deletions(-)

M fedbox.go
M statusbar.go
M ui.go
M fedbox.go => fedbox.go +29 -5
@@ 92,6 92,9 @@ func FedBOX(rootIRIs []string, st []config.Storage, e env.Type, l *logrus.Logger

func (f *fedbox) Load(iri pub.IRI, ff ...filters.Check) (pub.Item, error) {
	for _, st := range f.stores {
		if pub.IsNil(st.root) || !iri.Contains(st.root.GetLink(), true) {
			continue
		}
		col, err := st.s.Load(iri, ff...)
		if err == nil {
			return col, nil


@@ 669,12 672,33 @@ func (a accumFn) LoadFromSearch(ctx context.Context, f *fedbox, iris ...pub.IRI)
	return nil
}

func pubUrl(it pub.Item) string {
	name := ""
func name(it pub.Item) string {
	n := ""
	pub.OnActor(it, func(a *pub.Actor) error {
		if a.PreferredUsername != nil {
			n = a.PreferredUsername.First().String()
		}
		return nil
	})
	if n != "" {
		return n
	}
	pub.OnObject(it, func(o *pub.Object) error {
		u, _ := o.URL.GetLink().URL()
		name = u.Hostname()
		if o.Name != nil {
			n = o.Name.First().String()
			return nil
		}
		if pub.IsNil(o.URL) {
			if u, err := o.URL.GetLink().URL(); err == nil {
				n = u.Hostname()
				return nil
			}
			return nil
		}
		if u, err := o.ID.GetLink().URL(); err == nil {
			n = u.Hostname()
		}
		return nil
	})
	return name
	return n
}

M statusbar.go => statusbar.go +6 -4
@@ 15,8 15,8 @@ import (
	"github.com/muesli/reflow/margin"
	"github.com/muesli/reflow/truncate"
	te "github.com/muesli/termenv"
	"golang.org/x/text/language"
	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

type statusState uint8


@@ 41,7 41,6 @@ type statusModel struct {
	*commonModel

	width int
	logo  string
	state statusState

	spinner spinner.Model


@@ 98,13 97,16 @@ func ucfirst(s string) string {
	}
	return strings.Join(pieces, " ")
}

func (s *statusModel) statusBarView(b *strings.Builder) {
	percent := clamp(int(math.Round(s.percent)), 0, 100)
	scrollPercent := fmt.Sprintf(" %d%% ", percent)

	spinner := s.spinner.View()
	logo := logoView(name(s.root), s.env)

	// Empty space
	w := max(0, s.width-lipgloss.Width(spinner)-lipgloss.Width(s.logo)-lipgloss.Width(scrollPercent)-1)
	w := max(0, s.width-lipgloss.Width(spinner)-lipgloss.Width(logo)-lipgloss.Width(scrollPercent)-1)

	render := statusBarMessageStyle
	message := truncate.StringWithTail(s.message, uint(w), ellipsis)


@@ 113,7 115,7 @@ func (s *statusModel) statusBarView(b *strings.Builder) {
		message = ucfirst(s.error.Error())
	}

	b.WriteString(s.logo)
	b.WriteString(logo)
	b.WriteString(render(
		lipgloss.JoinHorizontal(lipgloss.Left,
			margin.String(message, uint(w), 1),

M ui.go => ui.go +13 -2
@@ 10,6 10,7 @@ import (
	"github.com/charmbracelet/bubbles/key"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	pub "github.com/go-ap/activitypub"
	tree "github.com/mariusor/bubbles-tree"
	"github.com/muesli/reflow/wordwrap"
	"github.com/sirupsen/logrus"


@@ 106,18 107,22 @@ func newModel(ff *fedbox, env env.Type, l *logrus.Logger) *model {
	m := new(model)
	m.commonModel = new(commonModel)
	m.commonModel.logFn = l.Infof
	m.commonModel.env = env

	m.f = ff

	m.tree = newTreeModel(m.commonModel, initNodes(m.f))
	m.pager = newItemModel(m.commonModel)
	m.status = newStatusModel(m.commonModel)
	m.status.logo = logoView(pubUrl(ff.getRootNodes()), env)

	return m
}

type commonModel struct {
	f     *fedbox
	f    *fedbox
	root pub.Item
	env  env.Type

	logFn func(string, ...interface{})
}



@@ 192,6 197,12 @@ func (m *model) update(msg tea.Msg) tea.Cmd {
		m.currentNode = msg
		ctx, _ := context.WithTimeout(context.Background(), time.Millisecond*300)
		cmd := m.loadDepsForNode(ctx, m.currentNode)
		for _, st := range m.f.stores {
			if msg.GetLink().Contains(st.root.GetLink(), true) {
				m.root = st.root
				break
			}
		}
		cmds = append(cmds, nodeUpdateCmd(m.currentNode), cmd)
	case advanceMsg:
		cmds = append(cmds, m.Advance(msg))