moving app.go progressbar.go to the new ui package

This commit is contained in:
aditya-K2
2021-12-22 20:39:01 +05:30
parent a54bfc4948
commit 0d9227e019
4 changed files with 27 additions and 10 deletions

94
ui/app.go Normal file
View File

@@ -0,0 +1,94 @@
package ui
import (
"github.com/aditya-K2/tview"
"github.com/gdamore/tcell/v2"
)
var IMG_X, IMG_Y, IMG_W, IMG_H int
type Application struct {
App *tview.Application
ExpandedView *tview.Table
Navbar *tview.Table
SearchBar *tview.InputField
ProgressBar *progressBar
Pages *tview.Pages
}
func NewApplication() *Application {
var pBar *progressBar = newProgressBar()
expandedView := tview.NewTable()
Navbar := tview.NewTable()
searchBar := tview.NewInputField()
searchBar.SetFieldBackgroundColor(tcell.ColorDefault)
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()
})
expandedView.SetBackgroundColor(tcell.ColorDefault)
Navbar.SetBackgroundColor(tcell.ColorDefault)
searchBar.SetBackgroundColor(tcell.ColorDefault)
imagePreviewer.SetBackgroundColor(tcell.ColorDefault)
searchBar.SetTitle("Search").SetTitleAlign(tview.AlignLeft)
searchBar.SetAutocompleteBackgroundColor(tcell.GetColor("#15191a"))
searchBar.SetAutocompleteSelectBackgroundColor(tcell.GetColor("#e5e5e5"))
searchBar.SetAutocompleteMainTextColor(tcell.GetColor("#7f7f7f"))
searchBar.SetAutocompleteSelectedTextColor(tcell.GetColor("#111111"))
Navbar.SetBorder(true)
Navbar.SetSelectable(true, false)
Navbar.SetCell(0, 0, tview.NewTableCell("PlayList"))
Navbar.SetCell(1, 0, tview.NewTableCell("Files"))
Navbar.SetCell(2, 0, tview.NewTableCell("Most Played"))
Navbar.SetCell(3, 0, tview.NewTableCell("Search"))
searchNavFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(Navbar, 0, 4, false).
AddItem(imagePreviewer, 9, 3, false)
sNavExpViewFlex := tview.NewFlex().
AddItem(searchNavFlex, 17, 1, false).
AddItem(expandedView, 0, 4, false)
searchBar.SetBorder(true)
searchBarFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(searchBar, 3, 1, false).
AddItem(sNavExpViewFlex, 0, 1, false)
lex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(searchBarFlex, 0, 8, false).
AddItem(pBar.t, 5, 1, false)
expandedView.SetBorderPadding(1, 1, 1, 1).SetBorder(true)
expandedView.SetSelectable(true, false)
rootPages := tview.NewPages()
rootPages.AddPage("Main", lex, true, true)
App := tview.NewApplication()
App.SetRoot(rootPages, true).SetFocus(expandedView)
searchBar.SetDoneFunc(func(k tcell.Key) {
switch k {
case tcell.KeyEscape:
{
App.SetFocus(expandedView)
}
}
})
return &Application{
App: App,
ExpandedView: expandedView,
Navbar: Navbar,
SearchBar: searchBar,
ProgressBar: pBar,
Pages: rootPages,
}
}

98
ui/progressBar.go Normal file
View File

@@ -0,0 +1,98 @@
package ui
import (
"fmt"
"github.com/fhs/gompd/mpd"
"strconv"
"github.com/aditya-K2/gomp/utils"
"github.com/aditya-K2/tview"
"github.com/gdamore/tcell/v2"
)
var (
CurrentSong string
CONN *mpd.Client
RENDERER interface{ Send(string) }
)
func SetConnection(c *mpd.Client) {
CONN = c
}
func SetRenderer(r interface{ Send(string) }) {
RENDERER = r
}
// The progressBar is just a string which is separated by the color formatting String
// for e.g
// "[:#fbff00:]******************`innerText`[-:-:-] "
// the above string shows represents the progress until [-:-:-]
// [-:-:-] this string represents resetting colors so the substring before it would be with a
// colored background. this is done by calculating the innerRect of the table and taking that length as
// 100% and then representing the rest of the information in relation to it
type progressBar struct {
t *tview.Table
}
// 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() *progressBar {
p := progressBar{}
a := tview.NewTable().
SetCell(0, 0, tview.NewTableCell("")).
SetCell(1, 0, tview.NewTableCell("")).
SetCell(2, 0, tview.NewTableCell(""))
a.SetBackgroundColor(tcell.ColorDefault)
a.SetBorder(true)
a.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
p.updateTitle()
p.updateProgress()
return p.t.GetInnerRect()
})
CurrentSong = ""
p = progressBar{a}
return &p
}
func (s *progressBar) updateTitle() {
_currentAttributes, err := CONN.CurrentSong()
if err == nil {
song := "[green::bi]" + _currentAttributes["Title"] + "[-:-:-] - " + "[blue::b]" + _currentAttributes["Artist"] + "\n"
s.t.GetCell(0, 0).Text = song
if len(_currentAttributes) == 0 && CurrentSong != "" {
CurrentSong = ""
RENDERER.Send("stop")
} else if song != CurrentSong && len(_currentAttributes) != 0 {
RENDERER.Send(_currentAttributes["file"])
CurrentSong = song
}
}
}
func (s *progressBar) updateProgress() {
_status, err := CONN.Status()
_, _, _width, _ := s.t.GetInnerRect()
el, err1 := strconv.ParseFloat(_status["elapsed"], 8)
du, err := strconv.ParseFloat(_status["duration"], 8)
if du != 0 {
percentage := el / du * 100
if err == nil && err1 == nil {
s.t.SetTitle(fmt.Sprintf("[[::i] %s [-:-:-]Shuffle: %s Repeat: %s Volume: %s ]", utils.FormatString(_status["state"]), utils.FormatString(_status["random"]), utils.FormatString(_status["repeat"]), _status["volume"])).SetTitleAlign(tview.AlignRight)
s.t.GetCell(2, 0).Text = utils.GetText(float64(_width), percentage, utils.StrTime(el)+"/"+utils.StrTime(du)+"("+strconv.FormatFloat(percentage, 'f', 2, 32)+"%"+")")
} else {
s.t.SetTitle(fmt.Sprintf("[[::i] %s [-:-:-]Shuffle: %s Repeat: %s]", utils.FormatString(_status["state"]), utils.FormatString(_status["random"]), utils.FormatString(_status["repeat"]))).SetTitleAlign(tview.AlignRight)
s.t.GetCell(2, 0).Text = ""
}
} else {
s.t.SetTitle(fmt.Sprintf("[[::i] %s [-:-:-]Shuffle: %s Repeat: %s Volume: %s ]", utils.FormatString(_status["state"]), utils.FormatString(_status["random"]), utils.FormatString(_status["repeat"]), _status["volume"])).SetTitleAlign(tview.AlignRight)
s.t.GetCell(2, 0).Text = utils.GetText(float64(_width), 0, " ---:---")
}
}