diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ab8087..dd3bcbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +- **Posters all the same size:** the details pane always shows a poster in a 2:3 box. Images + slightly off that shape (some Radarr/Sonarr hand out) fill it, trimmed at the long sides; images + far from it (square, landscape) are shown whole and centred. Before, such posters came out + smaller than the others in the same list. - **Posters and casts are kept between runs** (in `~/.cache/arr-tui`; posters 30 days and at most 200 MB, casts 7 days), so they show at once after a restart and are not downloaded again. - **Downloading titles in the Library:** the dot before a movie or series turns blue while it is in diff --git a/SPECS.md b/SPECS.md index 4565b92..5ddca63 100644 --- a/SPECS.md +++ b/SPECS.md @@ -64,7 +64,7 @@ Notes: - Global speed / pause state in the status bar. Pause/resume and delete a SABnzbd job. - **Status bar / help:** the bottom bar lists the keys that work in the current view or panel; the top right shows the connection state of each service; `F9` opens the full key reference. - **Posters:** the detail panes show the poster at the top right; the head (title, facts, path) and the plot are one text that flows along the poster's left side and continues at full width below it (text is wrapped by arr-tui itself, with a narrower width for the first lines); on panes too small for the poster, the text simply uses the full width. Two ways to draw the poster, chosen by `ui.cover_style`: - - **Real images** (`kitty`, and `auto` in kitty/Ghostty): the Kitty graphics protocol with Unicode placeholders. The poster (kept up to 640 px tall) is uploaded as PNG (`a=t,f=100,q=2`, 4096-byte base64 chunks) and placed virtually with the cell footprint (`a=p,U=1,c=,r=`) right away. Every footprint (crop and cell size) is its own image id, placed once and never changed: Ghostty kept showing the old, small placement when the size of an existing one was changed, so a new size uploads under a new id and deletes the old one. The poster is shown by writing U+10EEEE placeholder characters whose combining marks give row and column (the 297-entry table from the protocol) and whose foreground color is the image id. Since it is ordinary text, popups, dialogs and view changes cover it correctly and tcell's diffing keeps it in place. The footprint uses the real cell size in pixels (the window's pixel size divided by its cell count, rounded down to whole pixels so padding around the grid does not skew it; all images are deleted whenever the window or cell size changes, block characters stand in while the size keeps changing, and real images are uploaded afresh once it has been stable for 300 ms, because uploading and placing for every intermediate size of a window drag raced with the terminal's own resizing) and a box whose shape is within 3% of the poster's; the remaining sliver is cropped from the poster's edges before upload (one upload per size, the previous one is deleted), because the terminal letterboxes an image that does not fit its box exactly. As a second safeguard the placeholder cells use the panes' background color, so anything the picture does not cover blends in (without it the terminal's own, slightly different, default background showed as grey bars beside the poster). Writes to the terminal go through the screen's tty on the UI goroutine, so they cannot interleave with drawing. Requires 24-bit color; otherwise, and inside tmux/screen, the block fallback is used. Terminals are recognised from `TERM`/`TERM_PROGRAM`/`KITTY_WINDOW_ID`/`GHOSTTY_RESOURCES_DIR`, and then asked before the screen starts (a Kitty graphics query and a background-color query followed by a device-attributes request, 300 ms timeout, waiting with select(2) because a read deadline does not work on a terminal device on macOS): only a terminal that answers the query gets real images, also with `cover_style: kitty`, because a terminal without the protocol prints the commands as text. The Log says which way posters are drawn. With `ARR_TUI_DEBUG_GFX=1` the Log (F7) lists every window size, the cell shape, every upload (poster size, crop, cells) and every change of what is drawn (image id, position, cells); with a path (`ARR_TUI_DEBUG_GFX=/tmp/arr-gfx.log`) the same lines also go to that file with millisecond timestamps, so they can be read without leaving the view that shows the problem. The go-termimg library was looked at and not used: its placeholder support is marked experimental, its diacritic table does not match the protocol's, and it writes images straight to the terminal instead of through the screen. + - **Real images** (`kitty`, and `auto` in kitty/Ghostty): the Kitty graphics protocol with Unicode placeholders. The poster (kept up to 640 px tall) is uploaded as PNG (`a=t,f=100,q=2`, 4096-byte base64 chunks) and placed virtually with the cell footprint (`a=p,U=1,c=,r=`) right away. Every footprint (crop and cell size) is its own image id, placed once and never changed: Ghostty kept showing the old, small placement when the size of an existing one was changed, so a new size uploads under a new id and deletes the old one. The poster is shown by writing U+10EEEE placeholder characters whose combining marks give row and column (the 297-entry table from the protocol) and whose foreground color is the image id. Since it is ordinary text, popups, dialogs and view changes cover it correctly and tcell's diffing keeps it in place. The footprint uses the real cell size in pixels (the window's pixel size divided by its cell count, rounded down to whole pixels so padding around the grid does not skew it; all images are deleted whenever the window or cell size changes, block characters stand in while the size keeps changing, and real images are uploaded afresh once it has been stable for 300 ms, because uploading and placing for every intermediate size of a window drag raced with the terminal's own resizing) and a box whose shape is within 3% of 2:3 (every poster gets the same 2:3 box: an image within 12% of that shape fills it, trimmed at its long sides, also with block characters; one further off, e.g. square or landscape, is shown whole at the largest size that fits, centred in the box); the remaining sliver is cropped from the poster's edges before upload (one upload per size, the previous one is deleted), because the terminal letterboxes an image that does not fit its box exactly. As a second safeguard the placeholder cells use the panes' background color, so anything the picture does not cover blends in (without it the terminal's own, slightly different, default background showed as grey bars beside the poster). Writes to the terminal go through the screen's tty on the UI goroutine, so they cannot interleave with drawing. Requires 24-bit color; otherwise, and inside tmux/screen, the block fallback is used. Terminals are recognised from `TERM`/`TERM_PROGRAM`/`KITTY_WINDOW_ID`/`GHOSTTY_RESOURCES_DIR`, and then asked before the screen starts (a Kitty graphics query and a background-color query followed by a device-attributes request, 300 ms timeout, waiting with select(2) because a read deadline does not work on a terminal device on macOS): only a terminal that answers the query gets real images, also with `cover_style: kitty`, because a terminal without the protocol prints the commands as text. The Log says which way posters are drawn. With `ARR_TUI_DEBUG_GFX=1` the Log (F7) lists every window size, the cell shape, every upload (poster size, crop, cells) and every change of what is drawn (image id, position, cells); with a path (`ARR_TUI_DEBUG_GFX=/tmp/arr-gfx.log`) the same lines also go to that file with millisecond timestamps, so they can be read without leaving the view that shows the problem. The go-termimg library was looked at and not used: its placeholder support is marked experimental, its diacritic table does not match the protocol's, and it writes images straight to the terminal instead of through the screen. - **Block characters** (`quadrant`, `sextant`, `cells`, and the fallback): every cell shows several pixels in the best two-color split (`quadrant`: 2×2 pixels, glyphs from the Block Elements range every font has; `sextant`: 2×3 pixels, sharper, glyphs from Symbols for Legacy Computing; `cells`: 1 pixel per cell drawn as background color only, because macOS Terminal draws block elements from the font with the halves off the cell's middle). `auto` picks cells in macOS Terminal (`TERM_PROGRAM=Apple_Terminal`), sextants on kitty, WezTerm, foot and Ghostty, and on other terminals (not on macOS, whose terminals do not use fontconfig's fonts) when fontconfig lists a font with the sextant characters (`fc-list :charset=1fb00`, about 20 ms at startup; terminals draw glyphs their font lacks from fallback fonts; skipped over SSH because the installed fonts are the remote machine's), and quadrants otherwise. Posters are JPEG, PNG, GIF or WebP (Radarr and Sonarr keep whatever bytes the provider sent under a `.jpg` name, which can be WebP), decoded and downscaled in the process; for any other format the Log says what the data looked like (e.g. AVIF, an HTML error page, or its first bytes). Library items fetch `/MediaCover//poster-250.jpg` (250×375; `poster-500.jpg` when posters are real images, which otherwise enlarge the small copy) from their Radarr/Sonarr instance with the API key (then the original `poster.jpg`; an instance that answers 200 with an HTML page, i.e. wants a login for `/MediaCover`, is also tried with the key as the `apikey` query parameter; and if the instance has no usable copy, the provider's `remoteUrl`, fetched without credentials); search results fall back to the provider's `remoteUrl` (fetched without credentials). Posters are cached in memory (the terminal's copies are deleted when the cache is dropped and on exit), failed downloads are not retried for two minutes and are written to the Log (F7) with the reason, and the poster shrinks or disappears when the pane is too short. `ui.covers: false` disables it. - **Calendar view (F5):** a two-week window (starting today, moved a week at a time with `[`/`]`, `t` returns to today) with the library's movie releases and airing episodes, grouped by day. Radarr `GET /calendar` supplies the cinema, digital and physical release dates (taken as written: midnight UTC means that calendar day) and Sonarr `GET /calendar?includeSeries=true` the episodes with their air time (shown in local time). Unmonitored items are included with `u`. A movie appears once per release date in the window. Each entry shows type, title, episode or release kind and status (downloaded / missing / upcoming); the detail pane shows poster and plot; `Enter`/`i` starts the release search and `s` an automatic search. diff --git a/internal/ui/cover.go b/internal/ui/cover.go index f85f74d..fdf7034 100644 --- a/internal/ui/cover.go +++ b/internal/ui/cover.go @@ -311,13 +311,26 @@ func (c *coverView) aspectOfCells() float64 { func (c *coverView) setImage(img *image.RGBA) { c.img, c.note, c.cells = img, "", nil } func (c *coverView) setNote(note string) { c.img, c.note, c.cells = nil, note, nil } -// aspect returns the width and height of the poster (2:3 until one is loaded). -func (c *coverView) aspect() (w, h int) { - if c.img == nil { - return 2, 3 +// Every poster gets a box of the usual poster shape, 2:3, so all posters of a +// list have the same size whatever image Radarr/Sonarr hand out. +const ( + posterW, posterH = 2, 3 + // posterShapeTolerance is how far an image's shape may differ from 2:3 and + // still fill the box (trimmed at the long sides); one further off is shown + // whole inside it. + posterShapeTolerance = 0.12 +) + +// aspect returns the shape of the box the poster is shown in. +func (c *coverView) aspect() (w, h int) { return posterW, posterH } + +// nearPosterShape reports whether an image is close enough to 2:3 to fill the box. +func nearPosterShape(b image.Rectangle) bool { + if b.Dx() <= 0 || b.Dy() <= 0 { + return false } - b := c.img.Bounds() - return b.Dx(), b.Dy() + d := float64(b.Dx())*posterH/(float64(b.Dy())*posterW) - 1 + return d >= -posterShapeTolerance && d <= posterShapeTolerance } // coverFitTolerance is how far the shape of the cell box may differ from the @@ -376,19 +389,26 @@ func (c *coverView) Draw(screen tcell.Screen) { return } + // The 2:3 box, right-aligned in the area. An image close to that shape + // fills it (trimmed); one far from it (square, landscape) is shown whole, + // centred in the box. b := c.img.Bounds() - cols, rows := coverCells(b.Dx(), b.Dy(), w, h, c.aspectOfCells()) + boxW, boxH := coverCells(posterW, posterH, w, h, c.aspectOfCells()) + cols, rows := boxW, boxH + if !nearPosterShape(b) { + cols, rows = coverCells(b.Dx(), b.Dy(), boxW, boxH, c.aspectOfCells()) + } if cols < 1 || rows < 1 { return } - if c.drawGraphics(screen, x+w-cols, y, cols, rows) { + x += w - boxW + (boxW-cols)/2 + y += (boxH - rows) / 2 + if c.drawGraphics(screen, x, y, cols, rows) { return } if c.cells == nil || c.cols != cols || c.rows != rows { c.cols, c.rows, c.cells = cols, rows, c.build(cols, rows) } - // Right-align the poster inside its area, so a narrower poster sits against the edge. - x += w - cols for row := 0; row < rows; row++ { for col := 0; col < cols; col++ { cell := c.cells[row*cols+col] @@ -445,7 +465,10 @@ func (c *coverView) drawGraphics(screen tcell.Screen, x, y, cols, rows int) bool // build renders the poster into cols×rows cells. func (c *coverView) build(cols, rows int) []styledCell { st := c.style - scaled := boxScale(c.img, cols*st.sw, rows*st.sh) + // Trim the image to the shape of the cells first, as for real images, so + // it is not stretched. + crop := cropToBox(c.img.Bounds(), cols, rows, c.aspectOfCells()) + scaled := boxScale(c.img.SubImage(crop), cols*st.sw, rows*st.sh) cells := make([]styledCell, 0, cols*rows) px := make([]color.RGBA, st.sw*st.sh) for row := 0; row < rows; row++ { diff --git a/internal/ui/cover_test.go b/internal/ui/cover_test.go index edd9142..44d6296 100644 --- a/internal/ui/cover_test.go +++ b/internal/ui/cover_test.go @@ -358,3 +358,48 @@ func TestCellsStyleIsFlat(t *testing.T) { t.Errorf("got %q %v %v", ch, fg, bg) } } + +func TestPostersShareTheTwoByThreeBox(t *testing.T) { + sim := tcell.NewSimulationScreen("") + if err := sim.Init(); err != nil { + t.Fatal(err) + } + defer sim.Fini() + sim.SetSize(40, 30) + + // extent returns the columns and rows covered by poster cells (white). + extent := func(w, h int) (x0, y0, x1, y1 int) { + img := image.NewRGBA(image.Rect(0, 0, w, h)) + for i := range img.Pix { + img.Pix[i] = 255 + } + sim.Clear() + c := newCoverView(quadrantStyle, nil, func() float64 { return 2 }) + c.setImage(img) + c.SetRect(0, 0, 40, 30) + c.Draw(sim) + x0, y0, x1, y1 = 99, 99, -1, -1 + for y := 0; y < 30; y++ { + for x := 0; x < 40; x++ { + _, _, st, _ := sim.GetContent(x, y) + if _, bg, _ := st.Decompose(); bg == tcell.NewRGBColor(255, 255, 255) { + x0, y0, x1, y1 = min(x0, x), min(y0, y), max(x1, x), max(y1, y) + } + } + } + return + } + ax0, ay0, ax1, ay1 := extent(2000, 3000) // the usual shape + bx0, by0, bx1, by1 := extent(2100, 3000) // 5% wider: trimmed, same box + if ax0 != bx0 || ay0 != by0 || ax1 != bx1 || ay1 != by1 { + t.Errorf("2:3 at %d,%d-%d,%d but 0.7 at %d,%d-%d,%d", ax0, ay0, ax1, ay1, bx0, by0, bx1, by1) + } + if ay0 != 0 || ax1 != 39 { + t.Errorf("the box sits at the top right: %d,%d-%d,%d", ax0, ay0, ax1, ay1) + } + // A square image is shown whole: as wide as the box, shorter, centred. + sx0, sy0, sx1, sy1 := extent(1000, 1000) + if sx0 != ax0 || sx1 != ax1 || sy1-sy0 >= ay1-ay0 || sy0 <= ay0 || (sy0-ay0)-(ay1-sy1) > 1 || (ay1-sy1)-(sy0-ay0) > 1 { + t.Errorf("square at %d,%d-%d,%d in the box %d,%d-%d,%d", sx0, sy0, sx1, sy1, ax0, ay0, ax1, ay1) + } +}