Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
Needed a major version bump because some public API had changed
previously.
  • Loading branch information
benjajaja committed Oct 7, 2024
1 parent bc6642f commit df4605b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ratatui-image"
version = "1.0.5"
version = "2.0.0"
edition = "2021"
autoexamples = true
authors = ["Benjamin Große <ste3ls@gmail.com>"]
Expand Down
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[Crate Badge]: https://img.shields.io/crates/v/ratatui-image?logo=rust&style=flat-square
[Crate]: https://crates.io/crates/ratatui-image
[Docs Badge]: https://img.shields.io/docsrs/ratatui-image?logo=rust&style=flat-square
[Docs]: https://docs.rs/ratatui-image/0.8.0/ratatui_image/
[Docs]: https://docs.rs/ratatui-image/latest/ratatui_image/index.html

### Showcase:

Expand All @@ -20,20 +20,23 @@
[ratatui] is an immediate-mode TUI library.
ratatui-image tackles 3 general problems when rendering images with an immediate-mode TUI:

1. **Query the terminal for available graphics protocols**.
**Query the terminal for available graphics protocols**

Some terminals may implement one or more graphics protocols, such as Sixels, or the iTerm2 or
Kitty graphics protocols. Guess by env vars. If that fails, query the terminal with some
control sequences.
Fallback to "halfblocks" which uses some unicode half-block characters with fore- and
background colors.

2. **Query the terminal for the font-size in pixels.**
**Query the terminal for the font-size in pixels.**

If there is an actual graphics protocol available, it is necessary to know the font-size to
be able to map the image pixels to character cell area. The image can be resized, fit, or
cropped to an area. Query the terminal for the window and columns/rows sizes, and derive the
font-size.

3. **Render the image by the means of the guessed protocol.**
**Render the image by the means of the guessed protocol.**

Some protocols, like Sixels, are essentially "immediate-mode", but we still need to avoid the
TUI from overwriting the image area, even with blank characters.
Other protocols, like Kitty, are essentially stateful, but at least provide a way to re-render
Expand Down Expand Up @@ -87,34 +90,34 @@ a desired columns+rows bound, and so on.

## Widget choice
* The [Image] widget does not adapt to rendering area (except not drawing at all if space
is insufficient), may be a bit more bug prone (overdrawing or artifacts), and is not friendly
with some of the protocols (e.g. the Kitty graphics protocol, which is stateful). Its big
upside is that it is _stateless_ (in terms of ratatui, i.e. immediate-mode), and thus can never
block the rendering thread/task. A lot of ratatui apps only use stateless widgets.
is insufficient), may be a bit more bug prone (overdrawing or artifacts), and is not friendly
with some of the protocols (e.g. the Kitty graphics protocol, which is stateful). Its big
upside is that it is _stateless_ (in terms of ratatui, i.e. immediate-mode), and thus can never
block the rendering thread/task. A lot of ratatui apps only use stateless widgets.
* The [StatefulImage] widget adapts to its render area, is more robust against overdraw bugs and
artifacts, and plays nicer with some of the graphics protocols.
The resizing and encoding is blocking by default, but it is possible to offload this to another
thread or async task (see `examples/async.rs`). It must be rendered with
[`render_stateful_widget`] (i.e. with some mutable state).
artifacts, and plays nicer with some of the graphics protocols.
The resizing and encoding is blocking by default, but it is possible to offload this to another
thread or async task (see `examples/async.rs`). It must be rendered with
[`render_stateful_widget`] (i.e. with some mutable state).

## Examples

* `examples/demo.rs` is a fully fledged demo.
* `examples/async.rs` shows how to offload resize and encoding to another thread, to avoid
blocking the UI thread.
blocking the UI thread.

The lib also includes a binary that renders an image file, but it is focused on testing.

## Features
* `rustix` (default) enables much better guessing of graphics protocols with `rustix::termios::tcgetattr`.
* `crossterm` or `termion` should match your ratatui backend. `termwiz` is available, but not
working correctly with ratatu-image.
working correctly with ratatu-image.
* `serde` for `#[derive]`s on [picker::ProtocolType] for convenience, because it might be
useful to save it in some user configuration.
useful to save it in some user configuration.
* `image-defaults` (default) just enables `image/defaults` (`image` has `default-features =
false`). To only support a selection of image formats and cut down dependencies, disable this
feature, add `image` to your crate, and enable its features/formats as desired. See
https://doc.rust-lang.org/cargo/reference/features.html#feature-unification.
feature, add `image` to your crate, and enable its features/formats as desired. See
https://doc.rust-lang.org/cargo/reference/features.html#feature-unification.

[ratatui]: https://github.com/ratatui-org/ratatui
[sixel]: https://en.wikipedia.org/wiki/Sixel
Expand Down
2 changes: 1 addition & 1 deletion README.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[Crate Badge]: https://img.shields.io/crates/v/ratatui-image?logo=rust&style=flat-square
[Crate]: https://crates.io/crates/ratatui-image
[Docs Badge]: https://img.shields.io/docsrs/ratatui-image?logo=rust&style=flat-square
[Docs]: https://docs.rs/ratatui-image/0.8.0/ratatui_image/
[Docs]: https://docs.rs/ratatui-image/latest/ratatui_image/index.html

