color-path
v2.0.0
Published
Add beautiful colors for displaying paths in console
Downloads
1,473
Readme
Color-path
Color path is a little helper to allow for easy great rendering file paths in the console. It uses chalk to handle the actual coloring.
colorize
method
The colorize
method is the main method exposed by color-path
. Its prototype is: colorPath(path, options)
.
Here in an usage example:
console.log(colorPath.colorize(__dirname + '/' + __filename));
path
String or pathinfo (Object)
path
can be either a String
containing a path to a file or a pathinfo
object (see pathinfo
below). It is more performant to use a pathinfo
object so if you render multiple times the same path it might be a good idea to use this version.
options
Optional / Object
Options allow to easily tweak the output of colorize
. All options below that describe colors can take a function instead of a string. If you provide one it will be called with the pathinfo
object representing your path & the option object with the default values populated. They can return a string or another function (coming from the option object for example).
options.colors
Defaults to true
. Deactivate colorization if false
.
options.file
String or Function
Defaults to:
function(info, options) {
return info.dirname && !info.dirname.match('node_modules/') ?
options.basename :
options.basepath
;
}
This means that the color will be defined by options.basename
if the path has a directory in it that does not contain a node_modules/
part. Otherwise it will use the options.basepath
color.
options.basename
String or Function
Defaults to cyan
. Color to use for printing the basename of the path (see options.path
above to know when this color will be picked).
options.rootpath
String or Function
Defaults to grey
. Color used for the process.cwd()
part of the path if the file is in a subfolder of the current working directory.
options.basepath
String or Function
Defaults to reset
. Color used for the folders between process.cwd()
and the basename
.
pathinfo
This method parses a path and return a preprocessed pathinfo object useful to optimize the colorize method. It can also be used to extract infos on the path easily. Looks like:
colorPath.pathinfo('/cwd/complete/path');
// returns
{
// this is a copy/paste of the path argument of pathinfo
path: '/cwd/complete/path',
// dirname of the path, '/' terminated ('' if no dirname)
dirname: '/cwd/complete/',
// rootpath === cwd if file is in cwd ('/' terminated)
rootpath: '/cwd/',
// basepath = dirname - cwd
basepath: 'complete/',
// basename = basename of the file
basename: 'path'
}
LICENSE
This package is licensed under the terms of the MIT license.