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
.idea
*.jpg
e.go

View File

@ -6,6 +6,7 @@ import (
"github.com/aditya-K2/fuzzy"
"github.com/aditya-K2/gomp/utils"
"github.com/aditya-K2/tview"
"github.com/gdamore/tcell/v2"
)
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 {
for k, v := range m {
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 {
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 {
break
@ -33,11 +44,29 @@ func UpdatePlaylist(inputTable *tview.Table) {
for i, j := range _playlistAttr {
_, _, w, _ := inputTable.GetInnerRect()
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 {
inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString("[green]"+j["Title"], w/3)))
inputTable.SetCell(i, 1, tview.NewTableCell(utils.GetFormattedString("[magenta]"+j["Artist"], w/3)))
inputTable.SetCell(i, 2, tview.NewTableCell("[yellow]"+j["Album"]))
inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString(j["Title"], w/3)).
SetAlign(tview.AlignLeft).
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) {
case [3]string:
{
inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString("[green]"+content.([3]string)[0], width/3)))
inputTable.SetCell(i, 1, tview.NewTableCell(utils.GetFormattedString("[magenta]"+content.([3]string)[1], width/3)))
inputTable.SetCell(i, 2, tview.NewTableCell(utils.GetFormattedString("[yellow]"+content.([3]string)[2], width/3)))
inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString(content.([3]string)[0], width/3)).
SetAlign(tview.AlignLeft).
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:
{
inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString("[green]"+content.([2]string)[0], width/3)))
inputTable.SetCell(i, 1, tview.NewTableCell(utils.GetFormattedString("[magenta]"+content.([2]string)[1], width/3)))
inputTable.SetCell(i, 0, tview.NewTableCell(utils.GetFormattedString(content.([2]string)[0], 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:
{
b := content.(string)
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 {
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"] != "" {
_, _, w, _ := inputTable.GetInnerRect()
inputTable.SetCell(i, 0,
tview.NewTableCell("[green]"+utils.GetFormattedString(_songAttributes[0]["Title"], w/3)).
SetAlign(tview.AlignLeft))
tview.NewTableCell(utils.GetFormattedString(_songAttributes[0]["Title"], w/3)).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorGreen).
Background(tcell.ColorBlack)))
inputTable.SetCell(i, 1,
tview.NewTableCell("[magenta]"+utils.GetFormattedString(_songAttributes[0]["Artist"], w/3)).
SetAlign(tview.AlignLeft))
tview.NewTableCell(utils.GetFormattedString(_songAttributes[0]["Artist"], w/3)).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorPurple).
Background(tcell.ColorBlack)))
inputTable.SetCell(i, 2,
tview.NewTableCell("[yellow]"+_songAttributes[0]["Album"]).
SetAlign(tview.AlignLeft))
tview.NewTableCell(_songAttributes[0]["Album"]).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorYellow).
Background(tcell.ColorBlack)))
} else if _songAttributes[0]["Title"] == "" {
inputTable.SetCell(i, 0,
tview.NewTableCell("[blue]"+j.Path).
SetAlign(tview.AlignLeft))
tview.NewTableCell(j.Path).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorBlue).
Background(tcell.ColorBlack).
Bold(true)))
}
} else {
inputTable.SetCell(i, 0,
tview.NewTableCell("[yellow::b]"+j.Path).
SetAlign(tview.AlignLeft))
tview.NewTableCell(j.Path).
SetAlign(tview.AlignLeft).
SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorYellow).
Background(tcell.ColorBlack).
Bold(true)))
}
}
}

View File

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