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