summaryrefslogtreecommitdiff
path: root/menu/banner.go
blob: c4fd78af80d940c5b3eb9119fba3e441c7819c0f (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package menu

import (
	"git.ewy.one/pik.git/flags"
	"git.ewy.one/pik.git/menu/style"
	"git.ewy.one/pik.git/model"
	"git.ewy.one/pik.git/paths"
	"github.com/charmbracelet/lipgloss"
	"os/exec"
	"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)
	})
	BannerDryColor = lipgloss.Color("1")
	BannerDryStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().Foreground(BannerDryColor).Bold(true).MarginRight(1)
	})
	BannerDefaultStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().Faint(true).MarginLeft(1)
	})
)

func Banner(source *model.Source, target model.Target, args ...string) string {
	var parts, argParts []string
	if *flags.Dry {
		parts = append(parts, BannerDryStyle.Render("DRY"))
	}
	parts = append(parts, BannerPromptStyle.Render("> "))
	parts = append(parts, BannerSelfStyle.Render("pik"))
	parts = append(parts, BannerSourceLabelStyle.Render(source.Label()))
	def := false
	if sub := target.Sub(); sub != nil {

		// remove "default" invocations
		if sub[len(sub)-1] == target.ShortestId() {
			sub = sub[:len(sub)-1]
			def = true
		}

		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...))
	}
	if def {
		parts = append(parts, BannerDefaultStyle.Render("# "+target.Label()))
	}
	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...))
}

var (
	OverrideStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle()
	})
	OverrideCaretColor = lipgloss.Color("1")
	OverrideCaretStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().Foreground(OverrideCaretColor).Bold(true)
	})
	OverrideTextStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().Faint(true)
	})
)

func OverrideWarning(t model.Target) string {
	return OverrideStyle.Render(lipgloss.JoinHorizontal(lipgloss.Left,
		OverrideCaretStyle.Render("! "),
		OverrideTextStyle.Render("overridden by "+t.Label()),
	))
}