rasterm

package module
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: MIT Imports: 15 Imported by: 9

README

rasterm

Package rasterm provides a simple way to encode images as terminal graphics, supporting Kitty, iTerm, and Sixel.

Unit Tests Go Reference Releases Discord Discussion

Fork/rewrite of rasterm.

rasterm sample output

Using

Install in the usual Go fashion:

$ go get -u github.com/kenshaw/rasterm@latest

Documentation

Overview

Package rasterm provides a simple way to encode images as terminal graphics, supporting Kitty, iTerm, and Sixel.

Example
package main

import (
	"image"
	"os"

	"github.com/kenshaw/rasterm"
	_ "image/png"
)

func main() {
	f, err := os.OpenFile("/path/to/image.png", os.O_RDONLY, 0)
	if err != nil {
		panic(err)
	}
	img, _, err := image.Decode(f)
	if err != nil {
		panic(err)
	}
	if err := rasterm.Encode(os.Stdout, img); err != nil {
		panic(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultJPEGQuality = 93

DefaultJPEGQuality is the default JPEG encode quality.

Functions

func Available added in v0.1.1

func Available() bool

Available returns true the Default encoder is available.

func Encode

func Encode(w io.Writer, img image.Image) error

Encode encodes the image to w using the Default encoder.

Types

type DefaultEncoder

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

DefaultEncoder wraps multiple terminal graphic encoders.

func NewDefaultEncoder

func NewDefaultEncoder(v ...Encoder) *DefaultEncoder

NewDefaultEncoder creates a wrapper for multiple terminal graphic encoders.

func (*DefaultEncoder) Available

func (r *DefaultEncoder) Available() bool

Available satisfies the Encoder interface.

func (*DefaultEncoder) Encode

func (r *DefaultEncoder) Encode(w io.Writer, img image.Image) error

Encode satisfies the Encoder interface.

type Encoder

type Encoder interface {
	Available() bool
	Encode(io.Writer, image.Image) error
}

Encoder provides a common interface for terminal graphic encoders.

func NewITermEncoder

func NewITermEncoder() Encoder

NewITermEncoder creates a iTerm terminal graphics encoder.

func NewKittyEncoder

func NewKittyEncoder() Encoder

NewKittyEncoder creates a Kitty terminal graphics encoder.

See: https://sw.kovidgoyal.net/kitty/graphics-protocol.html

func NewSixelEncoder

func NewSixelEncoder() Encoder

NewSixelEncoder creates a Sixel terminal graphics encoder.

type Error

type Error string

Error is an error.

const (
	// ErrTermGraphicsNotAvailable is the term graphics not available error.
	ErrTermGraphicsNotAvailable Error = "term graphics not available"
	// ErrNonTTY is the non tty error.
	ErrNonTTY Error = "non tty"
	// ErrTermResponseTimedOut is the term response timed out error.
	ErrTermResponseTimedOut Error = "term response timed out"
	// ErrUnknownTermType is the unknown term type error.
	ErrUnknownTermType Error = "unknown term type"
)

func (Error) Error

func (err Error) Error() string

Error satisfies the [error] interface.

type ITermEncoder

type ITermEncoder struct {
	NoNewline bool
}

ITermEncoder is a iTerm terminal graphics encoder.

See: https://iterm2.com/documentation-images.html

func (ITermEncoder) Available

func (ITermEncoder) Available() bool

Available satisfies the Encoder interface.

func (ITermEncoder) Encode

func (r ITermEncoder) Encode(w io.Writer, img image.Image) error

Encode satisfies the Encoder interface.

type KittyEncoder

type KittyEncoder struct {
	NoNewline bool
}

KittyEncoder is a Kitty terminal graphics encoder.

func (KittyEncoder) Available

func (KittyEncoder) Available() bool

Available satisfies the Encoder interface.

func (KittyEncoder) Encode

func (r KittyEncoder) Encode(w io.Writer, img image.Image) error

Encode satisfies the Encoder interface.

type SixelEncoder

type SixelEncoder struct {
	NoNewline bool
}

SixelEncoder is a Sixel terminal graphics encoder.

See: https://saitoha.github.io/libsixel/

func (SixelEncoder) Available

func (SixelEncoder) Available() bool

Available satisfies the Encoder interface.

func (SixelEncoder) Encode

func (r SixelEncoder) Encode(w io.Writer, img image.Image) error

Encode satisfies the Encoder interface.

type TermType

type TermType uint8

TermType is a terminal graphics type.

const (
	None TermType = iota
	Kitty
	ITerm
	Sixel
	Default TermType = ^TermType(0)
)

Terminal graphics types.

func (TermType) Available

func (typ TermType) Available() bool

Available returns true when the terminal graphics type is available.

func (TermType) Encode

func (typ TermType) Encode(w io.Writer, img image.Image) error

Encode encodes the image to w.

func (TermType) EnvValue added in v0.1.6

func (typ TermType) EnvValue() string

EnvValue returns the environment value name for the type.

func (TermType) MarshalText added in v0.1.5

func (typ TermType) MarshalText() ([]byte, error)

MarshalText satisfies the encoding.TextMarshaler interface.

func (TermType) String

func (typ TermType) String() string

String satisfies the fmt.Stringer interface.

func (*TermType) UnmarshalText added in v0.1.5

func (typ *TermType) UnmarshalText(buf []byte) error

UnmarshalText satisfies the encoding.TextUnmarshaler interface.

Jump to

Keyboard shortcuts

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