rutui 0.4 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,209 +1,212 @@
1
- ## Table Object Class
2
- # Creates an Table element
3
- #
4
- # Attributes (all accessible):
5
- # :x MUST
6
- # :y MUST
7
- # :table - the tabular data in an array (like: [[1,"A"], [2, "B"]])
8
- # :background - Background color (0-255)
9
- # :foreground - Foreground color (0-255)
10
- # :hover - Hover color (0-255)
11
- # :ascii - Draw ASCII decoration
12
- # :highlight - To highlight line
13
- # :highlight_direction - Highlight direction
14
- # :cols - Header and meta definitions (see example)
15
- # :pixel - Default pixel (colors) for border
16
- #
17
- class Table < BaseObject
18
- attr_accessor :table, :ascii, :highlight, :highlight_direction, :cols
19
-
20
- def initialize options
21
- @bg = options[:background]
22
- @hover = options[:hover]
23
- @fg = options[:foreground]
24
- @bg = Theme.get(:background).bg if @bg.nil?
25
- @fg = Theme.get(:textcolor) if @fg.nil?
26
- @bg2 = @bg if @bg2.nil?
27
- @ascii = options[:ascii]
28
- @ascii = true if @ascii.nil?
29
-
30
- @pixel = options[:pixel]
31
- @pixel = Theme.get(:border) if @pixel.nil?
32
-
33
- @table = options[:table]
34
- @table = [] if @table.nil?
35
-
36
- @cols = options[:cols]
37
- @cols = [] if @cols.nil?
38
-
39
- @highlight = options[:highlight]
40
- @highlight = 0 if @highlight.nil?
41
-
42
- @highlight_direction = options[:highlight_direction]
43
- @highlight_direction = :horizontal if @highlight_direction.nil?
44
-
45
- @x = options[:x]
46
- @x = 1 if @x.nil?
47
- @y = options[:y]
48
- @y = 1 if @y.nil?
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
- end
55
+ @meta = { :height => @table.size, :cols => 0 }
56
+ index_col_widths
65
57
 
