A Simple Implementation of ProgressBar
The ProgressBar is just a string which has a length equal to the
innerRect of the table. The length of the innerRect is considered to be
100% and the rest of the progress is tracked in relation to it.
The Progress is displayed with the help of the color formatting strings
in tview
       link: https://pkg.go.dev/github.com/rivo/tview#hdr-Colors
the progress length which is calculated by finding the percentage of the
innerRect that matches with the given completion and then at that length
the "[-:-:-]" is inserted with the help of the insertAt function in
utils.go. "[-:-:-]" represents resetting of colors.
			
			
This commit is contained in:
		
							parent
							
								
									0b08b284ab
								
							
						
					
					
						commit
						a8f6c78461
					
				
							
								
								
									
										63
									
								
								progressBar.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								progressBar.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,63 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/rivo/tview"
 | 
			
		||||
	"github.com/gdamore/tcell/v2"
 | 
			
		||||
	"github.com/fhs/gompd/mpd"
 | 
			
		||||
	"strconv"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// 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(conn mpd.Client) *progressBar {
 | 
			
		||||
	p := progressBar{}
 | 
			
		||||
 | 
			
		||||
	a := tview.NewTable().
 | 
			
		||||
			SetCell(0, 0, tview.NewTableCell("")).
 | 
			
		||||
			SetCell(1, 0, tview.NewTableCell(""))
 | 
			
		||||
 | 
			
		||||
	a.SetBorder(true)
 | 
			
		||||
 | 
			
		||||
	a.SetDrawFunc(func( s tcell.Screen, x, y, width, height int) (int,int,int,int) {
 | 
			
		||||
		p.updateTitle(conn)
 | 
			
		||||
		p.updateProgress(conn)
 | 
			
		||||
		return p.t.GetInnerRect()
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	p = progressBar{a}
 | 
			
		||||
 | 
			
		||||
	return &p
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *progressBar) updateTitle(conn mpd.Client){
 | 
			
		||||
	_currentAttributes, err := conn.CurrentSong()
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		s.t.GetCell(0,0).Text = _currentAttributes["Artist"] + " - " +_currentAttributes["Title"]
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *progressBar) updateProgress(conn mpd.Client){
 | 
			
		||||
	_status,err := conn.Status()
 | 
			
		||||
	_, _, _width, _ := s.t.GetInnerRect()
 | 
			
		||||
	el, err1 := strconv.ParseFloat(_status["elapsed"], 8)
 | 
			
		||||
	du , err := strconv.ParseFloat(_status["duration"], 8)
 | 
			
		||||
	percentage := el / du * 100
 | 
			
		||||
	if err == nil && err1 == nil{
 | 
			
		||||
		s.t.GetCell(1,0).Text = getText(float64(_width), percentage, convertToStrings(el) + "/" + convertToStrings(du) + "(" + strconv.FormatFloat(percentage, 'f', 2, 32) + "%" + ")")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								utils.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
			
		||||
package main
 | 
			
		||||
import(
 | 
			
		||||
	"strings"
 | 
			
		||||
	"strconv"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func convertToStrings(e float64) string{
 | 
			
		||||
	a := int(e)
 | 
			
		||||
	var min, seconds string
 | 
			
		||||
	if (a/60 < 10){
 | 
			
		||||
		min = "0"
 | 
			
		||||
		min += strconv.Itoa(a/60)
 | 
			
		||||
	} else {
 | 
			
		||||
		min = strconv.Itoa(a/60)
 | 
			
		||||
	}
 | 
			
		||||
	if (a%60 < 10){
 | 
			
		||||
		seconds = "0"
 | 
			
		||||
		seconds += strconv.Itoa(a%60)
 | 
			
		||||
	} else{
 | 
			
		||||
		seconds = strconv.Itoa(a%60)
 | 
			
		||||
	}
 | 
			
		||||
	return min + ":" + seconds
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func insertAt(inputString, stringTobeInserted string, index int) string{
 | 
			
		||||
    s := inputString[:index] + stringTobeInserted + inputString[index:]
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getText(width , percentage float64, eta string) string{
 | 
			
		||||
	q := "[#000000:#ffffff]"
 | 
			
		||||
	var a string
 | 
			
		||||
	a += strings.Repeat(" ", int(width) - len(eta))
 | 
			
		||||
	a = insertAt(a, eta, int(width/2) - 10)
 | 
			
		||||
	a = insertAt(a, "[-:-:-]", int(width * percentage / 100))
 | 
			
		||||
	q += a
 | 
			
		||||
	return q
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user