Class: Files

Inherits:
Object show all
Defined in:
lib/xiki/files.rb

Constant Summary collapse

@@dir_hash =
{
'j' => '.js',
'r' => '.rb',
's' => '/spec/',
'a' => '/app/',
'm' => '/app/models/',
'v' => '/app/views/',
'c' => '/app/controllers/',
'n' => '.notes',
}

Class Method Summary collapse

Class Method Details

.append(path, txt) ⇒ Object



286
287
288
289
290
291
292
# File 'lib/xiki/files.rb', line 286

def self.append path, txt

  return if View.name =~ /_log.notes$/
  path = File.expand_path path
  txt = "#{txt.strip}\n"
  File.open(path, "a") { |f| f << txt }
end

.copyObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/xiki/files.rb', line 84

def self.copy
  from = Files.file_name

  View.next
  to = View.dir
  View.previous
  if Keys.prefix_u?
    $el.rename_file(from, to)
  elsif Keys.prefix_uu
    command = "cp -R \"#{from}\" \"#{to}\""
    Console.run command, :sync => true
  else
    $el.copy_file(from, to)
  end

  # Refresh
  self.refresh_me_and_next_dired
end

.current(times = nil) ⇒ Object



145
146
147
# File 'lib/xiki/files.rb', line 145

def self.current times=nil
  Buffers.tree times
end

.delete_current_fileObject



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/xiki/files.rb', line 307

def self.delete_current_file

  dest_path = View.file
  return View.beep("- There's no file for this buffer!") if ! dest_path

  View.beep :times=>3
  View.flash "- Delete CURRENT file (#{View.name})?", :times=>4
  answer = Keys.input :chars=>1, :prompt=>"- Delete current file for sure?"   #"

  return View.flash("- cancelled!") if answer !~ /y/i

  command = "rm \"#{dest_path}\""

  result = Console.run command, :sync=>true
  if (result||"").any?
    View.beep
    View.message "#{result}"
    return
  end

  return View.kill

end

.dir_of(path) ⇒ Object



278
279
280
281
282
283
284
# File 'lib/xiki/files.rb', line 278

