ansi2txt 24.03.29
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +33 -0
- data/Rakefile +8 -0
- data/exe/ansi2txt +12 -0
- data/lib/ansi2txt/version.rb +5 -0
- data/lib/ansi2txt.rb +73 -0
- data/sig/ansi2txt.rbs +4 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f5c732f0e9371ef07f2a22f87c1606bcbfd76480025da8b5622217a2231a9046
|
4
|
+
data.tar.gz: 7a534eacee1cb355a04b7764e7fd0ec69a9235959f3152cd272225e90cf11668
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a16617b31fd0751f808e619b4aff7f6f2849188f0e6ade866b3097ee24ccaeadc5cb64a468b6b2d79657e14f9dbd6901ad3fad929292a3239b11c3588d37ab59
|
7
|
+
data.tar.gz: e664952096dcb4b9b511a3d51f4c118c49900df5a8023b29a86a38d413c59cf2fd008e9c3c64bd07b3264363d34d7657d39b93669ea4354658a653bb76589c35
|
data/.rubocop.yml
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 MURATA Mitsuharu
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# ansi2txt
|
2
|
+
ansi2txt converts ANSI escape sequences to readable plain text.
|
3
|
+
|
4
|
+
This library is a Ruby port based on [colorized-logs](https://github.com/kilobyte/colorized-logs).
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
```bash
|
9
|
+
gem install ansi2txt
|
10
|
+
```
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
An example of converting input from an IO object to plain text.
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
input = File.open("typescript")
|
18
|
+
puts Ansi2txt::ANSI2TXT.from_io(input)
|
19
|
+
```
|
20
|
+
|
21
|
+
An example of converting input from standard input to plain text.
|
22
|
+
|
23
|
+
```bash
|
24
|
+
cat typescript | ansi2txt
|
25
|
+
```
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Himeyama/ansi2txt.
|
30
|
+
|
31
|
+
## License
|
32
|
+
|
33
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/exe/ansi2txt
ADDED
data/lib/ansi2txt.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "ansi2txt/version"
|
4
|
+
require "stringio"
|
5
|
+
require "tempfile"
|
6
|
+
|
7
|
+
module Ansi2txt
|
8
|
+
class Error < StandardError; end
|
9
|
+
|
10
|
+
# ANSI2TXT converts ANSI escape sequences to readable plain text
|
11
|
+
class ANSI2TXT
|
12
|
+
def initialize(**kwargs)
|
13
|
+
@input = kwargs[:input]
|
14
|
+
@input = $stdin if @input.nil?
|
15
|
+
@output = kwargs[:output]
|
16
|
+
@output = $stdout if @output.nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
def getchar
|
20
|
+
@input.getc
|
21
|
+
end
|
22
|
+
|
23
|
+
def putchar(chara)
|
24
|
+
@output.putc(chara)
|
25
|
+
end
|
26
|
+
|
27
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
28
|
+
def ansi2txt
|
29
|
+
loop do
|
30
|
+
ch = getchar
|
31
|
+
while ch.eql?("\r")
|
32
|
+
ch = getchar
|
33
|
+
putchar("\r") unless ch.eql?("\n")
|
34
|
+
end
|
35
|
+
|
36
|
+
if ch.eql?("\e")
|
37
|
+
if (ch = getchar).eql?("[")
|
38
|
+
while (ch = getchar).eql?(";") || ch.between?("0", "9") || ch.eql?("?")
|
39
|
+
end
|
40
|
+
elsif ch.eql?("]") && (ch = getchar).between?("0", "9")
|
41
|
+
loop do
|
42
|
+
if (ch = getchar).nil? || "\a".eql?(ch)
|
43
|
+
break
|
44
|
+
elsif ch.eql?("\e")
|
45
|
+
ch = getchar
|
46
|
+
break
|
47
|
+
end
|
48
|
+
end
|
49
|
+
elsif ["%", "(", ")"].include?(ch)
|
50
|
+
ch = getchar
|
51
|
+
end
|
52
|
+
elsif !ch.nil?
|
53
|
+
putchar(ch)
|
54
|
+
end
|
55
|
+
|
56
|
+
break if ch.nil?
|
57
|
+
end
|
58
|
+
end
|
59
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
|
60
|
+
|
61
|
+
def self.from_io(input)
|
62
|
+
output = Tempfile.open("")
|
63
|
+
ansi2text = Ansi2txt::ANSI2TXT.new(input: input, output: output)
|
64
|
+
ansi2text.ansi2txt
|
65
|
+
output.rewind
|
66
|
+
result = output.read
|
67
|
+
output.close
|
68
|
+
result
|
69
|
+
end
|
70
|
+
|
71
|
+
attr_reader :input, :output
|
72
|
+
end
|
73
|
+
end
|
data/sig/ansi2txt.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ansi2txt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 24.03.29
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- HIKARI
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-03-29 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ansi2txt converts ANSI escape sequences to readable plain text.
|
14
|
+
email:
|
15
|
+
- hikari.photon+dev@gmail.com
|
16
|
+
executables:
|
17
|
+
- ansi2txt
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rubocop.yml"
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- exe/ansi2txt
|
26
|
+
- lib/ansi2txt.rb
|
27
|
+
- lib/ansi2txt/version.rb
|
28
|
+
- sig/ansi2txt.rbs
|
29
|
+
homepage: https://github.com/Himeyama/ansi2txt
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata:
|
33
|
+
homepage_uri: https://github.com/Himeyama/ansi2txt
|
34
|
+
source_code_uri: https://github.com/Himeyama/ansi2txt
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.6.0
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubygems_version: 3.5.3
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: ansi2txt converts ANSI escape sequences to readable plain text.
|
54
|
+
test_files: []
|