Module: FileLibrary

Defined in:
lib/accu-file.rb

Overview

Primary module which contains the methods.

Class Method Summary collapse

Class Method Details

.completion(dir, text) ⇒ Object

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.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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_fileObject

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
# 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) ⇒ Object

An implementation of SelectFile using WindowTerminal.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 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