rutui 0.4 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/rutui.rb +3 -21
- data/lib/rutui/ansi.rb +46 -0
- data/lib/rutui/axx.rb +66 -66
- data/lib/rutui/figlet.rb +126 -127
- data/lib/rutui/objects.rb +309 -250
- data/lib/rutui/pixel.rb +20 -18
- data/lib/rutui/screen.rb +125 -83
- data/lib/rutui/screenmanager.rb +73 -60
- data/lib/rutui/sprites.rb +50 -49
- data/lib/rutui/table.rb +183 -180
- data/lib/rutui/textfield.rb +122 -99
- data/lib/rutui/theme.rb +53 -52
- metadata +16 -17
- data/lib/rutui/util.rb +0 -78
data/lib/rutui/table.rb
CHANGED
@@ -1,209 +1,212 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
# :
|
7
|
-
#
|
8
|
-
# :
|
9
|
-
# :
|
10
|
-
#
|
11
|
-
# :
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
@reverse = false
|
51
|
-
@header = false
|
52
|
-
@header = true if options[:header] == true
|
53
|
-
|
54
|
-
@meta = { :height => @table.size, :cols => 0 }
|
55
|
-
index_col_widths
|
56
|
-
|
57
|
-
create
|
58
|
-
|
59
|
-
@height = @table.size
|
60
|
-
#@height += 2 if @ascii
|
61
|
-
@width = @table[0].size
|
1
|
+
module RuTui
|
2
|
+
## Table Object Class
|
3
|
+
# Creates an Table element
|
4
|
+
#
|
5
|
+
# Attributes (all accessible):
|
6
|
+
# :x MUST
|
7
|
+
# :y MUST
|
8
|
+
# :table - the tabular data in an array (like: [[1,"A"], [2, "B"]])
|
9
|
+
# :background - Background color (0-255)
|
10
|
+
# :foreground - Foreground color (0-255)
|
11
|
+
# :hover - Hover color (0-255)
|
12
|
+
# :ascii - Draw ASCII decoration
|
13
|
+
# :highlight - To highlight line
|
14
|
+
# :highlight_direction - Highlight direction
|
15
|
+
# :cols - Header and meta definitions (see example)
|
16
|
+
# :pixel - Default pixel (colors) for border
|
17
|
+
#
|
18
|
+
class Table < BaseObject
|
19
|
+
attr_accessor :table, :ascii, :highlight, :highlight_direction, :cols
|
20
|
+
|
21
|
+
def initialize options
|
22
|
+
@bg = options[:background]
|
23
|
+
@hover = options[:hover]
|
24
|
+
@fg = options[:foreground]
|
25
|
+
@bg = -1 if @bg.nil?
|
26
|
+
@fg = Theme.get(:textcolor) if @fg.nil?
|
27
|
+
@bg2 = @bg if @bg2.nil?
|
28
|
+
@ascii = options[:ascii]
|
29
|
+
@ascii = true if @ascii.nil?
|
30
|
+
|
31
|
+
@pixel = options[:pixel]
|
32
|
+
@pixel = Theme.get(:border) if @pixel.nil?
|
33
|
+
|
34
|
+
@table = options[:table]
|
35
|
+
@table = [] if @table.nil?
|
36
|
+
|
37
|
+
@cols = options[:cols]
|
38
|
+
@cols = [] if @cols.nil?
|
39
|
+
|
40
|
+
@highlight = options[:highlight]
|
41
|
+
@highlight = 0 if @highlight.nil?
|
42
|
+
|
43
|
+
@highlight_direction = options[:highlight_direction]
|
44
|
+
@highlight_direction = :horizontal if @highlight_direction.nil?
|
45
|
+
|
46
|
+
@x = options[:x]
|
47
|
+
@x = 1 if @x.nil?
|
48
|
+
@y = options[:y]
|
49
|
+
@y = 1 if @y.nil?
|
62
50
|
|
51
|
+
@reverse = false
|
52
|
+
@header = false
|
53
|
+
@header = true if options[:header] == true
|
63
54
|
|
64
|
-
|
55
|
+
@meta = { :height => @table.size, :cols => 0 }
|
56
|
+
index_col_widths
|
65
57
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
create
|
72
|
-
end
|
58
|
+
create
|
59
|
+
|
60
|
+
@height = @table.size
|
61
|
+
#@height += 2 if @ascii
|
62
|
+
@width = @table[0].size
|
73
63
|
|
74
|
-
##
|
75
|
-
# Add line to table
|
76
|
-
def add line
|
77
|
-
@table << line
|
78
|
-
@height += 1
|
79
|
-
index_col_widths
|
80
|
-
create
|
81
|
-
end
|
82
64
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
@
|
65
|
+
end
|
66
|
+
|
67
|
+
##
|
68
|
+
# Set data
|
69
|
+
def set_table table
|
70
|
+
@table = table
|
89
71
|
index_col_widths
|
90
72
|
create
|
91
73
|
end
|
92
|
-
end
|
93
74
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
75
|
+
##
|
76
|
+
# Add line to table
|
77
|
+
def add line
|
78
|
+
@table << line
|
79
|
+
@height += 1
|
80
|
+
index_col_widths
|
81
|
+
create
|
82
|
+
end
|
102
83
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
@cols.each_with_index do |col, index|
|
112
|
-
fg = @pixel.fg
|
113
|
-
fg = @cols[index][:title_color] if !@cols[index].nil? and !@cols[index][:title_color].nil?
|
114
|
-
chars = "".to_s.split("")
|
115
|
-
chars = " #{ col[:title] }".to_s.split("") if !col.nil? and !col[:title].nil?
|
116
|
-
chars.each_with_index do |e, char_count|
|
117
|
-
_obj << Pixel.new(fg,@bg,e)
|
118
|
-
end
|
119
|
-
(@meta[:max_widths][index]-chars.size+2).times do |i|
|
120
|
-
_obj << Pixel.new(@pixel.fg,@bg," ")
|
121
|
-
end
|
122
|
-
_obj << Pixel.new(@pixel.fg,@bg,"|") if @ascii
|
84
|
+
##
|
85
|
+
# Delete line from table by index
|
86
|
+
def delete line_id
|
87
|
+
if !@table[line_id].nil?
|
88
|
+
@table.delete_at(line_id)
|
89
|
+
@height -= 1
|
90
|
+
index_col_widths
|
91
|
+
create
|
123
92
|
end
|
124
|
-
obj << _obj
|
125
93
|
end
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
94
|
+
|
95
|
+
##
|
96
|
+
# highlight line
|
97
|
+
def highlight line_id
|
98
|
+
return highlight if line_id.nil?
|
99
|
+
@highlight = line_id
|
100
|
+
@reverse = false
|
101
|
+
create
|
102
|
+
end
|
103
|
+
|
104
|
+
##
|
105
|
+
# create or recreate the table object
|
106
|
+
def create
|
107
|
+
obj = []
|
108
|
+
if @header
|
109
|
+
obj << ascii_table_line if @ascii
|
110
|
+
_obj = []
|
111
|
+
_obj << Pixel.new(@pixel.fg,@bg,"|") if @ascii
|
112
|
+
_obj << nil
|
113
|
+
@cols.each_with_index do |col, index|
|
114
|
+
fg = @pixel.fg
|
115
|
+
fg = @cols[index][:title_color] if !@cols[index].nil? and !@cols[index][:title_color].nil?
|
116
|
+
chars = "".to_s.split("")
|
117
|
+
chars = "#{ col[:title] }".to_s.split("") if !col.nil? and !col[:title].nil?
|
118
|
+
chars.each_with_index do |e, char_count|
|
119
|
+
_obj << Pixel.new(fg,@bg,e)
|
141
120
|
end
|
121
|
+
(@meta[:max_widths][index]-chars.size+2).times do |i|
|
122
|
+
_obj << nil
|
123
|
+
end
|
124
|
+
_obj << Pixel.new(@pixel.fg,@bg,"|") if @ascii
|
142
125
|
end
|
126
|
+
obj << _obj
|
127
|
+
end
|
128
|
+
obj << ascii_table_line if @ascii
|
129
|
+
@table.each_with_index do |line, lindex|
|
130
|
+
bg = @bg
|
131
|
+
bg = @hover if lindex == @highlight and @highlight_direction == :horizontal
|
132
|
+
_obj = []
|
133
|
+
_obj << Pixel.new(@pixel.fg,bg,"|") if @ascii
|
134
|
+
line.each_with_index do |col, index|
|
135
|
+
fg = @fg
|
136
|
+
fg = @cols[index][:color] if !@cols[index].nil? and !@cols[index][:color].nil?
|
137
|
+
|
138
|
+
if @highlight_direction == :vertical
|
139
|
+
if index == @highlight
|
140
|
+
bg = @hover
|
141
|
+
else
|
142
|
+
bg = @bg
|
143
|
+
end
|
144
|
+
end
|
143
145
|
|
144
146
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
147
|
+
chars = col.to_s.split("")
|
148
|
+
_obj << nil
|
149
|
+
max_chars = nil
|
150
|
+
max_chars = @cols[index][:max_length]+1 if !@cols[index].nil? and !@cols[index][:max_length].nil?
|
151
|
+
max_chars = @cols[index][:length]+1 if !@cols[index].nil? and !@cols[index][:length].nil?
|
152
|
+
chars.each_with_index do |e, char_count|
|
153
|
+
break if !max_chars.nil? and char_count >= max_chars
|
154
|
+
_obj << Pixel.new(fg,bg,e)
|
155
|
+
end
|
156
|
+
(@meta[:max_widths][index]-chars.size+1).times do |i|
|
157
|
+
_obj << nil
|
158
|
+
end
|
157
159
|
|
158
|
-
|
159
|
-
|
160
|
+
bg = @bg if @highlight_direction == :vertical
|
161
|
+
_obj << Pixel.new(@pixel.fg,bg,"|") if @ascii
|
162
|
+
end
|
163
|
+
obj << _obj
|
160
164
|
end
|
161
|
-
obj <<
|
165
|
+
obj << ascii_table_line if @ascii
|
166
|
+
@obj = obj
|
162
167
|
end
|
163
|
-
obj << ascii_table_line if @ascii
|
164
|
-
@obj = obj
|
165
|
-
end
|
166
168
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
169
|
+
##
|
170
|
+
# sort by column
|
171
|
+
def sort col
|
172
|
+
@table.sort! { |a,b| a[col] <=> b[col] }
|
173
|
+
@table.reverse! if @reverse
|
174
|
+
if @reverse == false
|
175
|
+
@reverse = true
|
176
|
+
else
|
177
|
+
@reverse = false
|
178
|
+
end
|
179
|
+
create
|
176
180
|
end
|
177
|
-
create
|
178
|
-
end
|
179
181
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
_obj << Pixel.new(@pixel.fg,@bg,"+")
|
184
|
-
@meta[:max_widths].each do |index,mw|
|
185
|
-
(mw+2).times do |i|
|
186
|
-
_obj << Pixel.new(@pixel.fg,@bg,"-")
|
187
|
-
end
|
182
|
+
private
|
183
|
+
def ascii_table_line
|
184
|
+
_obj = []
|
188
185
|
_obj << Pixel.new(@pixel.fg,@bg,"+")
|
186
|
+
@meta[:max_widths].each do |index,mw|
|
187
|
+
(mw+2).times do |i|
|
188
|
+
_obj << Pixel.new(@pixel.fg,@bg,"-")
|
189
|
+
end
|
190
|
+
_obj << Pixel.new(@pixel.fg,@bg,"+")
|
191
|
+
end
|
192
|
+
_obj
|
189
193
|
end
|
190
|
-
_obj
|
191
|
-
end
|
192
194
|
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
195
|
+
def index_col_widths
|
196
|
+
@meta[:cols] = 0
|
197
|
+
@meta[:max_widths] = {}
|
198
|
+
@table.each do |line|
|
199
|
+
cols = 0
|
200
|
+
if !line.nil?
|
201
|
+
line.each_with_index do |col, index|
|
202
|
+
@meta[:max_widths][index] = col.size if @meta[:max_widths][index].nil?
|
203
|
+
@meta[:max_widths][index] = col.size if @meta[:max_widths][index] < col.size
|
204
|
+
@meta[:max_widths][index] = @cols[index][:max_length] if !@cols.nil? and !@cols[index].nil? and !@cols[index][:max_length].nil?
|
205
|
+
@meta[:max_widths][index] = @cols[index][:length] if !@cols.nil? and !@cols[index].nil? and !@cols[index][:length].nil?
|
206
|
+
end
|
207
|
+
@meta[:cols] = cols if cols > @meta[:cols]
|
204
208
|
end
|
205
|
-
@meta[:cols] = cols if cols > @meta[:cols]
|
206
209
|
end
|
207
210
|
end
|
208
211
|
end
|
209
|
-
end
|
212
|
+
end
|
data/lib/rutui/textfield.rb
CHANGED
@@ -1,112 +1,135 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
# :
|
7
|
-
#
|
8
|
-
# :
|
9
|
-
# :
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
# :
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
1
|
+
module RuTui
|
2
|
+
## Textfield Object Class
|
3
|
+
# Creates an Textfield element
|
4
|
+
#
|
5
|
+
# Methods:
|
6
|
+
# :set_focus Set focus to this textfield
|
7
|
+
# :take_focus Unset focus
|
8
|
+
# :write CHAR Write char to textfield (if has focus)
|
9
|
+
# :set_text TEXT Set text
|
10
|
+
# :get_text Returns text
|
11
|
+
# :count Text count
|
12
|
+
#
|
13
|
+
# Attributes (all accessible):
|
14
|
+
# :x MUST
|
15
|
+
# :y MUST
|
16
|
+
# :pixel - pixel from which colors get inherted
|
17
|
+
# :focus_pixel - pixel used when on focus (defaults to pixel)
|
18
|
+
# :allow - allowed chars
|
19
|
+
# :password - boolean: is password field?
|
20
|
+
# :focus - has focus?
|
21
|
+
#
|
22
|
+
class Textfield < BaseObject
|
23
|
+
attr_accessor :focus, :password, :allow
|
24
|
+
|
25
|
+
def initialize options
|
26
|
+
@width = 14
|
27
|
+
@width = options[:width] if !options[:width].nil?
|
28
|
+
|
29
|
+
@pixel = options[:pixel]
|
30
|
+
@pixel = Theme.get(:border) if @pixel.nil?
|
31
|
+
|
32
|
+
@focus_pixel = options[:focus_pixel]
|
33
|
+
@focus_pixel = @pixel if @focus_pixel.nil?
|
34
|
+
|
35
|
+
@x = options[:x]
|
36
|
+
@x = 1 if @x.nil?
|
37
|
+
@y = options[:y]
|
38
|
+
@y = 1 if @y.nil?
|
39
|
+
|
40
|
+
@allow = (0..9).to_a + ('a'..'z').to_a + ('A'..'Z').to_a + [" ", ":", "<", ">", "|", "#",
|
41
|
+
"@", "*", ",", "!", "?", ".", "-", "_", "=", "[", "]", "(", ")", "{", "}", ";"]
|
42
|
+
@allow = options[:allow] if !options[:allow].nil?
|
43
|
+
|
44
|
+
@height = 1
|
45
|
+
|
46
|
+
@focus = false
|
47
|
+
@password = false
|
48
|
+
@password = true if options[:password] == true
|
49
|
+
|
50
|
+
@text = ""
|
51
|
+
|
52
|
+
create
|
53
|
+
end
|
48
54
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
+
##
|
56
|
+
# set focus
|
57
|
+
def set_focus
|
58
|
+
@focus = true
|
59
|
+
RuTui::Ansi.position @x, @y
|
60
|
+
create
|
61
|
+
end
|
55
62
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
63
|
+
##
|
64
|
+
# take focus
|
65
|
+
def take_focus
|
66
|
+
@focus = false
|
67
|
+
create
|
68
|
+
end
|
61
69
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
@text += char.chr if @allow.include? char.chr
|
67
|
-
@text = @text[0..-2] if char == 127
|
70
|
+
##
|
71
|
+
# count
|
72
|
+
def count
|
73
|
+
@text.size
|
68
74
|
end
|
69
|
-
create
|
70
|
-
end
|
71
75
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
76
|
+
##
|
77
|
+
# write to textfield
|
78
|
+
def write char
|
79
|
+
if @focus
|
80
|
+
@text += char if @allow.include? char
|
81
|
+
@text = @text[0..-2] if char == :backspace or char == :ctrl_h # its 8 on windows
|
82
|
+
end
|
83
|
+
create
|
84
|
+
end
|
78
85
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
86
|
+
##
|
87
|
+
# set text
|
88
|
+
def set_text text
|
89
|
+
@text = text
|
90
|
+
create
|
91
|
+
end
|
84
92
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
_obj = []
|
90
|
-
if (@text.size+2) < @width
|
91
|
-
text = @text
|
92
|
-
else
|
93
|
-
text = @text.split(//).last(@width).join("").to_s
|
93
|
+
##
|
94
|
+
# get text
|
95
|
+
def get_text
|
96
|
+
@text
|
94
97
|
end
|
95
98
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
99
|
+
##
|
100
|
+
# create or recreate the table object
|
101
|
+
def create
|
102
|
+
obj = []
|
103
|
+
_obj = []
|
104
|
+
if (@text.size+2) < @width
|
105
|
+
text = @text
|
106
|
+
else
|
107
|
+
text = @text.split(//).last(@width).join("").to_s
|
101
108
|
end
|
102
|
-
end
|
103
109
|
|
104
|
-
|
105
|
-
|
110
|
+
if password
|
111
|
+
_text = text
|
112
|
+
text = ""
|
113
|
+
_text.size.times do |i|
|
114
|
+
text += "*"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
text.to_s.split("").each do |t|
|
119
|
+
if @focus && @focus_pixel != @pixel
|
120
|
+
_obj << Pixel.new(@focus_pixel.fg, @focus_pixel.bg, t)
|
121
|
+
else
|
122
|
+
_obj << Pixel.new(@pixel.fg, @pixel.bg, t)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
if @focus && @focus_pixel != @pixel
|
127
|
+
_obj << Pixel.new(@focus_pixel.fg, @focus_pixel.bg, "_")
|
128
|
+
else
|
129
|
+
_obj << Pixel.new(@pixel.fg, @pixel.bg, "_")
|
130
|
+
end
|
131
|
+
obj << _obj
|
132
|
+
@obj = obj
|
106
133
|
end
|
107
|
-
|
108
|
-
_obj << Pixel.new(@pixel.fg, @pixel.bg, "_")
|
109
|
-
obj << _obj
|
110
|
-
@obj = obj
|
111
134
|
end
|
112
|
-
end
|
135
|
+
end
|