command

package
v1.14.4 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package command provides interfaces for the shell commands and programs on the host.

Index

Constants

View Source
const (
	HWZip     = "hwzip"     // Hwzip the zip decompression command for files using obsolete methods.
	Arc       = "arc"       // Arc is the arc decompression command.
	Arj       = "arj"       // Arj is the arj decompression command.
	BSDTar    = "bsdtar"    // BSDTar is the tar decompression command.
	Cab       = "gcab"      // Cab is the gcab decompression command for Microsoft Cabinet.
	Lha       = "lha"       // Lha is the lha/lzh decompression command.
	Lsar      = "lsar"      // Lsar is The Unarchive list command usable on multiple types.
	Unar      = "unar"      // Unar is The Unarchiver decompression command usable on multiple types.
	Unrar     = "unrar"     // Unrar is the rar decompression command.
	Unzip     = "unzip"     // Unzip is the zip decompression command.
	Zip7      = "7zz"       // Zip7 is the 7-Zip decompression command.
	Ansilove  = "ansilove"  // Ansilove is the ansilove text to image command.
	Cwebp     = "cwebp"     // Cwebp is the Google create webp command.
	Gif2webp  = "gif2webp"  // Gif2webp is the Google gif to webp command.
	Ilbmtoppm = "ilbmtoppm" // ilbmtoppm belongs to Netpdm for ILBM image conversion.
	Magick    = "magick"    // Magick is the ImageMagick v7+ command.
	Optipng   = "optipng"   // Optipng is the PNG optimizer command.
	Pamscale  = "pamscale"  // Pamscale is used by Netpdm for ILBM image conversion.
)
View Source
const (
	ANSICap = 350_000 // ANSICap is the maximum file size in bytes for an ANSI encoded text file.
)
View Source
const (
	CmdTimeout = 10 * time.Second
)

Variables

View Source
var (
	ErrAlign      = errors.New("command: invalid align choice")
	ErrCrop       = errors.New("command: invalid crop choice")
	ErrThumb      = errors.New("command: invalid thumb choice")
	ErrIsAnsi     = errors.New("command: text is ansi encoded, cannot crop")
	ErrIsEmpty    = errors.New("command: file is empty")
	ErrIsFile     = errors.New("command: directory path points to a file")
	ErrNoImages   = errors.New("command: no images found")
	ErrNoMatch    = errors.New("command: no match value is present")
	ErrNoSupport  = errors.New("command: file format has no support")
	ErrUnknownImg = errors.New("command: file is not a known image format")
	ErrValue      = errors.New("command: argument is empty")
	ErrVersion    = errors.New("command: application version mismatch")
)
View Source
var Infos = [...]string{
	"HWZip ver 2+",
	"archive utility ver 5+",
	"arj32 ver 3+",
	"BSDTar 3+",
	"gcab ver 1.6+",
	"Lhasa command line LHA tool",
	"The Unarchiver v1.10+",
	"The Unarchiver v1.10+",
	"UNRAR freeware (c) Alexander Roshal",
	"UnZip Info-ZIP ver 6+",
	"7-Zip ver 24+",
	"ansilove/c ver 4+",
	"Google WebP ver 1+",
	"Google GIF to WebP ver 1+",
	"ILBM image Netpdm converter",
	"ImageMagick ver 7+",
	"OptiPNG optimizer ver 0.7+",
	"Netpdm image scaler",
}

Infos returns details for the list of the execute command names used by the application.

Lookups returns a list of the execute command names used by the application.

Functions

func CopyFile

func CopyFile(sl *slog.Logger, src, dst string) error

CopyFile copies the src file to the dst file and path.

func ImagesDelete added in v0.10.0

func ImagesDelete(unid string, dirs ...string) error

ImagesDelete removes images matching the unid from the specified directories. The unid is the unique identifier shared across preview and thumbnail variants. Returns ErrNoImages if no matching files were found and removed.

func ImagesPixelate added in v0.10.0

func ImagesPixelate(ctx context.Context, sl *slog.Logger, unid string, dirs ...string) error

ImagesPixelate converts images matching unid across the specified directories into pixelated versions in-place.

func Lookup added in v1.14.0

func Lookup(file string) (string, error)

Lookup returns an error if the command is not found in the system path.

func LookupS added in v1.14.0

func LookupS(ctx context.Context, name, flag, match string) error

LookupS returns an error when the match string is not found in the named command output.

func LookupUnrar

func LookupUnrar(ctx context.Context) error

LookupUnrar returns an error if the name Alexander Roshal is not found in the unrar version output.

func OptimizePNG

func OptimizePNG(ctx context.Context, sl *slog.Logger, src string) error

OptimizePNG optimizes the src PNG image in-place using optipng. It is safe to call within a deferred function.

func Run

func Run(ctx context.Context, sl *slog.Logger, name string, arg ...string) ([]byte, error)

Run looks for the command in the system path and executes it with the arguments. The name of the command to run must be provided, and any options provided using arg.

Types

type Align added in v0.10.0

type Align int

Align is args type that represents the alignment of the thumbnail image.

const (
	Top    Align = iota // Top uses the top alignment of the preview image
	Middle              // Middle uses the center alignment of the preview image
	Bottom              // Bottom uses the bottom alignment of the preview image
	Left                // Left uses the left alignment of the preview image
	Right               // Right uses the right alignment of the preview image
)

func (Align) String added in v1.14.0

