Class: AtticPath::Input
- Inherits:
-
Object
- Object
- AtticPath::Input
- Defined in:
- lib/attic-path/input.rb
Instance Attribute Summary collapse
-
#attic_c ⇒ Object
Returns the value of attribute attic_c.
Instance Method Summary collapse
-
#all_files ⇒ Object
All the files/folders of the current_dir are stored here, if file_ids are enabled every file/folder will have a unique ID.
-
#array ⇒ Object
The output as an array.
-
#cd ⇒ Object
The cd function to move from dir to dir.
-
#check_dir(input) ⇒ Object
Whenever a dir is requested, it will be checked here if it exists or not.
-
#check_file(file) ⇒ Object
Checks if a certain file exists when asked for.
-
#command ⇒ Object
The input starts here, whenever a command has been set, and Attic Path isn’t done this function will be called.
-
#count ⇒ Object
The output counted.
-
#current_dir(input) ⇒ Object
The current dir the user is in is set here, everytime the location is updated the @the_dir variable changes.
-
#file_ids ⇒ Object
The ID for each file/folder is set here.
-
#flush ⇒ Object
The STDOUT.flush if true.
-
#grab ⇒ Object
All the files that were grabbed in an array.
-
#grab_id ⇒ Object
This method is called if the grab method is used with ID instead of file/folder name.
-
#grabfile ⇒ Object
The grab method, to grab select files.
-
#grabfile_name ⇒ Object
This method is called if the grab method is used with file/folder name instead of ID.
-
#help ⇒ Object
–help function.
-
#initialize(attic_c) ⇒ Input
constructor
A new instance of Input.
-
#input ⇒ Object
Code starts here, if the pathing is true then it will be enabled.
-
#invalid ⇒ Object
Simple error message.
-
#ls ⇒ Object
The ls method.
-
#mv ⇒ Object
The mv method.
-
#output ⇒ Object
The output is converted from array to a string.
-
#submit ⇒ Object
Checks if the input is valid for submitting with the selected options If it is not valid the user will be redirected back to input.
Constructor Details
#initialize(attic_c) ⇒ Input
Returns a new instance of Input.
6 7 8 |
# File 'lib/attic-path/input.rb', line 6 def initialize(attic_c) @attic_c = attic_c end |
Instance Attribute Details
#attic_c ⇒ Object
Returns the value of attribute attic_c.
4 5 6 |
# File 'lib/attic-path/input.rb', line 4 def attic_c @attic_c end |
Instance Method Details
#all_files ⇒ Object
All the files/folders of the current_dir are stored here, if file_ids are enabled every file/folder will have a unique ID.
57 58 59 60 61 62 63 |
# File 'lib/attic-path/input.rb', line 57 def all_files Dir.chdir(@the_dir) @all_files = Dir.glob("*"); if attic_c.file_id == true file_ids() end end |
#array ⇒ Object
The output as an array
482 483 484 |
# File 'lib/attic-path/input.rb', line 482 def array @input_array end |
#cd ⇒ Object
The cd function to move from dir to dir
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/attic-path/input.rb', line 146 def cd if attic_c.cd == true if @input[1] == "--help" puts attic_c.cd_help command() else if @input[1] == nil || @input[1][0] == "/" if @input[1] == nil current_dir("/") else current_dir(File.join("/", @input[1][1..999999])) end elsif @input[1][0..1] == "~/" current_dir(File.join(ENV["HOME"], @input[1][2..999999])) elsif @input[1][0] == "#" && attic_c.file_id == true i = 0 @file_ids.each do |f| if @input[1] == "##{f}" check_dir(@all_files[f]) break end if i+1 == @file_ids.count puts "Folder id wasn't found" command() end i += 1 end else check_dir(@input[1]) end end else invalid() end end |
#check_dir(input) ⇒ Object
Whenever a dir is requested, it will be checked here if it exists or not.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/attic-path/input.rb', line 26 def check_dir(input) if File.directory? File.join(@the_dir, input) current_dir(input) elsif File.exist? File.join(@the_dir, input) puts "\nThe path #{File.join(@the_dir, input)} is a file." command() else puts "\nThe path #{File.join(@the_dir, input)} Doesn't exist." command() end end |
#check_file(file) ⇒ Object
Checks if a certain file exists when asked for.
47 48 49 50 51 52 53 |
# File 'lib/attic-path/input.rb', line 47 def check_file(file) if File.exists? file else puts "File or folder doesn't exist" command() end end |
#command ⇒ Object
The input starts here, whenever a command has been set, and Attic Path isn’t done this function will be called.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/attic-path/input.rb', line 81 def command # If pathing is true, display the current_dir. if attic_c.pathing_comment != nil puts "\n"+attic_c.pathing_comment else puts "\n" end if attic_c.pathing == true puts ">>#{@the_dir}" all_files() end # STDOUT.flush if set it is set to true in the options flush() # Converts the input to an array, in order to set multiple functions in 1 input @input = $stdin.gets.chomp @input_array = @input.scan(/\w+/) @input = @input.split(" ") # All the commands availabe if @input[0] == "--help" help() elsif @input[0] == "cd" cd() elsif @input[0] == "ls" ls() elsif @input[0] == "cp" cp() elsif @input[0] == "mv" mv() elsif @input[0] == "grab" grabfile() else submit() end end |
#count ⇒ Object
The output counted
487 488 489 |
# File 'lib/attic-path/input.rb', line 487 def count @input.count end |
#current_dir(input) ⇒ Object
The current dir the user is in is set here, everytime the location is updated the @the_dir variable changes.
40 41 42 43 44 |
# File 'lib/attic-path/input.rb', line 40 def current_dir(input) Dir.chdir(input) @the_dir = Dir.getwd() command() end |
#file_ids ⇒ Object
The ID for each file/folder is set here.
66 67 68 69 70 71 72 73 |
# File 'lib/attic-path/input.rb', line 66 def file_ids @file_ids = [] i = 0 @all_files.each do |f| @file_ids << i i += 1 end end |
#flush ⇒ Object
The STDOUT.flush if true
133 134 135 136 137 |
# File 'lib/attic-path/input.rb', line 133 def flush if attic_c.flush == true STDOUT.flush end end |
#grab ⇒ Object
All the files that were grabbed in an array
492 493 494 |
# File 'lib/attic-path/input.rb', line 492 def grab @grab_array end |
#grab_id ⇒ Object
This method is called if the grab method is used with ID instead of file/folder name
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/attic-path/input.rb', line 278 def grab_id() i = 0 this_count = @input.count - 1 puts this_count if this_count > 1 if @grab_array.count + this_count < attic_c.grab_count+1 o = 0 while o < this_count do @file_ids.each do |f| if @input[o+1] == "##{f}" o += 1 grab_file = File.join(@the_dir, @all_files[f]) check_file(grab_file) puts "Adding #{grab_file} to array" @grab_array << grab_file i = 0 @grab_num += 1 if @grab_num == attic_c.grab_count && attic_c.grab_exit == true && o == this_count elsif o == this_count && attic_c.grab_exit == nil command() end end if i == @file_ids.count puts attic_c.grab_not_found if o == this_count command() end end i += 1 end end else puts attic_c.grab_full command() end elsif @grab_array.count + 1 < attic_c.grab_count+1 @file_ids.each do |f| if @input[1] == "##{f}" grab_file = File.join(@the_dir, @all_files[f]) check_file(grab_file) puts "Adding #{grab_file} to array" @grab_array << grab_file @grab_num += 1 if @grab_num == attic_c.grab_count && attic_c.grab_exit == true else command() end break end if i+1 == @file_ids.count puts attic_c.grab_not_found command() end i += 1 end else puts attic_c.grab_full command() end end |
#grabfile ⇒ Object
The grab method, to grab select files. A user can select multiple files at once.
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/attic-path/input.rb', line 381 def grabfile if attic_c.grab == true if @input[1] == "--help" puts attic_c.grab_help command() else if attic_c.grab == true && @input[1] != nil @grab_file = File.join(@the_dir, @input[1]) if @input[1] == "--help" puts "grab help" elsif @input[1] == "all" if @all_files.count + @grab_num > attic_c.grab_count puts attic_c.grab_full command() else zecount = @all_files.count + @grab_num puts zecount.to_s @all_files.each do |a| @grab_array << File.join(@the_dir, a) puts "Adding " + File.join(@the_dir, a) + " to array.." @grab_num += 1 end if attic_c.grab_exit == true flush() else command() end end else if attic_c.file_id == true && @input[1][0] == "#" grab_id() else if attic_c.grab_count == true grabfile_name() elsif @grab_num != attic_c.grab_count grabfile_name() if @grab_num == attic_c.grab_count && attic_c.grab_exit == true flush() else command() end else if @grab_num == attic_c.grab_count && attic_c.grab_exit == true flush() else puts attic_c.grab_full command() end end end end elsif @input[1] == nil puts attic_c.grab_no_file command() else invalid() end end end end |
#grabfile_name ⇒ Object
This method is called if the grab method is used with file/folder name instead of ID
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/attic-path/input.rb', line 341 def grabfile_name if @input[2] != nil the_count = @input.count-1 if the_count + @grab_array.count < attic_c.grab_count+1 i = 1 puts @input.count while i < the_count+1 grab_file = File.join(@the_dir, @input[i]) check_file(grab_file) @grab_array << grab_file puts "Adding #{grab_file} to array" if i == the_count if attic_c.grab_exit == true else command() end end i += 1 end else puts attic_c.grab_full command() end else if @grab_array.count != attic_c.grab_count check_file(@grab_file) @grab_array << @grab_file puts "Adding #{@grab_file} to array.." if attic_c.grab_count != true @grab_num += 1 end else puts attic_c.grab_full command() end end end |
#help ⇒ Object
–help function
140 141 142 143 |
# File 'lib/attic-path/input.rb', line 140 def help puts "Help is here!" command() end |
#input ⇒ Object
Code starts here, if the pathing is true then it will be enabled.
15 16 17 18 19 20 21 22 23 |
# File 'lib/attic-path/input.rb', line 15 def input if attic_c.pathing == true @grab_array = [] @grab_num = 0 current_dir(".") else command() end end |
#invalid ⇒ Object
Simple error message
467 468 469 470 |
# File 'lib/attic-path/input.rb', line 467 def invalid puts "Invalid command '#{@input[0]}'" command() end |
#ls ⇒ Object
The ls method
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/attic-path/input.rb', line 183 def ls if attic_c.ls == true if @input[1] == "--help" puts attic_c.ls_help command() else Dir.chdir(@the_dir) if @input[1] == "-a" && attic_c.lsa == true ls = Dir.glob("{*,.*}"); else ls = Dir.glob("*"); end rows = [] if ls.count == 1 if attic_c.file_id == true rows << ["#0", ls[0]] else rows << [ls[0]] end table = Terminal::Table.new :rows => rows puts table else i = 0 while i < ls.count if attic_c.file_id == true rows << ["##{@file_ids[i]}", ls[i], "##{@file_ids[i+1]}", ls[i+1]] i+= 2 else rows << [ls[i], ls[i+1]] i+= 2 end end table = Terminal::Table.new :rows => rows puts table end command() end else invalid() end end |
#mv ⇒ Object
The mv method
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/attic-path/input.rb', line 226 def mv if attic_c.mv == true if @input[1] == "--help" puts attic_c.mv_help command() else if @input[1] != nil && @input[2] != nil file_1 = File.join(@the_dir, @input[1]) file_2 = File.join(@the_dir, @input[2]) if @input[1][0] == "#" number1 = @input[1][1..999].to_i number2 = @input[2][1..999].to_i file_id_1 = File.join(@the_dir, @all_files[number1]) file_id_2 = File.join(@the_dir, @all_files[number2]) if @input[2][0] == "#" if File.exists? file_id_1 if File.exists? file_id_2 FileUtils.mv(file_id_1, file_id_2) else puts "Incorrect paths" command() end else puts "Incorrect paths" command() end else end end if File.exists? file_1 if File.exists? file_2 FileUtils.mv(file_1, file_2) command() else puts "Incorrect paths" command() end else puts "Incorrect paths" command() end else puts "No target selected" command() end end end end |
#output ⇒ Object
The output is converted from array to a string
477 478 479 |
# File 'lib/attic-path/input.rb', line 477 def output @input.map { |i| i.to_s }.join(" ") end |
#submit ⇒ Object
Checks if the input is valid for submitting with the selected options If it is not valid the user will be redirected back to input
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/attic-path/input.rb', line 444 def submit if attic_c.submit == true if @input[0] == attic_c.submit_command if attic_c.no_blank == true && @input[1] == nil puts attic_c.no_blank_error command() else @input.delete_at(0) flush() end else puts "Invalid command, type '#{attic_c.submit_command} [your text]' to submit" command() end else if attic_c.no_blank == true && @input[0] == nil puts attic_c.no_blank_error command() end end end |