bashparser 1.0.0

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.
Files changed (2) hide show
  1. data/lib/bashparser.rb +106 -0
  2. metadata +45 -0
data/lib/bashparser.rb ADDED
@@ -0,0 +1,106 @@
1
+ class BashParser
2
+ INACTIVE = nil
3
+ ESCAPE_BYTE = 27
4
+ CURSOR_MOVEMENT_ESCAPE_CHARS = ['H', 'J']
5
+
6
+ def reset_buffer
7
+ @x = 0
8
+ @y = 0
9
+ @buffer = []
10
+ @escape_code = INACTIVE
11
+ end
12
+
13
+ def initialize
14
+ reset_buffer
15
+ end
16
+
17
+ def display_screen(all_input = nil)
18
+ data = []
19
+ if all_input != nil
20
+ reset_buffer
21
+ data = parse_input(all_input)
22
+ else
23
+ data = get_buffer_contents
24
+ end
25
+ data.each { |line| puts line }
26
+ end
27
+
28
+ def parse_input(input)
29
+ input.each_byte do |byte|
30
+ write_byte_to_buffer(byte) if @escape_code == INACTIVE
31
+ read_for_escape_codes(byte)
32
+ end
33
+ get_buffer_contents
34
+ end
35
+
36
+ private
37
+ def get_buffer_contents
38
+ @buffer.dup.map do |line|
39
+ (line || []).map { |char| char || ' ' }.join
40
+ end
41
+ end
42
+
43
+ def read_for_escape_codes(byte)
44
+ char = byte.chr
45
+ @escape_code += char if @escape_code != INACTIVE
46
+ @escape_code = '' if byte == ESCAPE_BYTE
47
+
48
+ byte_is_letter = char != char.swapcase
49
+ if @escape_code != INACTIVE and byte_is_letter
50
+ handle_escape_code
51
+ @escape_code = INACTIVE
52
+ end
53
+ end
54
+
55
+ def write_byte_to_buffer(byte)
56
+ if byte == 13
57
+ @y += 2
58
+ @x = 0
59
+ elsif byte == 8
60
+ @x -= 1
61
+ @x = 0 if @x < 0
62
+ elsif byte == 10
63
+ @y += 1
64
+ elsif byte >= 32
65
+ @buffer[@y] = [] unless @buffer[@y]
66
+ @buffer[@y][@x] = byte.chr
67
+ @x += 1
68
+ end
69
+ end
70
+
71
+ def get_cursor_position_after_escape(escape_code)
72
+ return [@y, @x] unless escape_code
73
+ if escape_code == '[H' or escape_code == '[2J'
74
+ [0, 0]
75
+ elsif escape_code[-1] == 'H'
76
+ position = escape_code[1..-2].split(';')
77
+ [position[0].to_i - 1, position[1].to_i - 1]
78
+ elsif escape_code[-1] == 'd'
79
+ distance = escape_code[1..-2].to_i
80
+ [distance - 1, @x]
81
+ elsif escape_code == '[C'
82
+ [@y, @x + 1]
83
+ elsif escape_code[-1] == 'C'
84
+ distance = escape_code[1..-2].to_i
85
+ [@y, @x + distance]
86
+ elsif escape_code[-1] == 'G'
87
+ distance = escape_code[1..-2].to_i
88
+ [@y, distance - 1]
89
+ else
90
+ [@y, @x]
91
+ end
92
+ end
93
+
94
+ def handle_escape_code
95
+ if @escape_code == '[2J'
96
+ reset_buffer
97
+ elsif @escape_code == '[2S'
98
+ @buffer[@y] = []
99
+ elsif @escape_code == '[J'
100
+ @buffer = @buffer[0..@y]
101
+ @buffer[@y] = @buffer[@y][0..@x] if @buffer[@y]
102
+ @buffer[@y][@x] = ' ' if @buffer[@y]
103
+ end
104
+ @y, @x = get_cursor_position_after_escape(@escape_code)
105
+ end
106
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bashparser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Smith
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Translates text and Terminal Escape Codes into a readable format
15
+ email: john@ishared.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/bashparser.rb
21
+ homepage: https://github.com/ChessTeach/bashparser
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.11
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Translates text and Terminal Escape Codes into a readable format
45
+ test_files: []