attic-path 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,107 @@
1
+ Attic-Path
2
+ ========
3
+
4
+ A replacement for gets.chomp.
5
+
6
+ About
7
+ =====
8
+
9
+ Attic Path is a gem that gives you more options to gets.chomp.
10
+ With Attic Path you can browse your system while being in the gets.chomp.
11
+ The user can select files and add them into an array.
12
+ Here are the current available options for Attic Path.
13
+
14
+
15
+
16
+ Options
17
+ ======
18
+
19
+ There are a number of options you can use with Attic-Path
20
+
21
+ List of options:
22
+ * submit - If set to true the user has to type the submit_command before being able to store the string.
23
+ * submit_command - Set the name for the submit command.
24
+ * no_blank - If you don't want the user to submit a blank string, set this to true.
25
+ * no_blank_error - Set the error message for the no_blank option
26
+ * flush - Set flush to true to let Attic-Path STDOUT.flush automatically
27
+ * pathing - Set pathing to true if you want the user to be able to see in which directory he is. Pathing is also needed for the next few options.
28
+ * file_id - Set this to true if you want all files / folders to have a unique id per folder. What this does is makes it easier to navigate around or select files/folder with the grab method.
29
+ * cd - Set this to true in order to let the user cd around his system while being inside the input.
30
+ * ls - Set to true if you want the user to be able to use ls command while inside the input
31
+ * lsa - Set to true in order to enable the "ls -a" command, shows all files including hidden files in a folder.
32
+ * mv - Set to true if you want the user to be able to use mv command while inside the input
33
+ * grab - Set to true if you want the user to be able to use the grab command while inside the input. The grab command lets the user 'grab' a certain file. When he does this the file will be put into an array, and you can get that array out of the output.
34
+ * grab_count - Set max allowed files that can be inserted into array, set true if unlimited
35
+ * grab_exit - Set to true if you want to exit input once the array has enough item
36
+ * grab_no_file - Set error message if no file is selected
37
+ * grab_full - Set error message for when grab is full
38
+ * grab_not_found - Set error message for when grab isn't found
39
+
40
+
41
+
42
+ Installing
43
+ =======
44
+
45
+ Just require the gem and add this to your code to adjust the options.
46
+
47
+ ## AtticPath Options
48
+ ```ruby
49
+ attic_c = AtticPathCommands.new do |c|
50
+
51
+ c.submit = true # Set to true if user has to use the submit_command before submitting
52
+
53
+ c.submit_command = "submit" # If submit if true then you can set your submit command
54
+
55
+ c.no_blank = true # If you don't want blank inputs set this to true
56
+
57
+ c.no_blank_error = "Input can't be blank" # If no_blank is true, set your error message
58
+
59
+ c.flush = true # Set to true if you want AtticInput to automatically do STDOUT.flush
60
+
61
+
62
+
63
+ c.pathing = true # Set to true if you want to be able to browse your system
64
+ # and use any available terminal commands that are set to true
65
+
66
+ if c.pathing == true # These options are only available if pathing is enabled
67
+ c.file_id = true # Set to true to enable file id, example: instead of cd Users you can do cd -1
68
+
69
+ c.cd = true # Set to true if you want the cd command to be usable
70
+
71
+ c.ls = true # Set to true if you want the ls command to be usable
72
+ c.lsa = nil # Set to true if you want the ls -a command to be usable
73
+
74
+ c.mv = true # Set to true if you want the mv command to be usable
75
+
76
+ c.grab = true # Set to true if you want to be able to put files in an array
77
+ c.grab_count = 4 # Set max allowed files that can be inserted into array, set true if unlimited
78
+ c.grab_exit = nil # Set to true if you want to exit input once the array has enough item
79
+ c.grab_no_file = "You didn't select a file." # Set error message if no file is selected
80
+ c.grab_full = "Can't select more than 4 items" # Set error message for when grab is full
81
+ c.grab_not_found = "File you typed in wasn't found" # Set error message for when grab isn't found
82
+ end
83
+ end
84
+ ```
85
+
86
+ AtticPath Input Functions
87
+ =========================
88
+
89
+ # attic_path = AtticPathInput.new(attic_c)
90
+ New input, you don't HAVE to repeat this if your app is straight forward
91
+
92
+ # attic_path.input
93
+ This is the input, like gets.chomp
94
+
95
+ # attic_path.output
96
+ This is the output for your input, it'll just output a string
97
+
98
+ # attic_path.array
99
+ This is the output but as an array
100
+
101
+ # puts attic_path.grab
102
+ This is the grab function. An array with all the files / folders that the user stored
103
+
104
+ Notes
105
+ =====
106
+
107
+ Please note that this gem is still beta version, I am not responsible if something breaks when using this. Use at own risk.
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ require "fileutils"
3
+ require "terminal-table"
4
+
5
+ # require File.expand_path("../../lib/attic_path_input.rb", __FILE__)
6
+ # require File.expand_path("../../lib/attic_path_commands.rb", __FILE__)
7
+
8
+ $:.unshift File.dirname(__FILE__)
9
+ %w(attic_path_commands attic_path_input).each do |file|
10
+ require "attic-path/#{file}"
11
+ end
12
+
13
+ begin
14
+ require 'fileutils'
15
+ require 'terminal-table'
16
+ rescue LoadError
17
+ puts "\n You must have fileutils and terminal-table installed in order to use attic-path."
18
+ puts "fileutils should already be installed."
19
+ puts "gem install terminal-table"
20
+ puts "if this still doesn't fix this problem then you might not have fileutils"
21
+ exit 1
22
+ end
@@ -0,0 +1,7 @@
1
+ class AtticPathCommands
2
+ attr_accessor :submit, :submit_command, :no_blank, :no_blank_error, :flush, :pathing, :file_id, :cd, :ls, :mv, :lsa, :grab, :grab_count, :grab_exit, :grab_no_file, :grab_full, :grab_not_found
3
+
4
+ def initialize(&block)
5
+ instance_eval(&block) if block_given?
6
+ end
7
+ end
@@ -0,0 +1,455 @@
1
+ class AtticPathInput
2
+
3
+ attr_accessor :attic_c
4
+
5
+ def initialize(attic_c)
6
+ @attic_c = attic_c
7
+ end
8
+
9
+ ############################################
10
+ # Dir setup #
11
+ ############################################
12
+
13
+ # Code starts here, if the pathing is true then it will be enabled.
14
+ def input
15
+ if attic_c.pathing == true
16
+ @grab_array = []
17
+ @grab_num = 0
18
+ current_dir(".")
19
+ else
20
+ command()
21
+ end
22
+ end
23
+
24
+ # Whenever a dir is requested, it will be checked here if it exists or not.
25
+ def check_dir(input)
26
+ if File.directory? File.join(@the_dir, input)
27
+ current_dir(input)
28
+ elsif File.exist? File.join(@the_dir, input)
29
+ puts "\nThe path #{File.join(@the_dir, input)} is a file."
30
+ command()
31
+ else
32
+ puts "\nThe path #{File.join(@the_dir, input)} Doesn't exist."
33
+ command()
34
+ end
35
+ end
36
+
37
+ # The current dir the user is in is set here, everytime the location is updated
38
+ # the @the_dir variable changes.
39
+ def current_dir(input)
40
+ Dir.chdir(input)
41
+ @the_dir = Dir.getwd()
42
+ command()
43
+ end
44
+
45
+ # Checks if a certain file exists when asked for.
46
+ def check_file(file)
47
+ if File.exists? file
48
+ else
49
+ puts "File or folder doesn't exist"
50
+ command()
51
+ end
52
+ end
53
+
54
+ # All the files/folders of the current_dir are stored here, if file_ids are enabled
55
+ # every file/folder will have a unique ID.
56
+ def all_files
57
+ Dir.chdir(@the_dir)
58
+ @all_files = Dir.glob("*");
59
+ if attic_c.file_id == true
60
+ file_ids()
61
+ end
62
+ end
63
+
64
+ # The ID for each file/folder is set here.
65
+ def file_ids
66
+ @file_ids = []
67
+ i = 0
68
+ @all_files.each do |f|
69
+ @file_ids << i
70
+ i += 1
71
+ end
72
+ end
73
+
74
+ ############################################
75
+ # Command inputs #
76
+ ############################################
77
+
78
+ # The input starts here, whenever a command has been set, and Attic Path isn't done
79
+ # this function will be called.
80
+ def command
81
+ # If pathing is true, display the current_dir.
82
+ if attic_c.pathing == true
83
+ puts "\n>>#{@the_dir}"
84
+ all_files()
85
+ end
86
+
87
+ # STDOUT.flush if set it is set to true in the options
88
+ flush()
89
+
90
+ # Converts the input to an array, in order to set multiple functions in 1 input
91
+ @input = gets.chomp
92
+ @input_array = @input.scan(/\w+/)
93
+ @input = @input.split(" ")
94
+
95
+ # All the commands availabe
96
+
97
+ if @input[0] == "--help"
98
+ help()
99
+
100
+ elsif @input[0] == "cd"
101
+ cd()
102
+
103
+ elsif @input[0] == "ls"
104
+ ls()
105
+
106
+ elsif @input[0] == "cp"
107
+ cp()
108
+
109
+ elsif @input[0] == "mv"
110
+ mv()
111
+
112
+ elsif @input[0] == "grab"
113
+ grabfile()
114
+
115
+ else
116
+ submit()
117
+ end
118
+ end
119
+
120
+ ############################################
121
+ # Command functions #
122
+ ############################################
123
+
124
+ # The STDOUT.flush if true
125
+ def flush
126
+ if attic_c.flush == true
127
+ STDOUT.flush
128
+ end
129
+ end
130
+
131
+ # --help function
132
+ def help
133
+ puts "Help is here!"
134
+ command()
135
+ end
136
+
137
+ # The cd function to move from dir to dir
138
+ def cd
139
+ if attic_c.cd == true
140
+ if @input[1] == "--help"
141
+ puts "cd help"
142
+ command()
143
+ else
144
+ if @input[1] == nil || @input[1][0] == "/"
145
+ if @input[1] == nil
146
+ current_dir("/")
147
+ else
148
+ current_dir(File.join("/", @input[1][1..999999]))
149
+ end
150
+ elsif @input[1][0..1] == "~/"
151
+ current_dir(File.join(ENV["HOME"], @input[1][2..999999]))
152
+ elsif @input[1][0] == "#" && attic_c.file_id == true
153
+ i = 0
154
+ @file_ids.each do |f|
155
+ if @input[1] == "##{f}"
156
+ check_dir(@all_files[f])
157
+ break
158
+ end
159
+ if i+1 == @file_ids.count
160
+ puts "Folder id wasn't found"
161
+ command()
162
+ end
163
+ i += 1
164
+ end
165
+ else
166
+ check_dir(@input[1])
167
+ end
168
+ end
169
+ else
170
+ invalid()
171
+ end
172
+ end
173
+
174
+ # The ls method
175
+ def ls
176
+ if attic_c.ls == true
177
+ if @input[1] == "--help"
178
+ puts "ls help"
179
+ command()
180
+ else
181
+ Dir.chdir(@the_dir)
182
+ if @input[1] == "-a" && attic_c.lsa == true
183
+ ls = Dir.glob("{*,.*}");
184
+ else
185
+ ls = Dir.glob("*");
186
+ end
187
+ rows = []
188
+ if ls.count == 1
189
+ if attic_c.file_id == true
190
+ rows << ["#0", ls[0]]
191
+ else
192
+ rows << [ls[0]]
193
+ end
194
+ table = Terminal::Table.new :rows => rows
195
+ puts table
196
+ else
197
+ i = 0
198
+ while i < ls.count
199
+ if attic_c.file_id == true
200
+ rows << ["##{@file_ids[i]}", ls[i], "##{@file_ids[i+1]}", ls[i+1]]
201
+ i+= 2
202
+ else
203
+ rows << [ls[i], ls[i+1]]
204
+ i+= 2
205
+ end
206
+ end
207
+ table = Terminal::Table.new :rows => rows
208
+ puts table
209
+ end
210
+ command()
211
+ end
212
+ else
213
+ invalid()
214
+ end
215
+ end
216
+
217
+ # The mv method
218
+ def mv
219
+ if @input[1] != nil && @input[2] != nil
220
+ file_1 = File.join(@the_dir, @input[1])
221
+ file_2 = File.join(@the_dir, @input[2])
222
+ if @input[1][0] == "#"
223
+ number1 = @input[1][1..999].to_i
224
+ number2 = @input[2][1..999].to_i
225
+
226
+ file_id_1 = File.join(@the_dir, @all_files[number1])
227
+ file_id_2 = File.join(@the_dir, @all_files[number2])
228
+ if @input[2][0] == "#"
229
+ if File.exists? file_id_1
230
+ if File.exists? file_id_2
231
+ FileUtils.mv(file_id_1, file_id_2)
232
+ else
233
+ puts "Incorrect paths"
234
+ command()
235
+ end
236
+ else
237
+ puts "Incorrect paths"
238
+ command()
239
+ end
240
+ else
241
+
242
+ end
243
+ end
244
+ if File.exists? file_1
245
+ if File.exists? file_2
246
+ FileUtils.mv(file_1, file_2)
247
+ command()
248
+ else
249
+ puts "Incorrect paths"
250
+ command()
251
+ end
252
+ else
253
+ puts "Incorrect paths"
254
+ command()
255
+ end
256
+ else
257
+ puts "No target selected"
258
+ command()
259
+ end
260
+ end
261
+
262
+ # This method is called if the grab method is used with ID instead of file/folder name
263
+ def grab_id()
264
+ i = 0
265
+ this_count = @input.count - 1
266
+ puts this_count
267
+ if this_count > 1
268
+ if @grab_array.count + this_count < attic_c.grab_count+1
269
+ o = 0
270
+ while o < this_count do
271
+ @file_ids.each do |f|
272
+ if @input[o+1] == "##{f}"
273
+ o += 1
274
+ grab_file = File.join(@the_dir, @all_files[f])
275
+ check_file(grab_file)
276
+ puts "Adding #{grab_file} to array"
277
+ @grab_array << grab_file
278
+ i = 0
279
+ @grab_num += 1
280
+ if @grab_num == attic_c.grab_count && attic_c.grab_exit == true && o == this_count
281
+
282
+ elsif o == this_count && attic_c.grab_exit == nil
283
+ command()
284
+ end
285
+ end
286
+ if i == @file_ids.count
287
+ puts attic_c.grab_not_found
288
+ if o == this_count
289
+ command()
290
+ end
291
+ end
292
+ i += 1
293
+ end
294
+ end
295
+ else
296
+ puts attic_c.grab_full
297
+ command()
298
+ end
299
+ elsif @grab_array.count + 1 < attic_c.grab_count+1
300
+ @file_ids.each do |f|
301
+ if @input[1] == "##{f}"
302
+ grab_file = File.join(@the_dir, @all_files[f])
303
+ check_file(grab_file)
304
+ puts "Adding #{grab_file} to array"
305
+ @grab_array << grab_file
306
+ @grab_num += 1
307
+ if @grab_num == attic_c.grab_count && attic_c.grab_exit == true
308
+ else
309
+ command()
310
+ end
311
+ break
312
+ end
313
+ if i+1 == @file_ids.count
314
+ puts attic_c.grab_not_found
315
+ command()
316
+ end
317
+ i += 1
318
+ end
319
+ else
320
+ puts attic_c.grab_full
321
+ command()
322
+ end
323
+ end
324
+
325
+ # This method is called if the grab method is used with file/folder name instead of ID
326
+ def grabfile_name
327
+ if @input[2] != nil
328
+ the_count = @input.count-1
329
+ if the_count + @grab_array.count < attic_c.grab_count+1
330
+ i = 1
331
+ puts @input.count
332
+ while i < the_count+1
333
+ grab_file = File.join(@the_dir, @input[i])
334
+ check_file(grab_file)
335
+ @grab_array << grab_file
336
+ puts "Adding #{grab_file} to array"
337
+ if i == the_count
338
+ if attic_c.grab_exit == true
339
+ else
340
+ command()
341
+ end
342
+ end
343
+ i += 1
344
+ end
345
+ else
346
+ puts attic_c.grab_full
347
+ command()
348
+ end
349
+ else
350
+ if @grab_array.count != attic_c.grab_count
351
+ check_file(@grab_file)
352
+ @grab_array << @grab_file
353
+ puts "Adding #{@grab_file} to array"
354
+ if attic_c.grab_count != true
355
+ @grab_num += 1
356
+ end
357
+ else
358
+ puts attic_c.grab_full
359
+ command()
360
+ end
361
+ end
362
+ end
363
+
364
+ # The grab method, to grab select files.
365
+ # A user can select multiple files at once.
366
+ def grabfile
367
+ if attic_c.grab == true && @input[1] != nil
368
+ @grab_file = File.join(@the_dir, @input[1])
369
+ if @input[1] == "--help"
370
+ puts "grab help"
371
+ else
372
+ if attic_c.file_id == true && @input[1][0] == "#"
373
+ grab_id()
374
+ else
375
+ if attic_c.grab_count == true
376
+ grabfile_name()
377
+ elsif @grab_num != attic_c.grab_count
378
+ grabfile_name()
379
+ if @grab_num == attic_c.grab_count && attic_c.grab_exit == true
380
+ flush()
381
+ else
382
+ command()
383
+ end
384
+ else
385
+ if @grab_num == attic_c.grab_count && attic_c.grab_exit == true
386
+ flush()
387
+ else
388
+ puts attic_c.grab_full
389
+ command()
390
+ end
391
+ end
392
+ end
393
+ end
394
+ elsif @input[1] == nil
395
+ puts attic_c.grab_no_file
396
+ command()
397
+ else
398
+ invalid()
399
+ end
400
+ end
401
+
402
+ # Checks if the input is valid for submitting with the selected options
403
+ # If it is not valid the user will be redirected back to input
404
+ def submit
405
+ if attic_c.submit == true
406
+ if @input[0] == attic_c.submit_command
407
+ if attic_c.no_blank == true && @input[1] == nil
408
+ puts attic_c.no_blank_error
409
+ command()
410
+ else
411
+ @input.delete_at(0)
412
+ flush()
413
+ end
414
+ else
415
+ puts "Invalid command, type '#{attic_c.submit_command} [your text]' to submit"
416
+ command()
417
+ end
418
+ else
419
+ if attic_c.no_blank == true && @input[0] == nil
420
+ puts attic_c.no_blank_error
421
+ command()
422
+ end
423
+ end
424
+ end
425
+
426
+ # Simple error message
427
+ def invalid
428
+ puts "Invalid command '#{@input[0]}'"
429
+ command()
430
+ end
431
+
432
+ ############################################
433
+ # AtticInput functions #
434
+ ############################################
435
+
436
+ # The output is converted from array to a string
437
+ def output
438
+ @input.map { |i| i.to_s }.join(" ")
439
+ end
440
+
441
+ # The output as an array
442
+ def array
443
+ @input_array
444
+ end
445
+
446
+ # The output counted
447
+ def count
448
+ @input.count
449
+ end
450
+
451
+ # All the files that were grabbed in an array
452
+ def grab
453
+ @grab_array
454
+ end
455
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attic-path
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kevin van Rooijen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-29 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fileutils
16
+ requirement: &70219838787400 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70219838787400
25
+ - !ruby/object:Gem::Dependency
26
+ name: terminal-table
27
+ requirement: &70219838786500 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70219838786500
36
+ description:
37
+ email: attichacker@gmail.com
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - README.md
43
+ - lib/attic-path.rb
44
+ - lib/attic-path/attic_path_commands.rb
45
+ - lib/attic-path/attic_path_input.rb
46
+ homepage: http://rubygems.org/gems/attic-path
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.11
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Attic Path is a gem that gives you more options for gets.chomp. With Attic
70
+ Path you can browse your system while being in the gets.chomp. The user can select
71
+ files and add them into an array.
72
+ test_files: []