CandyGifView
Terminal GIF viewer
candy-gifview is a terminal GIF viewer. Point it at a .gif and it decodes the file with ext-gd, sizes the frame grid to your terminal, and plays the animation in the alternate screen as 24-bit background blocks or as an ASCII luminance ramp. The decode, render and playback layer lives in sugar-animation. This package is the command-line front end over it, and GIF is the only input format it reads.
Install
composer require sugarcraft/candy-gifview # binary lands in vendor/bin/candy-gifview
Requires PHP 8.3 or newer and ext-gd, which is declared in composer.json, so the platform refuses an install without it. The CLI also checks for ext-gd before it touches the file, and exits non-zero with a clear message if it is missing.
A rolling phar is published on GitHub Releases as candy-gifview.phar. The channel is frozen at the 2026-11-04 build, commit f1c90bb34, because rolling rebuilds are blocked fleet-wide. Until the release page shows an artifact newer than that date, prefer the Composer install. To check a download, compare the .sha256 sidecar and the .phar.commit sidecar. No minisign key is anchored for this app yet, so these two files show what you hold rather than who built it.
sha256sum -c candy-gifview.phar.sha256 # integrity
grep -m1 . candy-gifview.phar.commit # which build you actually hold
Quickstart
candy-gifview my-animation.gif # solid 24-bit blocks (the default preset)
candy-gifview my-animation.gif density # ASCII luminance ramp
candy-gifview --fps=12 --paused clip.gif # a uniform 12 fps, starting on the first frame
With a project-local install, run ./vendor/bin/candy-gifview instead. The synopsis is candy-gifview [options] <gif> [solid|density]. Flags may appear before or after the positional arguments.
Concepts
The viewer has three stages. Intake reads the file and decodes it into frames, sized to a grid of terminal cells. Playback schedules those frames on the clock the GIF's own delays define, or on a rate you choose. The renderer paints each frame with SGR escapes into the alternate screen, and quitting restores the terminal's prior contents.
Both presets emit 24-bit truecolor SGR. solid paints the cell background (48;2) with a space glyph. density paints the foreground (38;2) on a luminance ramp of characters. The viewer paints with escape sequences rather than a terminal graphics protocol such as kitty, sixel or iTerm2, so it works anywhere 24-bit color does. Inline graphics are a different route, offered by sugar-image.
Sizing the viewport
The viewer sizes the frame grid through sugar-core's terminal façade. Each axis is read on its own. A positive-integer COLUMNS or LINES wins for its axis, and an axis left unset is probed from stdout. Off a terminal, such as piped output or CI, an unset axis falls back to a deterministic 60 by 24 cells. A probe failure therefore cannot crash the run, and the worst answer is the fallback grid.
Frames are decoded once, at the launch size
Growing the window reveals no extra detail, because frames were decoded at the launch size. Shrinking the window re-clamps the grid and crops the frame. Only a restart re-queries the terminal, and a --watch reload also re-decodes at the frozen launch budget.
Tasks
Play at a fixed rate or a multiple of the GIF's rate
candy-gifview --fps=10 clip.gif # a uniform 10 frames per second
candy-gifview --speed=2 clip.gif # twice the GIF's average rate
candy-gifview --speed=24fps clip.gif # an absolute 24 frames per second
candy-gifview --fps=10 --speed=2 clip.gif # plays at 20 fps: the multiplier composes onto the rate
The rate arithmetic runs on three slots. A rate slot is written by --fps=N and by --speed=Nfps, so the last of those wins. A multiplier from --speed=N composes onto whatever the rate slot holds, so the flag order does not matter. --speed=max is a latch: once seen, it overrides every other rate flag and a later flag cannot clear it.
Stop by itself, for scripts and CI
COLUMNS=80 LINES=24 script -qfec 'candy-gifview --duration=5 clip.gif' /dev/null # five seconds of playback under a pseudo-terminal
timeout 5 candy-gifview loop.gif # stdin held open by a terminal or a pipe: exit 124 when the timer fires
candy-gifview clip.gif < /dev/null # stdin at end of file: exits at once with status 0
An animation with no finite loop count repeats forever, so something outside the animation has to end the run. Stdin at end of file is one such thing, and it ends playback at once. A run with stdin from /dev/null therefore never plays for its --duration. --duration=N times the run only when stdin is a terminal, which is why the first command runs under script. The COLUMNS and LINES pair sizes the grid there, because a pseudo-terminal opened by script may report no size. Over an open pipe that is not a terminal, --duration does not stop the run, so use timeout(1) instead. --duration counts playback time, not wall time, so paused time does not count. It needs at least two frames, and a single-frame file with --duration is refused.
Reload the file when it changes
candy-gifview --watch frames.gif
--watch polls the file every 10 ms on the event loop and reloads it when it changes. A reload runs its decode synchronously inside the poll tick, so a large replacement file can stall one tick. A reload that trips a cap keeps the stale frames rather than blanking the screen.
Start paused and step through frames
candy-gifview --paused clip.gif # opens on the first frame; Space resumes, arrows step
Playback can start paused, and it is fully steppable by key. ← and → step one frame and wrap at the ends. This is also the way to meet the reduced-motion preference, since the viewer does not read the environment for it.
Answer in another language
candy-gifview --lang=de clip.gif # German messages, whatever LC_* says
The --lang flag is resolved before the first message prints, so it works on hosts where no LC_* variable exists, such as native Windows cmd. An unknown code is refused in plain English, and the refusal names the locales that are available.
API reference
The public classes live in SugarCraft\GifView\, except Version, which lives in SugarCraft\GifView\Support\. Most of the work happens in the sugar-animation classes that this app wires together, so the list below covers the app's own surface.
Cli
| Member | Description |
|---|---|
Cli::main(array $argv, $stdout, $stderr, ?\Closure $play = null, ?\Closure $watchStat = null, ?\Closure $decode = null): int | The viewer front end. $argv includes the script name at index 0. The return value is the exit code. The optional closures are test seams: $play receives the wired Player instead of the blocking program loop, $watchStat answers stat() for the watcher, and $decode answers the frames and loop count for a path. |
Cli::runningVersion(): string | The version this run reports. Inside a phar it is the build stamp. Otherwise it is Version::CURRENT. |
Cli::viewportCells(): array | The grid size the viewer uses, from the environment or the terminal, or the 60 by 24 fallback. |
Cli::FALLBACK_CELLS_W, Cli::FALLBACK_CELLS_H | The fallback grid, 60 by 24 cells. |
Cli::MAX_FRAMES | The outer frame ceiling, 4096. The engine's own limit of 256 frames applies first. |
Cli::MAX_INPUT_BYTES | The file size cap, 64 MiB. A file over the cap is refused. |
Cli::MAX_RESIDENT_CELLS | The resident-cell budget for the frames, 6,000,000 cells for the current viewport. |
Version and Lang
| Class | Member | Description |
|---|---|---|
| Version | CURRENT = '0.1.0', TAG_PREFIX = 'candy-gifview/' | The compiled-in version and the tag prefix that release stamps carry. |
| Version | resolve(string $archive, string $fallback = self::CURRENT) | The stamp inside a running phar, or the fallback when the stamp is absent or unparseable. |
| Version | fromPharMetadata(string $archivePath) | Read and normalize the stamp from a phar's metadata, or return null. A corrupt archive returns null rather than throwing. |
| Version | normalizeStamp(string $stamped), isStampable(string $stamped) | Turn a stamp into a printable version, or null. A stamp is printable when it is a bare release such as 1.4.2, or a development identity such as dev-1a2b3c4 or dev-build. |
| Lang | Lang::t($key, $params) | Translate a message key for the active locale. It extends the shared i18n base and loads the catalogs under lang/. |
Classes from sugar-animation
The viewer's playback and decoding are provided by sugar-animation. Decoder holds the limits MAX_FRAMES (256 frames), MAX_CELLS (100,000 output cells) and MAX_TOTAL_PIXELS (2,000,000 source pixels). Player holds MAX_FPS, and it is the player that Cli::main() wires and passes to the $play seam. See the sugar-animation page for those classes.
Only methods declared in candy-gifview/src, or inherited from a parent declared there, are listed. A type that extends a class outside that tree, such as \RuntimeException, does not show the methods of that class.
SugarCraft\GifView
Cli final class
Command-line entry point: parse argv, size the decode grid to the viewport, hand the frames to sugar-animation's Player, and run the SugarCraft Program.
| Method | Summary |
|---|---|
static runningVersion(): string | The version this run reports: the phar archive's build stamp when executing inside one (InstalledVersions lies under the frozen phar vendor/, so the stamp is the only truth), else the compiled-in Support\Version::CURRENT. The archive is identified by \Phar::running(false), which is exactly the real path the \Phar constructor wants here (this class only ever runs required from inside the archive, never as the stub). |
static main(array $argv, $stdout, $stderr, ?\Closure $play = null, ?\Closure $watchStat = null, ?\Closure $decode = null): int | Run the viewer front end. |
static viewportCells(): array | Size the decode grid to the terminal viewport: rows-2, one row for the Player's status line and one for the trailing newline its view() emits — an over-tall final line scrolls the alt screen by one. The COLUMNS/LINES environment pair wins first (bare positive integers — the universal CLI escape hatch for hosts the probe cannot answer); otherwise the never-throwing sugar-core Tty facade probes STDOUT. Off a terminal (piped output, CI) the grid is the deterministic 60x24 fallback. On resize the engine re-clamps to rows-1 itself (sugar-animation Player; engine seam), so the reserve is exact for the initial paint. |
Lang final class
Per-library translation facade for candy-gifview.
SugarCraft\GifView\Support
Version final class
Single source of truth for the printed version.
| Method | Summary |
|---|---|
static resolve(string $archive, string $fallback = self::CURRENT): string | Resolve the version for the current run. |
static fromPharMetadata(string $archivePath): ?string | Read and normalize the version stamp inside an archive, or null when the path is empty, is not a file, is not a readable phar, or its stamp does not parse as a comparable release. Every failure mode falls back to null rather than throwing — a corrupt archive must not crash --version. |
static normalizeStamp(string $stamped): ?string | Normalize a build stamp to a printable version, or null when it names neither a comparable release nor a recognised development identity: TAG_PREFIX stripped, a leading v stripped, then the COMPARABLE release shape, then the DEV grammar. One grammar, one home — the producer (tools/build-phar.php) validates against exactly this rule through isStampable(), so a stamp that would be dropped at runtime is refused at build time instead of silently printing CURRENT. |
static isStampable(string $stamped): bool | Whether a producer may stamp this string at all (the consumer decides). |
Configuration
Options
| Option | Meaning |
|---|---|
--fps=N | Play at a uniform N frames per second, instead of the GIF's delays. N is a positive number, and fractions such as 12.5 work. Values above Player::MAX_FPS clamp silently. There is no lower clamp. |
--speed=N | Play at the GIF's average rate multiplied by N. N is positive and may be fractional. |
--speed=Nfps | An absolute rate of N frames per second. This shares the rate slot with --fps. |
--speed=max | Burn frames as fast as the scheduler's floors allow. |
--duration=N | Stop after N seconds of playback time. Needs at least two frames. |
--watch | Reload when the source file changes. A bare flag. |
--paused | Start paused on the first frame. A bare flag. |
--lang=CODE | Answer in one of the 16 bundled locales, regardless of the environment. |
-h, --help | Print the localized usage line and exit 0. |
-v, --version | Print the version stamp and exit 0. |
A standalone -- ends option parsing. Every token after it is a path, so a GIF named -foo.gif can be played as -- -foo.gif. A bare - is not read as stdin. It is an ordinary path, and it is refused as could not play '-'.
Environment
| Variable | Effect |
|---|---|
COLUMNS, LINES | A positive-integer pair that sets the viewport size. It wins over the terminal probe. The viewer reserves two rows of chrome, so whenever both axes resolve, the grid is LINES minus 2 rows, whether the values come from the environment or the probe. COLUMNS is used as given. Each axis is taken from the environment first and from the probe second. |
LC_ALL, LC_MESSAGES, LANG | Message locale, read in that order, with the first usable value winning. --lang overrides all three. |
The loop count in the file
A loop count of N in the GIF means N extra repeats after the first play, so N+1 passes in total. A count of 0, or a file with no loop extension, plays forever. That forever default deliberately differs from the single-play answer of libnsgif.
Keys and keybindings
| Key | Action |
|---|---|
| Space | Pause or resume |
| ← / Left | Step back one frame, wrapping |
| → / Right | Step forward one frame, wrapping |
| d | Toggle between the solid and density presets |
| q, Esc, Ctrl+C | Quit |
The key table is fixed in the player model. Keys cannot be rebound.
Theming
The two presets are the only styling. solid is the default and suits pictures, and density suits legibility, since its character ramp survives color-blind viewing and poor-color terminals. Switch with the preset argument at launch, or press d during playback.
candy-gifview clip.gif solid
candy-gifview clip.gif density
There is no 256-color fallback and no NO_COLOR or monochrome mode. On a terminal without truecolor the blocks degrade to default or nearby colors. On TERM=dumb or other capability-poor terminals, expect raw escape sequences rather than a usable frame. Transparent GIF pixels are composited against the terminal's own background.
Examples
The examples/ directory holds three PHP programs. They load the Composer autoloader, so run them from a checkout after composer install.
examples/view.phpbuilds a small synthetic animated GIF in a temporary file and plays it throughCli::main(), the same entry point the binary uses. The file is removed on exit. Any extra arguments are passed to the CLI, so a timed run needs a pseudo-terminal, as the command below shows. This is the program behind the demo GIF below.examples/refusals.phprunsCli::main()on four argument vectors,--help,--version, a bad preset and a missing file, with memory streams and a stub player. It prints each exit code and the first line of output, so you can see the exit-code contract without a terminal.examples/version-stamp.phpshows howVersion::normalizeStamp()andisStampable()treat a release tag, a bare release, a development build and an unrecognized string.
php examples/refusals.php < /dev/null
php examples/version-stamp.php < /dev/null
COLUMNS=80 LINES=24 script -qfec 'php examples/view.php --duration=1' /dev/null
Testing
The suite runs under PHPUnit 10. Run it once from the package root, with stdin redirected:
cd candy-gifview && composer install && vendor/bin/phpunit < /dev/null
The tests cover the entry point, the watch loop and the binary. CliTest and CliLoopWatchTest drive Cli::main() through its seams. BinBootTest and BinSizeQueryPolarityTest cover the bin/candy-gifview shim. WatchRealHostTest exercises the watcher against the real filesystem. VersionTest covers the stamp grammar, and LocaleParityTest checks that every lang/ catalog carries the same keys. PharBuildScriptTest covers the phar build.
Troubleshooting
The process never exits
The GIF has no loop count, so it repeats forever. The run keeps going while stdin stays open, such as at a terminal or a pipe that nothing closes. Quit it with a key, stop it with a signal, or let timeout(1) end it, and timeout then exits 124. With stdin at end of file the run exits at once instead, so a job that expects a long playback and gets a quick exit should check its stdin. --duration=N times the run only under a pseudo-terminal.
Exit 1 when ext-gd is missing, or a file fails to decode
The CLI needs ext-gd to decode GIFs. Install it, for example with apt install php-gd on Debian-based systems, and check it with php -m. A file that fails to decode also exits 1. Either way the message is written to stderr.
Output is raw escape sequences
The terminal lacks truecolor, or TERM is dumb. The blocks degrade on a limited palette, and density stays legible there. On TERM=dumb there is no usable frame, and the viewer cannot help with that.
The viewer is the wrong size
The grid is sized once at launch. A window that is grown after launch keeps the old frame detail, so restart the viewer. Off a terminal, the grid is the 60 by 24 fallback. Set COLUMNS and LINES to size it explicitly.
A large file is refused, or the first frames are missing
A file over 64 MiB is refused. The engine decodes at most 256 frames, and a longer file is cut to its first 256 frames. A notice on stderr says so. A file with exactly 256 frames gets the same notice, because a full window and a truncated stream look the same to the engine.
A --watch reload leaves the old frames on screen
A reload is refused when it trips a cap, and then the stale frames stay on screen. A large replacement file can also delay one tick, because its decode runs synchronously. Check the file's size against the caps listed under Limits.
Limits
| Limit | Value | Where |
|---|---|---|
| Frames decoded per animation | 256 | Decoder::MAX_FRAMES, the engine's intake window |
| Frames the CLI accepts | 4,096 | Cli::MAX_FRAMES, the outer ceiling |
| File size | 64 MiB | Cli::MAX_INPUT_BYTES, enforced by a bounded read |
| Resident frame cells | 6,000,000 | Cli::MAX_RESIDENT_CELLS |
| Output grid cells | 100,000 | Decoder::MAX_CELLS |
| Source canvas pixels | 2,000,000 | Decoder::MAX_TOTAL_PIXELS |
Intake is bounded by the read itself. At most the cap plus one byte is pulled, and the length actually seen decides, so a file that grows during the read cannot slip past the cap. FIFOs and devices are refused by an is_file() check before the open.
Exit codes
| Code | Meaning |
|---|---|
| 0 | --help or --version printed, or playback ended normally |
| 1 | A flag or argument refusal, the ext-gd refusal, or a decode failure. The message is on stderr. |
| 128+N | The loop was stopped by signal N. For example, 143 is SIGTERM. |
Accessibility
- The density preset encodes brightness as character density, not only as color, so it reads on colorblind viewing and poor-color terminals.
- Playback can start paused with
--paused, and it can be stepped one frame at a time with the arrow keys. Motion is never required to see the content. - The frame rate is capped at the player's own scheduler floors.
--speed=maxis opt-in and is never the default. - Reduced motion is not detected automatically. The viewer does not read
REDUCE_MOTIONorPREFERS_REDUCED_MOTION, so motion-averse users should pass--paused. This is a known gap against the fleet convention. - CLI messages are localized into 16 locales, and the playback status line follows the same locale, so
--langchanges both.
Upstream mapping
| Upstream | Where it shows up here |
|---|---|
| chicle, a CLI viewer over chafa | The flag-parser model. --speed with its multiplier and absolute forms, --watch and the rebind behavior follow chicle, and the source cites its option parser line by line. |
| namzug16/gifterm | The design antecedent. The decode, render and playback layer it mirrors is developed as sugar-animation. |
| libnsgif | The reference for GIF loop semantics, from which the forever-by-default loop count deliberately differs. |
SugarCraft is a native PHP project. This app is not a port of any single upstream. The docblocks in src/Cli.php cite the chicle source lines that the flag parser follows.
Architecture
Intake, then playback
Cli::main() parses flags, checks ext-gd, reads the file under its caps and decodes it into frames sized to the grid. It then wires a Player from sugar-animation and runs the program loop on it. The $play seam replaces that last step for tests and examples.
Language is fixed before output
--lang is scanned before anything prints, so the locale is set before the first message. The unknown-code refusal is an English literal, because a localized refusal would need a locale the user has not yet chosen.
Changelog and versioning
The version is Version::CURRENT, 0.1.0, in a checkout. Inside a phar, the build stamp replaces it. A release stamp is a bare version such as 1.4.2, and a development stamp is dev- followed by a commit hash or dev-build. A development stamp prints verbatim, so a development phar never looks like a stable release.
Related
- sugar-animation: the decode, render and playback layer this app runs on.
- sugar-image: inline graphics, for when a terminal graphics protocol is wanted.
- sugar-core: the terminal façade that sizes the viewport.
Demos.
VHS-recorded demo of examples/view.php, which plays a synthetic GIF through the CLI. Regenerated automatically on every push that touches the tape or the source.