~dricottone/nspotify

nspotify/tracks.go -rw-r--r-- 1.5 KiB
1064d33fDominic Ricottone Noted one more TODO 5 months ago
                                                                                
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
package main

// Spotify tracks interactions.

import (
	"fmt"
	"strings"

	"github.com/zmb3/spotify/v2"
	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

// Format an array of Spotify artists into a `&`-delimited list of artist
// names.
func FormatArtists(artists []spotify.SimpleArtist) string {
	buff := []string{}
	for _, artist := range artists {
		buff = append(buff, artist.Name)
		//artist_id := artist.ID
		//artist_uri := artist.URI
	}

	return strings.Join(buff, " & ")
}

// Format a duration in milliseconds into `[HH]:[MM]:[SS]` (or `[MM]:[SS]` if
// duration is less than 1 hour).
func FormatDuration(ms int) string {
	ms /= 1000
	s := ms % 60
	ms /= 60
	m := ms % 60
	h := ms / 60
	if h == 0 {
		return fmt.Sprintf("%d:%02d", m, s)
	}
	return fmt.Sprintf("%d:%02d:%02d", h, m, s)
}

// Create a row of table cells from a Spotify track.
func IntoCells(track *spotify.FullTrack) []*tview.TableCell {
	name := tview.NewTableCell(track.Name).SetTextColor(tcell.ColorWhite).SetReference(track.URI)
	artist := tview.NewTableCell(FormatArtists(track.Artists)).SetTextColor(tcell.ColorWhite)
	album := tview.NewTableCell(track.Album.Name).SetTextColor(tcell.ColorWhite)
	duration := tview.NewTableCell(FormatDuration(track.Duration)).SetAlign(tview.AlignRight).SetTextColor(tcell.ColorWhite)

	//id := track.ID
	//number := track.TrackNumber
	//album_id := track.Album.ID
	//album_uri := track.Album.URI
	//album_date := track.Album.ReleaseDate

	return []*tview.TableCell{name, artist, album, duration}
}