23
23
★☆
24
24
25
25
*/
26
- import { consoleOutput , getLeftRepeatChars , getRightRepeatChars , getStringWidth , isPlainObject , paddingCenter , paddingStart , paddingEnd } from "./utils.js"
27
- import deepmerge from 'deepmerge'
26
+ import { consoleOutput , getLeftRepeatChars , getRightRepeatChars , getStringWidth , paddingCenter , paddingStart , paddingEnd } from "./utils.js"
27
+ import deepmerge from 'deepmerge'
28
28
29
29
const DefaultBannersOptions = {
30
30
indent : " " , // 横幅缩进
@@ -34,34 +34,29 @@ const DefaultBannersOptions = {
34
34
} ,
35
35
title : {
36
36
align : "center" , // 标题对齐方式
37
- style : [ "" , "" , " green,bright", "" , "" ] , // 标题颜色
38
- wrapper : "=== " // ☆ ☆ ☆标题包裹符号,用来装饰
37
+ style : " green,bright", // 标题颜色
38
+ wrapper : "--== " // 原本是使用 ☆ ☆ ☆标题包裹符号比较好看,但是
39
39
} ,
40
40
align : "center" , // 横幅行默认对齐方式,默认居中
41
41
paddingLeft : 4 , // 左右空白宽度
42
42
paddingRight : 4 ,
43
- paddingTop : 1 ,
43
+ paddingTop : 0 , // 顶部空白行
44
44
paddingBottom : 1
45
45
}
46
46
47
47
function createBanner ( context , options ) {
48
48
const logger = this
49
- let opts = deepmerge ( DefaultBannersOptions , options )
50
- const colorizer = logger . getColorizer
51
-
49
+ let opts = deepmerge ( DefaultBannersOptions , options )
52
50
let lines = [ ] // [{text,style,align}]
53
51
function renderBanner ( ) {
54
52
// 计算最大行宽度
55
53
const paddingLeft = new Array ( opts . paddingLeft ) . fill ( " " ) . join ( "" )
56
54
const paddingRight = new Array ( opts . paddingRight ) . fill ( " " ) . join ( "" )
57
55
let totalWidth = lines . reduce ( ( width , line , index ) => {
58
56
if ( index === 0 ) { // 第一行视为标题行
59
- line . text . splice ( 0 , 0 , " " )
60
- line . text . splice ( 0 , 0 , opts . title . wrapper )
61
- line . text . push ( " " )
62
- line . text . push ( opts . title . wrapper )
57
+ line . text = `${ opts . title . wrapper } ${ logger . colorized ( line . text , opts . title . style ) } ${ opts . title . wrapper . reverse ( ) } `
63
58
}
64
- return Math . max ( width , getStringWidth ( line . text . join ( "" ) ) )
59
+ return Math . max ( width , getStringWidth ( line . text ) )
65
60
} , 0 ) + opts . paddingLeft + opts . paddingRight
66
61
if ( typeof ( opts . width ) === "number" && opts . width > totalWidth ) {
67
62
totalWidth = opts . width
@@ -74,38 +69,36 @@ function createBanner(context,options){
74
69
consoleOutput ( "║" + new Array ( totalWidth ) . fill ( " " ) . join ( "" ) + "║" )
75
70
} )
76
71
//
77
- // ║ ║ line = {text:[],style ,align}
72
+ // ║ ║ line = {text:[],vars ,align}
78
73
79
74
lines . map ( ( line , index ) => {
80
- let lineText = line . text . join ( "" )
75
+ let lineText = ( line . text || "" ) . trim ( )
76
+ let coloredLineText = lineText
81
77
let t = ""
82
78
let leftSpace = "" , rightSpace = ""
83
79
if ( lineText . trim ( ) === "" ) {
84
80
leftSpace = new Array ( totalWidth ) . fill ( " " ) . join ( "" )
85
81
} else {
82
+ coloredLineText = logger . getColorizedTemplate ( lineText , line . vars )
86
83
if ( line . align === "left" ) {
87
- t = paddingEnd ( lineText , totalWidth - opts . paddingLeft - opts . paddingRight )
84
+ t = paddingEnd ( coloredLineText , totalWidth - opts . paddingLeft - opts . paddingRight )
88
85
} else if ( line . align === "right" ) {
89
- t = paddingStart ( lineText , totalWidth - opts . paddingLeft - opts . paddingRight )
86
+ t = paddingStart ( coloredLineText , totalWidth - opts . paddingLeft - opts . paddingRight )
90
87
} else {
91
- t = paddingCenter ( lineText , totalWidth - opts . paddingLeft - opts . paddingRight )
88
+ t = paddingCenter ( coloredLineText , totalWidth - opts . paddingLeft - opts . paddingRight )
92
89
}
93
90
leftSpace = paddingLeft + getLeftRepeatChars ( t ) + ( index == 0 ? "" : "" )
94
91
rightSpace = paddingRight + getRightRepeatChars ( t ) + ( index == 0 ? "" : "" )
95
92
}
93
+
96
94
97
- let lineStyles = Array . isArray ( line . style ) ? line . style : ( line . style ? [ line . style ] : [ ] )
98
- if ( lineStyles . length === 0 && index === 0 ) lineStyles = opts . title . style
99
95
100
- const coloredLineTexts = lineStyles . length == 0 ? line . text : line . text . map ( ( t , i ) => {
101
- return colorizer ( i < lineStyles . length ? lineStyles [ i ] : lineStyles [ lineStyles . length - 1 ] ) ( t )
102
- } )
103
- logger . print ( "║" , leftSpace , ...coloredLineTexts , rightSpace , "║" , { append :"" } )
96
+ logger . print ( "║" , leftSpace , coloredLineText , rightSpace , "║" , { append :"" } )
104
97
// 在标题栏下增加一行分割线╟ ─ ╢
105
- // if(index===0){
106
- // //consoleOutput("║"+new Array(totalWidth).fill(" ").join("")+"║")
107
- // consoleOutput("╟"+new Array(totalWidth).fill("─").join("")+"╢")
108
- // }
98
+ if ( index === 0 ) {
99
+ //consoleOutput("║"+new Array(totalWidth).fill(" ").join("")+"║")
100
+ consoleOutput ( "╟" + new Array ( totalWidth ) . fill ( "─" ) . join ( "" ) + "╢" )
101
+ }
109
102
} )
110
103
111
104
// 留出空白行
@@ -115,32 +108,25 @@ function createBanner(context,options){
115
108
// ╚═══════════╝
116
109
consoleOutput ( "╚" + new Array ( totalWidth ) . fill ( "═" ) . join ( "" ) + "╝" )
117
110
}
118
-
119
- function getLineFromArgs ( ) {
120
- let linOptions = { align :opts . align , style :"" }
121
- let text = [ ]
122
- if ( arguments . length > 1 && isPlainObject ( arguments [ arguments . length - 1 ] ) ) {
123
- Object . assign ( linOptions , arguments [ arguments . length - 1 ] )
124
- text = [ ...arguments ] . slice ( 0 , - 1 )
125
- } else {
126
- text = [ ...arguments ]
127
- }
128
- return { text, ...linOptions }
129
- }
130
- return {
131
- addTitle ( text , options = { } ) {
132
- lines . splice ( 0 , 0 , getLineFromArgs ( ...arguments ) )
133
- } ,
134
- // add(text,options={}) add([text,text,text])
135
- add ( ) {
136
- lines . push ( getLineFromArgs ( ...arguments ) )
111
+ return {
112
+ add ( text , vars = { } , options = { } ) {
113
+ lines . push ( {
114
+ text,
115
+ vars,
116
+ ...options
117
+ } )
137
118
} ,
138
119
render :( ) => {
139
120
renderBanner ( )
140
121
}
141
122
}
142
123
143
124
}
125
+
126
+
127
+
128
+
129
+
144
130
/**
145
131
*
146
132
* @param {* } logger
0 commit comments