shellformat

package
v0.0.28 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package shellformat provides shell one-liner formatting for improved readability.

It parses shell commands using mvdan.cc/sh/v3/syntax (the shfmt parser) and reformats them with proper indentation and line breaks. The formatted output uses backslash continuations, making it valid shell that can be copy-pasted and executed directly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(input string, opts ...Option) (string, error)

Format parses a shell one-liner and formats it with proper indentation and line breaks for readability. The formatted output uses backslash continuations and is valid shell that can be copy-pasted and executed.

Short statements that fit within the configured max width are kept on a single line. Longer statements have their binary operators (&&, ||, |) placed at the beginning of continuation lines.

On parse error, the original input is returned unchanged with a nil error.

Types

type Option

type Option func(*config)

Option configures the formatter.

func WithIndent

func WithIndent(n int) Option

WithIndent sets the indentation width in spaces (default: 2).

func WithMaxWidth

func WithMaxWidth(n int) Option

WithMaxWidth sets the maximum line width threshold (default: 80). Statements shorter than this threshold are kept on a single line.

func WithVariant

func WithVariant(v Variant) Option

WithVariant sets the shell language variant (default: Bash).

type Variant

type Variant int

Variant represents a shell language variant.

const (
	// Bash is the default shell variant (GNU Bash).
	Bash Variant = iota
	// POSIX is the POSIX-compliant shell variant.
	POSIX
	// MkSH is the MirBSD Korn Shell variant.
	MkSH
)

Jump to

Keyboard shortcuts

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