summaryrefslogtreecommitdiff
path: root/menu/banner.go
blob: 5a4f561b52affb7b2fa7748d117d58bd90dbe441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package menu

import (
	"github.com/charmbracelet/lipgloss"
	"os/exec"
	"pik/menu/style"
	"pik/model"
	"pik/paths"
	"strings"
)

var (
	BannerStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle()
	})
	BannerSourceLabelStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().Faint(true).MarginRight(1)
	})
	BannerSubItemStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().Faint(true).MarginRight(1)
	})
	BannerSubStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle()
	})
	BannerSelfStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().MarginRight(1).Bold(true)
	})
	BannerPromptStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle()
	})
	BannerArgsStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().MarginLeft(1)
	})
	BannerArgStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle()
	})
	BannerTerminatorColor = lipgloss.Color("1")
	BannerTerminatorStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().Faint(true).Foreground(BannerTerminatorColor)
	})
)

func Banner(source *model.Source, target model.Target, args ...string) string {
	var parts, argParts []string
	parts = append(parts, BannerPromptStyle.Render("> "))
	parts = append(parts, BannerSelfStyle.Render("pik"))
	parts = append(parts, BannerSourceLabelStyle.Render(source.Label()))
	if sub := target.Sub(); sub != nil {
		for i, s := range sub {
			sub[i] = BannerSubItemStyle.Render(s)
		}
		parts = append(parts, BannerSubStyle.Render(sub...))
	}
	parts = append(parts, target.ShortestId())
	if args != nil {
		needsTerminator := false
		for _, a := range args {
			if strings.HasPrefix(a, "-") {
				needsTerminator = true
			}
			argParts = append(argParts, BannerArgStyle.Render(a))
		}

		if needsTerminator {
			argParts = append([]string{BannerTerminatorStyle.Render("--")}, argParts...)
		}

		parts = append(parts, BannerArgsStyle.Render(argParts...))
	}
	result := BannerStyle.Render(lipgloss.JoinHorizontal(lipgloss.Left, parts...))
	return result
}

var (
	CmdStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().Faint(true)
	})
	CmdDirStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle()
	})
	CmdArgStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle()
	})
)

func InlineCmd(cmd *exec.Cmd) string {
	var args []string
	for _, a := range cmd.Args {
		args = append(args, paths.ReplaceHome(a))
	}
	return CmdStyle.Render("  # "+CmdDirStyle.Render(paths.ReplaceHome(cmd.Dir)+":"), CmdArgStyle.Render(args...))
}