All Projects → dlenski → Wtf

dlenski / Wtf

Licence: gpl-3.0
Whitespace Total Fixer

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Wtf

Object.reduce
Reduces an object to a value that is the accumulated result of running each property in the object through a callback. JavaScript/node.js utility.
Stars: ✭ 11 (-72.5%)
Mutual labels:  utility
Goat
POSIX-compliant shell movement boosting hack for real ninjas (aka `cd x` and `cd ...`)
Stars: ✭ 27 (-32.5%)
Mutual labels:  utility
Shifty
☀️ A macOS menu bar app that gives you more control over Night Shift.
Stars: ✭ 990 (+2375%)
Mutual labels:  utility
Sketchdiff
💎 Generate visual differences between two Sketch files or a previous git commit
Stars: ✭ 14 (-65%)
Mutual labels:  utility
Python Common Cache
This project is a cache component based on the memory and it is lightweight, simple and customizable. 🐍 😃
Stars: ✭ 21 (-47.5%)
Mutual labels:  utility
Invite Contributors
automatically invite authors of merged pull requests to your organization
Stars: ✭ 30 (-25%)
Mutual labels:  utility
Pothosblocks
A collection of core processing blocks
Stars: ✭ 7 (-82.5%)
Mutual labels:  utility
Remixbot
A multifunctional Discord bot in development that allows you to easily control your discord server.
Stars: ✭ 39 (-2.5%)
Mutual labels:  utility
Text Minimap
Generate text minimap/preview using Braille Patterns
Stars: ✭ 21 (-47.5%)
Mutual labels:  utility
Itt
Iteration tools.
Stars: ✭ 32 (-20%)
Mutual labels:  utility
Executor
Watch for file changes and then execute command. Very nice for test driven development.
Stars: ✭ 14 (-65%)
Mutual labels:  utility
Concur
Sugar for infectious JavaScript inheritance, metaprogramming & mixins
Stars: ✭ 14 (-65%)
Mutual labels:  utility
Pinku
A Pinboard-to-buku importation utility
Stars: ✭ 30 (-25%)
Mutual labels:  utility
Forgefed
An extension to ActivityPub for web-based Git services federation.
Stars: ✭ 874 (+2085%)
Mutual labels:  vcs
Chmap
The Windows `charmap` utility for Linux
Stars: ✭ 39 (-2.5%)
Mutual labels:  utility
Bonjourmadame
Say "Hello ma'am!"
Stars: ✭ 9 (-77.5%)
Mutual labels:  utility
Pivotal Flow
Automate your pivotal workflow
Stars: ✭ 29 (-27.5%)
Mutual labels:  git-hooks
Git Hooks
github_email_verify: Avoid committing to github with your corporate email
Stars: ✭ 40 (+0%)
Mutual labels:  git-hooks
Flutter mono kit
A collection of convenient widgets and utils made by mono.
Stars: ✭ 39 (-2.5%)
Mutual labels:  utility
Unidump
hexdump(1) for Unicode data
Stars: ✭ 31 (-22.5%)
Mutual labels:  utility

Whitespace Total Fixer

Identifies and/or fixes inconsistent whitespace and line endings in text files, so that they don't clog up your commits to version control systems like Git, Mercurial, or Subversion.

Build Status

Why you should use it:

  • It's like an incomprehensible shell-script one-liner (e.g. sed -e 's/LINENOISE/'), but way better
  • It's similar to git stripspace, but more flexible and detailed.
  • wtf.py is a simple Python script with no dependencies beyond the standard Python library. It should run correctly with either Python 2.7 or 3.x.

Quick Install

curl https://raw.githubusercontent.com/dlenski/wtf/master/wtf.py > ~/bin/wtf.py && chmod 0755 !#:3

How to use it

See below for options to control exactly which whitespace issues it fixes, but here are some examples:

# consistent whitespace from programs that generate text files
dump_database_schema | wtf.py -o clean_output.sql

# in-place editing
wtf.py -i file1.txt file2.txt file3.txt
wtf.py -I.bak file1.txt file2.txt file3.txt # ditto, with backups

# summarize a bunch of files without actually editing them (-0)
find . -name "*.txt" -exec wtf.py -0 {} \;

# more advanced: find interesting text files. pass options and a directory
wtf-find() {
    find "${@: -1}" -not \( -name .svn -prune -o -name .git -prune \
         -o -name .hg -prune \) -type f -exec bash -c \
         'grep -Il "" "$1" &>/dev/null && wtf.py '"${*:1:$#-1}"' "$1"' _ {} \;
}
wtf-find -0 .

# exit status
wtf.py file1.txt file2.txt file3.txt > /dev/null
if (( $? == 10 )); then
  echo "issues fixed"
