termimg

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2023 License: MIT Imports: 8 Imported by: 0

README

PkgGoDev Go Report Card Lines of code MIT license

TermImg

termimg tries to draw images into terminals.

The rectangular drawing area is given in cell coordinates (not pixels). Origin is the upper left corner.

VERY EXPERIMENTAL!!

implemented drawing methods: sixel, iTerm2, kitty, Terminology, DomTerm, urxvt, X11, GDI+, block characters

Example CLI Tool

timg CLI Tool

demo.gif

installation:

go install github.com/srlehn/termimg/cmd/timg@master

usage:

$ timg
timg display terminal graphics

Usage:
  timg [flags]
  timg [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  list        list images
  properties  list terminal properties
  query       query terminal
  resolution  print terminal resolution
  runterm     open image in new terminal and screenshot
  scale       fit pixel area into a cell area while maintaining scale
  show        display image

Flags:
  -d, --debug    debug errors
  -h, --help     help for timg
  -s, --silent   silence errors

Use "timg [command] --help" for more information about a command.

The list command displays thumbnails of previewable files for a given directory similar to "lsix":

timg list ~/Pictures

The show command draws the image in the current terminal:

timg show -p 10,10,15x15 picture.png

Cell coordinates are optional for the show command, they are passed in this format: <x>,<y>,<w>x<h> where x is the column, y the row, w the width and h the height.

If an error occurs the --debug/-d flag shows where in the code it happens.

The runterm command starts the terminal specified with the -t flag. If no drawer is enforced by the optional -d flag, the best fitting one is used. This command is probably only useful for testing.

timg --debug=true runterm -t mlterm -d sixel -p 10,10,15x15 picture.png

Library Usage

One-Off Image Draw

import (
    _ "github.com/srlehn/termimg/drawers/all"
	_ "github.com/srlehn/termimg/terminals"
)

func main(){
    defer termimg.CleanUp()
    _ = termimg.DrawFile(`picture.png`, image.Rect(10,10,40,25))
}

with NewImage…()

For repeated image drawing create a term.Image via the NewImage…() functions. This allows caching of the encoded image.

import (
    _ "github.com/srlehn/termimg/drawers/all"
	_ "github.com/srlehn/termimg/terminals"
)

func main(){
	tm, _ := termimg.Terminal()
    defer tm.Close()
    timg := termimg.NewImageFileName(`picture.png`)
    _ = tm.Draw(timg, image.Rect(10,10,40,25))
}

Advanced

import (
    _ "github.com/srlehn/termimg/drawers/sane"
	_ "github.com/srlehn/termimg/terminals"
)

func main(){
	wm.SetImpl(wmimpl.Impl())
	opts := []term.Option{
		term.SetPTYName(`dev/pts/2`),
		term.SetTTYProvider(gotty.New, false),
		term.SetQuerier(qdefault.NewQuerier(), true),
		term.SetResizer(&rdefault.Resizer{}),
}
	tm, err := term.NewTerminal(opts...)
	if err != nil {
		log.Fatal(err)
	}
    defer tm.Close()
    var img image.Image // TODO load image
    timg := termimg.NewImage(img)
    if err := tm.Draw(timg, image.Rect(10,10,40,25)); err != nil {
		log.Fatal(err)
    }
}

The default options are packed together in termimg.DefaultConfig.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultConfig = term.Options{
		term.SetPTYName(ttyDefault),
		term.SetTTYProvider(ttyProvider, false),
		term.SetQuerier(querier, true),
		term.SetWindowProvider(windowProvider, true),
		term.SetResizer(resizer),
	}
)

Functions

func CleanUp

func CleanUp() error

CleanUp ...

func Draw

func Draw(img image.Image, bounds image.Rectangle) error

Draw ...

func DrawBytes

func DrawBytes(imgBytes []byte, bounds image.Rectangle) error

DrawBytes - for use with "embed", etc. requires the prior registration of a decoder. e.g.:

import _ "image/png"

func DrawFile

func DrawFile(imgFile string, bounds image.Rectangle) error

DrawFile ...

func NewImage

func NewImage(img image.Image) *term.Image

NewImage ...

func NewImageBytes

func NewImageBytes(imgBytes []byte) *term.Image

NewImageBytes - for use with "embed", etc. requires the prior registration of a decoder. e.g.:

import _ "image/png"

func NewImageFileName

func NewImageFileName(imgfile string) *term.Image

NewImageFileName ...

func Query

func Query(qs string, p term.Parser) (string, error)

Query ...

func Terminal

func Terminal() (*term.Terminal, error)

Terminal ...

Types

This section is empty.

Directories

Path Synopsis
cmd
termui_test command
timg command
drawers
all
x11
env
exc
xdg
query
resize
caire
Seam Carving for Content-Aware Image Resizing
Seam Carving for Content-Aware Image Resizing
rez
tty
gotty
Package gotty provides an implementation of term.TTY via github.com/mattn/go-tty.
Package gotty provides an implementation of term.TTY via github.com/mattn/go-tty.
tui
wm
framebuffer
Package framebuffer is an interface to linux framebuffer device.
Package framebuffer is an interface to linux framebuffer device.
wmimpl
actual implementation (for X11, Windows)
actual implementation (for X11, Windows)
x11

Jump to

Keyboard shortcuts

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