This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tui.go
156 lines (121 loc) · 3.7 KB
/
tui.go
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
package main
import (
"fmt"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
const useHighPerformanceRenderer = false
var (
paddingBlock, paddingInline = 1, 1
basePadding = 1
baseStyle = func() lipgloss.Style {
return lipgloss.NewStyle().Padding(basePadding)
}()
titleStyle = func() lipgloss.Style {
color := getConfig().TitleColor
b := lipgloss.NormalBorder()
return lipgloss.NewStyle().
Foreground(lipgloss.Color(color)).
Border(b, true, false, true, false).
Padding(paddingBlock, paddingInline)
}()
followActiveStyle = func() lipgloss.Style {
return lipgloss.NewStyle().Padding(paddingBlock*2, 0)
}()
activeLineStyle = func() lipgloss.Style {
color := getConfig().ActiveLineColor
return lipgloss.NewStyle().Foreground(lipgloss.Color(color)).Bold(true)
}
)
func (m model) Init() tea.Cmd {
return tea.Batch(listenForActivity(m.state, m.mpdConnection), waitForActivity(m.state))
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
switch msg := msg.(type) {
case tea.KeyMsg:
if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" {
return m, tea.Quit
}
if msg.String() == "ctrl+f" {
m.followLine = !m.followLine && m.isLyricSynced()
}
case responseMsg:
m.title = msg.title
m.lyricType = msg.lyricType
if m.isLyricSynced() {
m.content, m.activeLine = getSyncedContent()
} else {
m.followLine = false
m.content = parser.ToString()
}
cmds = append(cmds, waitForActivity(m.state))
case tea.WindowSizeMsg:
headerHeight := lipgloss.Height(m.headerView())
if !m.ready {
m.viewport = viewport.New(msg.Width, msg.Height-headerHeight)
m.viewport.HighPerformanceRendering = useHighPerformanceRenderer
m.ready = true
} else {
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height - headerHeight
}
}
// Just in case I decided to add style to this
content := m.viewport.Style.Render(m.content)
m.viewport.SetContent(content)
if m.lyricType == LyricSynced {
isVisible := isLineVisible(m.viewport.YOffset, m.viewport.Height, m.activeLine, m.viewport.TotalLineCount())
if !isVisible && m.followLine {
m.viewport.SetYOffset(m.activeLine)
}
}
m.viewport, cmd = m.viewport.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m model) View() string {
if !m.ready {
return "\n Initializing..."
}
return baseStyle.Render(fmt.Sprintf("%s\n%s", m.headerView(), m.viewport.View()))
}
func getSyncedContent() (string, int) {
content := ""
for index, lrcLine := range parser.Lyrics {
newLine := lipgloss.NewStyle().UnsetForeground().Render(lrcLine.Content)
if index == runner.CurIndex() {
newLine = activeLineStyle().Render(newLine)
}
if lrcLine.Timestamp != parser.Lyrics[len(parser.Lyrics)-1].Timestamp {
content += newLine + "\n"
}
}
return content, runner.CurIndex()
}
func isLineVisible(YOffset, height, lineIndex, totalLines int) bool {
if lineIndex < 0 || lineIndex >= totalLines {
return false
}
top := max(0, YOffset)
bottom := clamp(YOffset+height, top, totalLines)
return lineIndex >= top && lineIndex < bottom
}
func (m model) headerView() string {
syncIcon := ""
followActiveStyle.Foreground(lipgloss.Color("2"))
if !m.followLine {
syncIcon = ""
followActiveStyle.Foreground(lipgloss.Color("9"))
}
syncIcon = followActiveStyle.Render(syncIcon)
title := titleStyle.Width((m.viewport.Width - paddingInline - basePadding) - lipgloss.Width(syncIcon)).Render(m.title)
return lipgloss.JoinHorizontal(lipgloss.Top, title, syncIcon)
}
func (m model) isLyricSynced() bool {
return m.lyricType == LyricSynced
}