spaces

package module
v0.0.0-...-0d25adc Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2016 License: MIT Imports: 10 Imported by: 0

README

spaces

Install

Assuming you have the standard go tools installed and $GOPATH/bin is in your PATH:

$ go get github.com/vikasgorur/spaces/showspaces github.com/vikasgorur/spaces/trimspaces

trimspaces

A tool to remove trailing whitespaces recursively from source files in a directory.

Usage

Usage: ./trimspaces [-check] [-dir|-changed] [file1 ...]

Fix trailing spaces in input files (or stdin).

  -changed
    	operate only on files that have been changed (only works in git repos).
  -dir
    	operate recursively on all source files in the current directory.
  -verbose
    	run in verbose mode

Input must be provided either by the mode (dir/changed) or by a list of files on the command-line, or by standard input.

To fix all files under the current directory:

$ trimspaces -dir

To fix only changed files:

$ trimspaces -changed

To fix a given list of files:

$ trimspaces src/*.js

trimspaces is designed to be used with git repositories containing source code. It respects .gitignore and only modifies source files (files with extensions it recognizes). I use it as part of an alias to clean up whitespaces before every commit:

function commit {
    git add $(trimspaces -list-fixed -changed)
    git commit $*
}

showspaces

showspaces is a tool to highlight trailing spaces in files. It accepts the same arguments as trimspaces with one addition, a 'check' mode. In this mode no output is printed but the exit status is non-zero if any of the input files contain trailing spaces.

To highlight spaces in all changed files:

$ showspaces -changed

To check a given list of files:

$ showspaces -check src/*.js

FAQ

Why bother?

Trailing whitespaces are an annoyance. They can make pull requests and code reviews confusing because a whitespace change causes a line to be a part of the diff even though nothing else has changed. Most developers and open source projects prefer that they are removed.

For more, read Why Are Trailing Whitespaces Bad?

Why not a shell one-liner?

I could do this with a one-liner, but I'd rather have a tool that understands git and will work for repositories of any language.

Why not let the editor do this on save?

Configuring your editor to do this is a good practice. Sometimes, though, I'll use a different editor for a quick fix (vim) or I need to fix files that someone else edited.

Contributing

Please open an issue if you find a bug or contact me via email or twitter. Pull requests are welcome!

If you find the tool useful, tweet at me @vikasgorur.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsGitRoot

func IsGitRoot() bool

IsGitRoot returns true if the cwd is the root of a git repo.

func IsIgnored

func IsIgnored(path string, info os.FileInfo) bool

IsIgnored returns true if the path matches .gitignore.

func IsSourceFile

func IsSourceFile(path string, info os.FileInfo) bool

IsSourceFile returns true if the path is a source file.

func WalkChanged

func WalkChanged(fn WalkFunc) []string

WalkChanged walks only changed/added files in the git repository. returns a list of affected files.

func WalkDir

func WalkDir(fn WalkFunc) []string

WalkDir walks every file under the cwd. returns a list of affected files.

func WalkList

func WalkList(files []string, fn WalkFunc) []string

WalkList walks a given list (slice) of files. returns a list of affected files.

Types

type WalkFunc

type WalkFunc func(path string, info os.FileInfo, err error) (bool, error)

WalkFunc is a function that is called on every input file. boolean return value indicates whether the file was affected.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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