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 ( import (
"github.com/fhs/gompd/mpd" "github.com/fhs/gompd/mpd"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview" "github.com/rivo/tview"
) )
var IMG_X, IMG_Y, IMG_W, IMG_H int
type Application struct { type Application struct {
App *tview.Application App *tview.Application
expandedView *tview.Table expandedView *tview.Table
@ -13,14 +16,18 @@ type Application struct {
pBar *progressBar 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() expandedView := tview.NewTable()
Navbar := tview.NewTable() Navbar := tview.NewTable()
searchBar := tview.NewTable() searchBar := tview.NewTable()
imagePreviewer := tview.NewBox() imagePreviewer := tview.NewBox()
imagePreviewer.SetBorder(true) 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) searchBar.SetBorder(true).SetTitle("Search").SetTitleAlign(tview.AlignLeft)
Navbar.SetBorder(true) Navbar.SetBorder(true)
@ -32,10 +39,10 @@ func newApplication(conn mpd.Client) *Application {
searchNavFlex := tview.NewFlex().SetDirection(tview.FlexRow). searchNavFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(searchBar, 3, 1, false). AddItem(searchBar, 3, 1, false).
AddItem(Navbar, 0, 4, false). AddItem(Navbar, 0, 4, false).
AddItem(imagePreviewer, 10, 3, false) AddItem(imagePreviewer, 9, 3, false)
sNavExpViewFlex := tview.NewFlex(). sNavExpViewFlex := tview.NewFlex().
AddItem(searchNavFlex, 25, 1, false). AddItem(searchNavFlex, 17, 1, false).
AddItem(expandedView, 0, 4, false) AddItem(expandedView, 0, 4, false)
mainFlex := tview.NewFlex().SetDirection(tview.FlexRow). mainFlex := tview.NewFlex().SetDirection(tview.FlexRow).

15
main.go
View File

@ -24,7 +24,15 @@ func main() {
} }
defer conn.Close() 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() fileMap, err := conn.GetFiles()
dirTree := generateDirectoryTree(fileMap) dirTree := generateDirectoryTree(fileMap)
@ -165,6 +173,11 @@ func main() {
UI.App.Stop() UI.App.Stop()
return nil return nil
} }
case 115:
{
conn.Stop()
return nil
}
default: default:
{ {
return e return e

View File

@ -9,6 +9,9 @@ import (
"github.com/rivo/tview" "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 // The progressBar is just a string which is separated by the color formatting String
// for e.g // for e.g
// "[:#fbff00:]******************`innerText`[-:-:-] " // "[:#fbff00:]******************`innerText`[-:-:-] "
@ -23,7 +26,7 @@ type progressBar struct {
// This Function returns a progressBar with a table of two rows // This Function returns a progressBar with a table of two rows
// the First row will contain information about the current Song // the First row will contain information about the current Song
// and the Second one will contain the progressBar // and the Second one will contain the progressBar
func newProgressBar(conn mpd.Client) *progressBar { func newProgressBar(conn mpd.Client, r *Renderer) *progressBar {
p := progressBar{} p := progressBar{}
a := tview.NewTable(). a := tview.NewTable().
@ -34,20 +37,30 @@ func newProgressBar(conn mpd.Client) *progressBar {
a.SetBorder(true) a.SetBorder(true)
a.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) { 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) p.updateProgress(conn)
return p.t.GetInnerRect() return p.t.GetInnerRect()
}) })
p = progressBar{a} CurrentSong = ""
p = progressBar{a}
return &p return &p
} }
func (s *progressBar) updateTitle(conn mpd.Client) { func (s *progressBar) updateTitle(conn mpd.Client, r *Renderer) {
_currentAttributes, err := conn.CurrentSong() _currentAttributes, err := conn.CurrentSong()
if err == nil { 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))
} }
} }