dialogbind 0.9.1

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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/dialogbind.rb +204 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4c26d77179c387c148e245aaea10170e798a4b2dfa68e2813d99812b045f8683
4
+ data.tar.gz: f4a9a8ef1be6f577f335443215fc130b3b5a8424d96da8e685c8baa28abc909b
5
+ SHA512:
6
+ metadata.gz: ccf3e2f001e744556469cbfdf5bb209594ce6239647153b2e2274f74e441c56700e6585aefbcd18cdfd5007602f433b82710b015247fcd7d3b8cd2f1bd8a218d
7
+ data.tar.gz: bca641aa1a374afd1fc484db342f456cfc215e670373f924b65e37adc784a6e13459f6164c569bfd94b07b5de1e559d3f10bc132e33b173e9384f93a55e08802
data/lib/dialogbind.rb ADDED
@@ -0,0 +1,204 @@
1
+ #!/usr/bin/env ruby
2
+ # DialogBind - a simple library wrapping around message box displaying
3
+ # tools on Linux (xmessage and zenity) and macOS
4
+ #
5
+ # Copyright (C) Tim K 2018-2019 <timprogrammer@rambler.ru>.
6
+ # Licensed under MIT License.
7
+
8
+ $dialogbind_macos_script_cmd = ''
9
+ $dialogbind_version = '0.9.1'
10
+
11
+ def zenity(arg)
12
+ args_total = ''
13
+ arg.keys.each do |key|
14
+ if key[-1] == '%' then
15
+ key = key.sub('%', '')
16
+ end
17
+ if arg[key] == nil then
18
+ args_total += '--' + key
19
+ elsif arg[key].instance_of? Array then
20
+ arg[key].each do |nested_key|
21
+ if nested_key != nil then
22
+ args_total += "'" + nested_key.to_s.gsub("'", "") + "' "
23
+ end
24
+ end
25
+ else
26
+ args_total += '--' + key + "='" + arg[key].to_s.gsub("'", '"') + "'"
27
+ end
28
+ args_total += ' '
29
+ end
30
+ return system('zenity ' + args_total)
31
+ end
32
+
33
+ def xmessage(arg, buttons={ 'OK' => 0 }, file=false)
34
+ build_cmd = 'xmessage -center -buttons "'
35
+ first = true
36
+ buttons.keys.each do |button|
37
+ if first then
38
+ first = false
39
+ else
40
+ build_cmd += ','
41
+ end
42
+ build_cmd += button.gsub('"', '') + ':' + buttons[button].to_s
43
+ end
44
+ build_cmd += '" '
45
+ if file then
46
+ build_cmd += '-file '
47
+ end
48
+ build_cmd += '"' + arg.gsub('"', "'").gsub('!', '') + '"'
49
+ return system(build_cmd)
50
+ end
51
+
52
+ def macdialog(text, buttons=['OK'], type='dialog', error=false, dryrun=false)
53
+ text_fixed = text.gsub("!", "").gsub("'", '').gsub('"', '').gsub('$', '')
54
+ cmd = "osascript -e 'tell app \"System Events\" to display " + type + ' "' + text_fixed + '"'
55
+ if type != 'notification' then
56
+ cmd += ' buttons ' + buttons.to_s.gsub('[', '{').gsub(']', '}')
57
+ else
58
+ cmd += ' with title "' + File.basename($0) + '"'
59
+ end
60
+ if error then
61
+ cmd += ' with icon caution'
62
+ end
63
+ cmd += "'"
64
+ $dialogbind_macos_script_cmd = cmd
65
+ if dryrun == false then
66
+ return system(cmd + ' > /dev/null')
67
+ end
68
+ return false
69
+ end
70
+
71
+ $dialogbind_available_backends = [ 'xmessage', 'zenity', 'macos' ]
72
+ $dialogbind_dialog_backend = 'xmessage'
73
+
74
+ if system('command -v zenity > /dev/null 2>&1') then
75
+ $dialogbind_dialog_backend = 'zenity'
76
+ elsif `uname`.gsub("\n", "") == 'Darwin' then
77
+ $dialogbind_dialog_backend = 'macos'
78
+ end
79
+
80
+ if ENV.keys.include? 'DIALOGBIND_BACKEND' then
81
+ $dialogbind_dialog_backend = ENV['DIALOGBIND_BACKEND']
82
+ end
83
+ if $dialogbind_available_backends.include?($dialogbind_dialog_backend) == false then
84
+ raise 'Dialog backend "' + $dialogbind_dialog_backend + '" is not available. Available frontends: ' + $dialogbind_available_backends.join(', ')
85
+ end
86
+
87
+ def guiputs(text, title='DialogBind')
88
+ if $dialogbind_dialog_backend == 'xmessage' then
89
+ return xmessage(text, { 'OK' => 0 })
90
+ elsif $dialogbind_dialog_backend == 'zenity' then
91
+ return zenity({ 'info' => nil, 'title' => title, 'text' => text })
92
+ elsif $dialogbind_dialog_backend == 'macos' then
93
+ return macdialog(text)
94
+ else
95
+ puts title + ': ' + text
96
+ return true
97
+ end
98
+ return false
99
+ end
100
+
101
+ def guiyesno(text, title='DialogBind')
102
+ if $dialogbind_dialog_backend == 'xmessage' then
103
+ return xmessage(text, { 'Yes' => 0, 'No' => 1})
104
+ elsif $dialogbind_dialog_backend == 'zenity' then
105
+ return zenity('question' => nil, 'title' => title, 'text' => text)
106
+ elsif $dialogbind_dialog_backend == 'macos' then
107
+ macdialog(text, [ 'Yes', 'No' ], 'dialog', false, true)
108
+ output = `#{$dialogbind_macos_script_cmd}`.gsub("\n", "")
109
+ if output == nil || output.include?(':') == false then
110
+ return false
111
+ end
112
+ if output.split(':')[1].downcase == 'yes' then
113
+ return true
114
+ end
115
+ else
116
+ raise 'The selected backend does not support question message boxes.'
117
+ end
118
+ return false
119
+ end
120
+
121
+ def guierror(text, title='DialogBind')
122
+ if $dialogbind_dialog_backend == 'xmessage' then
123
+ return xmessage('ERROR. ' + text, { 'OK' => 0 })
124
+ elsif $dialogbind_dialog_backend == 'zenity' then
125
+ return zenity('error' => nil, 'title' => title, 'text' => text)
126
+ elsif $dialogbind_dialog_backend == 'macos' then
127
+ return macdialog(text, [ 'OK' ], 'dialog', true)
128
+ else
129
+ raise 'The selected backend does not support question message boxes.'
130
+ end
131
+ return false
132
+ end
133
+
134
+ def guiprogress(text='Please wait...', title='DialogBind')
135
+ if $dialogbind_dialog_backend == 'xmessage' then
136
+ return xmessage(text, { })
137
+ elsif $dialogbind_dialog_backend == 'zenity' then
138
+ return zenity({ 'progress' => nil, 'title' => title, 'text' => text, 'no-cancel' => nil, 'percentage' => 2, 'pulsate' => nil })
139
+ elsif $dialogbind_dialog_backend == 'macos' then
140
+ return macdialog(text, [], 'notification', false)
141
+ else
142
+ raise 'The selected backend does not support progress message boxes.'
143
+ return false
144
+ end
145
+ return false
146
+ end
147
+
148
+ def guilicense(file, title='DialogBind')
149
+ if File.exists?(file) == false then
150
+ guierror('File "' + file + '" does not exist.', title)
151
+ return false
152
+ end
153
+ if $dialogbind_dialog_backend == 'xmessage' then
154
+ return xmessage(file, { 'Accept' => 0, 'Decline' => 1 }, true)
155
+ elsif $dialogbind_dialog_backend == 'zenity' then
156
+ return zenity({ 'text-info' => nil, 'title' => title, 'filename' => file, 'checkbox' => 'I have read and accepted the terms of the license agreement.' })
157
+ else
158
+ raise 'The selected backend does not support license message boxes.'
159
+ return false
160
+ end
161
+ return false
162
+ end
163
+
164
+ def entry2buttonshash(entries)
165
+ hash = {}
166
+ count = 0
167
+ entries.each do |entry|
168
+ hash[entry] = count
169
+ count += 1
170
+ end
171
+ return hash
172
+ end
173
+
174
+ def guiselect(entries, text='Choose one of the items below:', title='DialogBind')
175
+ if entries.length > 2 then
176
+ raise 'More than 2 entries for guiselect are not supported by xmessage.'
177
+ end
178
+ if $dialogbind_dialog_backend == 'xmessage' then
179
+ if xmessage(text, entry2buttonshash(entries)) then
180
+ return entries[0]
181
+ else
182
+ return entries[1]
183
+ end
184
+ elsif $dialogbind_dialog_backend == 'zenity' then
185
+ array_of_items = [0, entries[0], nil, nil]
186
+ if entries.length > 1 then
187
+ array_of_items[2] = 1
188
+ array_of_items[3] = entries[1]
189
+ end
190
+ if zenity({'list' => nil, 'radiolist' => nil, 'text' => text, 'print-column' => 'ALL', 'column' => '#', 'column%' => 'Items', '' => array_of_items, ' > /tmp/zenity.sock 2>/dev/null' => nil }) then
191
+ return File.read('/tmp/zenity.sock').gsub("\n", "")
192
+ else
193
+ return nil
194
+ end
195
+ elsif $dialogbind_dialog_backend == 'macos' then
196
+ macdialog(text, entries, 'dialog', false, true)
197
+ output = `#{$dialogbind_macos_script_cmd}`.gsub("\n", "")
198
+ if output == nil || output.include?(':') == false then
199
+ return nil
200
+ end
201
+ return output.split(':')[1]
202
+ end
203
+ return nil
204
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dialogbind
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - Tim K
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - timprogrammer@rambler.ru
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/dialogbind.rb
21
+ homepage: https://gitlab.com/timkoi/dialogbind
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.0
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.0.4
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: DialogBind provides a Ruby API that wraps around Linux and macOS message
44
+ box-generating tools.
45
+ test_files: []