├── .gitignore
├── collaborators.md
├── bin.js
├── package.json
├── readme.md
└── index.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | .DS_Store
--------------------------------------------------------------------------------
/collaborators.md:
--------------------------------------------------------------------------------
1 | ## Collaborators
2 |
3 | line-wrap is only possible due to the excellent work of the following collaborators:
4 |
5 |
8 |
--------------------------------------------------------------------------------
/bin.js:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | var fs = require('fs')
4 | var wrap = require('./')
5 | var args = require('minimist')(process.argv.slice(2))
6 |
7 | var width = args.w || args.width || 80
8 | var stdin = args._[args._.length - 1] === '-'
9 | var noArgs = args._.length === 0
10 | var firstArg = args._[0]
11 |
12 | if (noArgs) {
13 | console.error('Usage: line-wrap [-w 80] [--width=80] [-]')
14 | process.exit(1)
15 | }
16 |
17 | var wrapper = wrap({width: width})
18 |
19 | if (stdin) process.stdin.pipe(wrapper).pipe(process.stdout)
20 | else fs.createReadStream(firstArg).pipe(wrapper).pipe(process.stdout)
21 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "line-wrap",
3 | "version": "1.0.0",
4 | "description": "writable stream that reformats text and wraps lines to a specific line width (default 80).",
5 | "main": "index.js",
6 | "bin": {
7 | "line-wrap": "bin.js"
8 | },
9 | "scripts": {
10 | "test": "echo \"Error: no test specified\" && exit 1"
11 | },
12 | "author": "max ogden",
13 | "license": "BSD",
14 | "dependencies": {
15 | "minimist": "^1.1.0",
16 | "pumpify": "^1.3.3",
17 | "split2": "^0.2.1",
18 | "through2": "^0.6.3",
19 | "visualwidth": "^0.1.0"
20 | },
21 | "devDependencies": {},
22 | "repository": {
23 | "type": "git",
24 | "url": "https://github.com/maxogden/line-wrap.git"
25 | },
26 | "bugs": {
27 | "url": "https://github.com/maxogden/line-wrap/issues"
28 | },
29 | "homepage": "https://github.com/maxogden/line-wrap"
30 | }
31 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # line-wrap
2 |
3 | writable stream that reformats text and wraps lines to a specific line width (default 80).
4 |
5 | also available as a command-line tool
6 |
7 | [](https://nodei.co/npm/line-wrap/)
8 |
9 | ## installation
10 |
11 | ```js
12 | # server/client
13 | npm install --save line-wrap
14 |
15 | # CLI
16 | npm install line-wrap -g
17 | ```
18 |
19 | ## CLI usage
20 |
21 | ```bash
22 | $ line-wrap
23 | Usage: line-wrap [-w 80] [--width=80] [-]
24 |
25 | $ cat hello.txt
26 | hello world
27 |
28 | $ line-wrap hello.txt -w 2
29 | he
30 | ll
31 | o
32 | wo
33 | rl
34 | d
35 | ```
36 |
37 | ## JS usage
38 |
39 | ```js
40 | var lineWrap = require('line-wrap')
41 | var wrapStream = lineWrap({width: 80}) // opts are optional, default is width: 80
42 |
43 | process.stdin.pipe(wrapStream).pipe(process.stdout)
44 | ```
45 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | var os = require('os')
2 | var split = require('split2')
3 | var through = require('through2')
4 | var vw = require('visualwidth')
5 | var pumpify = require('pumpify')
6 |
7 | // algorithm in a nutshell:
8 | // 1. get a line from input
9 | // 2. if a line is > width then slice into head and tail
10 | // 3. write head out, store tail
11 | // 4. repeat, but add tail to line from input
12 | // 5. when done, write any remaining tail out
13 |
14 | module.exports = function(opts) {
15 | if (!opts) opts = { width: 80 }
16 | var tail = ''
17 | var pipeline = [
18 | split(),
19 | through.obj(function data(obj, enc, next) {
20 | tail = writeLines(this, obj.toString(enc), opts.width)
21 | next()
22 | }, function end(done) {
23 | var self = this
24 | writeLines(this, tail, opts.width)
25 | })
26 | ]
27 | return pumpify(pipeline)
28 | }
29 |
30 | function writeLines(out, line, width) {
31 | var divided = divideLine(line, width)
32 | divided.lines.map(function(l) {
33 | out.push(l + os.EOL)
34 | })
35 | if (divided.tail.length > 0) out.push(divided.tail + os.EOL)
36 | return divided.tail
37 | }
38 |
39 | function divideLine(line, width) {
40 | var out = {
41 | lines: [],
42 | tail: ''
43 | }
44 | var head
45 | while (line.length > 0) {
46 | head = vw.truncate(line, width, '')
47 | line = line.slice(head.length)
48 | out.lines.push(head)
49 | out.tail = line
50 | }
51 | return out
52 | }
53 |
--------------------------------------------------------------------------------