summaryrefslogtreecommitdiff
path: root/menu/source.go
blob: 913c1ceb8b1bdcbb8e1c1e346e6e2e7f9294e723 (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
package menu

import (
	"github.com/charmbracelet/lipgloss"
	"pik/menu/style"
	"pik/model"
	"strconv"
)

var (
	SourceStyle = style.New(func() lipgloss.Style {
		st := lipgloss.NewStyle().PaddingBottom(1)
		return st
	})
	SourceHeaderBackground = lipgloss.Color("5")
	SourceHeaderStyle      = style.New(func() lipgloss.Style {
		st := lipgloss.NewStyle()
		return st

	})
	SourceLabelStyle = style.New(func() lipgloss.Style {
		st := lipgloss.NewStyle().Border(lipgloss.OuterHalfBlockBorder(), false, false, false, true).Background(SourceHeaderBackground).BorderBackground(SourceHeaderBackground).PaddingRight(1).PaddingLeft(1).MarginRight(1)
		return st

	})
	SourceIconStyle = style.New(func() lipgloss.Style {
		st := lipgloss.NewStyle().PaddingRight(1)
		return st

	})
	SourceTargetsStyle = style.New(func() lipgloss.Style {
		st := lipgloss.NewStyle()
		return st
	})
	SourcePathStyle = style.New(func() lipgloss.Style {
		st := lipgloss.NewStyle().Faint(true)
		return st
	})
)

func (m *Model) Source(src *model.HydratedSource) string {
	targets := make([]string, 0, len(src.Targets))
	for _, t := range src.HydratedTargets {
		targets = append(targets, m.Target(t))
	}

	targetContent := lipgloss.JoinVertical(lipgloss.Top, targets...)

	icon := SourceIconStyle.Render(Icon(src.Icon))

	parts := []string{
		SourceHeaderStyle.Render(lipgloss.JoinHorizontal(lipgloss.Left, SourceLabelStyle.Render(lipgloss.JoinHorizontal(lipgloss.Left, icon, src.Label()), SourcePathStyle.Render(src.ShortPath())))),
		SourceTargetsStyle.Render(targetContent),
	}

	if src.Git != nil {
		parts = append(parts, Git(src.Git))
	}

	return SourceStyle.Render(lipgloss.JoinVertical(lipgloss.Top,
		parts...,
	))
}

var (
	StateStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().MarginBottom(1)
	})
)

func (m *Model) State(st *model.HydratedState) string {
	sources := make([]string, 0, len(st.Sources))
	for _, hs := range st.HydratedSources {
		sources = append(sources, m.Source(hs))
	}

	return StateStyle.Render(lipgloss.JoinVertical(lipgloss.Top, sources...))
}

var (
	GitColor     = lipgloss.Color("4")
	GitInfoStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().Background(GitColor).Border(lipgloss.OuterHalfBlockBorder(), false, false, false, true).BorderBackground(GitColor).Padding(0, 1)
	})
	GitStatusStyle = style.New(func() lipgloss.Style {
		return lipgloss.NewStyle().Bold(true).Background(GitColor).PaddingLeft(1)
	})
	GitAddColor = lipgloss.Color("2")
	GitAddStyle = style.New(func() lipgloss.Style {
		return GitStatusStyle.Get().Foreground(GitAddColor)
	})
	GitRemoveColor = lipgloss.Color("1")
	GitRemoveStyle = style.New(func() lipgloss.Style {
		return GitStatusStyle.Get().Foreground(GitRemoveColor)
	})
	GitChangeColor = lipgloss.Color("5")
	GitChangeStyle = style.New(func() lipgloss.Style {
		return GitStatusStyle.Get().Foreground(GitChangeColor)
	})
)

func Git(info *model.GitInfo) string {
	var parts = []string{
		"  ",
		info.Branch,
	}

	if info.Insertions > 0 {
		parts = append(parts, GitAddStyle.Render("+"+strconv.Itoa(info.Insertions)))
		parts = append(parts, GitRemoveStyle.Render("-"+strconv.Itoa(info.Deletions)))
		parts = append(parts, GitChangeStyle.Render("~"+strconv.Itoa(info.Changes)))
	}

	return GitInfoStyle.Render(lipgloss.JoinHorizontal(lipgloss.Left,
		parts...,
	))
}