forked from langhorst/rdialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form2.rb
executable file
·177 lines (159 loc) · 4.65 KB
/
form2.rb
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env ruby
########################################################################
# Same as form1 but dialog will not go away unless all the fields are
# specified
########################################################################
require [File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'rdialog'].join('/')
class TestForm2
ME = File.basename($0)
if ENV['CHANGE_TITLE']
if ME =~ /(.+)\.rb$/
base = $1
puts "\033]0;mrdialog - #{base}\007"
end
end
def initialize
@hsh = {}
end
def show_warning
msg = ''
label = 'Username:'
if @hsh[label].length == 0
msg << "#{label} field is empty"
end
label = 'UID:'
if @hsh[label].length == 0
msg << "\n"
msg << "#{label} field is empty"
end
label = 'GID:'
if @hsh[label].length == 0
msg << "\n"
msg << "#{label} field is empty"
end
label = 'HOME:'
if @hsh[label].length == 0
msg << "\n"
msg << "#{label} field is empty"
end
dialog = RDialog.new
dialog.title = "ERROR"
dialog.clear = true
dialog.msgbox(msg, 10, 41)
end
def form_filled?
if @hsh['Username:'].length > 0 &&
@hsh['UID:'].length > 0 &&
@hsh['GID:'].length > 0 &&
@hsh['HOME:'].length > 0
return true
end
return false
end
def doit
user = ''
uid = ''
gid = ''
home = ENV["HOME"]
id = `id`.chomp
if id =~ /^uid=(\d+)\((.+)\)\sgid=(\d+)\(.*$/
uid = $1
user = $2
gid = $3
end
dialog = RDialog.new
dialog.clear = true
dialog.logger = Logger.new(ENV["HOME"] + "/dialog_" + ME + ".log")
text = <<EOF
In this example of form, if any field is empty a dialog
will POP show an error. The form will exit if Cancel
button is selectged or Esc key is pressed twice.
EOF
@hsh['Username:'] = user
@hsh['UID:'] = uid.to_s
@hsh['GID:'] = gid.to_s
@hsh['HOME:'] = home
flen = 60
form_data = Struct.new(:label, :ly, :lx, :item, :iy, :ix, :flen, :ilen)
# infinite loop. break out
# - if all the values of the form are filled in when OK button is pressed
# - if Esc button is pressed twice
loop do
items = []
label = "Username:"
data = form_data.new
data.label = label
data.ly = 1
data.lx = 1
data.item = @hsh[label]
data.iy = 1
data.ix = 10
data.flen = flen
data.ilen = 0
items.push(data.to_a)
data = form_data.new
label = "UID:"
data.label = label
data.ly = 2
data.lx = 1
data.item = @hsh[label]
data.iy = 2
data.ix = 10
data.flen = 0
data.ilen = 0
items.push(data.to_a)
data = form_data.new
label = "GID:"
data.label = label
data.ly = 3
data.lx = 1
data.item = @hsh[label]
data.iy =3
data.ix = 10
data.flen = flen
data.ilen = 0
items.push(data.to_a)
data = form_data.new
label = "HOME:"
data.label = label
data.ly = 4
data.lx = 1
data.item = @hsh[label]
data.iy = 4
data.ix = 10
data.flen = flen
data.ilen = 0
items.push(data.to_a)
dialog.title = "FORM"
@hsh = dialog.form(text, items, 20, 60, 0)
exit_code = dialog.exit_code
@hsh.each do |key, val|
puts "'#{key}' = #{val}"
end
puts "Exit code: #{exit_code}"
case exit_code
when dialog.dialog_cancel
puts "Username: #{@hsh["Username:"]}"
puts "CANCEL"
break
when dialog.dialog_esc
puts "Username: #{@hsh["Username:"]}"
puts "Escape.. exiting"
break
end
if !form_filled?
show_warning
else
puts "Resulting data:"
@hsh.each do |key, val|
puts "'#{key}' = #{val}"
end
break
end
end
end
end
if __FILE__ == $0
TestForm2.new.doit
end