Skip to content

Commit

Permalink
sixel-bytes, optional serde, Send+Sync, rustix::termios icotl, fixes
Browse files Browse the repository at this point in the history
* `sixel-bytes` encodes to `String` instead of file.
* `serde` for `BackendType` for e.g. user configs.
* `Send + Sync` for threads/tokio.
* `rustix::termios` to get window/font size until ratatui/crossterm get `window_size()`:
  [`ratatui::Backend::window_size()`](ratatui-org/ratatui#276) needs [`crossterm::terminal::window_size()`](crossterm-rs/crossterm#790).
  • Loading branch information
benjajaja committed Jul 22, 2023
1 parent 1d2ed16 commit c1a3e16
Show file tree
Hide file tree
Showing 14 changed files with 467 additions and 343 deletions.
89 changes: 72 additions & 17 deletions Cargo.lock

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

16 changes: 11 additions & 5 deletions Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "ratatu-image"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
autoexamples = true
authors = ["Benjamin Große <ste3ls@gmail.com>"]
Expand All @@ -16,22 +16,28 @@ exclude = [
rust-version = "1.65.0"

[features]
default = ["crossterm", "sixel"]
default = ["crossterm", "sixel", "rustix"]
crossterm = ["dep:crossterm"]
termion = ["dep:termion"]
termwiz = ["dep:termwiz"]
sixel = ["dep:sixel-rs"]
sixel = ["dep:sixel-bytes"]
serde = ["dep:serde"]
rustix = ["dep:rustix"]

[dependencies]
dyn-clone = "1.0.11"
image = { version = "0.24.5" }
# ratatui with "cell skipping" and "window_size", https://github.com/tui-rs-revival/ratatui/pull/215 and https://github.com/tui-rs-revival/ratatui/pull/276
ratatui = { git = "https://github.com/benjajaja/ratatui", rev = "8729b61fab885dc141c6e0968b4863386c544d9d", features = ["crossterm", "termion", "termwiz" ] }
sixel-rs = { version = "0.3.3", optional = true }
# sixel-rs = { version = "0.3.3", optional = true }
sixel-bytes = { version = "0.2.1", optional = true }
# crossterm with "window_size", https://github.com/crossterm-rs/crossterm/pull/790
crossterm = { git = "https://github.com/benjajaja/crossterm", rev = "db515a16f95b36b4871488aa543f564bc929d62e" , optional = true }
# crossterm = { git = "https://github.com/benjajaja/crossterm", rev = "db515a16f95b36b4871488aa543f564bc929d62e" , optional = true }
crossterm = { version = "0.25", optional = true }
termion = { version = "2.0", optional = true }
termwiz = { version = "0.20", optional = true }
serde = { version = "^1.0", optional = true }
rustix = { version = "^0.38.4", optional = true, features = ["stdio", "termios"] }

[[example]]
name = "demo"
Expand Down
27 changes: 20 additions & 7 deletions README.md
@@ -1,12 +1,18 @@
# Ratatu-image
# ratatu-image

![Screenshot](./assets/Screenshot.png)

Image widgets for [Ratatui]

[Ratatui]: https://github.com/tui-rs-revival/ratatui

**THIS CRATE IS EXPERIMENTAL!**

Render images with supported graphics protocols in the terminal with ratatui.
While this generally might seem *contra natura* and something fragile, it can be worthwhile in
some applications.

![Screenshot](./assets/screenshot.png)
## Implementation

The images are always resized so that they fit their nearest rectangle in columns/rows.
The reason for this is because the image shall be drawn in the same "render pass" as all
Expand All @@ -15,6 +21,8 @@ level, so there is no way to "clear" previous drawn text. This would leave artif
image border.
For this resizing it is necessary to query the terminal font size in width/height.

## Widgets

The [`FixedImage`] widget does not react to area resizes other than not overdrawing. Note that
some image protocols or their implementations might not behave correctly in this aspect and
overdraw or flicker outside of the image area.
Expand All @@ -26,26 +34,28 @@ this widget may have a much bigger performance impact.

Each widget is backed by a "backend" implementation of a given image protocol.

## Backends

Currently supported backends/protocols:

## Halfblocks
### Halfblocks
Uses the unicode character `` combined with foreground and background color. Assumes that the
font aspect ratio is roughly 1:2. Should work in all terminals.
## Sixel
### Sixel
Experimental: uses temporary files.
Uses [`sixel-rs`] to draw image pixels, if the terminal [supports] the [Sixel] protocol.

[`sixel-rs`]: https://github.com/orhun/sixel-rs
[supports]: https://arewesixelyet.com
[Sixel]: https://en.wikipedia.org/wiki/Sixel

# Examples
## Examples

For a more streamlined experience, see the [`crate::picker::Picker`] helper.

```rust
use image::{DynamicImage, ImageBuffer, Rgb};
use ratatui_imagine::{
use ratatu_image::{
backend::{
FixedBackend,
halfblocks::FixedHalfblocks,
Expand All @@ -55,7 +65,7 @@ use ratatui_imagine::{

let image: DynamicImage = ImageBuffer::<Rgb<u8>, Vec<u8>>::new(300, 200).into();

let source = ImageSource::new(image, (7, 14), None);
let source = ImageSource::new(image, "filename.png".into(), (7, 14), None);

let static_image = Box::new(FixedHalfblocks::from_source(
&source,
Expand All @@ -68,3 +78,6 @@ assert_eq!(15, static_image.rect().height);
let image_widget = FixedImage::new(&static_image);
```

Current version: 0.1.1

License: MIT
10 changes: 10 additions & 0 deletions README.tpl
@@ -0,0 +1,10 @@
# {{crate}}

![Screenshot](./assets/Screenshot.png)

{{readme}}

Current version: {{version}}

License: {{license}}

Binary file added assets/Jenkins.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes

0 comments on commit c1a3e16

Please sign in to comment.