Library: filter by file properties (quality, codec, HDR, DV, language)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+1
-1
@@ -341,7 +341,7 @@ func searchHints() string {
|
||||
}
|
||||
|
||||
func libraryHints() string {
|
||||
return "Tab movies/series · f filter · / text · t monitor · s search · Enter open"
|
||||
return "Tab list · f status · F file props · / text · t monitor · s search"
|
||||
}
|
||||
|
||||
func logHints() string { return "↑↓ scroll · G follow · c clear" }
|
||||
|
||||
@@ -22,6 +22,8 @@ const helpText = `[yellow]Global[-]
|
||||
Tab switch between the movies and series lists
|
||||
f cycle filter (all, missing, downloaded, monitored, unmonitored);
|
||||
filters apply to both lists
|
||||
F filter by file properties: quality, codec (x265, x264, AV1),
|
||||
HDR / DV / SDR, language. Series are read on demand.
|
||||
/ text filter (Enter or Esc returns to the list)
|
||||
t toggle monitored
|
||||
s automatic search (series: all missing, asks first)
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
|
||||
"git.stinnesbeck.com/nils/arr-tui/internal/arr"
|
||||
)
|
||||
|
||||
// attrFilter narrows the library by properties of the downloaded files. An
|
||||
// empty field means "any". All fields must match the same file; a series
|
||||
// matches when any of its files does.
|
||||
type attrFilter struct {
|
||||
res, codec, hdr, lang string
|
||||
}
|
||||
|
||||
const anyOption = "any"
|
||||
|
||||
var (
|
||||
resOptions = []string{"2160p", "1080p", "720p", "SD"}
|
||||
codecOptions = []string{arr.CodecX265, arr.CodecX264, arr.CodecAV1}
|
||||
hdrOptions = []string{"HDR", "DV", "SDR"}
|
||||
)
|
||||
|
||||
func (f attrFilter) active() bool { return f != attrFilter{} }
|
||||
|
||||
// String lists the active criteria for the header, e.g. "1080p x265 DV".
|
||||
func (f attrFilter) String() string {
|
||||
var parts []string
|
||||
for _, p := range []string{f.res, f.codec, f.hdr, f.lang} {
|
||||
if p != "" {
|
||||
parts = append(parts, p)
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, " ")
|
||||
}
|
||||
|
||||
func (f attrFilter) matchFile(a arr.FileAttrs) bool {
|
||||
switch f.res {
|
||||
case "":
|
||||
case "SD":
|
||||
if a.Resolution <= 0 || a.Resolution > 576 {
|
||||
return false
|
||||
}
|
||||
default:
|
||||
if f.res != itoa(a.Resolution)+"p" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if f.codec != "" && f.codec != a.Codec {
|
||||
return false
|
||||
}
|
||||
switch f.hdr {
|
||||
case "HDR":
|
||||
if !a.HDR {
|
||||
return false
|
||||
}
|
||||
case "DV":
|
||||
if !a.DV {
|
||||
return false
|
||||
}
|
||||
case "SDR":
|
||||
if a.HDR || a.DV {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if f.lang != "" && !slices.ContainsFunc(a.Languages, func(l string) bool { return strings.EqualFold(l, f.lang) }) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// match reports whether any of the files matches. Without files nothing matches.
|
||||
func (f attrFilter) match(files []arr.FileAttrs) bool {
|
||||
return slices.ContainsFunc(files, f.matchFile)
|
||||
}
|
||||
|
||||
// languagesOf collects the languages of all known files, sorted.
|
||||
func languagesOf(items ...[]libItem) []string {
|
||||
seen := map[string]bool{}
|
||||
var out []string
|
||||
for _, list := range items {
|
||||
for _, it := range list {
|
||||
for _, a := range it.attrs {
|
||||
for _, l := range a.Languages {
|
||||
if !seen[l] {
|
||||
seen[l] = true
|
||||
out = append(out, l)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
slices.Sort(out)
|
||||
return out
|
||||
}
|
||||
|
||||
func indexOf(options []string, v string) int {
|
||||
if i := slices.Index(options, v); i >= 0 {
|
||||
return i + 1 // option 0 is "any"
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// openAttrFilter shows the dialog to pick quality, codec, HDR and language.
|
||||
func (a *App) openAttrFilter(cur attrFilter, languages []string, apply func(attrFilter)) {
|
||||
if cur.lang != "" && !slices.Contains(languages, cur.lang) {
|
||||
languages = append([]string{cur.lang}, languages...)
|
||||
}
|
||||
withAny := func(o []string) []string { return append([]string{anyOption}, o...) }
|
||||
const (
|
||||
lRes = "Quality"
|
||||
lCodec = "Codec"
|
||||
lHDR = "HDR"
|
||||
lLang = "Language"
|
||||
)
|
||||
form := tview.NewForm().
|
||||
AddDropDown(lRes, withAny(resOptions), indexOf(resOptions, cur.res), nil).
|
||||
AddDropDown(lCodec, withAny(codecOptions), indexOf(codecOptions, cur.codec), nil).
|
||||
AddDropDown(lHDR, withAny(hdrOptions), indexOf(hdrOptions, cur.hdr), nil).
|
||||
AddDropDown(lLang, withAny(languages), indexOf(languages, cur.lang), nil)
|
||||
|
||||
pick := func(label string, options []string) string {
|
||||
i := selected(form, label)
|
||||
if i <= 0 {
|
||||
return ""
|
||||
}
|
||||
return options[i-1]
|
||||
}
|
||||
form.AddButton("Apply", func() {
|
||||
a.closeModal()
|
||||
apply(attrFilter{
|
||||
res: pick(lRes, resOptions),
|
||||
codec: pick(lCodec, codecOptions),
|
||||
hdr: pick(lHDR, hdrOptions),
|
||||
lang: pick(lLang, languages),
|
||||
})
|
||||
})
|
||||
form.AddButton("Reset", func() {
|
||||
a.closeModal()
|
||||
apply(attrFilter{})
|
||||
})
|
||||
form.AddButton("Cancel", a.closeModal)
|
||||
form.SetCancelFunc(a.closeModal)
|
||||
arrowNavigation(form)
|
||||
form.SetBorder(true).SetTitle(" Filter by file properties ")
|
||||
a.openModal(form, 50, 13)
|
||||
}
|
||||
+145
-8
@@ -49,6 +49,10 @@ type libItem struct {
|
||||
details string
|
||||
movie *radarr.Movie
|
||||
series *sonarr.Series
|
||||
// File properties for the attribute filter. For series they are fetched on
|
||||
// demand, attrsKnown tells whether that has happened.
|
||||
attrs []arr.FileAttrs
|
||||
attrsKnown bool
|
||||
}
|
||||
|
||||
var libFilters = []struct {
|
||||
@@ -78,10 +82,14 @@ func sizeOrBlank(b int64) string {
|
||||
|
||||
func newMovieItem(m radarr.Movie) libItem {
|
||||
it := libItem{
|
||||
id: m.ID, title: m.Title, monitored: m.Monitored, have: m.HasFile,
|
||||
attrsKnown: true,
|
||||
id: m.ID, title: m.Title, monitored: m.Monitored, have: m.HasFile,
|
||||
missing: m.Monitored && !m.HasFile && m.IsAvailable,
|
||||
size: sizeOrBlank(m.SizeOnDisk), movie: &m,
|
||||
}
|
||||
if m.HasFile && m.MovieFile != nil {
|
||||
it.attrs = []arr.FileAttrs{m.MovieFile.Attrs()}
|
||||
}
|
||||
it.label = tview.Escape(m.Title)
|
||||
if m.Year > 0 {
|
||||
it.label += fmt.Sprintf(" (%d)", m.Year)
|
||||
@@ -107,6 +115,7 @@ func newSeriesItem(s sonarr.Series) libItem {
|
||||
missing: s.Monitored && st.EpisodeFileCount < st.EpisodeCount,
|
||||
size: sizeOrBlank(st.SizeOnDisk), series: &s,
|
||||
}
|
||||
it.attrsKnown = st.EpisodeFileCount == 0 // nothing to fetch for series without files
|
||||
it.label = tview.Escape(s.Title)
|
||||
if s.Year > 0 {
|
||||
it.label += fmt.Sprintf(" (%d)", s.Year)
|
||||
@@ -145,12 +154,17 @@ func pathLine(p string) string {
|
||||
return "Path: " + tview.Escape(p) + "\n"
|
||||
}
|
||||
|
||||
// filterItems applies the filter preset and the text filter, sorted by title.
|
||||
func filterItems(items []libItem, filter int, text string) []libItem {
|
||||
// filterItems applies the filter preset, the text filter and the file
|
||||
// property filter, sorted by title. Items whose file properties are not known
|
||||
// yet do not match an active property filter.
|
||||
func filterItems(items []libItem, filter int, text string, af attrFilter) []libItem {
|
||||
text = strings.ToLower(strings.TrimSpace(text))
|
||||
var out []libItem
|
||||
for _, it := range items {
|
||||
if libFilters[filter].match(it) && strings.Contains(strings.ToLower(it.title), text) {
|
||||
if !libFilters[filter].match(it) || !strings.Contains(strings.ToLower(it.title), text) {
|
||||
continue
|
||||
}
|
||||
if !af.active() || af.match(it.attrs) {
|
||||
out = append(out, it)
|
||||
}
|
||||
}
|
||||
@@ -187,6 +201,19 @@ type libraryView struct {
|
||||
// UI-goroutine state.
|
||||
cur libKind // the pane that has (or last had) focus
|
||||
filter int
|
||||
attr attrFilter
|
||||
|
||||
// File properties of series, fetched on demand and cached. An entry is
|
||||
// reused while the series' file count and size stay the same.
|
||||
seriesFiles map[int]seriesFiles
|
||||
fileGen int // invalidates running fetches
|
||||
filePending int
|
||||
}
|
||||
|
||||
type seriesFiles struct {
|
||||
count int
|
||||
size int64
|
||||
attrs []arr.FileAttrs
|
||||
}
|
||||
|
||||
func newLibraryView(app *App, r *radarr.Client, s *sonarr.Client) *libraryView {
|
||||
@@ -195,6 +222,8 @@ func newLibraryView(app *App, r *radarr.Client, s *sonarr.Client) *libraryView {
|
||||
header: tview.NewTextView().SetDynamicColors(true),
|
||||
input: tview.NewInputField().SetLabel("Filter: ").SetFieldWidth(0),
|
||||
details: tview.NewTextView().SetDynamicColors(true).SetWordWrap(true),
|
||||
|
||||
seriesFiles: map[int]seriesFiles{},
|
||||
}
|
||||
v.details.SetBorder(true).SetTitle(" Details ")
|
||||
|
||||
@@ -241,7 +270,10 @@ func (v *libraryView) handleKey(ev *tcell.EventKey) *tcell.EventKey {
|
||||
case 'f':
|
||||
v.filter = (v.filter + 1) % len(libFilters)
|
||||
v.render()
|
||||
case 'F':
|
||||
v.app.openAttrFilter(v.attr, languagesOf(v.panes[kindMovies].all, v.panes[kindSeries].all), v.setAttrFilter)
|
||||
case 'r':
|
||||
v.seriesFiles = map[int]seriesFiles{} // a manual reload also refreshes file properties
|
||||
v.reload()
|
||||
case 'i':
|
||||
v.open()
|
||||
@@ -291,7 +323,7 @@ func (v *libraryView) reloadPane(p *libPane) {
|
||||
var series []sonarr.Series
|
||||
if series, err = v.sonarr.Series(ctx); err == nil {
|
||||
for _, s := range series {
|
||||
items = append(items, newSeriesItem(s))
|
||||
items = append(items, v.withCachedFiles(newSeriesItem(s)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -301,6 +333,9 @@ func (v *libraryView) reloadPane(p *libPane) {
|
||||
return
|
||||
}
|
||||
p.err = err
|
||||
if err == nil && kind == kindSeries {
|
||||
defer v.loadSeriesFiles()
|
||||
}
|
||||
if err != nil {
|
||||
// Keep showing the last good list; say why it is stale.
|
||||
v.app.SetMessage("[red]" + kind.service() + ": " + tview.Escape(err.Error()))
|
||||
@@ -325,10 +360,17 @@ func (v *libraryView) render() {
|
||||
for _, p := range v.panes {
|
||||
v.renderPane(p)
|
||||
}
|
||||
v.header.SetText(fmt.Sprintf("[::b]Library[::-] · %s %d/%d · %s %d/%d · filter: [yellow]%s[-]",
|
||||
filters := "[yellow]" + libFilters[v.filter].name + "[-]"
|
||||
if v.attr.active() {
|
||||
filters += " + [yellow]" + v.attr.String() + "[-]"
|
||||
if v.filePending > 0 {
|
||||
filters += fmt.Sprintf(" [gray](reading %d series…)[-]", v.filePending)
|
||||
}
|
||||
}
|
||||
v.header.SetText(fmt.Sprintf("[::b]Library[::-] · %s %d/%d · %s %d/%d · filter: %s",
|
||||
kindMovies, len(v.panes[kindMovies].shown), len(v.panes[kindMovies].all),
|
||||
kindSeries, len(v.panes[kindSeries].shown), len(v.panes[kindSeries].all),
|
||||
libFilters[v.filter].name))
|
||||
filters))
|
||||
v.renderDetails()
|
||||
}
|
||||
|
||||
@@ -337,7 +379,7 @@ func (v *libraryView) renderPane(p *libPane) {
|
||||
if r, _ := p.table.GetSelection(); r >= 1 && r <= len(p.shown) {
|
||||
selID = p.shown[r-1].id
|
||||
}
|
||||
p.shown = filterItems(p.all, v.filter, v.input.GetText())
|
||||
p.shown = filterItems(p.all, v.filter, v.input.GetText(), v.attr)
|
||||
|
||||
p.table.Clear()
|
||||
header(p.table, 0, "Title", "Status", "Size")
|
||||
@@ -448,3 +490,98 @@ func (v *libraryView) run(doing, done string, fn func(ctx context.Context) error
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
func (v *libraryView) setAttrFilter(f attrFilter) {
|
||||
v.attr = f
|
||||
v.render()
|
||||
v.loadSeriesFiles()
|
||||
}
|
||||
|
||||
// withCachedFiles fills in the file properties of a series from the cache.
|
||||
func (v *libraryView) withCachedFiles(it libItem) libItem {
|
||||
if it.attrsKnown || it.series == nil {
|
||||
return it
|
||||
}
|
||||
st := it.series.Statistics
|
||||
if c, ok := v.seriesFiles[it.id]; ok && c.count == st.EpisodeFileCount && c.size == st.SizeOnDisk {
|
||||
it.attrs, it.attrsKnown = c.attrs, true
|
||||
}
|
||||
return it
|
||||
}
|
||||
|
||||
// loadSeriesFiles fetches the file properties of all series that lack them,
|
||||
// but only while a property filter is active (it costs one request per series).
|
||||
func (v *libraryView) loadSeriesFiles() {
|
||||
if !v.attr.active() {
|
||||
return
|
||||
}
|
||||
p := v.panes[kindSeries]
|
||||
var todo []sonarr.Series
|
||||
for i, it := range p.all {
|
||||
p.all[i] = v.withCachedFiles(it)
|
||||
if !p.all[i].attrsKnown {
|
||||
todo = append(todo, *p.all[i].series)
|
||||
}
|
||||
}
|
||||
v.render()
|
||||
if len(todo) == 0 {
|
||||
return
|
||||
}
|
||||
v.fileGen++
|
||||
gen := v.fileGen
|
||||
v.filePending = len(todo)
|
||||
v.render()
|
||||
|
||||
type fetched struct {
|
||||
s sonarr.Series
|
||||
attrs []arr.FileAttrs
|
||||
err error
|
||||
}
|
||||
jobs := make(chan sonarr.Series)
|
||||
results := make(chan fetched)
|
||||
const workers = 6
|
||||
for range workers {
|
||||
go func() {
|
||||
for s := range jobs {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
files, err := v.sonarr.EpisodeFiles(ctx, s.ID)
|
||||
cancel()
|
||||
f := fetched{s: s, err: err}
|
||||
for _, mf := range files {
|
||||
f.attrs = append(f.attrs, mf.Attrs())
|
||||
}
|
||||
results <- f
|
||||
}
|
||||
}()
|
||||
}
|
||||
go func() {
|
||||
for _, s := range todo {
|
||||
jobs <- s
|
||||
}
|
||||
close(jobs)
|
||||
}()
|
||||
go func() {
|
||||
for range todo {
|
||||
f := <-results
|
||||
v.app.post(func() {
|
||||
if gen != v.fileGen {
|
||||
return
|
||||
}
|
||||
v.filePending--
|
||||
if f.err != nil {
|
||||
v.app.SetService("Sonarr", "", f.err)
|
||||
} else {
|
||||
v.seriesFiles[f.s.ID] = seriesFiles{f.s.Statistics.EpisodeFileCount, f.s.Statistics.SizeOnDisk, f.attrs}
|
||||
for i, it := range p.all {
|
||||
if it.id == f.s.ID {
|
||||
p.all[i].attrs, p.all[i].attrsKnown = f.attrs, true
|
||||
}
|
||||
}
|
||||
}
|
||||
if v.filePending%10 == 0 { // keep redraws cheap for big libraries
|
||||
v.render()
|
||||
}
|
||||
})
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.stinnesbeck.com/nils/arr-tui/internal/arr"
|
||||
"git.stinnesbeck.com/nils/arr-tui/internal/radarr"
|
||||
"git.stinnesbeck.com/nils/arr-tui/internal/sonarr"
|
||||
)
|
||||
@@ -49,7 +50,7 @@ func TestFilterItems(t *testing.T) {
|
||||
}
|
||||
ids := func(f int, text string) string {
|
||||
var s string
|
||||
for _, it := range filterItems(items, f, text) {
|
||||
for _, it := range filterItems(items, f, text, attrFilter{}) {
|
||||
s += itoa(it.id)
|
||||
}
|
||||
return s
|
||||
@@ -90,3 +91,66 @@ func TestEpisodeStatus(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttrFilter(t *testing.T) {
|
||||
uhd := arr.FileAttrs{Resolution: 2160, Codec: arr.CodecX265, HDR: true, DV: true, Languages: []string{"English", "German"}}
|
||||
hd := arr.FileAttrs{Resolution: 1080, Codec: arr.CodecX264, Languages: []string{"French"}}
|
||||
sd := arr.FileAttrs{Resolution: 480}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
f attrFilter
|
||||
files []arr.FileAttrs
|
||||
want bool
|
||||
}{
|
||||
{"inactive matches any file", attrFilter{}, []arr.FileAttrs{hd}, true},
|
||||
{"no files never matches an active filter", attrFilter{res: "1080p"}, nil, false},
|
||||
{"resolution", attrFilter{res: "1080p"}, []arr.FileAttrs{hd}, true},
|
||||
{"resolution mismatch", attrFilter{res: "2160p"}, []arr.FileAttrs{hd}, false},
|
||||
{"SD", attrFilter{res: "SD"}, []arr.FileAttrs{sd}, true},
|
||||
{"SD excludes HD", attrFilter{res: "SD"}, []arr.FileAttrs{hd}, false},
|
||||
{"codec", attrFilter{codec: "x265"}, []arr.FileAttrs{uhd}, true},
|
||||
{"hdr includes DV+HDR", attrFilter{hdr: "HDR"}, []arr.FileAttrs{uhd}, true},
|
||||
{"dv", attrFilter{hdr: "DV"}, []arr.FileAttrs{hd}, false},
|
||||
{"sdr", attrFilter{hdr: "SDR"}, []arr.FileAttrs{hd}, true},
|
||||
{"sdr excludes hdr", attrFilter{hdr: "SDR"}, []arr.FileAttrs{uhd}, false},
|
||||
{"language is case-insensitive", attrFilter{lang: "german"}, []arr.FileAttrs{uhd}, true},
|
||||
{"all criteria must match one file", attrFilter{res: "1080p", codec: "x265"}, []arr.FileAttrs{hd, uhd}, false},
|
||||
{"any file of a series", attrFilter{res: "1080p", codec: "x264"}, []arr.FileAttrs{uhd, hd}, true},
|
||||
} {
|
||||
if got := tc.f.match(tc.files); got != tc.want {
|
||||
t.Errorf("%s: match = %v, want %v", tc.name, got, tc.want)
|
||||
}
|
||||
}
|
||||
if s := (attrFilter{res: "1080p", hdr: "DV"}).String(); s != "1080p DV" {
|
||||
t.Errorf("String = %q", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterItemsByAttrs(t *testing.T) {
|
||||
items := []libItem{
|
||||
{id: 1, title: "A", attrs: []arr.FileAttrs{{Resolution: 1080, Codec: arr.CodecX265}}, attrsKnown: true},
|
||||
{id: 2, title: "B", attrs: []arr.FileAttrs{{Resolution: 1080, Codec: arr.CodecX264}}, attrsKnown: true},
|
||||
{id: 3, title: "C"}, // series whose files were not read yet
|
||||
}
|
||||
got := filterItems(items, 0, "", attrFilter{codec: arr.CodecX265})
|
||||
if len(got) != 1 || got[0].id != 1 {
|
||||
t.Errorf("got %+v", got)
|
||||
}
|
||||
if got := filterItems(items, 0, "", attrFilter{}); len(got) != 3 {
|
||||
t.Errorf("without filter: %d items", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMovieItemAttrs(t *testing.T) {
|
||||
m := radarr.Movie{ID: 1, Title: "M", HasFile: true, MovieFile: &arr.MediaFile{SceneName: "M.2160p.DV.x265-G"}}
|
||||
it := newMovieItem(m)
|
||||
if !it.attrsKnown || len(it.attrs) != 1 || !it.attrs[0].DV || it.attrs[0].Codec != arr.CodecX265 {
|
||||
t.Errorf("attrs = %+v", it.attrs)
|
||||
}
|
||||
if it := newMovieItem(radarr.Movie{ID: 2, Title: "N"}); !it.attrsKnown || len(it.attrs) != 0 {
|
||||
t.Errorf("movie without file: %+v", it)
|
||||
}
|
||||
if it := newSeriesItem(sonarr.Series{ID: 3, Title: "S", Statistics: sonarr.Statistics{EpisodeFileCount: 2}}); it.attrsKnown {
|
||||
t.Error("series with files must fetch its attributes")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user