Update Highlighting Algorithm & Using Tcell Styles

The Algorithm now only inserts the color string over a range i.e
if 1, 2, 3 are matches then instead of adding individually at 1, 2, 3
it adds the color string at 1 and null color string at 3. Also Using
tcell Styles for highlighting the table cells.
This commit is contained in:
aditya-K2 2021-12-29 22:22:34 +05:30
parent 921ab9e831
commit f7c2283355
3 changed files with 121 additions and 52 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
gomp gomp
.idea .idea
*.jpg *.jpg
e.go

View File

@ -6,6 +6,7 @@ import (
"github.com/aditya-K2/fuzzy" "github.com/aditya-K2/fuzzy"
"github.com/aditya-K2/gomp/utils" "github.com/aditya-K2/gomp/utils"
"github.com/aditya-K2/tview" "github.com/aditya-K2/tview"
"github.com/gdamore/tcell/v2"
) )
func UpdateBuffSearchView(inputTable *tview.Table, m fuzzy.Matches, f []FileNode) { func UpdateBuffSearchView(inputTable *tview.Table, m fuzzy.Matches, f []FileNode) {
@ -15,9 +16,19 @@ func UpdateBuffSearchView(inputTable *tview.Table, m fuzzy.Matches, f []FileNode
} else { } else {
for k, v := range m { for k, v := range m {
if len(f[v.Index].Children) != 0 { if len(f[v.Index].Children) != 0 {
inputTable.SetCellSimple(k, 0, utils.GetMatchedString(f[v.Index].Path, "#0000ff", "yellow", v.MatchedIndexes)) inputTable.SetCell(k, 0, tview.NewTableCell(utils.GetMatchedString(v.MatchedIndexes, f[v.Index].Path, "[#0000ff:-:bi]")).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorYellow).
Background(tcell.ColorBlack).
Bold(true)))
} else { } else {
inputTable.SetCellSimple(k, 0, utils.GetMatchedString(f[v.Index].Title, "#fbff00", "green", v.MatchedIndexes)) inputTable.SetCell(k, 0, tview.NewTableCell(utils.GetMatchedString(v.MatchedIndexes, f[v.Index].Title, "[#fbff00:-:bi]")).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorGreen).
Background(tcell.ColorBlack).
Bold(true)))
} }
if k == 15 { if k == 15 {
break break
@ -33,11 +44,29 @@ func UpdatePlaylist(inputTable *tview.Table) {
for i, j := range _playlistAttr { for i, j := range _playlistAttr {
_, _, w, _ := inputTable.GetInnerRect() _, _, w, _ := inputTable.GetInnerRect()
if j["Title"] == "" || j["Artist"] == "" || j["Album"] == "" { if j["Title"] == "" || j["Artist"] == "" || j["Album"] == "" {
inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString(j["file"], w/3))) inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString(j["file"], w/3)).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorBlue).
Background(tcell.ColorBlack).
Bold(true)))
} else { } else {
inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString("[green]"+j["Title"], w/3))) inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString(j["Title"], w/3)).
inputTable.SetCell(i, 1, tview.NewTableCell(utils.GetFormattedString("[magenta]"+j["Artist"], w/3))) SetAlign(tview.AlignLeft).
inputTable.SetCell(i, 2, tview.NewTableCell("[yellow]"+j["Album"])) SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorGreen).
Background(tcell.ColorBlack)))
inputTable.SetCell(i, 1, tview.NewTableCell(utils.GetFormattedString(j["Artist"], w/3)).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorPurple).
Background(tcell.ColorBlack)))
inputTable.SetCell(i, 2, tview.NewTableCell(j["Album"]).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorYellow).
Background(tcell.ColorBlack)))
} }
} }
} }
@ -52,20 +81,44 @@ func UpdateSearchView(inputTable *tview.Table, c []interface{}) {
switch content.(type) { switch content.(type) {
case [3]string: case [3]string:
{ {
inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString("[green]"+content.([3]string)[0], width/3))) inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString(content.([3]string)[0], width/3)).
inputTable.SetCell(i, 1, tview.NewTableCell(utils.GetFormattedString("[magenta]"+content.([3]string)[1], width/3))) SetAlign(tview.AlignLeft).
inputTable.SetCell(i, 2, tview.NewTableCell(utils.GetFormattedString("[yellow]"+content.([3]string)[2], width/3))) SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorGreen).
Background(tcell.ColorBlack)))
inputTable.SetCell(i, 1, tview.NewTableCell(utils.GetFormattedString(content.([3]string)[1], width/3)).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorPurple).
Background(tcell.ColorBlack)))
inputTable.SetCell(i, 2, tview.NewTableCell(utils.GetFormattedString(content.([3]string)[2], width/3)).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorYellow).
Background(tcell.ColorBlack)))
} }
case [2]string: case [2]string:
{ {
inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString("[green]"+content.([2]string)[0], width/3))) inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString(content.([2]string)[0], width/3)).
inputTable.SetCell(i, 1, tview.NewTableCell(utils.GetFormattedString("[magenta]"+content.([2]string)[1], width/3))) SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorYellow).
Background(tcell.ColorBlack)))
inputTable.SetCell(i, 1, tview.NewTableCell(utils.GetFormattedString(content.([2]string)[1], width/3)).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorPurple).
Background(tcell.ColorBlack)))
} }
case string: case string:
{ {
b := content.(string) b := content.(string)
if !strings.HasPrefix(b, WHITE_AND_BOLD) { if !strings.HasPrefix(b, WHITE_AND_BOLD) {
inputTable.SetCell(i, 0, tview.NewTableCell("[green]"+content.(string))) inputTable.SetCell(i, 0, tview.NewTableCell(content.(string)).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorPurple).
Background(tcell.ColorBlack)))
} else { } else {
inputTable.SetCell(i, 0, tview.NewTableCell(content.(string)).SetSelectable(false)) inputTable.SetCell(i, 0, tview.NewTableCell(content.(string)).SetSelectable(false))
} }
@ -82,26 +135,43 @@ func Update(f []FileNode, inputTable *tview.Table) {
if err == nil && _songAttributes[0]["Title"] != "" { if err == nil && _songAttributes[0]["Title"] != "" {
_, _, w, _ := inputTable.GetInnerRect() _, _, w, _ := inputTable.GetInnerRect()
inputTable.SetCell(i, 0, inputTable.SetCell(i, 0,
tview.NewTableCell("[green]"+utils.GetFormattedString(_songAttributes[0]["Title"], w/3)). tview.NewTableCell(utils.GetFormattedString(_songAttributes[0]["Title"], w/3)).
SetAlign(tview.AlignLeft)) SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorGreen).
Background(tcell.ColorBlack)))
inputTable.SetCell(i, 1, inputTable.SetCell(i, 1,
tview.NewTableCell("[magenta]"+utils.GetFormattedString(_songAttributes[0]["Artist"], w/3)). tview.NewTableCell(utils.GetFormattedString(_songAttributes[0]["Artist"], w/3)).
SetAlign(tview.AlignLeft)) SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorPurple).
Background(tcell.ColorBlack)))
inputTable.SetCell(i, 2, inputTable.SetCell(i, 2,
tview.NewTableCell("[yellow]"+_songAttributes[0]["Album"]). tview.NewTableCell(_songAttributes[0]["Album"]).
SetAlign(tview.AlignLeft)) SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorYellow).
Background(tcell.ColorBlack)))
} else if _songAttributes[0]["Title"] == "" { } else if _songAttributes[0]["Title"] == "" {
inputTable.SetCell(i, 0, inputTable.SetCell(i, 0,
tview.NewTableCell("[blue]"+j.Path). tview.NewTableCell(j.Path).
SetAlign(tview.AlignLeft)) SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorBlue).
Background(tcell.ColorBlack).
Bold(true)))
} }
} else { } else {
inputTable.SetCell(i, 0, inputTable.SetCell(i, 0,
tview.NewTableCell("[yellow::b]"+j.Path). tview.NewTableCell(j.Path).
SetAlign(tview.AlignLeft)) SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorYellow).
Background(tcell.ColorBlack).
Bold(true)))
} }
} }
} }

