Refactoring

This commit is contained in:
aditya-K2
2023-05-01 12:55:04 +05:30
parent defe13c034
commit 7d20da67d9
5 changed files with 142 additions and 88 deletions

View File

@@ -38,35 +38,14 @@ var (
Flex *tview.Flex
)
func setStyles() {
TrackStyle = config.Config.Colors.Track.Style()
AlbumStyle = config.Config.Colors.Album.Style()
ArtistStyle = config.Config.Colors.Artist.Style()
TimeStyle = config.Config.Colors.Timestamp.Style()
GenreStyle = config.Config.Colors.Genre.Style()
PlaylistNavStyle = config.Config.Colors.PlaylistNav.Style()
NavStyle = config.Config.Colors.Nav.Style()
ContextMenuStyle = config.Config.Colors.ContextMenu.Style()
NotSelectableStyle = config.Config.Colors.Null.Style()
func onConfigChange() {
setStyles()
setBorderRunes()
if coverArt != nil {
coverArt.RefreshState()
}
if config.Config.RoundedCorners {
tview.Borders.TopLeft = '╭'
tview.Borders.TopRight = '╮'
tview.Borders.BottomRight = '╯'
tview.Borders.BottomLeft = '╰'
tview.Borders.Vertical = '│'
tview.Borders.Horizontal = '─'
tview.Borders.TopLeftFocus = '╭'
tview.Borders.TopRightFocus = '╮'
tview.Borders.BottomRightFocus = '╯'
tview.Borders.BottomLeftFocus = '╰'
tview.Borders.VerticalFocus = '│'
tview.Borders.HorizontalFocus = '─'
tview.Styles.BorderColorFocus = config.Config.Colors.BorderFocus.Foreground()
tview.Styles.BorderColor = config.Config.Colors.Border.Foreground()
}
}
@@ -109,8 +88,8 @@ func rectWatcher() {
}
func NewApplication() *tview.Application {
setStyles()
config.OnConfigChange = setStyles
onConfigChange()
config.OnConfigChange = onConfigChange
App = tview.NewApplication()
root = NewRoot()

View File

@@ -1,18 +1,15 @@
package ui
import (
"errors"
"fmt"
"image"
"os"
"path/filepath"
"github.com/aditya-K2/gspt/config"
"github.com/aditya-K2/tview"
"github.com/aditya-K2/utils"
"github.com/gdamore/tcell/v2"
"github.com/nfnt/resize"
"github.com/zmb3/spotify/v2"
"gitlab.com/diamondburned/ueberzug-go"
)
@@ -52,24 +49,6 @@ func getImg(uri string) (image.Image, error) {
return img, nil
}
func fileName(a spotify.SimpleAlbum) string {
return fmt.Sprintf(filepath.Join(config.Config.CacheDir, "%s.jpg"), a.ID)
}
func getFontWidth() (int, int, error) {
w, h, err := ueberzug.GetParentSize()
if err != nil {
return 0, 0, err
}
_, _, rw, rh := root.Root.GetRect()
if rw == 0 || rh == 0 {
return 0, 0, errors.New("Unable to get row width and height")
}
fw := w / rw
fh := h / rh
return fw, fh, nil
}
func (c *CoverArt) RefreshState() {
if c.image != nil {
c.image.Clear()

68
ui/styles.go Normal file
View File

@@ -0,0 +1,68 @@
package ui
import (
"github.com/aditya-K2/gspt/config"
"github.com/aditya-K2/tview"
)
var (
borders = map[bool]map[string]rune{
true: {
"TopLeft": '╭',
"TopRight": '╮',
"BottomRight": '╯',
"BottomLeft": '╰',
"Vertical": '│',
"Horizontal": '─',
"TopLeftFocus": '╭',
"TopRightFocus": '╮',
"BottomRightFocus": '╯',
"BottomLeftFocus": '╰',
"VerticalFocus": '│',
"HorizontalFocus": '─',
},
false: {
"TopLeft": tview.Borders.TopLeft,
"TopRight": tview.Borders.TopRight,
"BottomRight": tview.Borders.BottomRight,
"BottomLeft": tview.Borders.BottomLeft,
"Vertical": tview.Borders.Vertical,
"Horizontal": tview.Borders.Horizontal,
"TopLeftFocus": tview.Borders.TopLeftFocus,
"TopRightFocus": tview.Borders.TopRightFocus,
"BottomRightFocus": tview.Borders.BottomRightFocus,
"BottomLeftFocus": tview.Borders.BottomLeftFocus,
"VerticalFocus": tview.Borders.VerticalFocus,
"HorizontalFocus": tview.Borders.HorizontalFocus,
},
}
)
func setBorderRunes() {
tview.Borders.TopLeft = borders[config.Config.RoundedCorners]["TopLeft"]
tview.Borders.TopRight = borders[config.Config.RoundedCorners]["TopRight"]
tview.Borders.BottomRight = borders[config.Config.RoundedCorners]["BottomRight"]
tview.Borders.BottomLeft = borders[config.Config.RoundedCorners]["BottomLeft"]
tview.Borders.Vertical = borders[config.Config.RoundedCorners]["Vertical"]
tview.Borders.Horizontal = borders[config.Config.RoundedCorners]["Horizontal"]
tview.Borders.TopLeftFocus = borders[config.Config.RoundedCorners]["TopLeftFocus"]
tview.Borders.TopRightFocus = borders[config.Config.RoundedCorners]["TopRightFocus"]
tview.Borders.BottomRightFocus = borders[config.Config.RoundedCorners]["BottomRightFocus"]
tview.Borders.BottomLeftFocus = borders[config.Config.RoundedCorners]["BottomLeftFocus"]
tview.Borders.VerticalFocus = borders[config.Config.RoundedCorners]["VerticalFocus"]
tview.Borders.HorizontalFocus = borders[config.Config.RoundedCorners]["HorizontalFocus"]
}
func setStyles() {
TrackStyle = config.Config.Colors.Track.Style()
AlbumStyle = config.Config.Colors.Album.Style()
ArtistStyle = config.Config.Colors.Artist.Style()
TimeStyle = config.Config.Colors.Timestamp.Style()
GenreStyle = config.Config.Colors.Genre.Style()
PlaylistNavStyle = config.Config.Colors.PlaylistNav.Style()
NavStyle = config.Config.Colors.Nav.Style()
ContextMenuStyle = config.Config.Colors.ContextMenu.Style()
NotSelectableStyle = config.Config.Colors.Null.Style()
tview.Styles.BorderColorFocus = config.Config.Colors.BorderFocus.Foreground()
tview.Styles.BorderColor = config.Config.Colors.Border.Foreground()
}

67
ui/utils.go Normal file
View File

@@ -0,0 +1,67 @@
package ui
import (
"errors"
"fmt"
"path/filepath"
"github.com/aditya-K2/gspt/config"
"github.com/aditya-K2/gspt/spt"
"github.com/zmb3/spotify/v2"
"gitlab.com/diamondburned/ueberzug-go"
)
func openPlaylistMenu(handler func(playlistId spotify.SimplePlaylist)) {
c := NewMenu()
cc := []string{}
// TODO: Better Error Handling
plist, ch := spt.CurrentUserPlaylists()
err := <-ch
if err != nil {
SendNotification(err.Error())
return
}
for _, v := range *(plist) {
cc = append(cc, v.Name)
}
c.Content(cc)
c.Title("Add to Playlist")
c.SetSelectionHandler(func(sel int) {
handler((*plist)[sel])
})
root.AddCenteredWidget(c)
}
func addToPlaylist(tracks []spotify.ID) {
openPlaylistMenu(func(sp spotify.SimplePlaylist) {
aerr := spt.AddTracksToPlaylist(sp.ID, tracks...)
if aerr != nil {
SendNotification(aerr.Error())
return
} else {
s := ""
if len(tracks) > 1 {
s = "s"
}
SendNotification("Added %d track%s to %s", len(tracks), s, sp.Name)
}
})
}
func fileName(a spotify.SimpleAlbum) string {
return fmt.Sprintf(filepath.Join(config.Config.CacheDir, "%s.jpg"), a.ID)
}
func getFontWidth() (int, int, error) {
w, h, err := ueberzug.GetParentSize()
if err != nil {
return 0, 0, err
}
_, _, rw, rh := root.Root.GetRect()
if rw == 0 || rh == 0 {
return 0, 0, errors.New("Unable to get row width and height")
}
fw := w / rw
fh := h / rh
return fw, fh, nil
}

View File

@@ -2,9 +2,7 @@ package ui
import (
"github.com/aditya-K2/gspt/config"
"github.com/aditya-K2/gspt/spt"
"github.com/gdamore/tcell/v2"
"github.com/zmb3/spotify/v2"
)
type defView struct {
@@ -73,40 +71,3 @@ type DefaultView struct {
}
func (d *DefaultView) DisableVisualMode() bool { return false }
func openPlaylistMenu(handler func(playlistId spotify.SimplePlaylist)) {
c := NewMenu()
cc := []string{}
// TODO: Better Error Handling
plist, ch := spt.CurrentUserPlaylists()
err := <-ch
if err != nil {
SendNotification(err.Error())
return
}
for _, v := range *(plist) {
cc = append(cc, v.Name)
}
c.Content(cc)
c.Title("Add to Playlist")
c.SetSelectionHandler(func(sel int) {
handler((*plist)[sel])
})
root.AddCenteredWidget(c)
}
func addToPlaylist(tracks []spotify.ID) {
openPlaylistMenu(func(sp spotify.SimplePlaylist) {
aerr := spt.AddTracksToPlaylist(sp.ID, tracks...)
if aerr != nil {
SendNotification(aerr.Error())
return
} else {
s := ""
if len(tracks) > 1 {
s = "s"
}
SendNotification("Added %d track%s to %s", len(tracks), s, sp.Name)
}
})
}