summaryrefslogtreecommitdiff
path: root/internal/client/ui/loadscreen/loadscreen.go
blob: 869237fd4e5cd82c6a64316bf604c53ce83897ff (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package loadscreen

import (
	"time"

	"github.com/charmbracelet/bubbles/spinner"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"

	"github.com/kyren223/eko/internal/client/ui"
	"github.com/kyren223/eko/pkg/assert"
)

var (
	style         = lipgloss.NewStyle().Border(lipgloss.ThickBorder()).Padding(1, 3).MarginTop(2)
	loadingFrames = circleTrail(4, 4, 0, true, "  ", "░░", "▒▒", "▓▓", "██")
	loading       = spinner.Spinner{
		Frames: loadingFrames,
		FPS:    time.Second / time.Duration(len(loadingFrames)),
	}
)

func circleTrail(width int, height int, offset int, clockwise bool, bg string, trail ...string) (states []string) {
	slots := (width+height)*2 - 4
	slotWidth := lipgloss.Width(bg)
	slotHeight := lipgloss.Height(bg)

	assert.Assert(slots-len(trail) >= 0, "not enough space to fit trail")
	assert.AddData("slotWidth", slotWidth)
	assert.AddData("slotHeight", slotHeight)
	defer assert.RemoveData("slotWidth")
	defer assert.RemoveData("slotHeight")
	for _, elem := range trail {
		w := lipgloss.Width(elem)
		h := lipgloss.Height(elem)
		assert.AddData("elemWidth", w)
		assert.AddData("elemHeight", h)
		assert.Assert(slotWidth == w, "bg and all trails must have the same height")
		assert.Assert(slotHeight == h, "bg and all trails must have the same height")
		assert.RemoveData("elemWidth")
		assert.RemoveData("elemHeight")
	}

	matrix := make([][]string, height)
	for i := range matrix {
		matrix[i] = make([]string, width)
	}

	for i := 0; i < slots; i++ {
		for y := range matrix {
			for x := range matrix[y] {
				matrix[y][x] = bg
			}
		}

		x, y := 0, 0
		for j := 0; j < i+offset; j++ {
			if clockwise {
				if y == 0 && x != width-1 {
					x++
				} else if x == width-1 && y != height-1 {
					y++
				} else if y == height-1 && x != 0 {
					x--
				} else if x == 0 && y != 0 {
					y--
				}
			} else {
				if x == 0 && y != height-1 {
					y++
				} else if y == height-1 && x != width-1 {
					x++
				} else if x == width-1 && y != 0 {
					y--
				} else if y == 0 && x != 0 {
					x--
				}
			}
		}

		for _, trail := range trail {
			matrix[y][x] = trail
			if clockwise {
				if y == 0 && x != width-1 {
					x++
				} else if x == width-1 && y != height-1 {
					y++
				} else if y == height-1 && x != 0 {
					x--
				} else if x == 0 && y != 0 {
					y--
				}
			} else {
				if x == 0 && y != height-1 {
					y++
				} else if y == height-1 && x != width-1 {
					x++
				} else if x == width-1 && y != 0 {
					y--
				} else if y == 0 && x != 0 {
					x--
				}
			}
		}

		var rows []string
		for _, row := range matrix {
			rows = append(rows, lipgloss.JoinHorizontal(0, row...))
		}
		state := lipgloss.JoinVertical(0, rows...)
		states = append(states, state)
	}

	return states
}

type Updater func(msg tea.Msg) tea.Cmd

type Model struct {
	sp      spinner.Model
	content string
	updater Updater
}

func New(content string, updater Updater) Model {
	width := lipgloss.Width(content)
	height := lipgloss.Height(content)
	content = lipgloss.NewStyle().Width(width).Height(height).Render(content)
	return Model{
		sp:      spinner.New(spinner.WithSpinner(loading)),
		content: style.Render(content),
	}
}

func (m Model) Init() tea.Cmd {
	return m.sp.Tick
}

func (m Model) View() string {
	return lipgloss.Place(
		ui.Width, ui.Height,
		lipgloss.Center, lipgloss.Center,
		lipgloss.JoinVertical(lipgloss.Center, m.sp.View(), m.content),
	)
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {

	case tea.KeyMsg:
		key := msg.Type
		switch key {
		case tea.KeyCtrlC:
			return m, tea.Quit
		}

	case spinner.TickMsg:
		var cmd tea.Cmd
		m.sp, cmd = m.sp.Update(msg)
		return m, cmd
	}

	return m, m.updater(msg)
}