1+ import { getColumns } from '@clack/core' ;
2+ import { wrap } from '@macfja/ansi' ;
13import color from 'picocolors' ;
4+ import { cursor , erase } from 'sisteransi' ;
25import {
36 type CommonOptions ,
47 S_BAR ,
@@ -13,57 +16,70 @@ export interface LogMessageOptions extends CommonOptions {
1316 symbol ?: string ;
1417 spacing ?: number ;
1518 secondarySymbol ?: string ;
19+ removeLeadingSpace ?: boolean ;
1620}
1721
1822export const log = {
19- message : (
20- message : string | string [ ] = [ ] ,
23+ message : async (
24+ message : string | Iterable < string > | AsyncIterable < string > = [ ] ,
2125 {
2226 symbol = color . gray ( S_BAR ) ,
27+ spacing = 0 ,
2328 secondarySymbol = color . gray ( S_BAR ) ,
2429 output = process . stdout ,
25- spacing = 1 ,
30+ removeLeadingSpace = true ,
2631 } : LogMessageOptions = { }
2732 ) => {
28- const parts : string [ ] = [ ] ;
29- for ( let i = 0 ; i < spacing ; i ++ ) {
30- parts . push ( ` ${ secondarySymbol } ` ) ;
31- }
32- const messageParts = Array . isArray ( message ) ? message : message . split ( '\n' ) ;
33- if ( messageParts . length > 0 ) {
34- const [ firstLine , ... lines ] = messageParts ;
35- if ( firstLine . length > 0 ) {
36- parts . push ( `${ symbol } ${ firstLine } ` ) ;
37- } else {
38- parts . push ( symbol ) ;
33+ output . write ( ` ${ color . gray ( S_BAR ) } \n` ) ;
34+ let first = true ;
35+ let lastLine = '' ;
36+ const iterable = typeof message === 'string' ? [ message ] : message ;
37+ const isAsync = Symbol . asyncIterator in iterable ;
38+ for await ( let chunk of iterable ) {
39+ const width = getColumns ( output ) ;
40+ if ( first ) {
41+ lastLine = `${ symbol } ` ;
42+ chunk = '\n' . repeat ( spacing ) + chunk ;
43+ first = false ;
3944 }
40- for ( const ln of lines ) {
41- if ( ln . length > 0 ) {
42- parts . push ( `${ secondarySymbol } ${ ln } ` ) ;
43- } else {
44- parts . push ( secondarySymbol ) ;
45- }
45+ const newLineRE = removeLeadingSpace ? / \n * / g : / \n / g;
46+ const lines =
47+ lastLine . substring ( 0 , 3 ) +
48+ wrap ( `${ lastLine . substring ( 3 ) } ${ chunk } ` , width ) . replace (
49+ newLineRE ,
50+ `\n${ secondarySymbol } `
51+ ) ;
52+ output ?. write ( cursor . move ( - 999 , 0 ) + erase . lines ( 1 ) ) ;
53+ output ?. write ( lines ) ;
54+ lastLine = lines . substring ( Math . max ( 0 , lines . lastIndexOf ( '\n' ) + 1 ) ) ;
55+ if ( ! isAsync ) {
56+ lastLine = `${ secondarySymbol } ` ;
57+ output ?. write ( '\n' ) ;
4658 }
4759 }
48- output . write ( `${ parts . join ( '\n' ) } \n` ) ;
60+ if ( isAsync ) {
61+ output . write ( '\n' ) ;
62+ }
4963 } ,
50- info : ( message : string , opts ?: LogMessageOptions ) => {
51- log . message ( message , { ...opts , symbol : color . blue ( S_INFO ) } ) ;
64+ info : async ( message : string , opts ?: LogMessageOptions ) => {
65+ await log . message ( message , { ...opts , symbol : color . blue ( S_INFO ) } ) ;
5266 } ,
53- success : ( message : string , opts ?: LogMessageOptions ) => {
54- log . message ( message , { ...opts , symbol : color . green ( S_SUCCESS ) } ) ;
67+ success : async ( message : string , opts ?: LogMessageOptions ) => {
68+ await log . message ( message , { ...opts , symbol : color . green ( S_SUCCESS ) } ) ;
5569 } ,
56- step : ( message : string , opts ?: LogMessageOptions ) => {
57- log . message ( message , { ...opts , symbol : color . green ( S_STEP_SUBMIT ) } ) ;
70+ step : async ( message : string , opts ?: LogMessageOptions ) => {
71+ await log . message ( message , { ...opts , symbol : color . green ( S_STEP_SUBMIT ) } ) ;
5872 } ,
59- warn : ( message : string , opts ?: LogMessageOptions ) => {
60- log . message ( message , { ...opts , symbol : color . yellow ( S_WARN ) } ) ;
73+ warn : async ( message : string , opts ?: LogMessageOptions ) => {
74+ await log . message ( message , { ...opts , symbol : color . yellow ( S_WARN ) } ) ;
6175 } ,
6276 /** alias for `log.warn()`. */
63- warning : ( message : string , opts ?: LogMessageOptions ) => {
64- log . warn ( message , opts ) ;
77+ warning : async ( message : string , opts ?: LogMessageOptions ) => {
78+ await log . warn ( message , opts ) ;
6579 } ,
66- error : ( message : string , opts ?: LogMessageOptions ) => {
67- log . message ( message , { ...opts , symbol : color . red ( S_ERROR ) } ) ;
80+ error : async ( message : string , opts ?: LogMessageOptions ) => {
81+ await log . message ( message , { ...opts , symbol : color . red ( S_ERROR ) } ) ;
6882 } ,
6983} ;
84+
85+ export const stream = log ;
0 commit comments