66
- ##
67
- # Set data
68
- def set_table table
69
- @table = table
70
- index_col_widths
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
- # Delete line from table by index
85
- def delete line_id
86
- if !@table[line_id].nil?
87
- @table.delete_at(line_id)
88
- @height -= 1
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
- # highlight line
96
- def highlight line_id
97
- return highlight if line_id.nil?
98
- @highlight = line_id
99
- @reverse = false
100
- create
101
- end
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
- # create or recreate the table object
105
- def create
106
- obj = []
107
- if @header
108
- obj << ascii_table_line if @ascii
109
- _obj = []
110
- _obj << Pixel.new(@pixel.fg,@bg,"|") if @ascii
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
- obj << ascii_table_line if @ascii
127
- @table.each_with_index do |line, lindex|
128
- bg = @bg
129
- bg = @hover if lindex == @highlight and @highlight_direction == :horizontal
130
- _obj = []
131
- _obj << Pixel.new(@pixel.fg,bg,"|") if @ascii
132
- line.each_with_index do |col, index|
133
- fg = @fg
134
- fg = @cols[index][:color] if !@cols[index].nil? and !@cols[index][:color].nil?
135
-
136
- if @highlight_direction == :vertical
137
- if index == @highlight
138
- bg = @hover
139
- else
140
- bg = @bg
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
- chars = col.to_s.split("")
146
- _obj << Pixel.new(@pixel.fg,bg," ")
147
- max_chars = nil
148
- max_chars = @cols[index][:max_length]+1 if !@cols[index].nil? and !@cols[index][:max_length].nil?
149
- max_chars = @cols[index][:length]+1 if !@cols[index].nil? and !@cols[index][:length].nil?
150
- chars.each_with_index do |e, char_count|
151
- break if !max_chars.nil? and char_count >= max_chars
152
- _obj << Pixel.new(fg,bg,e)
153
- end
154
- (@meta[:max_widths][index]-chars.size+1).times do |i|
155
- _obj << Pixel.new(@pixel.fg,bg," ")
156
- end
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
- bg = @bg if @highlight_direction == :vertical
159
- _obj << Pixel.new(@pixel.fg,bg,"|") if @ascii
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 << _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
- # sort by column
169
- def sort col
170
- @table.sort! { |a,b| a[col] <=> b[col] }
171
- @table.reverse! if @reverse
172
- if @reverse == false
173
- @reverse = true
174
- else
175
- @reverse = false
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
- private
181
- def ascii_table_line
182
- _obj = []
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
- def index_col_widths
194
- @meta[:cols] = 0
195
- @meta[:max_widths] = {}
196
- @table.each do |line|
197
- cols = 0
198
- if !line.nil?
199
- line.each_with_index do |col, index|
200
- @meta[:max_widths][index] = col.size if @meta[:max_widths][index].nil?
201
- @meta[:max_widths][index] = col.size if @meta[:max_widths][index] < col.size
202
- @meta[:max_widths][index] = @cols[index][:max_length] if !@cols.nil? and !@cols[index].nil? and !@cols[index][:max_length].nil?
203
- @meta[:max_widths][index] = @cols[index][:length] if !@cols.nil? and !@cols[index].nil? and !@cols[index][:length].nil?
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
@@ -1,112 +1,135 @@
1
- ## Textfield Object Class
2
- # Creates an Textfield element
3
- #
4
- # Methods:
5
- # :set_focus Set focus to this textfield
6
- # :write CHAR Write char to textfield (if has focus)
7
- # :set_text TEXT Set text
8
- # :get_text Returns text
9
- # :count Text count
10
- #
11
- # Attributes (all accessible):
12
- # :x MUST
13
- # :y MUST
14
- # :pixel - pixel from which colors get inherted
15
- # :allow - allowed chars
16
- # :password - boolean: is password field?
17
- # :focus - has focus?
18
- #
19
- class Textfield < BaseObject
20
- attr_accessor :focus, :password, :border, :allow
21
-
22
- def initialize options
23
- @width = 14
24
- @width = options[:width] if !options[:width].nil?
25
-
26
- @pixel = options[:pixel]
27
- @pixel = Theme.get(:border) if @pixel.nil?
28
-
29
- @x = options[:x]
30
- @x = 1 if @x.nil?
31
- @y = options[:y]
32
- @y = 1 if @y.nil?
33
-
34
- @allow = (0..9).to_a + ('a'..'z').to_a + ('A'..'Z').to_a + [" ", ":", "<", ">", "|", "#",
35
- "@", "*", ",", "!", "?", ".", "-", "_", "=", "[", "]", "(", ")", "{", "}", ";"]
36
- @allow = options[:allow] if !options[:allow].nil?
37
-
38
- @height = 1
39
-
40
- @focus = false
41
- @password = false
42
- @password = true if options[:password] == true
43
-
44
- @text = ""
45
-
46
- create
47
- end
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
- # set focus
51
- def set_focus
52
- @focus = true
53
- Ansi.position @x, @y
54
- end
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
- # count
58
- def count
59
- @text.size
60
- end
63
+ ##
64
+ # take focus
65
+ def take_focus
66
+ @focus = false
67
+ create
68
+ end
61
69
 
62
- ##
63
- # write to textfield
64
- def write char
65
- if @focus
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
- # set text
74
- def set_text text
75
- @text = text
76
- create
77
- end
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
- # get text
81
- def get_text
82
- @text
83
- end
86
+ ##
87
+ # set text
88
+ def set_text text
89
+ @text = text
90
+ create
91
+ end
84
92
 
85
- ##
86
- # create or recreate the table object
87
- def create
88
- obj = []
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
- if password
97
- _text = text
98
- text = ""
99
- _text.size.times do |i|
100
- text += "*"
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
- text.to_s.split("").each do |t|
105
- _obj << Pixel.new(@pixel.fg, @pixel.bg, t)
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