Class: Ansi2txt::ANSI2TXT

Inherits:
Object
  • Object
show all
Defined in:
lib/ansi2txt.rb

Overview

ANSI2TXT converts ANSI escape sequences to readable plain text

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ ANSI2TXT

Returns a new instance of ANSI2TXT.



12
13
14
15
16
17
# File 'lib/ansi2txt.rb', line 12

def initialize(**kwargs)
  @input = kwargs[:input]
  @input = $stdin if @input.nil?
  @output = kwargs[:output]
  @output = $stdout if @output.nil?
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



71
72
73
# File 'lib/ansi2txt.rb', line 71

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



71
72
73
# File 'lib/ansi2txt.rb', line 71

def output
  @output
end

Class Method Details

.from_io(input) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



61
62
63
64
65
66
67
68
69
# File 'lib/ansi2txt.rb', line 61

def self.from_io(input)
  output = Tempfile.open("")
  ansi2text = Ansi2txt::ANSI2TXT.new(input: input, output: output)
  ansi2text.ansi2txt
  output.rewind
  result = output.read
  output.close
  result
end

Instance Method Details

#ansi2txtObject

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ansi2txt.rb', line 28

def ansi2txt
  loop do
    ch = getchar
    while ch.eql?("\r")
      ch = getchar
      putchar("\r") unless ch.eql?("\n")
    end

    if ch.eql?("\e")
      if (ch = getchar).eql?("[")
        while (ch = getchar).eql?(";") || ch.between?("0", "9") || ch.eql?("?")
        end
      elsif ch.eql?("]") && (ch = getchar).between?("0", "9")
        loop do
          if (ch = getchar).nil? || "\a".eql?(ch)
            break
          elsif ch.eql?("\e")
            ch = getchar
            break
          end
        end
      elsif ["%", "(", ")"].include?(ch)
        ch = getchar
      end
    elsif !ch.nil?
      putchar(ch)
    end

    break if ch.nil?
  end
end

#getcharObject



19
20
21
# File 'lib/ansi2txt.rb', line 19

def getchar
  @input.getc
end

#putchar(chara) ⇒ Object



23
24
25
# File 'lib/ansi2txt.rb', line 23

def putchar(chara)
  @output.putc(chara)
end