def self.dir_of path
  if File.directory? path
    path =~ /\/$/ ? path : "#{path}/"
  else
    path[/.+\//]
  end
end

.do_clean_quotesObject



225
226
227
228
229
# File 'lib/xiki/files.rb', line 225

def self.do_clean_quotes
  with(:save_excursion) do
    $el.clean_crazy_quotes
  end
end

.do_load_fileObject



216
217
218
219
220
221
222
223
# File 'lib/xiki/files.rb', line 216

def self.do_load_file
  $el.revert_buffer(true, true, true) rescue nil

  View.message "Reverted file"
  return if ! Keys.prefix_u

  View.message $el.auto_revert_mode ? "Enabled Auto-revert" : "Disabled Auto-revert"
end

.edited_arrayObject



141
142
143
# File 'lib/xiki/files.rb', line 141

def self.edited_array
  $el.elvar.editedhistory_history.to_a
end

.encoding_binaryObject



332
333
334
# File 'lib/xiki/files.rb', line 332

def self.encoding_binary
  [{:encoding => 'binary'}]
end

.enter_fileObject

Use File.dirname

def self.dir path   # Extract dir from path
  return "" unless path =~ /\//
  path.sub(/(.+)\/.*/, "\\1")   # Cut of path
end


242
243
244
245
246
# File 'lib/xiki/files.rb', line 242

def self.enter_file
  path = File.expand_path(Keys.bookmark_as_path(:include_file=>1))
  path << "/" if File.directory? path
  View.insert(path)
end

.file_name(options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/xiki/files.rb', line 116

def self.file_name options={}
  if options[:path]
    return $el.buffer_file_name || $el.dired_get_filename #'no_dir
  end
  if $el.buffer_file_name
    return $el.file_name_nondirectory($el.buffer_file_name)
  end
  $el.dired_get_filename(:no_dir)
end

.historyObject

*path



149
150
151
152
153
# File 'lib/xiki/files.rb', line 149

def self.history # *path
  paths = history_array#[0..400]
  paths.map!{|i| i.sub(/(.+\/)(.+)/, "- @\\1\n  - \\2")}
  paths.join("\n")
end

.history_arrayObject



155
156
157
# File 'lib/xiki/files.rb', line 155

def self.history_array
  $el.elvar.recentf_list.to_a
end

.history_tree(times = nil) ⇒ Object



159
160
161
162
# File 'lib/xiki/files.rb', line 159

def self.history_tree times=nil
  times ||= History.prefix_times
  puts CodeTree.tree_search_option + Tree.paths_to_tree(history_array[0..(times-1)])
end

.in_dir(path) ⇒ Object

Returns contents of a dir. Just a wrapper around Dir.entries that removes “.” and “..”



295
296
297
# File 'lib/xiki/files.rb', line 295

def self.in_dir path
  Dir.entries(path).select{|o| o !~ /^\.+$/}
end


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xiki/files.rb', line 4

def self.menu
  "
  - @edited/
  - @current/
  - tree) @Buffers.tree 25/
  - .history/
  - docs/
    > Summary
    | The 'files' menu deals with navigating the filesystem.
    |
    - keys/
      > Navigating
      | open+tree - navigate the files in a bookmarked dir
      |   (type '/' for the root)
      | search+bookmark - searches file contents in a bookmarked dir
      |
      > Recent Files
      | open+current - Show files currently open
      | open+edited - Show files recently edited
      | open+history - Show files recently open
      |
      > Saving
      | as+file - saves the file
      |   (using the normal emacs shortcut won't update the difflog)
      |
      > File Trees
      | do+name+file
      |   Renames a file (the file in a tree that the cursor is on).
      |
      | do+kill+file
      |   Deletes a file (the file in a tree that the cursor is on).
      |
      | do+copy+to
      |   Copies a file in the filesystem.  Used in a tree.  To use:
      |
      |   - 1) Put the cursor on a file in a tree
      |   - 2) do as+spot to remember that's the file you want to copy
      |   - 3) Put cursor on a file you want to copy it to
      |     - If you don't want to over-write an existing file
      |       - add the filename to the tree that you want to exist
      |   - 4) Type do+copy+to
      |
      > See
      @diff_log/docs/
  "
end

.openObject

Lets user open a file



63
64
65
66
# File 'lib/xiki/files.rb', line 63

def self.open
  return View.open $el.read_file_name("Open: ") if Keys.prefix_u
  View.open $el.read_file_name("Open: ", "/")
end

.open?(name) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/xiki/files.rb', line 198

def self.open? name
  $el.buffer_list.find{|b| $el.buffer_file_name(b) == name}
end

.open_as(file, application) ⇒ Object

This is currently mac-specific



259
260
261
262
263
# File 'lib/xiki/files.rb', line 259

def self.open_as file, application
  command = "open -a \"#{application}\" \"#{file}\""

  $el.shell_command(command)
end

.open_editedObject



182
183
184
185
186
187
# File 'lib/xiki/files.rb', line 182

def self.open_edited
  case Keys.prefix
  when :u, 8;  Launcher.open("- edited/tree/")
  else  Launcher.open("- edited/")
  end
end

.open_historyObject



189
190
191
192
193
194
195
196
# File 'lib/xiki/files.rb', line 189

def self.open_history
  case Keys.prefix
  when nil;  Keys.prefix = nil; Launcher.open("- Files.history/")
  when 0;  Launcher.open("- Files.history_tree/")
  when :u;  Launcher.open("- Files.history_tree 7/")
  else  Launcher.open("- Files.history_tree #{Keys.prefix}/")
  end
end

.open_in_os(path = nil) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/xiki/files.rb', line 202

