| 13 | * @param {string} [content] |
| 14 | */ |
| 15 | export function println(level: LogLevel, head: string, content?: string) { |
| 16 | const color: Record<LogLevel, Chalk> = { |
| 17 | [LogLevel.ACCESS]: chalk.blue, |
| 18 | [LogLevel.INFO]: chalk.green, |
| 19 | [LogLevel.WARNING]: chalk.yellow, |
| 20 | [LogLevel.ERROR]: chalk.red, |
| 21 | [LogLevel.DEBUG]: chalk.magenta, |
| 22 | } |
| 23 | if ((head as any) instanceof Error && (head as any).stack && (head as any).stack.length > 0) { |
| 24 | console.log(head) |
| 25 | return |
| 26 | } |
| 27 | if (typeof head === 'object' && !content) { |
| 28 | try { |
| 29 | head = JSON.stringify(head) |
| 30 | } catch (e) { |
| 31 | console.log(head) |
| 32 | return |
| 33 | } |
| 34 | shell.echo(color[level](head)) |
| 35 | } |
| 36 | if (!head) { |
| 37 | if (!content) { |
| 38 | return |
| 39 | } |
| 40 | head = content |
| 41 | } |
| 42 | |
| 43 | const MIN_HEAD_LENGTH = 10 |
| 44 | const emptyHead = head.replace(/[\u4e00-\u9fa5]/g, 'aa') |
| 45 | |
| 46 | const headLength = Math.max(emptyHead.length + 2, MIN_HEAD_LENGTH) |
| 47 | const fillLength = Math.max(MIN_HEAD_LENGTH - emptyHead.length, 0) |
| 48 | |
| 49 | if (!content) { |
| 50 | shell.echo(color[level](head)) |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | if (content && typeof content !== 'string') { |
| 55 | shell.echo(color[level](head)) |
| 56 | shell.echo(content) |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | ;(content ?? '') |
| 61 | .replace('/\r\n/g', '\n') |
| 62 | .split('\n') |
| 63 | .map((c) => chunk(c, __columns - headLength).map((str) => str.join(''))) |
| 64 | .reduce((r, c) => r.concat(c)) |
| 65 | .forEach((str, i) => { |
| 66 | const _head = i ? ' '.repeat(headLength) : color[level](`${head}${' '.repeat(fillLength)}`) |
| 67 | shell.echo(_head + str) |
| 68 | }) |
| 69 | } |
| 70 | |
| 71 | const out = { |
| 72 | access(head: string, content?: string) { |