Skip to content
프로젝트

소개

:desktop_computer: Show high resolution images on your terminal !

디자인 및 미디어

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

TerminalImage

CI Gem Version Code Climate codecov Libraries.io dependency status for GitHub repo GitHub

TerminalImage is a library to show images on terminals. TerminalImage supports iTerm2, sixel via libsixel, and 24-bit ANSI colors (see Fallback). Support for Kitty is unreleased.

Installation

To install from RubyGems, add this line to your application's Gemfile. RubyGems version 1.1.0 does not support Kitty.

gem 'terminal_image'

To use Kitty graphics and the size: option, install from master with this Git dependency in your Gemfile and run bundle install.

gem 'terminal_image', github: 'unhappychoice/terminal_image', branch: 'master'

For sixel output, install libsixel's img2sixel command on the host running Ruby.

Usage

See the getting started guide for a first walkthrough.

Command line

The CLI is unreleased. With Git and Ruby 3.3 or later, build and install the gem from master to use the executable with --size.

git clone --branch master https://github.com/unhappychoice/terminal_image.git
cd terminal_image
gem build terminal_image.gemspec
gem install ./terminal_image-1.1.0.gem

Make sure the EXECUTABLE DIRECTORY shown by gem environment is on PATH. Then pass a local image path or an HTTP(S) URL.

terminal_image --help
terminal_image --size 40x12 images/sample.png
terminal_image https://raw.githubusercontent.com/unhappychoice/terminal_image/master/images/sample.png

Once a RubyGems release includes the CLI and --size, install it with gem install terminal_image.

To run the CLI directly from a checkout of master, run bundle install in the checkout and use:

bundle exec exe/terminal_image --help
bundle exec exe/terminal_image --size 40x12 images/sample.png
bundle exec exe/terminal_image https://raw.githubusercontent.com/unhappychoice/terminal_image/master/images/sample.png

The command writes previews to stdout and reports errors on stderr with a nonzero exit status.

Ruby

# Print image by File object
TerminalImage.show(File.open('your-image-path.png'))

# Print image from a trusted URL
TerminalImage.show_url('https://raw.githubusercontent.com/unhappychoice/terminal_image/master/images/sample.png')

# Get encoded string ready to be displayed 
string = TerminalImage.encode(File.open('your-image-path.png'))

# Get encoded string ready to be displayed from a trusted URL
string = TerminalImage.encode_url('https://raw.githubusercontent.com/unhappychoice/terminal_image/master/images/sample.png')

Pass only trusted URLs to TerminalImage.show_url and TerminalImage.encode_url. Both methods make network requests from the calling process before inspecting the image, so they can reach destinations accessible only from that host.

If your application accepts untrusted URLs, use a downloader that enforces your destination policy before sending the initial request and before following each redirect. Pass the downloaded file to TerminalImage.show or TerminalImage.encode. Validating the image response alone cannot enforce the destination policy because the request has already been sent.

When using the iTerm2 backend, encode, show, and their URL variants raise TerminalImage::InvalidImage (a subclass of TerminalImage::UnsupportedFormat) when the image width or height cannot be determined. In that case, show and show_url write nothing to stdout.

In the unreleased version, both URL methods allow 30 seconds for the entire download, including redirects. Pass download_timeout: 10 to set a different deadline in seconds. The value must be finite and positive. If the deadline expires, the methods raise Timeout::Error before rendering.

show_url and encode_url accept HTTP and HTTPS responses up to 10 MiB. Larger responses raise TerminalImage::ImageTooLarge before rendering.

Preview dimensions

Pass --size COLSxROWS on the command line or size: [columns, rows] to any of the four Ruby methods. Both limits must be positive integers. For example:

File.open('screenshot.png', 'rb') { |file| TerminalImage.show(file, size: [40, 12]) }

Sized previews fit within both limits, preserve the aspect ratio to the nearest pixel, and only scale down. ANSI uses two image pixel rows per terminal row and also respects the detected terminal width. Native graphics use pixel dimensions reported by the terminal. If that information is unavailable, a sized preview uses ANSI with the format requirements described in Fallback.

Kitty

Kitty support is unreleased. Use the Git dependency in Installation to enable it.

PNG images use the Kitty graphics protocol automatically when TERM=xterm-kitty and neither TMUX nor STY is set to a nonempty value, even when TERM_PROGRAM=iTerm.app. The PNG is sent inline without external tools and is resized in Ruby when it exceeds a requested size.

Fallback

All four methods accept a sixel: keyword. For a known destination, pass sixel: false to select ANSI or sixel: true to select sixel when img2sixel is available, with ANSI as the fallback. When omitted or set to nil, the library tries Kitty for PNG images under the conditions above, then iTerm2 when TERM_PROGRAM is iTerm.app, then sixel if img2sixel is available, then ANSI.

ANSI output uses 24-bit ("truecolor") colors. Each character cell represents two vertically adjacent pixels. When the top pixel is visible, an upper half block (▀) uses the top pixel as its foreground color and the bottom pixel as its background color if the bottom pixel is visible. When only the bottom pixel is visible, a lower half block (▄) uses the bottom pixel as its foreground color. When neither pixel is visible, the cell is a space. Pixels with alpha below 128 (on a 0–255 scale) use the terminal's default background. The image is scaled down to the terminal width (IO.console, then $COLUMNS, then 80 columns) and is never scaled up. The ANSI fallback raises TerminalImage::ImageTooLarge if the source image exceeds 50 million pixels (width × height) before resizing, even if the resized image would fit.

Supported formats:

  • PNG is decoded with the pure-Ruby chunky_png gem and needs nothing else.
  • JPEG, GIF, and WebP are converted to PNG with the ImageMagick CLI (magick, or convert for ImageMagick 6) when it is installed. Only the first frame of an animated image is shown. Without ImageMagick they raise TerminalImage::UnsupportedFormat, a subclass of TerminalImage::UnsupportedTerminal. Conversion also raises this error when the bundled security policy cannot be verified, including on Windows and when a per-user ImageMagick policy is configured. Each ImageMagick version probe has a 2-second deadline. A version probe that times out is treated as unavailable. Policy verification has the same deadline. Conversion has a 10-second deadline and raises TerminalImage::ConversionTimeout, a subclass of TerminalImage::UnsupportedFormat, if the deadline expires.
  • Other formats raise TerminalImage::UnsupportedFormat in the ANSI fallback.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/unhappychoice/terminal_image. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the TerminalImage project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Author

@unhappychoice

관련 프로젝트 살펴보기

불러오는 중…