### Showcase:

Expand Down
58 changes: 12 additions & 46 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# We only need the nightly overlay in the devShell because .rs files are formatted with nightly.
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; };
rust = pkgs.rust-bin.stable."1.74.0".default;
rust = pkgs.rust-bin.stable."1.81.0".default;
in
with pkgs;
{
Expand All @@ -32,6 +32,8 @@
pkg-config
cargo-tarpaulin
cargo-watch
cargo-semver-checks
cargo-release
];
};
});
Expand Down
35 changes: 19 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
//! [ratatui] is an immediate-mode TUI library.
//! ratatui-image tackles 3 general problems when rendering images with an immediate-mode TUI:
//!
//! 1. **Query the terminal for available graphics protocols**.
//! **Query the terminal for available graphics protocols**
//!
//! Some terminals may implement one or more graphics protocols, such as Sixels, or the iTerm2 or
//! Kitty graphics protocols. Guess by env vars. If that fails, query the terminal with some
//! control sequences.
//! Fallback to "halfblocks" which uses some unicode half-block characters with fore- and
//! background colors.
//!
//! 2. **Query the terminal for the font-size in pixels.**
//! **Query the terminal for the font-size in pixels.**
//!
//! If there is an actual graphics protocol available, it is necessary to know the font-size to
//! be able to map the image pixels to character cell area. The image can be resized, fit, or
//! cropped to an area. Query the terminal for the window and columns/rows sizes, and derive the
//! font-size.
//!
//! 3. **Render the image by the means of the guessed protocol.**
//! **Render the image by the means of the guessed protocol.**
//!
//! Some protocols, like Sixels, are essentially "immediate-mode", but we still need to avoid the
//! TUI from overwriting the image area, even with blank characters.
//! Other protocols, like Kitty, are essentially stateful, but at least provide a way to re-render
Expand Down Expand Up @@ -70,34 +73,34 @@
//!
//! # Widget choice
//! * The [Image] widget does not adapt to rendering area (except not drawing at all if space
//! is insufficient), may be a bit more bug prone (overdrawing or artifacts), and is not friendly
//! with some of the protocols (e.g. the Kitty graphics protocol, which is stateful). Its big
//! upside is that it is _stateless_ (in terms of ratatui, i.e. immediate-mode), and thus can never
//! block the rendering thread/task. A lot of ratatui apps only use stateless widgets.
//! is insufficient), may be a bit more bug prone (overdrawing or artifacts), and is not friendly
//! with some of the protocols (e.g. the Kitty graphics protocol, which is stateful). Its big
//! upside is that it is _stateless_ (in terms of ratatui, i.e. immediate-mode), and thus can never
//! block the rendering thread/task. A lot of ratatui apps only use stateless widgets.
//! * The [StatefulImage] widget adapts to its render area, is more robust against overdraw bugs and
//! artifacts, and plays nicer with some of the graphics protocols.
//! The resizing and encoding is blocking by default, but it is possible to offload this to another
//! thread or async task (see `examples/async.rs`). It must be rendered with
//! [`render_stateful_widget`] (i.e. with some mutable state).
//! artifacts, and plays nicer with some of the graphics protocols.
//! The resizing and encoding is blocking by default, but it is possible to offload this to another
//! thread or async task (see `examples/async.rs`). It must be rendered with
//! [`render_stateful_widget`] (i.e. with some mutable state).
//!
//! # Examples
//!
//! * `examples/demo.rs` is a fully fledged demo.
//! * `examples/async.rs` shows how to offload resize and encoding to another thread, to avoid
//! blocking the UI thread.
//! blocking the UI thread.
//!
//! The lib also includes a binary that renders an image file, but it is focused on testing.
//!
//! # Features
//! * `rustix` (default) enables much better guessing of graphics protocols with `rustix::termios::tcgetattr`.
//! * `crossterm` or `termion` should match your ratatui backend. `termwiz` is available, but not
//! working correctly with ratatu-image.
//! working correctly with ratatu-image.
//! * `serde` for `#[derive]`s on [picker::ProtocolType] for convenience, because it might be
//! useful to save it in some user configuration.
//! useful to save it in some user configuration.
//! * `image-defaults` (default) just enables `image/defaults` (`image` has `default-features =
//! false`). To only support a selection of image formats and cut down dependencies, disable this
//! feature, add `image` to your crate, and enable its features/formats as desired. See
//! https://doc.rust-lang.org/cargo/reference/features.html#feature-unification.
//! feature, add `image` to your crate, and enable its features/formats as desired. See
//! https://doc.rust-lang.org/cargo/reference/features.html#feature-unification.
//!
//! [ratatui]: https://github.com/ratatui-org/ratatui
//! [sixel]: https://en.wikipedia.org/wiki/Sixel
Expand Down
1 change: 1 addition & 0 deletions src/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ pub fn env_exists(name: &str) -> bool {
/// * wezterm
/// * foot
/// * konsole (kitty protocol)
///
/// NOTE: "tested" means that it guesses correctly, not necessarily rendering correctly.
fn query_device_attrs(is_tmux: bool) -> Result<ProtocolType> {
use rustix::termios::{LocalModes, OptionalActions};
Expand Down

0 comments on commit df4605b

Please sign in to comment.