util

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const StdinIndicator = "-"

StdinIndicator is the conventional indicator for stdin input.

Variables

View Source
var (
	ErrFileNotFound     = errors.New("file not found")
	ErrNotPDF           = errors.New("not a valid PDF file")
	ErrInvalidPages     = errors.New("invalid page specification")
	ErrPasswordRequired = errors.New("password required")
	ErrWrongPassword    = errors.New("incorrect password")
	ErrCorruptPDF       = errors.New("PDF file is corrupted")
	ErrOutputExists     = errors.New("output file already exists")
)

Common error types

View Source
var ProgressBarTheme = progressbar.Theme{
	Saucer:        "=",
	SaucerHead:    ">",
	SaucerPadding: " ",
	BarStart:      "[",
	BarEnd:        "]",
}

ProgressBarTheme is the default theme for progress bars.

View Source
var SupportedImageExtensions = []string{".png", ".jpg", ".jpeg", ".tif", ".tiff"}

SupportedImageExtensions contains all supported image file extensions.

Functions

func AtomicWrite

func AtomicWrite(path string, data []byte) error

AtomicWrite writes data to a file atomically by writing to a temp file first

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies a file from src to dst

func EnsureDir

func EnsureDir(path string) error

EnsureDir creates a directory if it doesn't exist

func EnsureParentDir

func EnsureParentDir(path string) error

EnsureParentDir creates the parent directory of a file path if it doesn't exist

func ExpandPageRanges

func ExpandPageRanges(ranges []PageRange) []int

ExpandPageRanges expands a slice of PageRange to individual page numbers

func FileExists

func FileExists(path string) bool

FileExists checks if a file exists

func FinishProgressBar added in v1.4.0

func FinishProgressBar(bar *progressbar.ProgressBar)

FinishProgressBar prints a newline after the progress bar if it exists.

func FormatError

func FormatError(err error) string

FormatError formats an error for display to the user

func FormatFileSize

func FormatFileSize(bytes int64) string

FormatFileSize formats a file size in bytes to a human-readable string

func FormatPageRanges

func FormatPageRanges(pages []int) string

FormatPageRanges converts a slice of page numbers back to a compact range string

func GenerateOutputFilename

func GenerateOutputFilename(input, suffix string) string

GenerateOutputFilename generates an output filename based on the input and a suffix

func GetFileSize

func GetFileSize(path string) (int64, error)

GetFileSize returns the size of a file in bytes

func IsDir

func IsDir(path string) bool

IsDir checks if a path is a directory

func IsFileNotFound

func IsFileNotFound(err error) bool

IsFileNotFound checks if an error is a file not found error

func IsImageFile added in v1.4.0

func IsImageFile(path string) bool

IsImageFile checks if a file has a supported image extension.

func IsPasswordRequired

func IsPasswordRequired(err error) bool

IsPasswordRequired checks if an error indicates a password is required

func IsStdinInput added in v1.4.0

func IsStdinInput(path string) bool

IsStdinInput returns true if the input path indicates stdin.

func IsStdinPiped added in v1.4.0

func IsStdinPiped() bool

IsStdinPiped returns true if stdin has data piped to it (not a terminal).

func NewBytesProgressBar added in v1.4.0

func NewBytesProgressBar(description string, total int64) *progressbar.ProgressBar

NewBytesProgressBar creates a progress bar for byte-based progress (e.g., downloads).

func NewProgressBar added in v1.4.0

func NewProgressBar(description string, total, threshold int) *progressbar.ProgressBar

NewProgressBar creates a consistent progress bar with the given description and total count. Returns nil if total is at or below the threshold for showing progress.

func ParseAndExpandPages

func ParseAndExpandPages(input string) ([]int, error)

ParseAndExpandPages parses a page range string and returns individual page numbers

func ParseReorderSequence added in v1.3.1

func ParseReorderSequence(spec string, totalPages int) ([]int, error)

ParseReorderSequence parses a reorder sequence string and returns the page numbers in order. Supports individual pages (1,3,5), ranges (1-10), special values (end), reverse ranges (10-1), and page duplication (1,2,3,1 repeats page 1 at the end).

func ReadFromStdin added in v1.4.0

func ReadFromStdin() (path string, cleanup func(), err error)

ReadFromStdin reads stdin to a temporary file and returns the path and cleanup function. The caller is responsible for calling the cleanup function when done.

func ResolveInputPath added in v1.4.0

func ResolveInputPath(input string) (path string, cleanup func(), err error)

ResolveInputPath resolves an input path, handling stdin indicator. Returns the actual path to use and a cleanup function. If the input is not stdin, cleanup is a no-op.

func ValidatePDFFile

func ValidatePDFFile(path string) error

ValidatePDFFile checks if a file exists and has a .pdf extension

func ValidatePDFFiles

func ValidatePDFFiles(paths []string) error

ValidatePDFFiles validates multiple PDF files in parallel

func ValidatePageNumbers

func ValidatePageNumbers(pages []int, totalPages int) error

ValidatePageNumbers checks if all page numbers are within the valid range

func WrapError

func WrapError(operation string, file string, err error) error

WrapError wraps an error with additional context

func WriteToStdout added in v1.4.0

func WriteToStdout(path string) error

WriteToStdout writes a file's contents to stdout.

Types

type OutputFormat added in v1.4.0

type OutputFormat string

OutputFormat represents the output format type.

const (
	FormatHuman OutputFormat = ""
	FormatJSON  OutputFormat = "json"
	FormatCSV   OutputFormat = "csv"
	FormatTSV   OutputFormat = "tsv"
)

func ParseOutputFormat added in v1.4.0

func ParseOutputFormat(s string) OutputFormat

ParseOutputFormat parses a string into an OutputFormat.

type OutputFormatter added in v1.4.0

type OutputFormatter struct {
	Format OutputFormat
	Writer io.Writer
}

OutputFormatter handles formatted output in various formats.

func NewOutputFormatter added in v1.4.0

func NewOutputFormatter(format string) *OutputFormatter

NewOutputFormatter creates a new OutputFormatter with the given format.

func (*OutputFormatter) IsStructured added in v1.4.0

func (f *OutputFormatter) IsStructured() bool

IsStructured returns true if the format is a structured format (JSON, CSV, TSV).

func (*OutputFormatter) Print added in v1.4.0

func (f *OutputFormatter) Print(data interface{}) error

Print outputs data in the configured format. For JSON, data is marshaled directly. For other formats, data should be a struct or map.

func (*OutputFormatter) PrintTable added in v1.4.0

func (f *OutputFormatter) PrintTable(headers []string, rows [][]string) error

PrintTable outputs tabular data in the configured format.

type PDFError

type PDFError struct {
	Operation string
	File      string
	Cause     error
	Hint      string
}

PDFError represents a user-friendly error for PDF operations

func NewPDFError

func NewPDFError(operation, file string, cause error) *PDFError

NewPDFError creates a new PDFError

func (*PDFError) Error

func (e *PDFError) Error() string

func (*PDFError) Unwrap

func (e *PDFError) Unwrap() error

func (*PDFError) WithHint

func (e *PDFError) WithHint(hint string) *PDFError

WithHint adds a hint to a PDFError

type PageRange

type PageRange struct {
	Start int
	End   int
}

PageRange represents a range of pages

func ParsePageRanges

func ParsePageRanges(input string) ([]PageRange, error)

ParsePageRanges parses a page range string like "1-5,7,10-12" Returns a slice of PageRange structs

Jump to

Keyboard shortcuts

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