elif (( $? == 20 )); then
  echo "unfixed issues!"
fi

Git pre-commit hooks

You can use Git pre-commit hooks to automatically run wtf and cleanup whitespace in your repository prior to every commit. (You can bypass the hook, however, with git commit -n.)

Create the file .git/hooks/pre-commit in your repository, and ensure that it is executable (chmod +x .git/hooks/pre-commit).

Careful version

This version only modifies your commits, and does not touch Git's working tree. If you understand and use Git's index ("staging area"), you should use this version. It will play nicely with git add --patch.

Note that when a file changes in your commit but remains unchanged in your working tree, the file will be marked modified by git status immediately after the commit.

This version will run wtf, with the default options, on all the to-be-committed text files.

Simple version

This version modifies Git's working tree as well as your commits. This version will not play nicely with git add --patch.

This version will run wtf -i, with any other options you add to wtf_options, on all the to-be-committed text files. They will be cleaned up in the commit, as well as in your working tree.

#!/bin/sh
wtf_options=''

# get a list of to-be-committed filepaths, EXCLUDING files considered
# by Git to be binary
committees=$(git diff --cached --numstat --diff-filter=ACMRTU|egrep -v ^-|cut -f3-)

# Run Whitespace Total Fixer in-place, and re-add files modified by it
for committee in $committees
do
	wtf -i $wtf_options "$committee" || git add "$committee"
done

Exciting origin story

One day at work, I spent way too much time wrangling commits laden with whitespace issues generated by recalcitrant text-editing tools on multiple platforms.

That evening, I went home and spent way too much time writing this program instead!

Whitespace issues addressed

WTF currently fixes, or simply reports, a few common types of whitespace issues. Most of these issues offer three possible command-line options enabling the user to fix, report, or ignore the issue.

  • Remove trailing spaces at the ends of lines (default is fix):

      -t, --trail-space
      -T, --report-trail-space
      -It, --ignore-trail-space
    
  • Remove blank lines at the ends of files (default is fix):

      -b, --eof-blanks
      -B, --report-eof-blanks
      -Ib, --ignore-eof-blanks
    
  • Ensure that a new-line character appears at the end of the file (default is fix):

      -n, --eof-newl
      -N, --report-eof-newl
      -In, --ignore-eof-newl
    
  • Make sure that all lines have matching EOL markers (that is, no mixing of lf/crlf). Default is to fix non-matching EOL characters by making them all the same as the first line of the file. The desired EOL markers can also be set to a specific value (lf/crlf/native), in which case all lines will unconditionally receive this marker.

      -E ENDING, --coerce-eol ENDING
      -e ENDING, --expect-eol ENDING
      -Ie, --ignore-eol
    
  • Check for spaces followed by tabs in the whitespace at the beginning of a line; fixing this condition requires setting either --change-tabs or --change-spaces. The default is to report and warn:

      -s, --tab-space-mix
      -S, --report-tab-space-mix
      -Is, --ignore-tab-space-mix
    
  • Change tabs in the whitespace at the beginning of a line to the specified number of spaces (default is not to change tabs at all). This option will not touch leading tabs when they are mixed with spaces, unless --tab-space-mix is also specified.

      -x SPACES, --change-tabs SPACES
    
  • Change the specified number of consecutive spaces in the whitespace at the beginning of a line to tabs (default is not to change spaces at all). This option will not touch leading tabs when they are mixed with spaces, unless --tab-space-mix is also specified.

      -y SPACES, --change-spaces SPACES
    

Reporting

Unless the -q/--quiet option is used, WTF will summarize each file processed in which any whitespace issues were found and/or fixed. With -v it will also report issue-free files.

$ wtf -0 nightmare.txt    # -0 is equivalent to > /dev/null
nightmare.txt LINE 8: WARNING: spaces followed by tabs in whitespace at beginning of line
nightmare.txt:
    CHOPPED 1 lines with trailing space
    CHOPPED 0 blank lines at EOF
    ADDED newline at EOF
    CHANGED 1 line endings which didn't match crlf from first line
    WARNED ABOUT 1 lines with tabs/spaces mix

WTF will return the following exit codes on successful operation:

  • 0: no issues seen (or -X/--no-exit-codes specified)
  • 10: issues fixed
  • 20: unfixed issues seen

Todo

  • Stability tests?
  • Unicode tests?

Bugs

Corrupts source code files written in the Whitespace programming language.

Anything else?

Bash completion

If you are having problems with tab completion in Bash, it is probably because of default completion rules for the unrelated utility wtf(6). You can override this by adding the following to your ~/.bashrc:

complete -o default -o bashdefault wtf

Author

© Daniel Lenski <[email protected]> (2014-2020)

License

GPL v3 or later

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].