sloc
Create stats of your source code:
- physical lines
- lines of code (source)
- lines with comments
- lines with single-line comments
- lines with block comments
- lines mixed up with source and comments
- empty lines within block comments
- empty lines
- lines with TODO's
Supported outputs
In addition to the default terminal output (see examples below), sloc provides an alternative set of output formatters:
- CSV
- JSON
- Command line table
Install
To use sloc as an application install it globally:
sudo npm install -g sloc
If you're going to use it as a Node.js module within your project:
npm install --save sloc
Browser
You can also use sloc within your browser application.
Link sloc.js
in your HTML file:
<script src="lib/sloc.js"></script>
sloc is also available via bower:
bower install sloc
Note: You need to compile the coffee-script files yourself. If you want to use a pre-compiled bower package, you can run
bower install sloc-bower
Usage
CLI
sloc [option] <file>|<directory>
Options:
-h, --help output usage information
-V, --version output the version number
-e, --exclude <regex> regular expression to exclude files and folders
-i, --include <regex> regular expression to include files and folders
-f, --format <format> format output: json, csv, cli-table
--format-option [value] add formatter option
-k, --keys <keys> report only numbers of the given keys
-d, --details report stats of each analyzed file
-a, --alias <custom ext>=<standard ext> alias custom ext to act like standard ext (eg. php5=php,less=css)
e.g.:
$ sloc src/
---------- Result ------------
Physical : 1202
Source : 751
Comment : 322
Single-line comment : 299
Block comment : 23
Mixed : 116
Empty : 245
Number of files read : 10
------------------------------
or
$ sloc --details \
--format cli-table \
--keys total,source,comment \
--exclude i18n*.\.coffee \
--format-option no-head src/
βββββββββββββββββββββββββββββββββββ¬βββββββββββ¬βββββββββ¬ββββββββββ
β src/cli.coffee β 98 β 74 β 7 β
βββββββββββββββββββββββββββββββββββΌβββββββββββΌβββββββββΌββββββββββ€
β src/helpers.coffee β 26 β 20 β 0 β
βββββββββββββββββββββββββββββββββββΌβββββββββββΌβββββββββΌββββββββββ€
β src/sloc.coffee β 196 β 142 β 20 β
βββββββββββββββββββββββββββββββββββΌβββββββββββΌβββββββββΌββββββββββ€
β src/formatters/simple.coffee β 44 β 28 β 7 β
βββββββββββββββββββββββββββββββββββΌβββββββββββΌβββββββββΌββββββββββ€
β src/formatters/csv.coffee β 25 β 14 β 5 β
βββββββββββββββββββββββββββββββββββΌβββββββββββΌβββββββββΌββββββββββ€
β src/formatters/cli-table.coffee β 22 β 13 β 0 β
βββββββββββββββββββββββββββββββββββ΄βββββββββββ΄βββββββββ΄ββββββββββ
Node.js
Or use it in your own node module
var fs = require('fs');
var sloc = require('sloc');
fs.readFile("mySourceFile.coffee", "utf8", function(err, code){
if(err){ console.error(err); }
else{
var stats = sloc(code,"coffee");
for(i in sloc.keys){
var k = sloc.keys[i];
console.log(k + " : " + stats[k]);
}
}
});
Browser usage
var sourceCode = "foo();\n /* bar */\n baz();";
var stats = window.sloc(sourceCode,"javascript");
Contribute an new formatter
-
Fork this repo
-
add the new formatter into
src/formatters/
that exports a method with three arguments:- results (object)
- global options (object)
- formatter specific options (array)
-
add the formatter in
src/cli.coffee
-
open a pull request
sloc adapters
-
Grunt
-
Gulp
-
Jenkins
-
Istanbul
-
Codemetrics
Supported languages
- Assembly
- Agda
- Brightscript
- C / C++
- C#
- Clojure / ClojureScript
- CoffeeScript / IcedCoffeeScript
- Crystal
- CSS / SCSS / SASS / LESS / Stylus
- Dart
- Erlang
- F#
- Fortran
- Go
- Groovy
- Handlebars
- Haskell
- Haxe
- Hilbert
- HTML
- hy
- Jade
- Java
- JavaScript
- JSX
- Julia
- Kotlin
- LaTeX
- LilyPond
- LiveScript
- Lua
- MJS
- Mochi
- Monkey
- Mustache
- Nim
- Nix
- Objective-C / Objective-C++
- OCaml
- Perl 5
- PHP
- Python
- R
- Racket
- Ren'Py
- Ruby
- Rust
- Scala
- Squirrel
- SVG
- Swift
- Typescript
- Visual Basic
- XML
- Yaml
Run tests
npm test
Build
npm run prepublish
Changelog
see CHANGELOG.md
License
sloc is licensed under the MIT license