View File

@ -1,7 +1,6 @@
package utils package utils
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"strings" "strings"
@ -138,36 +137,35 @@ func CheckDirectoryFmt(path string) string {
} }
} }
func GetMatchedString(s string, color string, nulcol string, matchedIndexes []int) string { func GetMatchedString(a []int, s, color string) string {
// The indexes that we will receive from the matchedIndexes are always sorted so we have to just // The Matches are sorted so we just have to traverse the Matches and if the two adjacent matches are not consecutive
// add the color string at // then we append the color string at the start + offset and the nulcol ( reset ) at end + offset + 1 and then reset
// `indexValue + ( len(colorString) * k )` // start and end to a[k+1] for e.g if matches := []int{1, 2, 4, 5, 6, 9} then the start will be 1 and end will be 1
// where k is the index of the indexValue in the matchedIndexes slice // now until we reach `4` the value of end will change to `2` that means when we reach `4` the s string will be
// and we will need to also reset the colors, For that we check if the next indexValue in the matchedIndexes for // `O[yellow:-:-]ut[-:-:-]putString` after that until we reach the end will be changed and finally become `6` and the
// the current indexValue is not the consecutive value ( v + 1 ) if yes ( is not consecutive ) then we add the reset // s string will be `O[yellow:-:-]ut[-:-:-]p[yellow:-:-]utS[-:-:-]tring`
// color string at the k + 1 index in the string. // Please note that after around 45 simulatenously highlighted characters tview stops highlighting and the color
// for e.g. // sequences are rendered hope no one has that big of search query.
// if we have the following matchedIndexes slice start := a[0]
// []int{ 1, 3, 4, 6} end := a[0]
// During the First Iteration matchedIndexes[k] = 1 and and matchedIndexes[k+1] are not consecutive so the nulcol offset := 0
// string will be added to the matchedIndexes[k] + 1 index of the string nulcol := "[-:-:-]"
// During the Second Iteration as 3, 4 are consecutive the nulcol will be skipped. for k := range a {
color = fmt.Sprintf("[%s:-:bi]", color) if k < len(a)-1 && a[k+1]-a[k] == 1 {
nulcol = fmt.Sprintf("[%s:-:b]", nulcol) end = a[k+1]
nulc := 0 } else if k < len(a)-1 {
for k := range matchedIndexes { s = InsertAt(s, color, start+offset)
s = InsertAt(s, color, matchedIndexes[k]+(len(color)*k)+nulc) offset += len(color)
if k < len(matchedIndexes)-1 && matchedIndexes[k]-matchedIndexes[k+1] != 1 { s = InsertAt(s, nulcol, end+offset+1)
s = InsertAt(s, nulcol, (matchedIndexes[k]+1)+(len(color)*(k+1))+nulc) offset += len(nulcol)
nulc += len(nulcol) start = a[k+1]
} end = a[k+1]
if k == len(matchedIndexes)-1 { } else if k == len(a)-1 {
s = InsertAt(s, nulcol, ((matchedIndexes[len(matchedIndexes)-1] + 1) + s = InsertAt(s, color, start+offset)
(len(matchedIndexes) * len(color)) + offset += len(color)
(len(nulcol) * (len(matchedIndexes) - 1)))) s = InsertAt(s, nulcol, end+offset+1)
offset += len(nulcol)
} }
} }
// Adding the Nulcol at the Start
s = nulcol + s
return s return s
} }