Skip to content

Commit 8f4f8d0

Browse files
committedNov 5, 2021
Remove n-selector
1 parent 1d5a56e commit 8f4f8d0

10 files changed

+1818
-2122
lines changed
 

‎index.d.ts

-6
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,6 @@ export function createImage(source: string): Image;
8989
* @return {Object} The columnify configuration.
9090
*/
9191
declare function panel(buffer_: string, delimiter_: string, width_: number): any;
92-
/**
93-
* Create an n-selector for module modes
94-
*
95-
* @type {Function}
96-
*/
97-
export const renderMode: Function;
9892
/**
9993
* Create a text wrapping instance.
10094
*

‎index.js

+17-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import columnify from 'columnify';
2-
import { createSelector } from '@thebespokepixel/n-selector';
32
import ansiRegex from 'ansi-regex';
43
import { Buffer } from 'node:buffer';
54
import { fileURLToPath } from 'node:url';
@@ -18,9 +17,9 @@ class Tokeniser {
1817
* Create a new tokeniser
1918
* @param {RegExp} tokenisingRegex - The regex that forms the word boundaries.
2019
*/
21-
constructor(tokenisingRegex) {
20+
constructor(mode, tokenisingRegex) {
2221
this.tokenisingRegex = tokenisingRegex || (function () {
23-
switch (renderMode.selected) {
22+
switch (mode) {
2423
case 'keep':
2524
return /^.*$/gm
2625
default:
@@ -77,6 +76,7 @@ class LineFitter {
7776
*/
7877
constructor(options) {
7978
[
79+
this.mode,
8080
this.margin,
8181
this.desiredWidth,
8282
this.tabWidth,
@@ -112,7 +112,7 @@ class LineFitter {
112112
return false
113113
}
114114
const overlap = this.cursor + token.trimEnd().length - this.desiredWidth;
115-
switch (renderMode.selected) {
115+
switch (this.mode) {
116116
case 'hard':
117117
if (overlap > 0) {
118118
const head = token.trimEnd().substring(0, token.length - overlap);
@@ -148,13 +148,14 @@ class LineFitter {
148148
/**
149149
* Creates a line fitter - a new line of wrapped text..
150150
* @private
151+
* @param {string} mode The wrapping mode
151152
* @param {string} margin The left margin, made up of spaces
152153
* @param {number} width The width the line can take up
153154
* @param {number} tabWidth Desired TAB width
154155
* @return {LineFitter} The line fitter.
155156
*/
156-
function createLineFitter(margin, width, tabWidth) {
157-
return new LineFitter([margin, width, tabWidth])
157+
function createLineFitter(mode, margin, width, tabWidth) {
158+
return new LineFitter([mode, margin, width, tabWidth])
158159
}
159160

160161
/**
@@ -170,15 +171,17 @@ class WrapTool {
170171
* @param {RegExp} $0.tokenRegex - An optional regex passed to the Tokeniser
171172
*/
172173
constructor({
174+
mode,
173175
left,
174176
width,
175177
tabWidth,
176178
tokenRegex,
177179
}) {
180+
this.mode = mode;
178181
this.margin = ' '.repeat(left);
179182
this.desiredWidth = width;
180183
this.tabWidth = tabWidth;
181-
this.tokeniser = createTokeniser(tokenRegex);
184+
this.tokeniser = createTokeniser(mode);
182185
}
183186
/**
184187
* Apply instance settings to source text.
@@ -188,12 +191,12 @@ class WrapTool {
188191
wrap(text) {
189192
this.lines = [];
190193
const tokens = this.tokeniser.process(text);
191-
let currentLine = createLineFitter(this.margin, this.desiredWidth, this.tabWidth);
194+
let currentLine = createLineFitter(this.mode, this.margin, this.desiredWidth, this.tabWidth);
192195
while (tokens.length > 0) {
193196
const overflow = currentLine.add(tokens.shift());
194197
if (overflow) {
195198
this.lines.push(currentLine.toString());
196-
currentLine = createLineFitter(this.margin, this.desiredWidth, this.tabWidth);
199+
currentLine = createLineFitter(this.mode, this.margin, this.desiredWidth, this.tabWidth);
197200
if (overflow !== true && overflow !== false) {
198201
tokens.unshift(overflow);
199202
}
@@ -350,17 +353,6 @@ function panel(buffer_, delimiter_, width_) {
350353
}
351354
}
352355

353-
/**
354-
* Create an n-selector for module modes
355-
*
356-
* @type {Function}
357-
*/
358-
const renderMode = createSelector([
359-
'soft',
360-
'hard',
361-
'keep',
362-
'container'
363-
], 0, 'configuration_mode');
364356
/**
365357
* Truwrap - take input from write() and composed a wrapped text block.
366358
*
@@ -407,10 +399,10 @@ class Truwrap {
407399
}
408400
return 2
409401
})();
410-
renderMode.select(mode);
411402
this.viewHandler = (() => {
412403
if (this.ttyActive && mode !== 'container') {
413404
return createWrapTool({
405+
mode: this.mode,
414406
left,
415407
width: this.viewWidth,
416408
tabWidth,
@@ -442,7 +434,7 @@ class Truwrap {
442434
switch (true) {
443435
case !this.ttyActive:
444436
return this.ttyWidth
445-
case renderMode.selected === 'container':
437+
case this.mode === 'container':
446438
return this.ttyWidth
447439
default:
448440
return this.viewWidth
@@ -460,7 +452,7 @@ class Truwrap {
460452
switch (true) {
461453
case !this.ttyActive:
462454
return columnify(content_, configuration)
463-
case renderMode.selected === 'container':
455+
case this.mode === 'container':
464456
return columnify(content_, configuration)
465457
default:
466458
return this.viewHandler.wrap(columnify(content_, configuration))
@@ -510,7 +502,7 @@ class Truwrap {
510502
switch (true) {
511503
case !this.ttyActive:
512504
return content_
513-
case renderMode.selected === 'container':
505+
case this.mode === 'container':
514506
return content_
515507
default:
516508
return this.viewHandler.wrap(content_)
@@ -540,4 +532,4 @@ function truwrap(options) {
540532
return new Truwrap(options)
541533
}
542534

543-
export { Truwrap, createImage, panel as parsePanel, renderMode, truwrap };
535+
export { Truwrap, createImage, panel as parsePanel, truwrap };

‎package-lock.json

+1,766-2,057
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
"xo": "^0.46.3"
4242
},
4343
"dependencies": {
44-
"@thebespokepixel/meta": "^3.0.4",
45-
"@thebespokepixel/n-selector": "^3.0.2",
46-
"@thebespokepixel/string": "^2.0.1",
4744
"ansi-regex": "^6.0.1",
4845
"columnify": "^1.5.4",
4946
"lodash": "^4.17.21",
@@ -135,4 +132,4 @@
135132
]
136133
]
137134
}
138-
}
135+
}

‎readme.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ writer.write("Some text to write...", "...and some more.")
5555
writer.write("A new paragraph, if not implicitly present.")
5656
writer.end() // Close the stream
5757
```
58+
59+
`mode` can be set to the following values:
60+
61+
- `soft`: words are broken before the column width, giving a left justified appearance.
62+
- `hard`: words are split at the column width.
63+
- `keep`: lines longer than the column width are kept.
64+
- `container`: left and right margins are ignored, giving a full width line.
65+
5866
As `outStream` was specified, wrapped output is written directly to the stream.
5967

6068
### Images
@@ -69,7 +77,7 @@ const image = createImage({
6977
file: join(dirname(fileURLToPath(import.meta.url)), '../media/test.png'),
7078
width: 'auto', // Number of chars wide you'd like image. 'auto' to take it from the image/set height.
7179
height: 1, // Number of lines the image will take
72-
space: ' ' // A text string that is printed under the image so you can flow the wrapped text around it.
80+
space: ' ' // A text string that is printed under the image so you can flow the wrapped text around it.
7381
})
7482

7583
var renderer = truwrap({

‎src/docs/example.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ writer.write("Some text to write...", "...and some more.")
2020
writer.write("A new paragraph, if not implicitly present.")
2121
writer.end() // Close the stream
2222
```
23+
24+
`mode` can be set to the following values:
25+
26+
- `soft`: words are broken before the column width, giving a left justified appearance.
27+
- `hard`: words are split at the column width.
28+
- `keep`: lines longer than the column width are kept.
29+
- `container`: left and right margins are ignored, giving a full width line.
30+
2331
As `outStream` was specified, wrapped output is written directly to the stream.
2432

2533
### Images
@@ -34,7 +42,7 @@ const image = createImage({
3442
file: join(dirname(fileURLToPath(import.meta.url)), '../media/test.png'),
3543
width: 'auto', // Number of chars wide you'd like image. 'auto' to take it from the image/set height.
3644
height: 1, // Number of lines the image will take
37-
space: ' ' // A text string that is printed under the image so you can flow the wrapped text around it.
45+
space: ' ' // A text string that is printed under the image so you can flow the wrapped text around it.
3846
})
3947

4048
var renderer = truwrap({

‎src/index.js

+4-18
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,10 @@
33
╰─────────┴─────────────────────────────────────────────────────────────────── */
44

55
import columnify from 'columnify'
6-
import {createSelector} from '@thebespokepixel/n-selector'
76
import createWrapTool from './lib/wrap-tool.js'
87
import createImage from './lib/image.js'
98
import parsePanel from './lib/panel.js'
109

11-
/**
12-
* Create an n-selector for module modes
13-
*
14-
* @type {Function}
15-
*/
16-
export const renderMode = createSelector([
17-
'soft',
18-
'hard',
19-
'keep',
20-
'container'
21-
], 0, 'configuration_mode')
22-
2310
/**
2411
* Truwrap - take input from write() and composed a wrapped text block.
2512
*
@@ -73,11 +60,10 @@ export class Truwrap {
7360
return 2
7461
})()
7562

76-
renderMode.select(mode)
77-
7863
this.viewHandler = (() => {
7964
if (this.ttyActive && mode !== 'container') {
8065
return createWrapTool({
66+
mode: this.mode,
8167
left,
8268
width: this.viewWidth,
8369
tabWidth,
@@ -119,7 +105,7 @@ export class Truwrap {
119105
switch (true) {
120106
case !this.ttyActive:
121107
return this.ttyWidth
122-
case renderMode.selected === 'container':
108+
case this.mode === 'container':
123109
return this.ttyWidth
124110
default:
125111
return this.viewWidth
@@ -138,7 +124,7 @@ export class Truwrap {
138124
switch (true) {
139125
case !this.ttyActive:
140126
return columnify(content_, configuration)
141-
case renderMode.selected === 'container':
127+
case this.mode === 'container':
142128
return columnify(content_, configuration)
143129
default:
144130
return this.viewHandler.wrap(columnify(content_, configuration))
@@ -198,7 +184,7 @@ export class Truwrap {
198184
switch (true) {
199185
case !this.ttyActive:
200186
return content_
201-
case renderMode.selected === 'container':
187+
case this.mode === 'container':
202188
return content_
203189
default:
204190
return this.viewHandler.wrap(content_)

‎src/lib/line-fitter.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
╰──────────────────────┴────────────────────────────────────────────────────── */
55

66
import ansiRegex from 'ansi-regex'
7-
import {renderMode} from '../index.js'
87

98
const newlineRegex = /^>\/\\\/\/__<$/
109
const tabRegex = /^>T\/\\B<$/
@@ -20,6 +19,7 @@ class LineFitter {
2019
*/
2120
constructor(options) {
2221
[
22+
this.mode,
2323
this.margin,
2424
this.desiredWidth,
2525
this.tabWidth,
@@ -62,7 +62,7 @@ class LineFitter {
6262

6363
const overlap = this.cursor + token.trimEnd().length - this.desiredWidth
6464

65-
switch (renderMode.selected) {
65+
switch (this.mode) {
6666
case 'hard':
6767
if (overlap > 0) {
6868
const head = token.trimEnd().substring(0, token.length - overlap)
@@ -104,11 +104,12 @@ class LineFitter {
104104
/**
105105
* Creates a line fitter - a new line of wrapped text..
106106
* @private
107+
* @param {string} mode The wrapping mode
107108
* @param {string} margin The left margin, made up of spaces
108109
* @param {number} width The width the line can take up
109110
* @param {number} tabWidth Desired TAB width
110111
* @return {LineFitter} The line fitter.
111112
*/
112-
export default function createLineFitter(margin, width, tabWidth) {
113-
return new LineFitter([margin, width, tabWidth])
113+
export default function createLineFitter(mode, margin, width, tabWidth) {
114+
return new LineFitter([mode, margin, width, tabWidth])
114115
}

‎src/lib/tokeniser.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
╰───────────────────┴───────────────────────────────────────────────────────── */
55

66
import ansiRegex from 'ansi-regex'
7-
import {renderMode} from '../index.js'
87

98
const tabRegex = /\t/g
109
const newlineRegex = /\n/g
@@ -18,9 +17,9 @@ class Tokeniser {
1817
* Create a new tokeniser
1918
* @param {RegExp} tokenisingRegex - The regex that forms the word boundaries.
2019
*/
21-
constructor(tokenisingRegex) {
20+
constructor(mode, tokenisingRegex) {
2221
this.tokenisingRegex = tokenisingRegex || (function () {
23-
switch (renderMode.selected) {
22+
switch (mode) {
2423
case 'keep':
2524
return /^.*$/gm
2625
default:

‎src/lib/wrap-tool.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ class WrapTool {
1818
* @param {RegExp} $0.tokenRegex - An optional regex passed to the Tokeniser
1919
*/
2020
constructor({
21+
mode,
2122
left,
2223
width,
2324
tabWidth,
2425
tokenRegex,
2526
}) {
27+
this.mode = mode
2628
this.margin = ' '.repeat(left)
2729
this.desiredWidth = width
2830
this.tabWidth = tabWidth
29-
this.tokeniser = createTokeniser(tokenRegex)
31+
this.tokeniser = createTokeniser(mode, tokenRegex)
3032
}
3133

3234
/**
@@ -38,13 +40,13 @@ class WrapTool {
3840
this.lines = []
3941
const tokens = this.tokeniser.process(text)
4042

41-
let currentLine = createLineFitter(this.margin, this.desiredWidth, this.tabWidth)
43+
let currentLine = createLineFitter(this.mode, this.margin, this.desiredWidth, this.tabWidth)
4244

4345
while (tokens.length > 0) {
4446
const overflow = currentLine.add(tokens.shift())
4547
if (overflow) {
4648
this.lines.push(currentLine.toString())
47-
currentLine = createLineFitter(this.margin, this.desiredWidth, this.tabWidth)
49+
currentLine = createLineFitter(this.mode, this.margin, this.desiredWidth, this.tabWidth)
4850
if (overflow !== true && overflow !== false) {
4951
tokens.unshift(overflow)
5052
}

0 commit comments

Comments
 (0)
Please sign in to comment.