Skip to content

Commit 184a114

Browse files
committed
refactor: Replace hardcoded ANSI escape sequences with named constants
- Add DISABLE_LINE_WRAP and ENABLE_LINE_WRAP constants for line wrap control - Add ANSI_RESET constant for color reset sequences - Improve code maintainability and readability
1 parent 33ea4b9 commit 184a114

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

‎src/_terminal-utils.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import stringWidth from 'string-width';
99
const SYNC_START = '\x1B[?2026h'; // Start sync mode
1010
const SYNC_END = '\x1B[?2026l'; // End sync mode
1111

12+
// Line wrap control sequences
13+
const DISABLE_LINE_WRAP = '\x1B[?7l'; // Disable automatic line wrapping
14+
const ENABLE_LINE_WRAP = '\x1B[?7h'; // Enable automatic line wrapping
15+
16+
// ANSI reset sequence
17+
const ANSI_RESET = '\u001B[0m'; // Reset all formatting and colors
18+
1219
/**
1320
* Manages terminal state for live updates
1421
* Provides a clean interface for terminal operations with automatic TTY checking
@@ -109,7 +116,7 @@ export class TerminalManager {
109116
if (!this.alternateScreenActive && this.stream.isTTY) {
110117
this.stream.write(ansiEscapes.enterAlternativeScreen);
111118
// Turn off line wrapping to prevent text from breaking badly
112-
this.stream.write('\x1B[?7l');
119+
this.stream.write(DISABLE_LINE_WRAP);
113120
this.alternateScreenActive = true;
114121
}
115122
}
@@ -120,7 +127,7 @@ export class TerminalManager {
120127
exitAlternateScreen(): void {
121128
if (this.alternateScreenActive && this.stream.isTTY) {
122129
// Re-enable line wrap
123-
this.stream.write('\x1B[?7h');
130+
this.stream.write(ENABLE_LINE_WRAP);
124131
this.stream.write(ansiEscapes.exitAlternativeScreen);
125132
this.alternateScreenActive = false;
126133
}
@@ -254,7 +261,7 @@ export function createProgressBar(
254261
bar += fillChar.repeat(fillWidth);
255262
bar += emptyChar.repeat(emptyWidth);
256263
if (color !== '') {
257-
bar += '\u001B[0m'; // Reset color
264+
bar += ANSI_RESET; // Reset color
258265
}
259266
bar += rightBracket;
260267

0 commit comments

Comments
 (0)