package ui import ( "context" "fmt" "slices" "strings" "time" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" "git.stinnesbeck.com/nils/arr-tui/internal/arr" "git.stinnesbeck.com/nils/arr-tui/internal/radarr" "git.stinnesbeck.com/nils/arr-tui/internal/sonarr" ) type libKind int const ( kindMovies libKind = iota kindSeries ) func (k libKind) String() string { if k == kindMovies { return "Movies" } return "Series" } func (k libKind) service() string { if k == kindMovies { return "Radarr" } return "Sonarr" } // libItem is one row of the library: a movie or a series in display form. type libItem struct { id int title string // plain, used for sorting and filtering label string // escaped title with year monitored bool have bool // at least one file on disk missing bool // monitored and not complete status string size string details string movie *radarr.Movie series *sonarr.Series } var libFilters = []struct { name string match func(libItem) bool }{ {"all", func(libItem) bool { return true }}, {"missing", func(i libItem) bool { return i.missing }}, {"downloaded", func(i libItem) bool { return i.have }}, {"monitored", func(i libItem) bool { return i.monitored }}, {"unmonitored", func(i libItem) bool { return !i.monitored }}, } func monMark(monitored bool) string { if monitored { return "[green]●[-]" } return "[gray]○[-]" } func sizeOrBlank(b int64) string { if b <= 0 { return "" } return formatSize(b) } func newMovieItem(m radarr.Movie) libItem { it := libItem{ 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, } it.label = tview.Escape(m.Title) if m.Year > 0 { it.label += fmt.Sprintf(" (%d)", m.Year) } switch { case m.HasFile: it.status = "[green]✓ " + tview.Escape(m.QualityName()) case !m.Monitored: it.status = "[gray]unmonitored" case !m.IsAvailable: it.status = "[gray]not released" default: it.status = "[yellow]missing" } it.details = movieDetails(m) + monitoredLine(m.Monitored) + sizeLine(m.SizeOnDisk) + pathLine(m.Path) return it } func newSeriesItem(s sonarr.Series) libItem { st := s.Statistics it := libItem{ id: s.ID, title: s.Title, monitored: s.Monitored, have: st.EpisodeFileCount > 0, missing: s.Monitored && st.EpisodeFileCount < st.EpisodeCount, size: sizeOrBlank(st.SizeOnDisk), series: &s, } it.label = tview.Escape(s.Title) if s.Year > 0 { it.label += fmt.Sprintf(" (%d)", s.Year) } counts := fmt.Sprintf("%d/%d", st.EpisodeFileCount, st.EpisodeCount) switch { case !s.Monitored: it.status = "[gray]" + counts + " unmonitored" case st.EpisodeFileCount >= st.EpisodeCount: it.status = "[green]" + counts default: it.status = "[yellow]" + counts } it.details = seriesDetails(s) + monitoredLine(s.Monitored) + sizeLine(st.SizeOnDisk) return it } func monitoredLine(m bool) string { if m { return "Monitored: yes\n" } return "Monitored: [gray]no[-]\n" } func sizeLine(b int64) string { if b <= 0 { return "" } return "Size on disk: " + formatSize(b) + "\n" } func pathLine(p string) string { if p == "" { return "" } 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 { 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) { out = append(out, it) } } slices.SortFunc(out, func(a, b libItem) int { return strings.Compare(strings.ToLower(a.title), strings.ToLower(b.title)) }) return out } // libPane is one of the two lists (movies, series) of the library view. type libPane struct { kind libKind table *tview.Table all []libItem shown []libItem loaded bool err error gen int // invalidates stale loads } // libraryView shows the movies of Radarr and the series of Sonarr side by side. type libraryView struct { app *App radarr *radarr.Client sonarr *sonarr.Client root *tview.Flex header *tview.TextView input *tview.InputField details *tview.TextView panes [2]*libPane // UI-goroutine state. cur libKind // the pane that has (or last had) focus filter int } func newLibraryView(app *App, r *radarr.Client, s *sonarr.Client) *libraryView { v := &libraryView{ app: app, radarr: r, sonarr: s, header: tview.NewTextView().SetDynamicColors(true), input: tview.NewInputField().SetLabel("Filter: ").SetFieldWidth(0), details: tview.NewTextView().SetDynamicColors(true).SetWordWrap(true), } v.details.SetBorder(true).SetTitle(" Details ") lists := tview.NewFlex() for _, kind := range []libKind{kindMovies, kindSeries} { p := &libPane{kind: kind, table: tview.NewTable().SetSelectable(true, false).SetFixed(1, 0)} p.table.SetBorder(true) p.table.SetSelectionChangedFunc(func(int, int) { v.renderDetails() }) p.table.SetSelectedFunc(func(int, int) { v.open() }) p.table.SetInputCapture(v.handleKey) p.table.SetFocusFunc(func() { v.cur = kind p.table.SetBorderColor(tcell.ColorYellow) v.renderDetails() }) p.table.SetBlurFunc(func() { p.table.SetBorderColor(tview.Styles.BorderColor) }) v.panes[kind] = p lists.AddItem(p.table, 0, 1, kind == kindMovies) } v.root = tview.NewFlex().SetDirection(tview.FlexRow). AddItem(v.header, 1, 0, false). AddItem(v.input, 1, 0, false). AddItem(lists, 0, 3, true). AddItem(v.details, 0, 2, false) v.input.SetChangedFunc(func(string) { v.render() }) v.input.SetDoneFunc(func(tcell.Key) { v.app.app.SetFocus(v.pane().table) }) v.render() return v } func (v *libraryView) pane() *libPane { return v.panes[v.cur] } func (v *libraryView) handleKey(ev *tcell.EventKey) *tcell.EventKey { switch ev.Key() { case tcell.KeyTab, tcell.KeyBacktab: v.app.app.SetFocus(v.panes[1-v.cur].table) return nil case tcell.KeyRune: switch ev.Rune() { case '/': v.app.app.SetFocus(v.input) case 'f': v.filter = (v.filter + 1) % len(libFilters) v.render() case 'r': v.reload() case 'i': v.open() case 's': v.search() case 't': v.toggleMonitored() default: return ev } return nil } return ev } // activate is called when the view becomes visible; it always refreshes so // changes made elsewhere show up. func (v *libraryView) activate() { v.app.app.SetFocus(v.pane().table) v.render() v.reload() } // reload fetches both lists in parallel. func (v *libraryView) reload() { for _, p := range v.panes { v.reloadPane(p) } } func (v *libraryView) reloadPane(p *libPane) { p.gen++ gen, kind := p.gen, p.kind go func() { ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() var items []libItem var err error if kind == kindMovies { var movies []radarr.Movie if movies, err = v.radarr.Movies(ctx); err == nil { for _, m := range movies { items = append(items, newMovieItem(m)) } } } else { var series []sonarr.Series if series, err = v.sonarr.Series(ctx); err == nil { for _, s := range series { items = append(items, newSeriesItem(s)) } } } v.app.post(func() { v.app.SetService(kind.service(), "", err) if gen != p.gen { return } p.err = err if err != nil { // Keep showing the last good list; say why it is stale. v.app.SetMessage("[red]" + kind.service() + ": " + tview.Escape(err.Error())) } else { p.all, p.loaded = items, true } v.render() }) }() } func (v *libraryView) selected() (libItem, bool) { p := v.pane() r, _ := p.table.GetSelection() if r < 1 || r > len(p.shown) { return libItem{}, false } return p.shown[r-1], true } 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[-]", 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)) v.renderDetails() } func (v *libraryView) renderPane(p *libPane) { selID := -1 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.table.Clear() header(p.table, 0, "Title", "Status", "Size") selRow := 0 for i, it := range p.shown { p.table.SetCell(i+1, 0, tview.NewTableCell(monMark(it.monitored)+" "+it.label).SetExpansion(1)) p.table.SetCell(i+1, 1, tview.NewTableCell(it.status)) p.table.SetCell(i+1, 2, tview.NewTableCell(it.size).SetAlign(tview.AlignRight)) if it.id == selID { selRow = i + 1 } } placeholder := "Nothing matches the filter" switch { case p.err != nil && !p.loaded: placeholder = "Could not load, press r to retry" case !p.loaded: placeholder = "Loading…" case len(p.all) == 0: placeholder = "Empty" } restoreSelection(p.table, len(p.shown), selRow, placeholder) if selRow == 0 { p.table.ScrollToBeginning() } title := fmt.Sprintf(" %s ", p.kind) if p.err != nil && p.loaded { title = fmt.Sprintf(" %s [red](outdated)[-] ", p.kind) } p.table.SetTitle(title) } func (v *libraryView) renderDetails() { if it, ok := v.selected(); ok { v.details.SetText(it.details) v.details.ScrollToBeginning() return } v.details.SetText("") } // open starts the interactive search (movie) or the episode drill-down (series). func (v *libraryView) open() { it, ok := v.selected() if !ok { return } if it.movie != nil { m := *it.movie v.app.openReleases("Releases for "+tview.Escape(movieTitleYear(m)), func(ctx context.Context) ([]arr.Release, error) { return v.radarr.Releases(ctx, m.ID) }, v.radarr.Grab) return } v.app.openSeries(v.sonarr, *it.series, v.reload) } func (v *libraryView) search() { it, ok := v.selected() if !ok { return } if it.movie != nil { v.run("Searching for "+it.label, "Search started for "+it.label, func(ctx context.Context) error { return v.radarr.SearchMovie(ctx, it.id) }, nil) return } v.app.confirm("Search for all missing episodes of "+it.label+"?", func() { v.run("Searching for "+it.label, "Search started for "+it.label, func(ctx context.Context) error { return v.sonarr.SearchSeries(ctx, it.id) }, nil) }) } func (v *libraryView) toggleMonitored() { it, ok := v.selected() if !ok { return } want := !it.monitored doing, done := "Monitoring ", "Monitored " if !want { doing, done = "Unmonitoring ", "Unmonitored " } v.run(doing+it.label, done+it.label, func(ctx context.Context) error { if it.movie != nil { return v.radarr.SetMonitored(ctx, it.id, want) } return v.sonarr.SetMonitored(ctx, it.id, want) }, v.reload) } // run executes fn off the UI goroutine, reports the outcome and then calls after. func (v *libraryView) run(doing, done string, fn func(ctx context.Context) error, after func()) { v.app.SetMessage(doing + "…") go func() { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() err := fn(ctx) v.app.post(func() { if err != nil { v.app.SetMessage("[red]" + tview.Escape(err.Error())) return } v.app.SetMessage("[green]" + done) if after != nil { after() } }) }() }