module FileLibrary

Primary module which contains the methods.

Public Class Methods

completion(dir,text) click to toggle source

Enables completion in the file manager by taking in a directory and input text and returning a matching file or the original text if no match is found.

# File lib/accu-file.rb, line 39
def self.completion(dir,text)
        #--
        result = text
        range = (0)..(text.length-1)
        Dir.foreach(dir) { |file|
                #puts file.slice(range), text
                if file.slice(range) == text then
                        result = file
                        break
                end
        }
        return result
        #++
end
select_file() click to toggle source

Selects a file using a simple command-line navigation system.

# File lib/accu-file.rb, line 56
def self.select_file()
        #--
        #origDir = Dir.pwd
        currentDirectory = ""
        selection = ""
        request = ""
        while selection == "" do
                currentDirectory = Dir.pwd
                puts "---------\nPWD: " + currentDirectory + "\n---------"
                Dir.foreach(currentDirectory) { |file|
                        if File.directory? file then
                                puts "|" + file + "| - dir"
                        else
                                puts "|" + file + "| - file"
                        end
                }
                puts "---------"
                request = gets
                request.slice!(-1)
                while (not (File.exists? request)) do
                        if not File.exists? request then
                                request = self.completion(currentDirectory,request)
                                if File.exists? request then
                                        if not File.directory? request then
                                                puts "Is this file correct: |" + request + "| |Y N| "
                                        else
                                                puts "Is this directory correct: |" + request + "| |Y N| "
                                        end
                                        if WindowTerminal.getchr.downcase != "y" then
                                                request = ""
                                        end
                                end
                        end
                        if not File.exists request then
                                puts "Invalid: |" + request + "|"
                                puts "Please enter a valid file name or directory."
                                request = gets
                                request.slice!(-1)
                        end
                end
                if File.directory? request then
                        Dir.chdir(request)
                else
                        selection = request
                end
        end
        selection = Dir.pwd + "/" + selection
        puts "File selected: |" + selection + "|"
        return selection
        #++
end
select_file_with_windows(manager=WindowTerminal::WindowManager.new) click to toggle source

An implementation of SelectFile using WindowTerminal.

# File lib/accu-file.rb, line 110
def self.select_file_with_windows(manager=WindowTerminal::WindowManager.new)
        #--
        # Declarations
        #origDir = Dir.pwd
        currentDirectory = ""
        selection = ""
        request = ""
        # Define window.
        window = WindowTerminal::Window.new(WindowTerminal::Orientation.new,"Browse for file.")
        text = WindowTerminal::WrappedText.new(WindowTerminal::Orientation.new(-1,0),"",5,:preserve)
        text2 = WindowTerminal::Text.new(WindowTerminal::Orientation.new(1,0),"",5)
        window.add_objects text2,text
        num = manager.new_page window
        manager.display_page num
        # Standard loop.
        while selection == "" do
                text2.set_text ""
                currentDirectory = Dir.pwd
                string = ""
                string << "---------\nPWD: " + currentDirectory + "\n---------\n"
                Dir.foreach(currentDirectory) { |file|
                        if File.directory? file then
                                string << "|" + file + "| dir\n"
                        else
                                string << "|" + file + "| file\n"
                        end
                }
                string << "---------\n"
                string = string.split("\n")
                range = 0..(string.length-1)
                text.set_text string[range].join("\n")
                WindowTerminal.screen_render
                char = WindowTerminal.getchr().downcase()
                while (char == "w") or (char == "s") do
                        if char == "w" then
                                start = range.begin - 1
                                start = 0 if start < 0
                                ending = range.end
                                range = start..ending
                        else
                                start = range.begin + 1
                                start = range.end if start > range.end
                                ending = range.end
                                range = start..ending
                        end
                        text.set_text string[range].join("\n")
                        WindowTerminal.screen_render
                        char = WindowTerminal.getchr().downcase()
                end
                #puts string
                text2.set_text "File: "
                WindowTerminal.screen_render
                request = ""
                while (not (File.exists? request)) do
                        text2.set_text "Invalid: |" + request + "| Please enter a valid file name or directory." if request != ""
                        WindowTerminal.screen_render
                        request = WindowTerminal.getchrs { |c,full|
                                text2.set_text "File: " + full
                                #WindowTerminal.screen_render
                        }
                        if not File.exists? request then
                                request = self.completion(currentDirectory,request)
                                if File.exists? request then
                                        if not File.directory? request then
                                                text2.set_text "Is this file correct: |" + request + "| |Y N| "
                                        else
                                                text2.set_text "Is this directory correct: |" + request + "| |Y N| "
                                        end
                                        WindowTerminal.screen_render
                                        if WindowTerminal.getchr.downcase != "y" then
                                                request = ""
                                        end
                                end
                        end
                end
                if File.directory? request then
                        Dir.chdir(request)
                else
                        selection = request
                end
        end
        selection = Dir.pwd + "/" + selection
        text2.set_text ""
        text.set_text "File selected: |" + selection + "|"
        WindowTerminal.screen_render
        WindowTerminal.getchr()
        # Cleanup window.
        manager.remove_page num
        # Return
        return selection
        #++
end