open 0.1.30

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,341 @@
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
+ dataset = nil
99
+ # ===================================================================== #
100
+ # Next get rid of components past a '#' token.
101
+ # ===================================================================== #
102
+ entry = entry[0, entry.index('#')] if entry.include? '#'
103
+ # ===================================================================== #
104
+ # === Handle fake "Symbols" next:
105
+ # ===================================================================== #
106
+ if entry.start_with? ':' # If this is a "symbol" we assume to do a special action.
107
+ name = entry[1 .. -1]
108
+ # =================================================================== #
109
+ # Since as of March 2023 we will distinguish two use cases:
110
+ #
111
+ # (1) first, we will try to use the dataset in the file called
112
+ # shortcuts.yml. If the input is part of a key in that file
113
+ # then the key will be fetched, and the associated files
114
+ # will be opened in the editor
115
+ #
116
+ # (2) otherwise we will look through RUBY_SRC and simply open
117
+ # the project-specific .rb file instead
118
+ #
119
+ # =================================================================== #
120
+ base_dir = RUBY_SRC+name
121
+ if File.exist?(FILE_SHORTCUTS)
122
+ dataset = YAML.load_file(FILE_SHORTCUTS)
123
+ end
124
+ if dataset and dataset.has_key?(name) # Invoke via: openineditors :gamebooks
125
+ entry = dataset[name]
126
+ elsif File.directory? base_dir
127
+ target = base_dir+'/lib/'+name+'/*.rb' # This is, sort of, the special action.
128
+ entry = Dir[target]
129
+ end
130
+ end
131
+ if entry.include? '#' # Then chop off. It is probably not necessary, though.
132
+ entry = entry[0..entry.index('#')]
133
+ end
134
+ if entry.respond_to?(:start_with?) and entry.start_with?('file://')
135
+ entry[0, 'file://'.size] = ''
136
+ end
137
+ entry
138
+ } if i.is_a? Array
139
+ i = i.flatten if i.is_a? Array
140
+ @input = i # Must be an Array.
141
+ end
142
+
143
+ # ========================================================================= #
144
+ # === try_to_find_expanded_alias_for
145
+ #
146
+ # We delegate towards FindExpandedAlias
147
+ # ========================================================================= #
148
+ def try_to_find_expanded_alias_for(i)
149
+ result = false # Default.
150
+ if Object.const_defined?(:Roebe) and
151
+ Roebe.const_defined?(:FindExpandedAlias)
152
+ _ = ::Roebe::FindExpandedAlias[i]
153
+ result = sanitize(_)
154
+ end
155
+ if result.include? ' ' # For instance, when it includes a ' &'
156
+ result = result[0, result.index(' ')]
157
+ end if result
158
+ return result
159
+ end
160
+
161
+ # ========================================================================= #
162
+ # === start_editor_in_background
163
+ # ========================================================================= #
164
+ def start_editor_in_background
165
+ system "#{@use_this_editor} &"
166
+ end
167
+
168
+ # ========================================================================= #
169
+ # === use_which_delay?
170
+ # ========================================================================= #
171
+ def use_which_delay?
172
+ Open.use_which_delay?.to_f
173
+ end
174
+
175
+ # ========================================================================= #
176
+ # === open_in_editor
177
+ #
178
+ # The optional second argument allows us to wait a little before
179
+ # opening something in the editor.
180
+ # ========================================================================= #
181
+ def open_in_editor(
182
+ i, optional_delay = nil
183
+ )
184
+ if i.is_a? Array
185
+ i.each {|entry|
186
+ open_in_editor(entry, optional_delay)
187
+ if @use_this_editor == 'bluefish'
188
+ sleep(use_which_delay?) if i.size > 3 # To prevent bluefish from acting silly.
189
+ end
190
+ }
191
+ else
192
+ i = i.to_s.dup # So to avoid frozen strings here.
193
+ if i.include? 'localhost'
194
+ i.gsub!(/^http\:\/\/localhost\//, MY_DATA+'/')
195
+ end
196
+ if i.end_with?(':') and !File.exist?(i)
197
+ i[-1,1] = ''
198
+ elsif i =~ /(:\d+)/ # Has a : and a number
199
+ i.gsub!(/#{$1.to_s.dup}/,'')
200
+ end
201
+ i = sanitize(i) if i.include? '$'
202
+ if i.count('/home') > 1
203
+ i.gsub!(/#{MY_DATA}#{MY_DATA}/, MY_DATA)
204
+ end
205
+ if i.start_with?('http:') and !File.exist?(i)
206
+ i.delete_prefix!('http:')
207
+ end
208
+ if File.exist? i
209
+ # =================================================================== #
210
+ # e 'Yes file exists here, we can thus open it.'
211
+ # =================================================================== #
212
+ esystem "#{editor?} #{i}"
213
+ else # Ok, the file does not exist at this point.
214
+ # First try a purl.
215
+ _ = BeautifulUrl::BeautifulUrl.new(i, replace: true)
216
+ if _.url_was_found
217
+ esystem "#{editor?} #{_.url_as_string}"
218
+ elsif Dir[i+'*'].size > 0 # Try to find matches
219
+ e 'Did not find '+sfile(i)+' but we may have found '+
220
+ 'at least one other entry.'
221
+ open_in_editor(Dir[i+'*'].reject {|entry| File.directory? entry })
222
+ elsif result = try_to_find_expanded_alias_for(i)
223
+ open_in_editor(result) if result
224
+ # ================================================================= #
225
+ # Now the string can include 'project_', in which case we
226
+ # will handle it in a special way.
227
+ # ================================================================= #
228
+ elsif i.include? 'project_'
229
+ i.gsub!(/project_/,'')
230
+ i << 'task'
231
+ open_in_editor(beautiful_url(i))
232
+ else # Ok we really did not find it.
233
+ cmd = USERFIND+' '+i
234
+ # e cmd # Whether we wish to output this command or not.
235
+ result = `#{cmd}`.chomp
236
+ if result.empty?
237
+ this_file_was_not_found(i)
238
+ else # else, we did indeed find it.
239
+ open_in_editor(result)
240
+ end
241
+ end
242
+ end
243
+ end
244
+ sleep(optional_delay) if optional_delay
245
+ end
246
+
247
+ # ========================================================================= #
248
+ # === set_editor
249
+ # ========================================================================= #
250
+ def set_editor(i = use_which_editor?)
251
+ @use_this_editor = i
252
+ end; alias editor= set_editor # === editor=
253
+
254
+ # ========================================================================= #
255
+ # === input?
256
+ # ========================================================================= #
257
+ def input?
258
+ @input
259
+ end
260
+
261
+ # ========================================================================= #
262
+ # === consider_opening_files
263
+ # ========================================================================= #
264
+ def consider_opening_files(
265
+ i = input?
266
+ )
267
+ if i.is_a? Array
268
+ i.flatten!
269
+ i.map! {|file|
270
+ file.chop! if file.end_with? ':'
271
+ file
272
+ }
273
+ if i.empty?
274
+ start_editor_in_background
275
+ else
276
+ optional_delay = 0
277
+ if i.size > 3
278
+ opn; e "More than 3 arguments were given, thus we will use "\
279
+ "a delay of #{simp(N_DELAY.to_s)} seconds."
280
+ optional_delay = N_DELAY
281
+ end
282
+ i.each {|entry|
283
+ open_in_editor(entry, optional_delay)
284
+ }
285
+ end
286
+ else
287
+ open_in_editor(i)
288
+ end
289
+ end
290
+
291
+ # ========================================================================= #
292
+ # === run (run tag)
293
+ # ========================================================================= #
294
+ def run
295
+ consider_opening_files
296
+ end
297
+
298
+ # ========================================================================= #
299
+ # === Open::InEditor[]
300
+ #
301
+ # This method can also pass blocks to the initializer.
302
+ # ========================================================================= #
303
+ def self.[](i, &block)
304
+ new(i, &block)
305
+ end
306
+
307
+ end
308
+
309
+ # =========================================================================== #
310
+ # === Open.in_editor
311
+ #
312
+ # Usage examples:
313
+ #
314
+ # Open.in_editor('/home/x/programming/ruby/src/open/open.gemspec')
315
+ #
316
+ # Or theb lock variant:
317
+ #
318
+ # Open.in_editor {{
319
+ # this_file: first_argument?,
320
+ # use_this_editor: :nano
321
+ # }}
322
+ #
323
+ # =========================================================================== #
324
+ def self.in_editor(
325
+ i = ARGV, &block
326
+ )
327
+ ::Open::InEditor.new(i, &block)
328
+ end
329
+
330
+ end
331
+
332
+ # =========================================================================== #
333
+ # === open_in_editor
334
+ # =========================================================================== #
335
+ def open_in_editor(i, &block)
336
+ Open.in_editor(i, &block)
337
+ end
338
+
339
+ if __FILE__ == $PROGRAM_NAME
340
+ Open::InEditor.new(ARGV)
341
+ 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,151 @@
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
+ # This method depends on the binary called "xsel".
75
+ # ========================================================================= #
76
+ def determine_xorg_buffer
77
+ # @xorg_buffer = `xclip -o` # Get the content of the Xorg Buffer here.
78
+ @xorg_buffer = `xsel -o` # Get the content of the Xorg Buffer here.
79
+ sanitize_xorg_buffer
80
+ end
81
+
82
+ # ========================================================================= #
83
+ # === sanitize_xorg_buffer
84
+ # ========================================================================= #
85
+ def sanitize_xorg_buffer
86
+ @xorg_buffer = @xorg_buffer.chomp
87
+ end
88
+
89
+ # ========================================================================= #
90
+ # === open_the_xorg_buffer_in_the_browser
91
+ # ========================================================================= #
92
+ def open_the_xorg_buffer_in_the_browser
93
+ _ = @xorg_buffer
94
+ _ = _.split('|') if _.include? '|'
95
+ if _.include?(' ') or _.include?(';') # As otherwise we could not really open it from the terminal.
96
+ _ = '"'+_+'"'
97
+ end
98
+ ::Open.in_browser(_) # This method is defined in open_in_browser.rb
99
+ end
100
+
101
+ # ========================================================================= #
102
+ # === output_xorg_buffer
103
+ # ========================================================================= #
104
+ def output_xorg_buffer
105
+ _ = @xorg_buffer
106
+ if @be_verbose
107
+ e "#{rev}Next trying to show the #{simp('Xorg-Buffer')} that "\
108
+ "will be used:"
109
+ e
110
+ e " #{sfancy(_)}"
111
+ e
112
+ end
113
+ end
114
+
115
+ # ========================================================================= #
116
+ # === run_everything
117
+ # ========================================================================= #
118
+ def run_everything
119
+ determine_xorg_buffer
120
+ output_xorg_buffer
121
+ open_the_xorg_buffer_in_the_browser
122
+ end
123
+
124
+ # ========================================================================= #
125
+ # === run (run tag)
126
+ # ========================================================================= #
127
+ def run
128
+ run_everything
129
+ end
130
+
131
+ # ========================================================================= #
132
+ # === Open::LastUrl[]
133
+ # ========================================================================= #
134
+ def self.[](i = ARGV)
135
+ new(i)
136
+ end
137
+
138
+ end
139
+
140
+ # =========================================================================== #
141
+ # === Open.last_url
142
+ # =========================================================================== #
143
+ def self.last_url(optional_arguments = ARGV)
144
+ ::Open::LastUrl[optional_arguments]
145
+ end; self.instance_eval { alias open_last_url last_url } # === Open.open_last_url
146
+
147
+ end
148
+
149
+ if __FILE__ == $PROGRAM_NAME
150
+ Open::LastUrl.new(ARGV)
151
+ 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