-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
261 lines (220 loc) · 5.55 KB
/
main.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
customHelpMenu "mankka/customHelpMenu"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
// Creating color theme for application
var (
White = lipgloss.Color("#cad3f5")
Green = lipgloss.Color("#a6da95")
Mauve = lipgloss.Color("#c6a0f6")
Lavender = lipgloss.Color("#b8c0e0")
Blue = lipgloss.Color("#8aadf4")
)
// Initialize styles for each section
var (
pathSelectStyle = lipgloss.NewStyle().
Padding(0, 3).
Foreground(Green)
inputErrStyle = lipgloss.NewStyle().
Padding(0, 3).
Foreground(Mauve)
pathselectQuestionStyle = lipgloss.NewStyle().
Padding(0, 3).
Foreground(Blue).
Bold(true)
listStyle = lipgloss.NewStyle().
Padding(0, 1).
BorderForeground(Mauve)
)
type Item struct {
title, desc string
}
func (i Item) Title() string { return i.title }
func (i Item) Description() string { return i.desc }
func (i Item) FilterValue() string { return i.title }
type Model struct {
list list.Model
ti textinput.Model
inputErr string
focused int
songDir []string
}
func (m Model) Init() tea.Cmd {
return tea.SetWindowTitle("Mankka")
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
if m.focused == 0 {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
case "enter", "tab":
m.inputErr = ""
m.focused = 1
cmds = m.createListOfFiles()
return m, tea.Batch(cmds...)
}
}
m.ti, cmd = m.ti.Update(msg)
} else if m.focused == 1 {
switch msg := msg.(type) {
case tea.KeyMsg:
if m.list.FilterState() == list.Filtering {
break
}
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
case "tab":
m.focused = 0
m.resetLists()
case "enter":
var songId string
songId = parseSongId(m.list.SelectedItem().FilterValue())
args := []string{"--volume=50", "--geometry=800x600", "--playlist-start=" + songId, "--loop-playlist=inf", "--no-video"}
args = append(args, m.songDir...)
return m, tea.Batch(
//tea.Printf("Selected song: %s", m.list.Index()),
tea.ExecProcess(exec.Command("mpv", args...), nil),
)
}
}
m.list, cmd = m.list.Update(msg)
}
return m, cmd
}
func (m Model) View() string {
if m.focused == 1 {
pathSelectStyle = lipgloss.NewStyle().
Padding(0, 1).
Foreground(White)
m.ti.TextStyle = pathSelectStyle
} else {
pathSelectStyle = lipgloss.NewStyle().
Padding(0, 1).
Foreground(Green)
}
return ("\n" +
pathselectQuestionStyle.
Render("Give a path to your directory to play files from: ") +
"\n\n" +
pathSelectStyle.
Render(m.ti.View()) +
"\n\n" +
inputErrStyle.Render(m.inputErr) +
"\n\n" +
listStyle.
Render(m.list.View()) +
"\n\n" +
fmt.Sprintln(customHelpMenu.CustomHelpView()) +
"\n")
}
func (m *Model) resetLists() {
if len(m.list.Items()) > 0 {
for i := len(m.list.Items()) - 1; i >= 0; i-- {
m.list.RemoveItem(i)
}
}
m.songDir = []string{}
}
func (m *Model) createListOfFiles() []tea.Cmd {
var (
cmds []tea.Cmd
insCmd tea.Cmd
)
dir, err := os.ReadDir(m.ti.Value())
if err != nil {
m.inputErr = "Couldn't find your path give another one"
m.focused = 0
return cmds
}
fileID := 0
for _, value := range dir {
// Make files and directories different from each other
m.songDir = append(m.songDir, m.ti.Value()+"/"+value.Name())
newEntry := Item{title: strconv.Itoa(fileID) + ". " + value.Name(), desc: ""}
insCmd = m.list.InsertItem(fileID, newEntry)
fileID++
cmds = append(cmds, insCmd)
}
return cmds
}
func parseSongId(inputSong string) string {
slicedcInputSong := strings.Split(inputSong, ".")
return slicedcInputSong[0]
}
func initialModel(CliArg []string) Model {
ti := textinput.New()
ti.Placeholder = "/Your/path/here"
if len(CliArg) > 1 && CliArg[1] == "." {
value, _ := os.Getwd()
ti.SetValue(value)
} else if len(CliArg) > 1 {
ti.SetValue(CliArg[1])
} else {
ti.SetValue("")
}
ti.Focus()
ti.TextStyle = pathSelectStyle
inputErr := ""
items := []list.Item{}
listStyling := list.NewDefaultDelegate()
listStyling.Styles.SelectedTitle = lipgloss.NewStyle().
Foreground(Green).
Padding(0, 1).
Border(lipgloss.NormalBorder(), false, false, false, true).BorderForeground(Green)
listStyling.Styles.SelectedDesc = lipgloss.NewStyle().
Foreground(Green).
Padding(0, 2)
listStyling.Styles.NormalTitle = lipgloss.NewStyle().
Foreground(Lavender).
Padding(0, 2)
listStyling.Styles.NormalDesc = lipgloss.NewStyle().
Foreground(Lavender).
Padding(0, 2)
list := list.New(items, listStyling, 100, 16)
list.Title = "List of playable files:"
list.Styles.Title = lipgloss.NewStyle().
Foreground(Blue).
Align(lipgloss.Left).
Bold(true)
list.Styles.NoItems = lipgloss.NewStyle().
Padding(0, 2)
list.SetShowStatusBar(false)
list.SetShowHelp(false)
//list.SetFilteringEnabled(false)
initSongDir := []string{}
return Model{
list: list,
ti: ti,
inputErr: inputErr,
focused: 0,
songDir: initSongDir,
}
}
func main() {
CliArg := os.Args
if _, err := exec.LookPath("mpv"); err != nil {
fmt.Println("You don't have mpv installed on your machine")
fmt.Println("Install mpv via installation script or from https://mpv.io/")
os.Exit(1)
}
if _, err := tea.NewProgram(initialModel(CliArg), tea.WithAltScreen()).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}