def self.open_in_os path=nil

  # If we're in a file tree, use path

  if path.nil? && FileTree.handles?
    path = Xiki.trunk[-1]
  end

  path ||= View.file
  path ||= View.dir

  $el.shell_command("open \"#{path}\"")
end

.open_in_window(file = nil) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/xiki/files.rb', line 265

def self.open_in_window file=nil

  file ||= FileTree.tree_path_or_this_file

  file = File.expand_path file

  # Else, reveal current file
  command = "open --reveal \"#{file}\""
  $el.shell_command(command)

  nil
end

.open_justObject



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/xiki/files.rb', line 164

def self.open_just
  key = Keys.input(:prompt => "Open just currently open files of type (enter a letter): ", :chars=>1)
  dir = @@dir_hash[key]
  return message("No dir matching '#{key}' found.  See Files\#@@dir_hash") unless dir
  if Keys.prefix_u?
    Keys.clear_prefix
    Launcher.open("- Files.edited(100, :dir => '#{dir}')/")
  else
    Launcher.open("- Buffers.tree(0, :dir => '#{dir}')/")
  end
end

.open_lastObject



176
177
178
179
180
# File 'lib/xiki/files.rb', line 176

def self.open_last
  key = Keys.input(:prompt => "Open just currently open files of type (enter a letter): ", :chars=>1)
  dir = @@dir_hash[key]
  View.open Files.edited_array.grep((Regexp.new(Regexp.escape(dir)))).first
end

.open_last_screenshotObject

This is currently mac-specific



249
250
251
252
253
254
255
256
# File 'lib/xiki/files.rb', line 249

def self.open_last_screenshot

  dirs = `ls -t #{Bookmarks["$dt"]}`
  screenshot = Bookmarks["$dt"]+dirs[/.+/]

  self.open_as screenshot, "Adobe Illustrator"

end

.open_nth(nth) ⇒ Object



299
300
301
302
303
304
305
# File 'lib/xiki/files.rb', line 299

def self.open_nth nth
  View.layout_files :no_blink=>1
  View.to_highest
  nth.times { Move.to_quote }
  Effects.blink
  Launcher.launch
end

.open_sudoObject



68
69
70
# File 'lib/xiki/files.rb', line 68

def self.open_sudo
  $el.find_file "/sudo:root@localhost:#{View.file || View.dir}"
end

.open_tailObject



133
134
135
136
137
138
139
# File 'lib/xiki/files.rb', line 133

def self.open_tail
  bm = Keys.input(:timed=>true, :prompt=>"Enter bookmark of file to tail (or period for current file): ")
  file = (bm == ".") ?
    View.file :
    Bookmarks["$#{bm}"]
  Console.run "tail -f #{file}", :buffer => "*tail of #{file}"
end

.refresh_me_and_next_diredObject



126
127
128
129
130
131
# File 'lib/xiki/files.rb', line 126

def self.refresh_me_and_next_dired
  $el.revert_buffer(true, true, true) if($el.elvar.dired_directory)
  $el.other_window 1
  $el.revert_buffer(true, true, true) if($el.elvar.dired_directory)
  $el.other_window -1
end

.saveObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/xiki/files.rb', line 72

def self.save
  # If prefix, save as
  if $el.elvar.current_prefix_arg
    $el.write_file $el.read_file_name("Save as: ")
  # Otherwise, difflog save
  else
    $el.difflog_save
  end
  # Refresh
  self.refresh_me_and_next_dired
end

.zipObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/xiki/files.rb', line 103

def self.zip
  #from = Files.file_name(:path => true)
  from = Files.file_name
  $el.other_window 1;  to = $el.elvar.default_directory;  $el.other_window -1
  if Keys.prefix_u?
    command = "unzip -d \"#{to}\" \"#{from}\""
  else
    command = "zip -r \"#{to}#{from}.zip\" \"#{from}\""
  end
  $el.message command
  $el.shell_command command
end