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.

@@ -0,0 +1,327 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === Open::InEditor
6
+ #
7
+ # This class can specifically be used to open a local file via your
8
+ # favourite editor. This is admittedly more useful for GUI-based editors
9
+ # such as "geany" or "gedit" than, say, "vim" or "nano".
10
+ #
11
+ # If the file was not found, and BeautifulUrl is available, then it will
12
+ # be called, to see whether that leads to a file that can be found, and
13
+ # if it still can not be found than "dfind" is used, which is based on
14
+ # "locate / updatedb".
15
+ #
16
+ # Usage example:
17
+ #
18
+ # require 'open/in_editor/in_editor.rb'
19
+ # Open.in_editor(ARGV)
20
+ #
21
+ # =========================================================================== #
22
+ # require 'open/in_editor/in_editor.rb'
23
+ # =========================================================================== #
24
+ require 'open/base/base.rb'
25
+
26
+ module Open
27
+
28
+ class InEditor < Base # === Open::InEditor
29
+
30
+ # ========================================================================= #
31
+ # === initialize
32
+ #
33
+ # The first argument should be the name of the file that we wish to open.
34
+ # ========================================================================= #
35
+ def initialize(
36
+ i = nil,
37
+ run_already = true
38
+ )
39
+ register_sigint
40
+ reset
41
+ set_input(i)
42
+ # ======================================================================= #
43
+ # === Handle blocks next
44
+ # ======================================================================= #
45
+ if block_given?
46
+ yielded = yield
47
+ # ===================================================================== #
48
+ # === Handle Hashes next
49
+ # ===================================================================== #
50
+ if yielded.is_a? Hash
51
+ # =================================================================== #
52
+ # === :use_this_editor
53
+ # =================================================================== #
54
+ if yielded.has_key? :use_this_editor
55
+ set_editor(
56
+ yielded[:use_this_editor]
57
+ )
58
+ end
59
+ # =================================================================== #
60
+ # === :this_file
61
+ # =================================================================== #
62
+ if yielded.has_key? :this_file
63
+ _ = yielded[:this_file]
64
+ set_input(_)
65
+ end
66
+ end
67
+ end
68
+ run if run_already
69
+ end
70
+
71
+ # ========================================================================= #
72
+ # === reset (reset tag)
73
+ # ========================================================================= #
74
+ def reset
75
+ super()
76
+ # ======================================================================= #
77
+ # === @use_this_editor
78
+ # ======================================================================= #
79
+ @use_this_editor = use_which_editor?
80
+ end
81
+
82
+ # ========================================================================= #
83
+ # === use_this_editor?
84
+ # ========================================================================= #
85
+ def use_this_editor?
86
+ @use_this_editor
87
+ end; alias editor? use_this_editor? # === editor?
88
+
89
+ # ========================================================================= #
90
+ # === set_input
91
+ # ========================================================================= #
92
+ def set_input(i = '')
93
+ if i.is_a? String
94
+ i = [i]
95
+ end # At this point, we have an array
96
+ i = i.flatten if i.is_a? Array
97
+ i.map! {|entry|
98
+ # ===================================================================== #
99
+ # Next get rid of components past a '#' token.
100
+ # ===================================================================== #
101
+ entry = entry[0, entry.index('#')] if entry.include? '#'
102
+ # ===================================================================== #
103
+ # === Handle fake "Symbols" next:
104
+ # ===================================================================== #
105
+ if entry.start_with? ':' # If this is a "symbol" we assume to do a special action.
106
+ name = entry[1..-1]
107
+ base_dir = RUBY_SRC+name
108
+ if File.directory? base_dir
109
+ target = base_dir+'/lib/'+name+'/*.rb' # This is, sort of, the special action.
110
+ entry = Dir[target]
111
+ end
112
+ end
113
+ if entry.include? '#' # Then chop off. It is probably not necessary, though.
114
+ entry = entry[0..entry.index('#')]
115
+ end
116
+ if entry.start_with? 'file://'
117
+ entry[0, 'file://'.size] = ''
118
+ end
119
+ entry
120
+ } if i.is_a? Array
121
+ i = i.flatten if i.is_a? Array
122
+ @input = i # Must be an Array.
123
+ end
124
+
125
+ # ========================================================================= #
126
+ # === try_to_find_expanded_alias_for
127
+ #
128
+ # We delegate towards FindExpandedAlias
129
+ # ========================================================================= #
130
+ def try_to_find_expanded_alias_for(i)
131
+ result = false # Default.
132
+ if Object.const_defined?(:Roebe) and
133
+ Roebe.const_defined?(:FindExpandedAlias)
134
+ _ = ::Roebe::FindExpandedAlias[i]
135
+ result = sanitize(_)
136
+ end
137
+ if result.include? ' ' # For instance, when it includes a ' &'
138
+ result = result[0, result.index(' ')]
139
+ end if result
140
+ return result
141
+ end
142
+
143
+ # ========================================================================= #
144
+ # === beautiful_url
145
+ #
146
+ # Wrapper around BeautifulUrl.
147
+ # ========================================================================= #
148
+ def beautiful_url(i)
149
+ return i unless Object.const_defined? :BeautifulUrl
150
+ array = BeautifulUrl::BeautifulUrl.new(i, replace: true).array
151
+ return array
152
+ end
153
+
154
+ # ========================================================================= #
155
+ # === start_editor_in_background
156
+ # ========================================================================= #
157
+ def start_editor_in_background
158
+ system "#{@use_this_editor} &"
159
+ end
160
+
161
+ # ========================================================================= #
162
+ # === open_in_editor
163
+ #
164
+ # The optional second argument allows us to wait a little before
165
+ # opening something in the editor.
166
+ # ========================================================================= #
167
+ def open_in_editor(
168
+ i, optional_delay = nil
169
+ )
170
+ if i.is_a? Array
171
+ i.each {|entry|
172
+ open_in_editor(entry, optional_delay)
173
+ if @use_this_editor == 'bluefish'
174
+ sleep 0.5 if i.size > 3 # To prevent bluefish from acting silly.
175
+ end
176
+ }
177
+ else
178
+ i = i.to_s.dup # So to avoid frozen strings here.
179
+ if i.include? 'localhost'
180
+ i.gsub!(/^http\:\/\/localhost\//, MY_DATA+'/')
181
+ end
182
+ if i.end_with?(':') and !File.exist?(i)
183
+ i[-1,1] = ''
184
+ elsif i =~ /(:\d+)/ # Has a : and a number
185
+ i.gsub!(/#{$1.to_s.dup}/,'')
186
+ end
187
+ i = sanitize(i) if i.include? '$'
188
+ if i.count('/home') > 1
189
+ i.gsub!(/#{MY_DATA}#{MY_DATA}/, MY_DATA)
190
+ end
191
+ if i.start_with?('http:') and !File.exist?(i)
192
+ i.delete_prefix!('http:')
193
+ end
194
+ if File.exist? i
195
+ # =================================================================== #
196
+ # e 'Yes file exists here, we can thus open it.'
197
+ # =================================================================== #
198
+ esystem "#{editor?} #{i}"
199
+ else # Ok, the file does not exist at this point.
200
+ # First try a purl.
201
+ _ = BeautifulUrl::BeautifulUrl.new(i, replace: true)
202
+ if _.url_was_found
203
+ esystem editor?+' '+_.url_as_string
204
+ elsif Dir[i+'*'].size > 0 # Try to find matches
205
+ e 'Did not find '+sfile(i)+' but we may have found '+
206
+ 'at least one other entry.'
207
+ open_in_editor(Dir[i+'*'].reject {|entry| File.directory? entry })
208
+ elsif result = try_to_find_expanded_alias_for(i)
209
+ open_in_editor(result) if result
210
+ # ================================================================= #
211
+ # Now the string can include 'project_', in which case we
212
+ # will handle it in a special way.
213
+ # ================================================================= #
214
+ elsif i.include? 'project_'
215
+ i.gsub!(/project_/,'')
216
+ i << 'task'
217
+ open_in_editor(beautiful_url(i))
218
+ else # Ok we really did not find it.
219
+ cmd = USERFIND+' '+i
220
+ # e cmd # Whether we wish to output this command or not.
221
+ result = `#{cmd}`.chomp
222
+ if result.empty?
223
+ this_file_was_not_found(i)
224
+ else # else, we did indeed find it.
225
+ open_in_editor(result)
226
+ end
227
+ end
228
+ end
229
+ end
230
+ sleep(optional_delay) if optional_delay
231
+ end
232
+
233
+ # ========================================================================= #
234
+ # === set_editor
235
+ # ========================================================================= #
236
+ def set_editor(i = use_which_editor?)
237
+ @use_this_editor = i
238
+ end; alias editor= set_editor # === editor=
239
+
240
+ # ========================================================================= #
241
+ # === input?
242
+ # ========================================================================= #
243
+ def input?
244
+ @input
245
+ end
246
+
247
+ # ========================================================================= #
248
+ # === consider_opening_files
249
+ # ========================================================================= #
250
+ def consider_opening_files(
251
+ i = input?
252
+ )
253
+ if i.is_a? Array
254
+ i.flatten!
255
+ i.map! {|file|
256
+ file.chop! if file.end_with? ':'
257
+ file
258
+ }
259
+ if i.empty?
260
+ start_editor_in_background
261
+ else
262
+ optional_delay = 0
263
+ if i.size > 3
264
+ opn; e "More than 3 arguments were given, thus we will use "\
265
+ "a delay of #{simp(N_DELAY.to_s)} seconds."
266
+ optional_delay = N_DELAY
267
+ end
268
+ i.each {|entry|
269
+ open_in_editor(entry, optional_delay)
270
+ }
271
+ end
272
+ else
273
+ open_in_editor(i)
274
+ end
275
+ end
276
+
277
+ # ========================================================================= #
278
+ # === run (run tag)
279
+ # ========================================================================= #
280
+ def run
281
+ consider_opening_files
282
+ end
283
+
284
+ # ========================================================================= #
285
+ # === Open::InEditor[]
286
+ #
287
+ # This method can also pass blocks to the initializer.
288
+ # ========================================================================= #
289
+ def self.[](i, &block)
290
+ new(i, &block)
291
+ end
292
+
293
+ end
294
+
295
+ # =========================================================================== #
296
+ # === Open.in_editor
297
+ #
298
+ # Usage examples:
299
+ #
300
+ # Open.in_editor('/home/x/programming/ruby/src/open/open.gemspec')
301
+ #
302
+ # Or theb lock variant:
303
+ #
304
+ # Open.in_editor {{
305
+ # this_file: first_argument?,
306
+ # use_this_editor: :nano
307
+ # }}
308
+ #
309
+ # =========================================================================== #
310
+ def self.in_editor(
311
+ i = ARGV, &block
312
+ )
313
+ ::Open::InEditor.new(i, &block)
314
+ end
315
+
316
+ end
317
+
318
+ # =========================================================================== #
319
+ # === open_in_editor
320
+ # =========================================================================== #
321
+ def open_in_editor(i, &block)
322
+ Open.in_editor(i, &block)
323
+ end
324
+
325
+ if __FILE__ == $PROGRAM_NAME
326
+ Open::InEditor.new(ARGV)
327
+ end # openineditor rorg
@@ -0,0 +1,143 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === Open::Last
6
+ #
7
+ # This class can be used to open the most recent program.
8
+ #
9
+ # Usage example:
10
+ #
11
+ # Open::Last.new(ARGV)
12
+ #
13
+ # =========================================================================== #
14
+ # require 'open/last/last.rb'
15
+ # =========================================================================== #
16
+ require 'open/base/base.rb'
17
+
18
+ module Open
19
+
20
+ class Last < Base # === Open::Last
21
+
22
+ require 'open/in_editor/in_editor.rb'
23
+
24
+ # ========================================================================= #
25
+ # === NAMESPACE
26
+ # ========================================================================= #
27
+ NAMESPACE = inspect
28
+
29
+ # ========================================================================= #
30
+ # === BY_DEFAULT_OPEN_N_FILES
31
+ # ========================================================================= #
32
+ BY_DEFAULT_OPEN_N_FILES = 1
33
+
34
+ # ========================================================================= #
35
+ # === initialize
36
+ # ========================================================================= #
37
+ def initialize(
38
+ i = BY_DEFAULT_OPEN_N_FILES,
39
+ run_already = true
40
+ )
41
+ reset
42
+ set_input(i)
43
+ run if run_already
44
+ end
45
+
46
+ # ========================================================================= #
47
+ # === reset (reset tag)
48
+ # ========================================================================= #
49
+ def reset
50
+ super()
51
+ # ======================================================================= #
52
+ # === @namespace
53
+ # ======================================================================= #
54
+ @namespace = NAMESPACE
55
+ end
56
+
57
+ # ========================================================================= #
58
+ # === set_input
59
+ # ========================================================================= #
60
+ def set_input(
61
+ i = BY_DEFAULT_OPEN_N_FILES
62
+ )
63
+ i = i.first if i.is_a? Array
64
+ i = BY_DEFAULT_OPEN_N_FILES if i.nil?
65
+ i = i.to_s.dup
66
+ @input = i
67
+ end
68
+
69
+ # ========================================================================= #
70
+ # === open_n_files?
71
+ # ========================================================================= #
72
+ def open_n_files?
73
+ @input # To-go for variable, for now at the last.
74
+ end
75
+
76
+ # ========================================================================= #
77
+ # === input?
78
+ # ========================================================================= #
79
+ def input?
80
+ @input
81
+ end
82
+
83
+ # ========================================================================= #
84
+ # === open_these_target_files
85
+ # ========================================================================= #
86
+ def open_these_target_files(
87
+ i = @target_files
88
+ )
89
+ if i.is_a? Array
90
+ i.each {|entry| open_these_target_files(entry) }
91
+ else
92
+ opnn; e "Now opening `#{sfile(i)}`."
93
+ Open.in_editor(i)
94
+ end
95
+ end
96
+
97
+ # ========================================================================= #
98
+ # === run (run tag)
99
+ # ========================================================================= #
100
+ def run
101
+ # ======================================================================= #
102
+ # Sort by date next. Could also use a pure ruby implementation there.
103
+ # ======================================================================= #
104
+ result = `ls -tl --quoting-style=literal`.split("\n")
105
+ result.reject! {|entry| entry.include? 'total' }
106
+ result.map! {|entry|
107
+ if entry.include? ' '
108
+ splitted = entry.split(' ')
109
+ # =================================================================== #
110
+ # We are only interested in the last part, as that will contain the
111
+ # filename.
112
+ # =================================================================== #
113
+ entry = splitted.last
114
+ end
115
+ entry
116
+ }
117
+ result.select! {|entry| File.file? entry }
118
+ open_these_entries = result[0 .. (open_n_files?.to_i - 1) ]
119
+ @target_files = open_these_entries
120
+ open_these_target_files(@target_files)
121
+ end
122
+
123
+ # ========================================================================= #
124
+ # === Open::Last[]
125
+ # ========================================================================= #
126
+ def self.[](i = '')
127
+ new(i)
128
+ end
129
+
130
+ end
131
+
132
+ # =========================================================================== #
133
+ # === Open.open_last
134
+ # =========================================================================== #
135
+ def self.open_last(i = ARGV)
136
+ ::Open::Last.new(i)
137
+ end
138
+
139
+ end
140
+
141
+ if __FILE__ == $PROGRAM_NAME
142
+ Open::Last.new(ARGV)
143
+ end # openlast
@@ -0,0 +1,146 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === Open::LastUrl
6
+ #
7
+ # This class has been created in order to open the content of the
8
+ # xorg-buffer via the browser, e. g. via palemoon, firefox or any
9
+ # other browser (defined elsewhere). The assumption here is that
10
+ # at the time of calling this class, the xorg-buffer contains a
11
+ # remote URL.
12
+ #
13
+ # No arguments have to be given to this class because it will simply
14
+ # fetch the URL by using the external program called `xclip`.
15
+ #
16
+ # Usage example from ruby code:
17
+ #
18
+ # Open::LastUrl.new(ARGV)
19
+ #
20
+ # =========================================================================== #
21
+ # require 'open/last_url/last_url.rb'
22
+ # =========================================================================== #
23
+ require 'open/base/base.rb'
24
+
25
+ module Open
26
+
27
+ class LastUrl < Base # === Open::LastUrl
28
+
29
+ require 'open/in_browser/in_browser.rb'
30
+
31
+ # ========================================================================= #
32
+ # === initialize
33
+ # ========================================================================= #
34
+ def initialize(
35
+ commandline_arguments = ARGV,
36
+ run_already = true
37
+ )
38
+ reset
39
+ menu(
40
+ commandline_arguments
41
+ )
42
+ run if run_already
43
+ end
44
+
45
+ # ========================================================================= #
46
+ # === reset (reset tag)
47
+ # ========================================================================= #
48
+ def reset
49
+ super()
50
+ end
51
+
52
+ # ========================================================================= #
53
+ # === menu (menu tag)
54
+ # ========================================================================= #
55
+ def menu(i)
56
+ if i.is_a? Array
57
+ i.each {|entry| menu(entry) }
58
+ else
59
+ case i
60
+ # ===================================================================== #
61
+ # === :quiet
62
+ # ===================================================================== #
63
+ when :quiet,
64
+ :silent,
65
+ :be_quiet
66
+ @be_verbose = false
67
+ end
68
+ end
69
+ end
70
+
71
+ # ========================================================================= #
72
+ # === determine_xorg_buffer
73
+ # ========================================================================= #
74
+ def determine_xorg_buffer
75
+ # @xorg_buffer = `xclip -o` # Get the content of the Xorg Buffer here.
76
+ @xorg_buffer = `xsel -o` # Get the content of the Xorg Buffer here.
77
+ sanitize_xorg_buffer
78
+ end
79
+
80
+ # ========================================================================= #
81
+ # === sanitize_xorg_buffer
82
+ # ========================================================================= #
83
+ def sanitize_xorg_buffer
84
+ @xorg_buffer = @xorg_buffer.chomp
85
+ end
86
+
87
+ # ========================================================================= #
88
+ # === open_the_xorg_buffer_in_the_browser
89
+ # ========================================================================= #
90
+ def open_the_xorg_buffer_in_the_browser
91
+ _ = @xorg_buffer
92
+ _ = _.split('|') if _.include? '|'
93
+ if _.include?(' ') or _.include?(';') # As otherwise we could not really open it from the terminal.
94
+ _ = '"'+_+'"'
95
+ end
96
+ Open.in_browser(_) # This method is defined in open_in_browser.rb
97
+ end
98
+
99
+ # ========================================================================= #
100
+ # === output_xorg_buffer
101
+ # ========================================================================= #
102
+ def output_xorg_buffer
103
+ _ = @xorg_buffer
104
+ if @be_verbose
105
+ e "#{rev}Now trying to display the #{simp('Xorg-Buffer')} we will use."
106
+ e " #{sfancy(_)}"
107
+ end
108
+ end
109
+
110
+ # ========================================================================= #
111
+ # === run_everything
112
+ # ========================================================================= #
113
+ def run_everything
114
+ determine_xorg_buffer
115
+ output_xorg_buffer
116
+ open_the_xorg_buffer_in_the_browser
117
+ end
118
+
119
+ # ========================================================================= #
120
+ # === run (run tag)
121
+ # ========================================================================= #
122
+ def run
123
+ run_everything
124
+ end
125
+
126
+ # ========================================================================= #
127
+ # === Roebe::OpenLastUrl[]
128
+ # ========================================================================= #
129
+ def self.[](i = ARGV)
130
+ new(i)
131
+ end
132
+
133
+ end
134
+
135
+ # =========================================================================== #
136
+ # === Open.last_url
137
+ # =========================================================================== #
138
+ def self.last_url(optional_arguments = ARGV)
139
+ Open::LastUrl[optional_arguments]
140
+ end; self.instance_eval { alias open_last_url last_url } # === Open.open_last_url
141
+
142
+ end
143
+
144
+ if __FILE__ == $PROGRAM_NAME
145
+ Open::LastUrl.new(ARGV)
146
+ end # ola
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === Open::NanoOpen
6
+ #
7
+ # Usage example:
8
+ #
9
+ # Open::NanoOpen.new
10
+ #
11
+ # =========================================================================== #
12
+ # require 'open/nano_open/nano_open.rb'
13
+ # =========================================================================== #
14
+ require 'open/base/base.rb'
15
+
16
+ module Open
17
+
18
+ class NanoOpen < Base # === Open::NanoOpen
19
+
20
+ require 'open/in_editor/in_editor.rb'
21
+
22
+ # ========================================================================= #
23
+ # === initialize
24
+ # ========================================================================= #
25
+ def initialize(
26
+ commandline_arguments = nil,
27
+ run_already = true
28
+ )
29
+ reset
30
+ set_commandline_arguments(
31
+ commandline_arguments
32
+ )
33
+ run if run_already
34
+ end
35
+
36
+ # ========================================================================= #
37
+ # === reset (reset tag)
38
+ # ========================================================================= #
39
+ def reset
40
+ super()
41
+ end
42
+
43
+ # ========================================================================= #
44
+ # === run (run tag)
45
+ # ========================================================================= #
46
+ def run
47
+ Open.in_editor {{
48
+ this_file: first_argument?,
49
+ use_this_editor: :nano
50
+ }}
51
+ end
52
+
53
+ # ========================================================================= #
54
+ # === Open::NanoOpen[]
55
+ # ========================================================================= #
56
+ def self.[](i = '')
57
+ new(i)
58
+ end
59
+
60
+ end; end
61
+
62
+ if __FILE__ == $PROGRAM_NAME
63
+ Open::NanoOpen.new(ARGV)
64
+ end # nanoopen