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 | "use strict" ;
|
28 |
|
29 |
|
30 |
|
31 | const Element = require( './Element.js' ) ;
|
32 |
|
33 |
|
34 |
|
35 | function Text( options ) {
|
36 |
|
37 | options = ! options ? {} : options.internal ? options : Object.create( options ) ;
|
38 | options.internal = true ;
|
39 |
|
40 | this.attr = options.attr || { bgColor: 'brightBlack' } ;
|
41 | this.leftPadding = options.leftPadding || '' ;
|
42 | this.rightPadding = options.rightPadding || '' ;
|
43 |
|
44 | if ( ! Array.isArray( options.content ) ) {
|
45 | options.content = [ options.content || '' ] ;
|
46 | }
|
47 |
|
48 | if ( ! options.width ) {
|
49 | options.width =
|
50 | Element.computeContentWidth( this.leftPadding , this.paddingHasMarkup )
|
51 | + Element.computeContentWidth( this.rightPadding , this.paddingHasMarkup )
|
52 | + ( Element.computeContentWidth( options.content , options.contentHasMarkup ) || 1 ) ;
|
53 | }
|
54 |
|
55 | options.height = options.content.length ;
|
56 |
|
57 | Element.call( this , options ) ;
|
58 |
|
59 | if ( this.elementType === 'Text' && ! options.noDraw ) { this.draw() ; }
|
60 | }
|
61 |
|
62 | module.exports = Text ;
|
63 |
|
64 | Text.prototype = Object.create( Element.prototype ) ;
|
65 | Text.prototype.constructor = Text ;
|
66 | Text.prototype.elementType = 'Text' ;
|
67 |
|
68 |
|
69 |
|
70 | Text.prototype.postDrawSelf = function() {
|
71 | if ( ! this.outputDst ) { return this ; }
|
72 |
|
73 | var gap , resumeAttr ;
|
74 |
|
75 | this.content.forEach( ( contentLine , lineNumber ) => {
|
76 |
|
77 | this.outputDst.moveTo( this.outputX , this.outputY + lineNumber ) ;
|
78 |
|
79 |
|
80 | if ( this.leftPadding ) {
|
81 | this.outputDst.put( { attr: this.attr , markup: this.paddingHasMarkup } , this.leftPadding ) ;
|
82 | }
|
83 |
|
84 |
|
85 | resumeAttr = this.outputDst.put( { attr: this.attr , resumeAttr: resumeAttr , markup: this.contentHasMarkup } , contentLine ) ;
|
86 |
|
87 |
|
88 | if ( this.rightPadding ) {
|
89 | this.outputDst.put( { attr: this.attr , markup: this.paddingHasMarkup } , this.rightPadding ) ;
|
90 | }
|
91 |
|
92 | gap = this.outputX + this.outputWidth - this.outputDst.cx ;
|
93 |
|
94 | if ( gap > 0 ) {
|
95 | this.outputDst.put( { attr: this.attr } , ' '.repeat( gap ) ) ;
|
96 | }
|
97 | } ) ;
|
98 | } ;
|
99 |
|