MyNixOS website logo
Description

A pretty-printer wrapper to faciliate ease of formatting during development.

omnifmt automatically formats code via external pretty-printers. The idea was taken from gofmt, just with a bit of expansion to more languages.

omnifmt

Project Status: Wip - Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. Build Status Release omnifmt on Stackage LTS omnifmt on Stackage Nightly

A pretty-printer wrapper to faciliate ease of formatting during development. omnifmt automatically formats code via external pretty-printers. The idea was taken from gofmt, just with a bit of expansion to more languages.

Formatted code is:

  • Easier to write: never worry about minor formatting concerns while hacking away.
  • Easier to read: when all code looks the same you need not mentally convert others' formatting style into something you can understand.
  • Easier to maintain: mechanical changes to the source don't cause unrelated changes to the file's formatting; diffs show only the real changes.
  • Uncontroversial: never have a debate about spacing or brace position ever again.

(Bullet points taken from https://blog.golang.org/go-fmt-your-code.)

Installing

Installing omnifmt is easiest done using either stack (recommended) or Cabal.

Using stack:

stack install omnifmt
export PATH=$PATH:~/.local/bin

Using Cabal:

cabal-install omnifmt
export PATH=$PATH:~/.cabal/bin

Usage

The omnifmt binary provides an interface for selecting files and piping them through external pretty-printers. It supports both prettifying the files immediately and performing dry-runs to see which files are ugly.

The basics:

By default omnifmt formats on all files found from the root directory; the root directory is the first parent directory with an '.omnifmt.yaml' config file.

Passing arguments to omnifmt will override this and only operate on the given files and directories.

Modes:

omnifmt can run in three different modes, normal, dry-run and diff.

Normal mode writes to (prettifies) all ugly files immediately and outputs the prettified file paths to stdout.

Dry-run mode outputs the ugly file paths to stdout.

Diff mode outputs a diff of all ugly files with their prettified version.

Configuration

Configuration is done via an '.omnifmt.yaml' file in the root directory. The file contains a list of programs that link extensions to a prettifying command, e.g.,

haskell:
    extensions: ["hs", "lhs"]
    command:    "stylish-haskell {{input}} > {{output}}"

javascript:
    extensions: ["js"]
    command:    "js-beautify -f {{input}}"

json:
    extensions: ["json"]
    command:    "json_pp"

ruby:
    extensions: ["rb"]
    command:    "ruby-beautify"

Each command declares how to read the input file and how to write to the output file. If the input variable is omitted, the file contents are fed to the command through stdin. Likewise if the output variable is omitted, the pretty contents are read from stdout. The output file is used to compare whether the original was pretty or ugly before writing to it.

The extensions field is pretty self explanatory, but if you use the same extension more than once then precedence goes to the program defined first.

Examples

See the docs/example-configs/ directory for some common pretty-printers and their corresponding omnifmt config (pull requests are welcome for adding more). Just don't forget to actually call the config file '.omnifmt.yaml'!

NB: I haven't tested them fully, be careful in case one is buggy.

Auto-completion

Add the following (depending on your shell) to include support for auto-completion.

Bash:

source <(omnifmt --bash-completion-script `which omnifmt`)

zsh:

autoload -Uz bashcompinit && bashcompinit
source <(omnifmt --bash-completion-script `which omnifmt`)
Metadata

Version

0.2.1.1

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows