Image Previews Implementation

This commit is contained in:
aditya-K2 2021-10-24 13:26:10 +05:30
parent 9b1e8fd4f7
commit 3ad3e58019
3 changed files with 43 additions and 10 deletions

15
App.go
View File

@ -2,9 +2,12 @@ package main
import (
"github.com/fhs/gompd/mpd"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
var IMG_X, IMG_Y, IMG_W, IMG_H int
type Application struct {
App *tview.Application
expandedView *tview.Table
@ -13,14 +16,18 @@ type Application struct {
pBar *progressBar
}
func newApplication(conn mpd.Client) *Application {
func newApplication(conn mpd.Client, r *Renderer) *Application {
var pBar *progressBar = newProgressBar(conn)
var pBar *progressBar = newProgressBar(conn, r)
expandedView := tview.NewTable()
Navbar := tview.NewTable()
searchBar := tview.NewTable()
imagePreviewer := tview.NewBox()
imagePreviewer.SetBorder(true)
imagePreviewer.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
IMG_X, IMG_Y, IMG_W, IMG_H = imagePreviewer.GetRect()
return imagePreviewer.GetInnerRect()
})
searchBar.SetBorder(true).SetTitle("Search").SetTitleAlign(tview.AlignLeft)
Navbar.SetBorder(true)
@ -32,10 +39,10 @@ func newApplication(conn mpd.Client) *Application {
searchNavFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(searchBar, 3, 1, false).
AddItem(Navbar, 0, 4, false).
AddItem(imagePreviewer, 10, 3, false)
AddItem(imagePreviewer, 9, 3, false)
sNavExpViewFlex := tview.NewFlex().
AddItem(searchNavFlex, 25, 1, false).
AddItem(searchNavFlex, 17, 1, false).
AddItem(expandedView, 0, 4, false)
mainFlex := tview.NewFlex().SetDirection(tview.FlexRow).

15
main.go
View File

@ -24,7 +24,15 @@ func main() {
}
defer conn.Close()
UI := newApplication(*conn)
r := newRenderer()
c, _ := conn.CurrentSong()
if len(c) != 0 {
r.Start(DBDIR + c["file"])
} else {
r.Start("stop")
}
UI := newApplication(*conn, r)
fileMap, err := conn.GetFiles()
dirTree := generateDirectoryTree(fileMap)
@ -165,6 +173,11 @@ func main() {
UI.App.Stop()
return nil
}
case 115:
{
conn.Stop()
return nil
}
default:
{
return e

View File

@ -9,6 +9,9 @@ import (
"github.com/rivo/tview"
)
var CurrentSong string
var DBDIR string = "PATH TO YOUR MPD DATABASE"
// The progressBar is just a string which is separated by the color formatting String
// for e.g
// "[:#fbff00:]******************`innerText`[-:-:-] "
@ -23,7 +26,7 @@ type progressBar struct {
// This Function returns a progressBar with a table of two rows
// the First row will contain information about the current Song
// and the Second one will contain the progressBar
func newProgressBar(conn mpd.Client) *progressBar {
func newProgressBar(conn mpd.Client, r *Renderer) *progressBar {
p := progressBar{}
a := tview.NewTable().
@ -34,20 +37,30 @@ func newProgressBar(conn mpd.Client) *progressBar {
a.SetBorder(true)
a.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
p.updateTitle(conn)
p.updateTitle(conn, r)
p.updateProgress(conn)
return p.t.GetInnerRect()
})
p = progressBar{a}
CurrentSong = ""
p = progressBar{a}
return &p
}
func (s *progressBar) updateTitle(conn mpd.Client) {
func (s *progressBar) updateTitle(conn mpd.Client, r *Renderer) {
_currentAttributes, err := conn.CurrentSong()
if err == nil {
s.t.GetCell(0, 0).Text = "[green::bi]" + _currentAttributes["Title"] + "[-:-:-] - " + "[blue::b]" + _currentAttributes["Artist"] + "\n"
song := "[green::bi]" + _currentAttributes["Title"] + "[-:-:-] - " + "[blue::b]" + _currentAttributes["Artist"] + "\n"
s.t.GetCell(0, 0).Text = song
if len(_currentAttributes) == 0 && CurrentSong != "" {
CurrentSong = ""
r.Send("stop")
} else if song != CurrentSong && len(_currentAttributes) != 0 {
r.Send(DBDIR + _currentAttributes["file"])
CurrentSong = song
}
// fmt.Println(len(_currentAttributes))
}
}