Skip to content

Commit 94f3f40

Browse files
committed
fix: enhance terminal timestamp
1 parent 05050b6 commit 94f3f40

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/logger.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class Logger {
104104
private fancy: boolean // Whether to use fancy terminal output
105105
private tagFormat: TagFormat
106106
private timestampPosition: 'left' | 'right'
107-
private readonly ANSI_PATTERN = /\x1B\[\d+m/g
107+
private readonly ANSI_PATTERN = /\u001B\[.*?m/g // Use Unicode escape for ANSI sequence
108108
private activeProgressBar: { // State for the active progress bar
109109
total: number
110110
current: number
@@ -809,6 +809,9 @@ export class Logger {
809809
private formatConsoleMessage(parts: { timestamp: string, icon?: string, tag?: string, message: string, level?: LogLevel, showTimestamp?: boolean }): string {
810810
const { timestamp, icon = '', tag = '', message, level, showTimestamp = true } = parts
811811

812+
// Helper function to strip ANSI codes
813+
const stripAnsi = (str: string) => str.replace(this.ANSI_PATTERN, '')
814+
812815
// If fancy mode is disabled, return a simple format
813816
if (!this.fancy) {
814817
const components = []
@@ -850,7 +853,10 @@ export class Logger {
850853
}
851854

852855
// Calculate padding needed to push timestamp to far right
853-
const padding = Math.max(1, terminalWidth - mainPart.length - timestamp.length)
856+
const visibleMainPartLength = stripAnsi(mainPart).trim().length
857+
const visibleTimestampLength = stripAnsi(timestamp).length
858+
const padding = Math.max(1, terminalWidth - 2 - visibleMainPartLength - visibleTimestampLength) // Re-apply -2 for right padding
859+
854860
return `${mainPart.trim()}${' '.repeat(padding)}${timestamp}`
855861
}
856862

0 commit comments

Comments
 (0)