imgcat

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2019 License: Apache-2.0 Imports: 6 Imported by: 7

README

imgcat

imgcat provides a convenient way to print images into iTerm2.

docs

Example

package main

import (
	"fmt"
	"os"

	"github.com/campoy/tools/imgcat"
	"github.com/pkg/errors"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Fprintf(os.Stderr, "usage:\n\t%s [image_path]*\n", os.Args[0])
		os.Exit(1)
	}

	enc, err := imgcat.NewEncoder(os.Stdout,
		imgcat.Inline(true),
		imgcat.Width(imgcat.Percent(100)))
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(1)
	}

	for _, path := range os.Args[1:] {
		if err := cat(enc, path); err != nil {
			fmt.Fprintf(os.Stderr, "%s\n", err)
		}
	}
}

func cat(enc *imgcat.Encoder, path string) error {
	f, err := os.Open(path)
	if err != nil {
		return errors.Wrapf(err, "could not open %s", path)
	}
	if err := enc.Encode(f); err != nil {
		return err
	}
	return f.Close()
}
Disclaimer

This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.

Documentation

Overview

Package imgcat provides a writer useful to show images directly into iterm2.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsSupported

func IsSupported() bool

IsSupported check whether imgcat works in the current terminal.

Types

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

An Encoder is used to encode images to iterm2.

func NewEncoder

func NewEncoder(w io.Writer, options ...Option) (*Encoder, error)

NewEncoder returns a encoder that encodes images for iterm2.

Example
package main

import (
	"log"
	"os"

	"github.com/campoy/tools/imgcat"
)

func main() {
	enc, err := imgcat.NewEncoder(os.Stdout, imgcat.Width(imgcat.Pixels(100)), imgcat.Inline(true), imgcat.Name("smiley.png"))
	if err != nil {
		log.Fatal(err)
	}

	f, err := os.Open("testdata/icon.png")
	if err != nil {
		log.Fatal(err)
	}
	defer func() { _ = f.Close() }()

	// Display the image in the terminal.
	if err := enc.Encode(f); err != nil {
		log.Fatal(err)
	}
}
Output:

func (*Encoder) Encode

func (enc *Encoder) Encode(r io.Reader) error

Encode encodes the given image into the output.

func (*Encoder) Writer

func (enc *Encoder) Writer() io.WriteCloser

Writer creates a writer that will encode whatever is written to it.

type Length

type Length string

Length is used by the Width and Height options.

func Auto

func Auto() Length

Auto keeps the image to its inherent size.

func Cells

func Cells(x int) Length

Cells gives a length in character cells.

func Percent

func Percent(x int) Length

Percent gives relative length to the session's width or height.

func Pixels

func Pixels(x int) Length

Pixels gives a length in pixels.

type Option

type Option string

An Option modifies how an image is displayed.

func Height

func Height(l Length) Option

Height to render, it can be in cells, pixels, percentage, or auto.

func Inline

func Inline(b bool) Option

Inline set to true causes the to be displayed inline. Otherwise, it will be downloaded with no visual representation in the terminal session. Defaults to false.

func Name

func Name(name string) Option

Name sents the filename for the image. Defaults to "Unnamed file".

func PreserveAspectRatio

func PreserveAspectRatio(b bool) Option

PreserveAspectRatio set to false causes the image's inherent aspect ratio will not be respected; otherwise, it will fill the specified width and height as much as possible without stretching. Defaults to true.

func Size

func Size(size int) Option

Size sets the file size in bytes. It's only used by the progress indicator.

func Width

func Width(l Length) Option

Width to render, it can be in cells, pixels, percentage, or auto.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL