Trakt lists: right-align the numbers
The grey number of every entry ("1.2k watching", "on 18k lists") stood
right after its title, so they did not line up. The rows are now padded
to the list's width on every draw where it changed, putting the numbers
against the right edge; a title too long for that keeps the number right
after it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+41
-7
@@ -36,6 +36,7 @@ type searchView struct {
|
||||
results []result
|
||||
notes []string // per result, a Trakt list's number ("1.2k watching"); nil for searches
|
||||
placeholder string // shown when there are no results
|
||||
notesWidth int // list width the rows were laid out for
|
||||
timer *time.Timer
|
||||
cancel context.CancelFunc
|
||||
gen int
|
||||
@@ -71,7 +72,7 @@ func newSearchView(app *App, be backend) *searchView {
|
||||
|
||||
left := tview.NewFlex().SetDirection(tview.FlexRow).
|
||||
AddItem(v.input, 1, 0, true).
|
||||
AddItem(v.list, 0, 1, false)
|
||||
AddItem(notesList{v.list, v}, 0, 1, false)
|
||||
v.root = tview.NewFlex().
|
||||
AddItem(left, 0, 1, true).
|
||||
AddItem(v.details, 0, 1, false)
|
||||
@@ -279,12 +280,10 @@ func (v *searchView) activate() {
|
||||
// render rebuilds the list, selecting index sel.
|
||||
func (v *searchView) render(sel int) {
|
||||
v.list.Clear()
|
||||
for i, r := range v.results {
|
||||
label := r.label()
|
||||
if i < len(v.notes) && v.notes[i] != "" {
|
||||
label += " [gray]" + tview.Escape(v.notes[i]) + "[-]"
|
||||
}
|
||||
v.list.AddItem(label, "", 0, nil)
|
||||
_, _, width, _ := v.list.GetInnerRect()
|
||||
v.notesWidth = width
|
||||
for i := range v.results {
|
||||
v.list.AddItem(v.itemText(i, width), "", 0, nil)
|
||||
}
|
||||
if len(v.results) == 0 {
|
||||
if v.placeholder != "" {
|
||||
@@ -298,6 +297,41 @@ func (v *searchView) render(sel int) {
|
||||
v.showDetails(sel)
|
||||
}
|
||||
|
||||
// itemText is the row of result i in a list width columns wide: the label,
|
||||
// and a Trakt list's number (if any) in grey against the right edge, so the
|
||||
// numbers of all rows line up. A label too long for that gets the number
|
||||
// right after it.
|
||||
func (v *searchView) itemText(i, width int) string {
|
||||
label := v.results[i].label()
|
||||
if i >= len(v.notes) || v.notes[i] == "" {
|
||||
return label
|
||||
}
|
||||
note := tview.Escape(v.notes[i])
|
||||
gap := width - tview.TaggedStringWidth(label) - tview.TaggedStringWidth(note)
|
||||
if gap < 2 {
|
||||
gap = 2
|
||||
}
|
||||
return label + strings.Repeat(" ", gap) + "[gray]" + note + "[-]"
|
||||
}
|
||||
|
||||
// notesList draws the results list, first laying the rows out again when the
|
||||
// width changed (the numbers of a Trakt list sit against the right edge).
|
||||
type notesList struct {
|
||||
*tview.List
|
||||
v *searchView
|
||||
}
|
||||
|
||||
func (n notesList) Draw(screen tcell.Screen) {
|
||||
v := n.v
|
||||
if _, _, width, _ := v.list.GetInnerRect(); width != v.notesWidth && len(v.notes) > 0 {
|
||||
v.notesWidth = width
|
||||
for i := range v.results {
|
||||
v.list.SetItemText(i, v.itemText(i, width), "")
|
||||
}
|
||||
}
|
||||
n.List.Draw(screen)
|
||||
}
|
||||
|
||||
func (v *searchView) showDetails(i int) {
|
||||
if i < 0 || i >= len(v.results) {
|
||||
v.details.Clear()
|
||||
|
||||
Reference in New Issue
Block a user