line-info
v0.2.0
Published
Gather information about a line based on content and cursor position
Downloads
713
Readme
line-info
Gather information about a line based on content and cursor position.
This was built as part of commander-completion, a shell completion library for commander.js.
Getting Started
Install the module with: npm install line-info
var lineInfo = require('line-info');
lineInfo({
// git checkout|world
line: 'git checkoutworld',
cursor: 12
});
/* Result is
{
line: {
value: 'git checkoutworld',
index: 12, // 'git checkout'.length
partialLeft: 'git checkout',
partialRight: 'world'
},
words: {
value: ['git', 'checkoutworld'],
index: 1, // Position of 'checkoutworld'
partialLeft: ['git', 'checkout'],
partialRight: ['world']
},
word: {
value: 'checkoutworld',
index: 8, // 'checkout'.length
partialLeft: 'checkout',
partialRight: 'world'
}
}
*/
// Accessed via `result.word.partialLeft`; // 'checkout'
Documentation
line-info
exposes a single function via its module.exports
lineInfo(params)
Collect info about a line given a set of shell completion parameters.
Input
- params
Object
- Container for additional parameters- line
String
- Input to analyze (e.g.npm publish
) - cursor
Number
- Position of cursor within string (e.g.ec|ho hai
has acursor
of 2)
- line
Returned
- retObj
Object
- Container for information- line
Object
- Container forparams.line
information- value
String
- Original input fromparams.line
- index
Number
- Original cursor position fromparams.cursor
- partialLeft
String
- Left section of line before cursor (e.g.'npm pub'
innpm pub|lish
) - partialRight
String
- Right section of line after cursor (e.g.'lish'
innpm pub|lish
)
- value
- words
Object
- Information about words inline
- value
String[]
- Array of words in theparams.line
- index
Number
- Index of word containing cursor - partialLeft
String[]
- Array of words fromretObj.line.partialLeft
(e.g.['npm', 'pub']
innpm pub|lish
)- Word containing
cursor
will be broken up here
- Word containing
- partialRight
String[]
- Array of words fromretObj.line.partialRight
(e.g.['lish']
innpm pub|lish
)- Word containing
cursor
will be broken up here
- Word containing
- value
- word
Object
- Information about word containingcursor
- value
String
- Word containingcursor
- index
Number
- Position ofcursor
within word - partialLeft
String
- Left section of word before cursor (e.g.'pub'
innpm pub|lish
) - partialRight
String
- Right section of word after cursor (e.g.'lish'
innpm pub|lish
)
- value
- line
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via grunt and test via npm test
.
Donating
Support this project and others by twolfson via gittip.
Unlicense
As of Dec 17 2013, Todd Wolfson has released this repository and its contents to the public domain.
It has been released under the UNLICENSE.