termimg

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2023 License: MIT Imports: 8 Imported by: 0

README

TermImg

termimg draws images into terminals. This repository includes the termimg library and the timg cli tool which can be used in shell scripts.

PkgGoDev Go Report Card Lines of code MIT license experimental

This module is still experimental. Most parts need more testing on platforms other than my own (Debian stable, X11). The API of some unfinished packages will change. There are still many small issues to be fixed. Please report them.

Images are being placed by using cell not pixel coordinates. (The origin is the upper left terminal corner just like with image.Image.) The latter doesn't make sense in the context of a terminal. termimg is able to fit images into cell boundaries while preserving their aspect ratio.

termimg implements several drawing methods: sixel, iTerm2, kitty, Terminology, DomTerm, urxvt, X11, GDI+, block characters - by default the most appropriate one will be chosen.

termimg is highly modular, most parts are exchangeable. Multiple term.TTY implementations exist for tty libraries used in common TUI frameworks.

The root package "termimg" contains an easy to use high-level API with a lot of default dependencies. Direct usage of the core package "/term" is recommended for more control and nearly no external dependencies. The "/drawers/sane" drawer collection will only use actual drawing methods, "/drawers/all" will try to produce an image by any means.

timg - tool for the CLI

demo gifs

demo_pic_ls.gif demo_vid.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
  scale       fit pixel area into a cell area while maintaining scale
  show        display image

Flags:
  -d, --debug             debug errors
  -h, --help              help for timg
  -l, --log-file string   log file
  -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 meant for testing purposes.

timg -d 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.SetLogFile(`termimg.log`, true),
        term.SetPTYName(`dev/pts/2`),
        term.SetTTYProvider(gotty.New, false),
        term.SetQuerier(qdefault.NewQuerier(), true),
        term.SetWindowProvider(wm.SetImpl(wmImplementation), 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(wm.SetImpl(wmImplementation), 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
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
video
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