func (align Align) String() string

func (Align) Thumbs added in v0.10.0

func (align Align) Thumbs(ctx context.Context, sl *slog.Logger, unid string, preview, thumbnail dir.Directory) error

Thumbs generates a cropped thumbnail from the source preview image using the specified alignment.

type Crop added in v0.10.0

type Crop int

Crop is an args type that represents the crop position of the preview image.

const (
	SquareTop Crop = iota // SquareTop crops the top of the image using args 1:1 ratio
	FourThree             // FourThree crops the top of the image using args 4:3 ratio
	OneTwo                // OneTwo crops the top of the image using args 1:2 ratio
)

func (Crop) Images added in v0.10.0

func (crop Crop) Images(ctx context.Context, sl *slog.Logger, unid string, preview dir.Directory) error

Images crops the preview image based on the crop position and ratio of the image.

func (Crop) String added in v1.14.0

func (crop Crop) String() string

type Dirs

type Dirs struct {
	Download  dir.Directory // Download is the directory path for the file downloads.
	Preview   dir.Directory // Preview is the directory path for the image previews.
	Thumbnail dir.Directory // Thumbnail is the directory path for the image thumbnails.
	Extra     dir.Directory // Extra is the directory path for the extra files.
}

Dirs points to the download, preview, thumbnail, and extra directories.

func (Dirs) BinTextImager added in v1.9.15

func (ds Dirs) BinTextImager(ctx context.Context, sl *slog.Logger, srcBinary, unid string) error

BinTextImager converts a binary text source file into a PNG preview using Ansilove, then processes the generated PNG through the standard text imager pipeline.

func (Dirs) DizDeferred added in v1.5.1

func (ds Dirs) DizDeferred(sl *slog.Logger, srcDIZ, unid string) error

DizDeferred copies a FILE_ID.DIZ text file into the extra directory.

func (Dirs) PictureImager added in v0.10.0

func (ds Dirs) PictureImager(
	ctx context.Context, sl *slog.Logger, srcImage, unid string,
) error

PictureImager creates both a preview image and a thumbnail image using the srcImage.

Thumbnails are generated as .webp or .png files, while the preview image format depends on the input format (.png, .jpeg, .avif, .webp, etc.).

func (Dirs) TextDeferred added in v1.2.2

func (ds Dirs) TextDeferred(ctx context.Context, sl *slog.Logger, srcText, unid string) error

TextDeferred creates a thumbnail (if one does not exist) and copies the source text file into the extra directory.

func (Dirs) TextImager added in v0.10.0

func (ds Dirs) TextImager(ctx context.Context, sl *slog.Logger, src, unid string, amigaFont bool) error

TextImager generates two images based on the text file provided by the src path. The provided unid must be a valid universal unique identifier.

Note that TextImager uses ansilove which doesn't support modern Unicode, this includes the common UTF-8 encoding or multibyte characters like emojis.

If the amigaFont is set to true, a Commodore Amiga Tapaz font is used to represent the text, otherwise an IBM VGA font is used. The amigaFont also displays less rows of text due to the Topaz font being taller.

func (Dirs) Thumbs added in v0.10.0

func (ds Dirs) Thumbs(ctx context.Context, sl *slog.Logger, unid string, generate Generate) error

Thumbs creates a thumbnail image from the corresponding preview image based on the thumb type.

type Generate added in v1.14.0

type Generate int

Generate is the type of thumbnail image to create.

const (
	Pixel Generate = iota // Pixel art or images with text
	Photo                 // Photographs or images with gradients
)

type Runner added in v1.14.0

type Runner struct {
	Timeout    time.Duration // Command timeout value, or leave empty to use 10 seconds
	Log        *slog.Logger  // Logger output or leave empty to use default
	WorkingDir string        // Working directory to execute the command or leave empty
}

func (*Runner) Run added in v1.14.0

func (r *Runner) Run(ctx context.Context, name string, arg ...string) ([]byte, error)

Run looks for the command in the system path and executes it with the arguments. The name of the command to run must be provided, and any options provided using arg.

type Text added in v1.14.0

type Text struct {
	UUID    string // unique universal ID used as the base filename for the output
	MaxRows int    // maximum number of rows to crop
	MaxCols int    // maximum number of to crop, or leave as 0 to use the default of 80
	UTF8    bool   // when true, MaxCols will crop  to Unicode rune count instead of ASCII characters
}

func (Text) Crop added in v1.14.0

func (t Text) Crop(sl *slog.Logger, src string) (string, error)

Crop reads the src text file and writes the number of MaxRows (lines) of text to a new file. The new file is stored in the same directory as the src but is given file name of UUID appended with a ".txt" extension. The text is truncated to MaxCols (runes or characters per line), but any leading empty lines are ignored.

If MaxRows is 0, a default value of 29 is used. If MaxCols is set to 0, a default of 80 is used. Both defaults allow for a 400x400 pixel thumbnail to be generated.

  • When successful the full path to the new file with the cropped text is returned.
  • If unsuccessful the src path is returned.
  • If a large number of ANSI sequences are detected then an ErrIsAnsi error is returned.

type Thumb added in v0.10.0

type Thumb struct {
	Source  string // Source image file
	UUID    string // unique universal ID used as the base filename for the output
	Pattern string // filename pattern for the temporary directory
	JPEG    bool   // make JPEG thumbnails instead of the default PNG format
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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