open 0.1.23

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,316 @@
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
+ # === start_editor_in_background
145
+ # ========================================================================= #
146
+ def start_editor_in_background
147
+ system "#{@use_this_editor} &"
148
+ end
149
+
150
+ # ========================================================================= #
151
+ # === open_in_editor
152
+ #
153
+ # The optional second argument allows us to wait a little before
154
+ # opening something in the editor.
155
+ # ========================================================================= #
156
+ def open_in_editor(
157
+ i, optional_delay = nil
158
+ )
159
+ if i.is_a? Array
160
+ i.each {|entry|
161
+ open_in_editor(entry, optional_delay)
162
+ if @use_this_editor == 'bluefish'
163
+ sleep 0.5 if i.size > 3 # To prevent bluefish from acting silly.
164
+ end
165
+ }
166
+ else
167
+ i = i.to_s.dup # So to avoid frozen strings here.
168
+ if i.include? 'localhost'
169
+ i.gsub!(/^http\:\/\/localhost\//, MY_DATA+'/')
170
+ end
171
+ if i.end_with?(':') and !File.exist?(i)
172
+ i[-1,1] = ''
173
+ elsif i =~ /(:\d+)/ # Has a : and a number
174
+ i.gsub!(/#{$1.to_s.dup}/,'')
175
+ end
176
+ i = sanitize(i) if i.include? '$'
177
+ if i.count('/home') > 1
178
+ i.gsub!(/#{MY_DATA}#{MY_DATA}/, MY_DATA)
179
+ end
180
+ if i.start_with?('http:') and !File.exist?(i)
181
+ i.delete_prefix!('http:')
182
+ end
183
+ if File.exist? i
184
+ # =================================================================== #
185
+ # e 'Yes file exists here, we can thus open it.'
186
+ # =================================================================== #
187
+ esystem "#{editor?} #{i}"
188
+ else # Ok, the file does not exist at this point.
189
+ # First try a purl.
190
+ _ = BeautifulUrl::BeautifulUrl.new(i, replace: true)
191
+ if _.url_was_found
192
+ esystem editor?+' '+_.url_as_string
193
+ elsif Dir[i+'*'].size > 0 # Try to find matches
194
+ e 'Did not find '+sfile(i)+' but we may have found '+
195
+ 'at least one other entry.'
196
+ open_in_editor(Dir[i+'*'].reject {|entry| File.directory? entry })
197
+ elsif result = try_to_find_expanded_alias_for(i)
198
+ open_in_editor(result) if result
199
+ # ================================================================= #
200
+ # Now the string can include 'project_', in which case we
201
+ # will handle it in a special way.
202
+ # ================================================================= #
203
+ elsif i.include? 'project_'
204
+ i.gsub!(/project_/,'')
205
+ i << 'task'
206
+ open_in_editor(beautiful_url(i))
207
+ else # Ok we really did not find it.
208
+ cmd = USERFIND+' '+i
209
+ # e cmd # Whether we wish to output this command or not.
210
+ result = `#{cmd}`.chomp
211
+ if result.empty?
212
+ this_file_was_not_found(i)
213
+ else # else, we did indeed find it.
214
+ open_in_editor(result)
215
+ end
216
+ end
217
+ end
218
+ end
219
+ sleep(optional_delay) if optional_delay
220
+ end
221
+
222
+ # ========================================================================= #
223
+ # === set_editor
224
+ # ========================================================================= #
225
+ def set_editor(i = use_which_editor?)
226
+ @use_this_editor = i
227
+ end; alias editor= set_editor # === editor=
228
+
229
+ # ========================================================================= #
230
+ # === input?
231
+ # ========================================================================= #
232
+ def input?
233
+ @input
234
+ end
235
+
236
+ # ========================================================================= #
237
+ # === consider_opening_files
238
+ # ========================================================================= #
239
+ def consider_opening_files(
240
+ i = input?
241
+ )
242
+ if i.is_a? Array
243
+ i.flatten!
244
+ i.map! {|file|
245
+ file.chop! if file.end_with? ':'
246
+ file
247
+ }
248
+ if i.empty?
249
+ start_editor_in_background
250
+ else
251
+ optional_delay = 0
252
+ if i.size > 3
253
+ opn; e "More than 3 arguments were given, thus we will use "\
254
+ "a delay of #{simp(N_DELAY.to_s)} seconds."
255
+ optional_delay = N_DELAY
256
+ end
257
+ i.each {|entry|
258
+ open_in_editor(entry, optional_delay)
259
+ }
260
+ end
261
+ else
262
+ open_in_editor(i)
263
+ end
264
+ end
265
+
266
+ # ========================================================================= #
267
+ # === run (run tag)
268
+ # ========================================================================= #
269
+ def run
270
+ consider_opening_files
271
+ end
272
+
273
+ # ========================================================================= #
274
+ # === Open::InEditor[]
275
+ #
276
+ # This method can also pass blocks to the initializer.
277
+ # ========================================================================= #
278
+ def self.[](i, &block)
279
+ new(i, &block)
280
+ end
281
+
282
+ end
283
+
284
+ # =========================================================================== #
285
+ # === Open.in_editor
286
+ #
287
+ # Usage examples:
288
+ #
289
+ # Open.in_editor('/home/x/programming/ruby/src/open/open.gemspec')
290
+ #
291
+ # Or theb lock variant:
292
+ #
293
+ # Open.in_editor {{
294
+ # this_file: first_argument?,
295
+ # use_this_editor: :nano
296
+ # }}
297
+ #
298
+ # =========================================================================== #
299
+ def self.in_editor(
300
+ i = ARGV, &block
301
+ )
302
+ ::Open::InEditor.new(i, &block)
303
+ end
304
+
305
+ end
306
+
307
+ # =========================================================================== #
308
+ # === open_in_editor
309
+ # =========================================================================== #
310
+ def open_in_editor(i, &block)
311
+ Open.in_editor(i, &block)
312
+ end
313
+
314
+ if __FILE__ == $PROGRAM_NAME
315
+ Open::InEditor.new(ARGV)
316
+ 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,149 @@
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}Next trying to show the #{simp('Xorg-Buffer')} that "\
106
+ "will be used:"
107
+ e
108
+ e " #{sfancy(_)}"
109
+ e
110
+ end
111
+ end
112
+
113
+ # ========================================================================= #
114
+ # === run_everything
115
+ # ========================================================================= #
116
+ def run_everything
117
+ determine_xorg_buffer
118
+ output_xorg_buffer
119
+ open_the_xorg_buffer_in_the_browser
120
+ end
121
+
122
+ # ========================================================================= #
123
+ # === run (run tag)
124
+ # ========================================================================= #
125
+ def run
126
+ run_everything
127
+ end
128
+
129
+ # ========================================================================= #
130
+ # === Open::LastUrl[]
131
+ # ========================================================================= #
132
+ def self.[](i = ARGV)
133
+ new(i)
134
+ end
135
+
136
+ end
137
+
138
+ # =========================================================================== #
139
+ # === Open.last_url
140
+ # =========================================================================== #
141
+ def self.last_url(optional_arguments = ARGV)
142
+ ::Open::LastUrl[optional_arguments]
143
+ end; self.instance_eval { alias open_last_url last_url } # === Open.open_last_url
144
+
145
+ end
146
+
147
+ if __FILE__ == $PROGRAM_NAME
148
+ Open::LastUrl.new(ARGV)
149
+ end # ola
@@ -0,0 +1,73 @@
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
+ # Open::NanoOpen.new(ARGV)
14
+ # =========================================================================== #
15
+ require 'open/base/base.rb'
16
+
17
+ module Open
18
+
19
+ class NanoOpen < Base # === Open::NanoOpen
20
+
21
+ require 'open/in_editor/in_editor.rb'
22
+
23
+ # ========================================================================= #
24
+ # === initialize
25
+ # ========================================================================= #
26
+ def initialize(
27
+ commandline_arguments = nil,
28
+ run_already = true
29
+ )
30
+ reset
31
+ set_commandline_arguments(
32
+ commandline_arguments
33
+ )
34
+ run if run_already
35
+ end
36
+
37
+ # ========================================================================= #
38
+ # === reset (reset tag)
39
+ # ========================================================================= #
40
+ def reset
41
+ super()
42
+ end
43
+
44
+ # ========================================================================= #
45
+ # === run (run tag)
46
+ # ========================================================================= #
47
+ def run
48
+ _ = first_argument?
49
+ if is_on_roebe?
50
+ require 'studium'
51
+ new_target = Studium.find_corresponding_exam_topic(_)
52
+ unless new_target == _
53
+ _ = Studium.directory_to_the_exam_topics+new_target
54
+ end
55
+ end
56
+ ::Open.in_editor {{
57
+ this_file: _,
58
+ use_this_editor: :nano
59
+ }}
60
+ end
61
+
62
+ # ========================================================================= #
63
+ # === Open::NanoOpen[]
64
+ # ========================================================================= #
65
+ def self.[](i = '')
66
+ new(i)
67
+ end
68
+
69
+ end; end
70
+
71
+ if __FILE__ == $PROGRAM_NAME
72
+ Open::NanoOpen.new(ARGV)
73
+ end # nanoopen