open 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of open might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/README.md +181 -0
- data/bin/open +7 -0
- data/bin/open_in_browser +7 -0
- data/bin/open_these_files +7 -0
- data/bin/open_with_delay +7 -0
- data/doc/README.gen +154 -0
- data/lib/open/base/base.rb +268 -0
- data/lib/open/constants/constants.rb +105 -0
- data/lib/open/in_browser/in_browser.rb +390 -0
- data/lib/open/in_editor/in_editor.rb +327 -0
- data/lib/open/last/last.rb +143 -0
- data/lib/open/last_url/last_url.rb +146 -0
- data/lib/open/nano_open/nano_open.rb +64 -0
- data/lib/open/open.rb +905 -0
- data/lib/open/project/project.rb +45 -0
- data/lib/open/requires/require_the_project.rb +15 -0
- data/lib/open/these_files/these_files.rb +116 -0
- data/lib/open/toplevel_code/toplevel_code.rb +104 -0
- data/lib/open/version/version.rb +19 -0
- data/lib/open/with_delay/with_delay.rb +261 -0
- data/lib/open/yaml/use_this_browser.yml +1 -0
- data/lib/open/yaml/use_this_editor.yml +1 -0
- data/lib/open.rb +5 -0
- data/open.gemspec +36 -0
- data/test/testing_open.rb +35 -0
- metadata +78 -0
@@ -0,0 +1,105 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# require 'open/constants/constants.rb'
|
6
|
+
# =========================================================================== #
|
7
|
+
module Open
|
8
|
+
|
9
|
+
require 'yaml'
|
10
|
+
require 'open/project/project.rb'
|
11
|
+
|
12
|
+
# ========================================================================= #
|
13
|
+
# === IN_BACKGROUND
|
14
|
+
# ========================================================================= #
|
15
|
+
IN_BACKGROUND = ' &'
|
16
|
+
|
17
|
+
# ========================================================================= #
|
18
|
+
# === HOME_DIRECTORY_OF_USER_X
|
19
|
+
#
|
20
|
+
# This constant is only useful on my home system.
|
21
|
+
# ========================================================================= #
|
22
|
+
HOME_DIRECTORY_OF_USER_X = '/home/x/'
|
23
|
+
|
24
|
+
# ========================================================================= #
|
25
|
+
# === RUBY_SRC
|
26
|
+
# ========================================================================= #
|
27
|
+
RUBY_SRC = "#{HOME_DIRECTORY_OF_USER_X}programming/ruby/src/"
|
28
|
+
|
29
|
+
# ========================================================================= #
|
30
|
+
# === PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME
|
31
|
+
# ========================================================================= #
|
32
|
+
PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME =
|
33
|
+
"#{HOME_DIRECTORY_OF_USER_X}programming/"
|
34
|
+
|
35
|
+
# ========================================================================= #
|
36
|
+
# === USERFIND
|
37
|
+
# ========================================================================= #
|
38
|
+
USERFIND = "find #{HOME_DIRECTORY_OF_USER_X}data -name"
|
39
|
+
|
40
|
+
# ========================================================================= #
|
41
|
+
# === MY_DATA
|
42
|
+
#
|
43
|
+
# This is functionality equivalent to the String '/home/x/data/'.
|
44
|
+
# ========================================================================= #
|
45
|
+
MY_DATA = "#{HOME_DIRECTORY_OF_USER_X}data/"
|
46
|
+
DATA_DIRECTORY_AT_HOME = MY_DATA # === DATA_DIRECTORY_AT_HOME
|
47
|
+
|
48
|
+
# ========================================================================= #
|
49
|
+
# === N_DELAY
|
50
|
+
#
|
51
|
+
# This delay is specifically used for delaying before batch-opening
|
52
|
+
# files via the Open.in_editor() functionality.
|
53
|
+
# ========================================================================= #
|
54
|
+
N_DELAY = 0.48
|
55
|
+
|
56
|
+
# ========================================================================= #
|
57
|
+
# === LOCATION_OF_BROWSER_YAML_FILE
|
58
|
+
#
|
59
|
+
# Here we must define where we store the location for our browser.
|
60
|
+
#
|
61
|
+
# That file will tell us which browser to use.
|
62
|
+
#
|
63
|
+
# cat $USERS_X/data/PC/yaml/browser.yml
|
64
|
+
#
|
65
|
+
# Most users will not have this file, so the code will have to remain
|
66
|
+
# flexible in this regard.
|
67
|
+
# ========================================================================= #
|
68
|
+
LOCATION_OF_BROWSER_YAML_FILE =
|
69
|
+
"#{project_yaml_directory?}use_this_browser.yml"
|
70
|
+
|
71
|
+
# ========================================================================= #
|
72
|
+
# === LOCATION_OF_EDITOR_YAML_FILE
|
73
|
+
# ========================================================================= #
|
74
|
+
LOCATION_OF_EDITOR_YAML_FILE =
|
75
|
+
"#{project_yaml_directory?}use_this_editor.yml"
|
76
|
+
|
77
|
+
# ========================================================================= #
|
78
|
+
# === USE_THIS_EDITOR
|
79
|
+
#
|
80
|
+
# This variant is hardcoded, but there are class-methods that can be used
|
81
|
+
# to overrule the setting.
|
82
|
+
# ========================================================================= #
|
83
|
+
if File.exist?(LOCATION_OF_EDITOR_YAML_FILE)
|
84
|
+
USE_THIS_EDITOR = YAML.load_file(LOCATION_OF_EDITOR_YAML_FILE)
|
85
|
+
else # else use a hardcoded default
|
86
|
+
USE_THIS_EDITOR = 'bluefish' # geany
|
87
|
+
end
|
88
|
+
|
89
|
+
# ========================================================================= #
|
90
|
+
# === USE_THIS_BROWSER
|
91
|
+
# ========================================================================= #
|
92
|
+
if File.exist?(LOCATION_OF_BROWSER_YAML_FILE)
|
93
|
+
USE_THIS_BROWSER = YAML.load_file(LOCATION_OF_BROWSER_YAML_FILE)
|
94
|
+
else # else use a hardcoded default
|
95
|
+
USE_THIS_BROWSER = 'firefox' # palemoon or chrome may be options too
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
if __FILE__ == $PROGRAM_NAME
|
101
|
+
puts Open::MY_DATA
|
102
|
+
puts Open::LOCATION_OF_BROWSER_YAML_FILE
|
103
|
+
puts Open::USE_THIS_EDITOR
|
104
|
+
puts Open::USE_THIS_BROWSER
|
105
|
+
end
|
@@ -0,0 +1,390 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# === Open::InBrowser
|
6
|
+
#
|
7
|
+
# This class can be used to open something in the browser.
|
8
|
+
#
|
9
|
+
# The correct browser to use is, by default, the one that is stored in
|
10
|
+
# the yaml file called 'use_this_browser.yml'. This can, evidently,
|
11
|
+
# only work if this file exists and can be found.
|
12
|
+
#
|
13
|
+
# If it is a local file then the absolute path will be used, determined
|
14
|
+
# via File.absolute_path().
|
15
|
+
#
|
16
|
+
# Usage example for this class:
|
17
|
+
#
|
18
|
+
# require 'open'
|
19
|
+
# Open::InBrowser.new(ARGV)
|
20
|
+
#
|
21
|
+
# =========================================================================== #
|
22
|
+
# To open a remote webpage in firefox only, do:
|
23
|
+
#
|
24
|
+
# use_this_browser = 'firefox -new-tab'
|
25
|
+
# _ = use_this_browser
|
26
|
+
# _ << ' "'+url.to_s+'"'
|
27
|
+
#
|
28
|
+
# =========================================================================== #
|
29
|
+
# require 'open/in_browser/in_browser.rb'
|
30
|
+
# =========================================================================== #
|
31
|
+
require 'open/base/base.rb'
|
32
|
+
|
33
|
+
module Open
|
34
|
+
|
35
|
+
class InBrowser < Base # === Open::InBrowser
|
36
|
+
|
37
|
+
# ========================================================================= #
|
38
|
+
# === initialize
|
39
|
+
#
|
40
|
+
# The first argument becomes the target URL that will be opened.
|
41
|
+
# ========================================================================= #
|
42
|
+
def initialize(
|
43
|
+
target_url = '',
|
44
|
+
run_already = true
|
45
|
+
)
|
46
|
+
register_sigint
|
47
|
+
reset
|
48
|
+
set_target_url(
|
49
|
+
target_url
|
50
|
+
)
|
51
|
+
# ======================================================================= #
|
52
|
+
# === Handle blocks given next
|
53
|
+
# ======================================================================= #
|
54
|
+
if block_given?
|
55
|
+
yielded = yield
|
56
|
+
case yielded
|
57
|
+
# ===================================================================== #
|
58
|
+
# === :open_in_background
|
59
|
+
# ===================================================================== #
|
60
|
+
when :open_in_background
|
61
|
+
@open_in_background = true
|
62
|
+
# ===================================================================== #
|
63
|
+
# === be_silent
|
64
|
+
# ===================================================================== #
|
65
|
+
when :be_silent,
|
66
|
+
:be_quiet
|
67
|
+
@show_the_sys_command_that_is_used = false
|
68
|
+
else
|
69
|
+
# =================================================================== #
|
70
|
+
# Handle Hashes next:
|
71
|
+
# =================================================================== #
|
72
|
+
if yielded.is_a? Hash
|
73
|
+
# ================================================================= #
|
74
|
+
# === :use_this_browser
|
75
|
+
#
|
76
|
+
# Usage example for this clause:
|
77
|
+
#
|
78
|
+
# Open.in_browser(remote_URL) {{ use_this_browser: :firefox }}
|
79
|
+
#
|
80
|
+
# ================================================================= #
|
81
|
+
if yielded.has_key? :use_this_browser
|
82
|
+
set_use_this_browser(yielded[:use_this_browser])
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
run if run_already
|
88
|
+
end
|
89
|
+
|
90
|
+
# ========================================================================= #
|
91
|
+
# === reset (reset tag)
|
92
|
+
# ========================================================================= #
|
93
|
+
def reset
|
94
|
+
super()
|
95
|
+
# ======================================================================= #
|
96
|
+
# === @append_this_string
|
97
|
+
# ======================================================================= #
|
98
|
+
@append_this_string = ''.dup
|
99
|
+
# ======================================================================= #
|
100
|
+
# === @show_the_sys_command_that_is_used
|
101
|
+
# ======================================================================= #
|
102
|
+
@show_the_sys_command_that_is_used = true
|
103
|
+
# ======================================================================= #
|
104
|
+
# === @use_this_browser
|
105
|
+
# ======================================================================= #
|
106
|
+
@use_this_browser = ::Open.use_which_browser?
|
107
|
+
# ======================================================================= #
|
108
|
+
# === @open_in_background
|
109
|
+
# ======================================================================= #
|
110
|
+
@open_in_background = false
|
111
|
+
end
|
112
|
+
|
113
|
+
# ========================================================================= #
|
114
|
+
# === check_for_intralink
|
115
|
+
#
|
116
|
+
# The second optional argument specifies the token to use for replacing
|
117
|
+
# empty spaces, if we have passed an Array as argument.
|
118
|
+
# ========================================================================= #
|
119
|
+
def check_for_intralink(
|
120
|
+
i = nil,
|
121
|
+
use_this_token_to_replace_empty_spaces = '_'
|
122
|
+
)
|
123
|
+
if i
|
124
|
+
if i.is_a? Array
|
125
|
+
i = i.join(' ').tr(' ', use_this_token_to_replace_empty_spaces)
|
126
|
+
end
|
127
|
+
i = i.to_s # Ensure that we have a String at this point.
|
128
|
+
unless i.empty?
|
129
|
+
@append_this_string << i
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# ========================================================================= #
|
135
|
+
# === set_target_url
|
136
|
+
#
|
137
|
+
# We prefer this to be an Array.
|
138
|
+
# ========================================================================= #
|
139
|
+
def set_target_url(i = '')
|
140
|
+
if i.is_a? Hash
|
141
|
+
# ===================================================================== #
|
142
|
+
# === :with_localhost
|
143
|
+
#
|
144
|
+
# Example for this entry point:
|
145
|
+
#
|
146
|
+
# Open::InBrowser.new(with_localhost: PORT_TO_USE)
|
147
|
+
#
|
148
|
+
# ===================================================================== #
|
149
|
+
if i.has_key? :with_localhost
|
150
|
+
i = "http://localhost:#{i.delete(:with_localhost)}/"
|
151
|
+
# ===================================================================== #
|
152
|
+
# === :port
|
153
|
+
#
|
154
|
+
# Example for this entry point:
|
155
|
+
#
|
156
|
+
# Open::InBrowser[port: 8080]
|
157
|
+
#
|
158
|
+
# ===================================================================== #
|
159
|
+
elsif i.has_key? :port
|
160
|
+
i = "http://localhost:#{i.delete(:port)}/"
|
161
|
+
end
|
162
|
+
end
|
163
|
+
i = [i] unless i.is_a? Array
|
164
|
+
i.flatten! if i.is_a? Array
|
165
|
+
i.map! {|entry|
|
166
|
+
case entry
|
167
|
+
when :default_sinatra_port
|
168
|
+
entry = 'http://localhost:4567/'
|
169
|
+
end
|
170
|
+
entry = beautiful_url(entry).dup
|
171
|
+
if File.exist?(entry)
|
172
|
+
entry = File.absolute_path(entry)
|
173
|
+
end
|
174
|
+
if entry.start_with?('/home/x/') and !entry.include?('images/')
|
175
|
+
entry.sub!(/\/home\/x\//,'http://localhost/')
|
176
|
+
end
|
177
|
+
# ===================================================================== #
|
178
|
+
# Perform some sanitize-operations next.
|
179
|
+
# ===================================================================== #
|
180
|
+
if entry and
|
181
|
+
((entry.include?("'") and !entry.include?('"')) or
|
182
|
+
entry.include?(' ')
|
183
|
+
)
|
184
|
+
entry = '"'+entry+'"'
|
185
|
+
elsif entry.include?(';') or entry.include?('(')
|
186
|
+
entry = '"'+entry+'"'
|
187
|
+
end
|
188
|
+
entry
|
189
|
+
}
|
190
|
+
@target_url = i
|
191
|
+
end
|
192
|
+
|
193
|
+
# ========================================================================= #
|
194
|
+
# === set_use_this_browser
|
195
|
+
# ========================================================================= #
|
196
|
+
def set_use_this_browser(
|
197
|
+
i = ::Open.use_which_browser?
|
198
|
+
)
|
199
|
+
@use_this_browser = i.to_s
|
200
|
+
end
|
201
|
+
|
202
|
+
# ========================================================================= #
|
203
|
+
# === open_in_browser
|
204
|
+
#
|
205
|
+
# This is the part that does the actual file-opening via the browser.
|
206
|
+
# ========================================================================= #
|
207
|
+
def open_in_browser(
|
208
|
+
optional_append_this_string = @append_this_string
|
209
|
+
)
|
210
|
+
_ = LOCATION_OF_BROWSER_YAML_FILE
|
211
|
+
browser = @use_this_browser # Use the generic browser by default.
|
212
|
+
# ======================================================================= #
|
213
|
+
# We must check first whether the file exists or not.
|
214
|
+
# ======================================================================= #
|
215
|
+
if File.exist? _
|
216
|
+
browser = YAML.load_file(_).strip.dup # This variable holds which browser to use.
|
217
|
+
if is_on_windows?
|
218
|
+
case browser
|
219
|
+
when 'palemoon'
|
220
|
+
browser.prepend(
|
221
|
+
"C:\\Program Files\\Pale Moon\\palemoon.exe"
|
222
|
+
)
|
223
|
+
end
|
224
|
+
end
|
225
|
+
# ===================================================================== #
|
226
|
+
# Different browsers have different ways to open a new tab.
|
227
|
+
# ===================================================================== #
|
228
|
+
if browser.include? 'opera' # Perform some sanitizing in this case.
|
229
|
+
browser << ' -newtab'
|
230
|
+
# =================================================================== #
|
231
|
+
# Or alternatively:
|
232
|
+
#
|
233
|
+
# opera --remote 'openURL(<url>, new-page)'
|
234
|
+
#
|
235
|
+
# =================================================================== #
|
236
|
+
elsif browser.include? 'firefox'
|
237
|
+
browser << ' -new-tab'
|
238
|
+
end
|
239
|
+
@target_url.each {|entry|
|
240
|
+
entry = entry.to_s.dup # .dup to avoid that it may be frozen.
|
241
|
+
if optional_append_this_string.size > 0
|
242
|
+
# ================================================================= #
|
243
|
+
# We prepend the '#' character, unless it starts with the
|
244
|
+
# '?' character.
|
245
|
+
# ================================================================= #
|
246
|
+
unless optional_append_this_string.start_with? '?'
|
247
|
+
optional_append_this_string.prepend '#'
|
248
|
+
end
|
249
|
+
entry << optional_append_this_string
|
250
|
+
end
|
251
|
+
# =================================================================== #
|
252
|
+
# Next, specifically handle the situation on my home system, when
|
253
|
+
# we are in a directory that includes a substring such as
|
254
|
+
# '/home/x/data/' and if the target file name ends with '.cgi'.
|
255
|
+
# =================================================================== #
|
256
|
+
if is_on_roebe? and
|
257
|
+
entry.end_with?('.cgi') and
|
258
|
+
!entry.start_with?('http') and
|
259
|
+
!entry.start_with?('http://localhost/')
|
260
|
+
if return_pwd.include?(DATA_DIRECTORY_AT_HOME)
|
261
|
+
# ================================================================= #
|
262
|
+
# We need a target URL such as:
|
263
|
+
# http://localhost/DATA/games/AITD/AITD.cgi
|
264
|
+
# in this case.
|
265
|
+
# ================================================================= #
|
266
|
+
unless entry.include? DATA_DIRECTORY_AT_HOME
|
267
|
+
entry.prepend(return_pwd)
|
268
|
+
end
|
269
|
+
entry.squeeze!('/')
|
270
|
+
quoted_regexp = Regexp.quote(DATA_DIRECTORY_AT_HOME)
|
271
|
+
entry.sub!(quoted_regexp, 'http://localhost/data/')
|
272
|
+
elsif return_pwd.include?(PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME)
|
273
|
+
unless entry.include? PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME
|
274
|
+
entry.prepend(return_pwd)
|
275
|
+
end
|
276
|
+
entry.squeeze!('/')
|
277
|
+
quoted_regexp = Regexp.quote(PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME)
|
278
|
+
entry.sub!(quoted_regexp, 'http://localhost/programming/')
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
if @open_in_background
|
283
|
+
entry << '&'
|
284
|
+
end
|
285
|
+
if entry.include? '&'
|
286
|
+
entry = '"'+entry+'"' unless entry.start_with? '"'
|
287
|
+
end
|
288
|
+
this_command = "#{browser} #{entry}"
|
289
|
+
if @show_the_sys_command_that_is_used
|
290
|
+
e this_command
|
291
|
+
end
|
292
|
+
unless entry.empty?
|
293
|
+
# Finally run the system-command.
|
294
|
+
system(this_command) # Here we always output.
|
295
|
+
end
|
296
|
+
} unless @target_url.nil?
|
297
|
+
else
|
298
|
+
browser = 'firefox' # Else, default to this here as a generic fallback.
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
# ========================================================================= #
|
303
|
+
# === run (run tag)
|
304
|
+
# ========================================================================= #
|
305
|
+
def run
|
306
|
+
open_in_browser
|
307
|
+
end
|
308
|
+
|
309
|
+
# ========================================================================= #
|
310
|
+
# === Open::InBrowser[]
|
311
|
+
# ========================================================================= #
|
312
|
+
def self.[](i = ARGV)
|
313
|
+
new(i)
|
314
|
+
end; self.instance_eval { alias open_in_browser [] } # === Open::InBrowser.open_in_browser
|
315
|
+
|
316
|
+
end
|
317
|
+
|
318
|
+
# ========================================================================= #
|
319
|
+
# === Open.open_in_browser
|
320
|
+
#
|
321
|
+
# Note that this method needs to be somewhat flexible, as we can provide
|
322
|
+
# varied input such as:
|
323
|
+
#
|
324
|
+
# Open.open_in_browser(port: 8080)
|
325
|
+
# Open.in_browser(remote_URL) {{ use_this_browser: :firefox }}
|
326
|
+
#
|
327
|
+
# ========================================================================= #
|
328
|
+
def self.open_in_browser(
|
329
|
+
i = ARGV, &block
|
330
|
+
)
|
331
|
+
# ======================================================================= #
|
332
|
+
# === Handle blocks next
|
333
|
+
# ======================================================================= #
|
334
|
+
if block_given?
|
335
|
+
yielded = yield
|
336
|
+
# ===================================================================== #
|
337
|
+
# === Handle Hashes next
|
338
|
+
# ===================================================================== #
|
339
|
+
if yielded.is_a? Hash
|
340
|
+
# =================================================================== #
|
341
|
+
# === Handle Hashes past this point
|
342
|
+
# =================================================================== #
|
343
|
+
# =================================================================== #
|
344
|
+
# === :delay
|
345
|
+
# =================================================================== #
|
346
|
+
if yielded.has_key? :delay
|
347
|
+
_ = yielded.delete(:delay)
|
348
|
+
if _.is_a? String
|
349
|
+
_ = _.sub(/ seconds/,'')
|
350
|
+
_ = _.sub(/ second/,'').to_f if _.include? ' second'
|
351
|
+
_ = _.to_f # Need a Float.
|
352
|
+
end
|
353
|
+
sleep(_)
|
354
|
+
end
|
355
|
+
end
|
356
|
+
# else
|
357
|
+
end
|
358
|
+
::Open::InBrowser.new(i, &block)
|
359
|
+
end; self.instance_eval { alias in_browser open_in_browser } # === Open.in_browser
|
360
|
+
self.instance_eval { alias open open_in_browser } # === Open.open
|
361
|
+
|
362
|
+
end
|
363
|
+
|
364
|
+
# =========================================================================== #
|
365
|
+
# === open_in_browser
|
366
|
+
#
|
367
|
+
# This method will open an URL in firefox.
|
368
|
+
#
|
369
|
+
# Usage example in pure Ruby:
|
370
|
+
#
|
371
|
+
# require 'open'; open_in_browser 'google.at'
|
372
|
+
#
|
373
|
+
# =========================================================================== #
|
374
|
+
def open_in_browser(
|
375
|
+
url = ARGV,
|
376
|
+
optional_intralink = nil,
|
377
|
+
&block
|
378
|
+
)
|
379
|
+
# ========================================================================= #
|
380
|
+
# Next, open the class that we defined above in this file here.
|
381
|
+
# ========================================================================= #
|
382
|
+
_ = Open::InBrowser.new(url, false, &block)
|
383
|
+
_.check_for_intralink(optional_intralink)
|
384
|
+
_.run
|
385
|
+
end; alias oib open_in_browser # oib shortcut.
|
386
|
+
|
387
|
+
if __FILE__ == $PROGRAM_NAME
|
388
|
+
open_in_browser(ARGV.first)
|
389
|
+
end # inbrowser
|
390
|
+
# inbrowser https://distrowatch.com/dwres.php?resource=ratings&distro=antix
|