clean-ansi 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +133 -0
- package/esm/index.d.ts +9 -0
- package/esm/index.js +31 -0
- package/index.d.ts +9 -0
- package/index.js +35 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Interweb, Inc. <developers@interweb.co>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# clean-ansi ๐งน
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="https://raw.githubusercontent.com/hyperweb-io/dev-utils/refs/heads/main/docs/img/logo.svg" width="80">
|
|
5
|
+
<br />
|
|
6
|
+
<strong>strip ANSI escape codes from strings</strong>
|
|
7
|
+
<br />
|
|
8
|
+
<br />
|
|
9
|
+
Remove all ANSI escape codes from terminal output
|
|
10
|
+
<br />
|
|
11
|
+
<br />
|
|
12
|
+
<a href="https://github.com/hyperweb-io/dev-utils/actions/workflows/ci.yml">
|
|
13
|
+
<img height="20" src="https://github.com/hyperweb-io/dev-utils/actions/workflows/ci.yml/badge.svg" />
|
|
14
|
+
</a>
|
|
15
|
+
<a href="https://github.com/hyperweb-io/dev-utils/blob/main/LICENSE">
|
|
16
|
+
<img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/>
|
|
17
|
+
</a>
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
## Why clean-ansi?
|
|
21
|
+
|
|
22
|
+
Terminal output often contains ANSI escape codes for colors, cursor movement, and formatting. `clean-ansi` removes these codes to give you clean, plain text that's perfect for:
|
|
23
|
+
|
|
24
|
+
- ๐งช Testing terminal applications and comparing output
|
|
25
|
+
- ๐ Logging to files without formatting codes
|
|
26
|
+
- ๐ Parsing command-line tool output
|
|
27
|
+
- ๐ Processing terminal data for analysis
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
npm install clean-ansi
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { cleanAnsi } from 'clean-ansi';
|
|
39
|
+
|
|
40
|
+
const coloredText = '\u001b[31mRed text\u001b[0m';
|
|
41
|
+
const cleanText = cleanAnsi(coloredText);
|
|
42
|
+
console.log(cleanText); // 'Red text'
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## API
|
|
46
|
+
|
|
47
|
+
### `cleanAnsi(input: string): string`
|
|
48
|
+
|
|
49
|
+
Removes all ANSI escape codes from the input string.
|
|
50
|
+
|
|
51
|
+
**Parameters:**
|
|
52
|
+
- `input` - The string containing ANSI escape codes
|
|
53
|
+
|
|
54
|
+
**Returns:**
|
|
55
|
+
- A string with all ANSI escape codes removed
|
|
56
|
+
|
|
57
|
+
**Handles:**
|
|
58
|
+
- CSI sequences (color codes, cursor movement, etc.)
|
|
59
|
+
- OSC sequences (OS commands like window title)
|
|
60
|
+
- Other escape sequences
|
|
61
|
+
|
|
62
|
+
### `stripAnsi(input: string): string`
|
|
63
|
+
|
|
64
|
+
Alias for `cleanAnsi` for compatibility.
|
|
65
|
+
|
|
66
|
+
## Examples
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { cleanAnsi } from 'clean-ansi';
|
|
70
|
+
|
|
71
|
+
// Remove color codes
|
|
72
|
+
cleanAnsi('\u001b[31mRed\u001b[0m \u001b[32mGreen\u001b[0m');
|
|
73
|
+
// Returns: 'Red Green'
|
|
74
|
+
|
|
75
|
+
// Remove cursor movement codes
|
|
76
|
+
cleanAnsi('Hello\u001b[AWorld');
|
|
77
|
+
// Returns: 'HelloWorld'
|
|
78
|
+
|
|
79
|
+
// Handle complex terminal output
|
|
80
|
+
cleanAnsi('\u001b[?25l\u001b[2J\u001b[H\u001b[31mError:\u001b[0m Message\u001b[?25h');
|
|
81
|
+
// Returns: 'Error: Message'
|
|
82
|
+
|
|
83
|
+
// Preserve newlines and whitespace
|
|
84
|
+
cleanAnsi('\u001b[31mLine 1\u001b[0m\nLine 2');
|
|
85
|
+
// Returns: 'Line 1\nLine 2'
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Design Philosophy
|
|
89
|
+
|
|
90
|
+
clean-ansi embraces simplicity:
|
|
91
|
+
- ๐ฏ Zero dependencies
|
|
92
|
+
- ๐ชถ Tiny footprint
|
|
93
|
+
- ๐ Fast and predictable
|
|
94
|
+
- ๐ Pure function
|
|
95
|
+
- ๐ Clear, focused API
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Development
|
|
100
|
+
|
|
101
|
+
### Setup
|
|
102
|
+
|
|
103
|
+
1. Clone the repository:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
git clone https://github.com/hyperweb-io/dev-utils.git
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
2. Install dependencies:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
cd dev-utils
|
|
113
|
+
pnpm install
|
|
114
|
+
pnpm build
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
3. Test the package of interest:
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
cd packages/<packagename>
|
|
121
|
+
pnpm test:watch
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Credits
|
|
125
|
+
|
|
126
|
+
Built for developers, with developers.
|
|
127
|
+
๐ https://launchql.com | https://hyperweb.io
|
|
128
|
+
|
|
129
|
+
## Disclaimer
|
|
130
|
+
|
|
131
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
132
|
+
|
|
133
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remove ANSI escape codes from a string.
|
|
3
|
+
* Handles:
|
|
4
|
+
* - CSI sequences: \u001b[... (color codes, cursor movement, etc.)
|
|
5
|
+
* - OSC sequences: \u001b]... (OS commands like window title)
|
|
6
|
+
* - Other escape sequences: \u001b followed by single characters
|
|
7
|
+
*/
|
|
8
|
+
export declare function cleanAnsi(input: string): string;
|
|
9
|
+
export declare const stripAnsi: typeof cleanAnsi;
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remove ANSI escape codes from a string.
|
|
3
|
+
* Handles:
|
|
4
|
+
* - CSI sequences: \u001b[... (color codes, cursor movement, etc.)
|
|
5
|
+
* - OSC sequences: \u001b]... (OS commands like window title)
|
|
6
|
+
* - Other escape sequences: \u001b followed by single characters
|
|
7
|
+
*/
|
|
8
|
+
export function cleanAnsi(input) {
|
|
9
|
+
if (!input)
|
|
10
|
+
return '';
|
|
11
|
+
// Match CSI (Control Sequence Introducer) sequences: ESC[ followed by parameters and final character
|
|
12
|
+
// Examples: \u001b[31m, \u001b[A, \u001b[2J, \u001b[?25h
|
|
13
|
+
const csiPattern = /\u001b\[[0-9;?]*[A-Za-z]/g;
|
|
14
|
+
// Match OSC (Operating System Command) sequences: ESC] followed by parameters and terminator
|
|
15
|
+
// Terminated by BEL (\u0007) or ESC\ (\u001b\\)
|
|
16
|
+
// Examples: \u001b]0;title\u0007, \u001b]2;title\u001b\\
|
|
17
|
+
// Handle BEL-terminated sequences
|
|
18
|
+
const oscBelPattern = /\u001b\][^\u0007]*\u0007/g;
|
|
19
|
+
// Handle ESC\-terminated sequences (more complex, need to match ESC\ specifically)
|
|
20
|
+
const oscEscPattern = /\u001b\][^\u001b]*\u001b\\/g;
|
|
21
|
+
// Match other escape sequences: ESC followed by single character
|
|
22
|
+
// Examples: \u001bD (index), \u001bM (reverse index), \u001b7 (save cursor), \u001b8 (restore cursor)
|
|
23
|
+
const otherEscPattern = /\u001b[0-9A-Za-z]/g;
|
|
24
|
+
return input
|
|
25
|
+
.replace(csiPattern, '')
|
|
26
|
+
.replace(oscBelPattern, '')
|
|
27
|
+
.replace(oscEscPattern, '')
|
|
28
|
+
.replace(otherEscPattern, '');
|
|
29
|
+
}
|
|
30
|
+
// Alias for compatibility
|
|
31
|
+
export const stripAnsi = cleanAnsi;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remove ANSI escape codes from a string.
|
|
3
|
+
* Handles:
|
|
4
|
+
* - CSI sequences: \u001b[... (color codes, cursor movement, etc.)
|
|
5
|
+
* - OSC sequences: \u001b]... (OS commands like window title)
|
|
6
|
+
* - Other escape sequences: \u001b followed by single characters
|
|
7
|
+
*/
|
|
8
|
+
export declare function cleanAnsi(input: string): string;
|
|
9
|
+
export declare const stripAnsi: typeof cleanAnsi;
|
package/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stripAnsi = void 0;
|
|
4
|
+
exports.cleanAnsi = cleanAnsi;
|
|
5
|
+
/**
|
|
6
|
+
* Remove ANSI escape codes from a string.
|
|
7
|
+
* Handles:
|
|
8
|
+
* - CSI sequences: \u001b[... (color codes, cursor movement, etc.)
|
|
9
|
+
* - OSC sequences: \u001b]... (OS commands like window title)
|
|
10
|
+
* - Other escape sequences: \u001b followed by single characters
|
|
11
|
+
*/
|
|
12
|
+
function cleanAnsi(input) {
|
|
13
|
+
if (!input)
|
|
14
|
+
return '';
|
|
15
|
+
// Match CSI (Control Sequence Introducer) sequences: ESC[ followed by parameters and final character
|
|
16
|
+
// Examples: \u001b[31m, \u001b[A, \u001b[2J, \u001b[?25h
|
|
17
|
+
const csiPattern = /\u001b\[[0-9;?]*[A-Za-z]/g;
|
|
18
|
+
// Match OSC (Operating System Command) sequences: ESC] followed by parameters and terminator
|
|
19
|
+
// Terminated by BEL (\u0007) or ESC\ (\u001b\\)
|
|
20
|
+
// Examples: \u001b]0;title\u0007, \u001b]2;title\u001b\\
|
|
21
|
+
// Handle BEL-terminated sequences
|
|
22
|
+
const oscBelPattern = /\u001b\][^\u0007]*\u0007/g;
|
|
23
|
+
// Handle ESC\-terminated sequences (more complex, need to match ESC\ specifically)
|
|
24
|
+
const oscEscPattern = /\u001b\][^\u001b]*\u001b\\/g;
|
|
25
|
+
// Match other escape sequences: ESC followed by single character
|
|
26
|
+
// Examples: \u001bD (index), \u001bM (reverse index), \u001b7 (save cursor), \u001b8 (restore cursor)
|
|
27
|
+
const otherEscPattern = /\u001b[0-9A-Za-z]/g;
|
|
28
|
+
return input
|
|
29
|
+
.replace(csiPattern, '')
|
|
30
|
+
.replace(oscBelPattern, '')
|
|
31
|
+
.replace(oscEscPattern, '')
|
|
32
|
+
.replace(otherEscPattern, '');
|
|
33
|
+
}
|
|
34
|
+
// Alias for compatibility
|
|
35
|
+
exports.stripAnsi = cleanAnsi;
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "clean-ansi",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
|
+
"description": "Remove ANSI escape codes from strings",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/hyperweb-io/dev-utils",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/hyperweb-io/dev-utils"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/hyperweb-io/dev-utils/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"copy": "makage assets",
|
|
24
|
+
"clean": "makage clean",
|
|
25
|
+
"prepublishOnly": "npm run build",
|
|
26
|
+
"build": "npm run clean && makage build-ts && npm run copy",
|
|
27
|
+
"lint": "eslint . --fix",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"makage": "0.1.5"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"ansi",
|
|
36
|
+
"strip",
|
|
37
|
+
"clean",
|
|
38
|
+
"escape codes",
|
|
39
|
+
"terminal",
|
|
40
|
+
"colors"
|
|
41
|
+
],
|
|
42
|
+
"gitHead": "ac1f58b6b805cd4c451e03e4b468a126326a7435"
|
|
43
|
+
}
|