-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
220 lines (192 loc) · 5.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* ────────╮
│ truwrap │ Smarter 24bit SGR aware console text wrapping
╰─────────┴─────────────────────────────────────────────────────────────────── */
import columnify from 'columnify'
import osLocale from 'os-locale'
import {createConsole} from 'verbosity'
import {stripIndent} from 'common-tags'
import meta from '@thebespokepixel/meta'
import {createSelector} from '@thebespokepixel/n-selector'
import createWrapTool from './lib/classes/wrap-tool'
import {colorReplacer} from './lib/colour'
import createImage from './lib/classes/image'
import parsePanel from './lib/classes/panel'
export const console = createConsole({outStream: process.stderr})
export const locale = osLocale.sync()
export const metadata = meta(__dirname)
export const renderMode = createSelector([
'soft',
'hard',
'keep',
'container'
], 0, 'configuration_mode')
/**
* Throw a error if a method remains unimplemented
* @private
* @return {undefined}
*/
function unimplemented() {
throw new Error('Unimplemented.')
}
/**
* Create a text wrapping instance.
*
* @param {Object} options options object
* @param {Number} options.left Left margin.
* @param {Number} options.right Right margin.
* @param {Number} options.width Manually set view width.
* @param {mode} options.mode [soft | hyphen | hard | keep | container]
* @param {Number} options.tabWidth Desired width of TAB character.
* @param {Stream.writable} options.outStream Where to direct output.
* @param {Regexp} options.tokenRegex Override the tokenisers regexp.
* @return {api} A truwrap api instance.
*/
export function truwrap({
left = 2,
right = 2,
width,
mode = 'soft',
tabWidth = 4,
outStream = process.stdout,
tokenRegex
}) {
const ttyActive = Boolean(width || outStream.isTTY || /keep|container/.test(mode))
const ttyWidth = (function () {
if (width) {
return width
}
if (outStream.isTTY) {
return outStream.columns || outStream.getWindowSize()[0]
}
return Infinity
})()
const viewWidth = (function () {
if (ttyWidth - left - right > 1) {
return ttyWidth - left - right
}
return 2
})()
renderMode.select(mode)
const viewHandler = (function () {
if (ttyActive && mode !== 'container') {
return createWrapTool({
left,
width: viewWidth,
tabWidth,
tokenRegex
})
}
return {}
})()
/**
* Truwap pulic API
* @public
*/
const api = {
/**
* End a block, setting blocking mode and flushing buffers if needed.
* @function
* @return {undefined} has side effect of writing to stream
*/
end() {
if (outStream._isStdio) {
outStream.write('\n')
} else {
outStream.end()
}
},
/**
* Fetch the width in characters of the wrapping view.
* @function
* @return {Number} wrapping width
*/
getWidth: unimplemented,
/**
* Create a multicolumn panel within this view
* @function
* @param {panelObject} content - Object for columnify
* @param {Object} configuration - Configuration for columnify
* @return {String} - The rendered panel.
*/
panel(content, configuration) {
if (outStream._isStdio) {
outStream.write(columnify(content, configuration))
}
return this
},
/**
* Generate linebreaks within this view
* @function
* @param {Number} newlines - number of new lines to add.
* @return {api} has side effect of writing to stream.
*/
break(newlines = 1) {
outStream.write('\n'.repeat(newlines))
return this
},
/**
* Similar to css' clear. Write a clearing newline to the stream.
* @function
* @return {api} has side effect of writing to stream.
*/
clear() {
outStream.write('\n')
return this
},
/**
* Write text via the wrapping logic
* @function
* @param {String} text - The raw, unwrapped test to wrap.
* @return {api} has side effect of writing to stream.
*/
write(text) {
outStream.write(text)
return this
}
}
switch (true) {
case !ttyActive:
console.info(colorReplacer`${'yellow|Non-TTY'}: width: Infinity`)
/**
* @name noTTY
* @private
* @returns {api} - A version of the API when no TTY is connected.
*/
return Object.assign(Object.create(api), {
getWidth: () => ttyWidth
})
case renderMode.selected === 'container':
console.info(`Container: width: ${width}, render mode: ${renderMode.selected}`)
/**
* @name container
* @private
* @returns {api} - A zero-margin container that content can be flowed into.
*/
return Object.assign(Object.create(api), {
getWidth: () => ttyWidth
})
default:
console.info(stripIndent(colorReplacer)`
${'green|Renderer'}:
mode ▸ ${renderMode.selected} [${locale}]
┆ ${left} ◂├╌╌╌╌ ${viewWidth} ╌╌╌╌┤▸ ${right} ┆
`, '\n')
/**
* @name wrap
* @private
* @returns {api} - The wrapping API.
*/
return Object.assign(Object.create(api), {
getWidth: () => viewWidth,
panel(content, configuration) {
outStream.write(viewHandler.wrap(columnify(content, configuration)))
return this
},
write(text) {
outStream.write(viewHandler.wrap(text))
return this
}
})
}
}
export {createImage, parsePanel}