Module: Rush::Commands

Included in:
Array, Dir, File, SearchResults
Defined in:
lib/rush/commands.rb

Overview

The commands module contains operations against Rush::File entries, and is mixed in to Rush::Entry and Array. This means you can run these commands against a single file, a dir full of files, or an arbitrary list of files.

Examples:

box['/etc/hosts'].search /localhost/       # single file
box['/etc/'].search /localhost/            # entire directory
box['/etc/**/*.conf'].search /localhost/   # arbitrary list

Instance Method Summary collapse

Instance Method Details

#entriesObject

The entries command must return an array of Rush::Entry items. This varies by class that it is mixed in to.



13
14
15
# File 'lib/rush/commands.rb', line 13

def entries
  raise "must define me in class mixed in to for command use"
end

#line_countObject

Count the number of lines in the contained files.



36
37
38
39
40
# File 'lib/rush/commands.rb', line 36

def line_count
  entries.inject(0) do |count, entry|
    count + (entry.dir? ? 0 : entry.lines.size)
  end
end

#mate(*args) ⇒ Object

Invoke TextMate on one or more files - only works locally.



53
54
55
# File 'lib/rush/commands.rb', line 53

def mate(*args)
  open_with('mate', *args)
end

#open(*args) ⇒ Object

Open file with xdg-open. Usage:

home.locate('mai_failz').open


60
61
62
# File 'lib/rush/commands.rb', line 60

def open(*args)
  open_with('xdg-open', *args)
end

#open_with(app, *args) ⇒ Object

Open file with any application you like. Usage:

home.locate('timetable').open_with :vim


67
68
69
70
# File 'lib/rush/commands.rb', line 67

def open_with(app, *args)
  names = dir? ? '' : entries.map(&:to_s).join(' ')
  system "cd #{dirname}; #{app.to_s} #{names} #{args.join(' ')}"
end

#output_of(app, *args) ⇒ Object



72
73
74
75
# File 'lib/rush/commands.rb', line 72

def output_of(app, *args)
  names = entries.map(&:to_s).join(' ')
  `cd #{dirname}; #{app.to_s} #{names} #{args.join(' ')}`
end

#replace_contents!(pattern, with_text) ⇒ Object

Search and replace file contents.



29
30
31
32
33
# File 'lib/rush/commands.rb', line 29

def replace_contents!(pattern, with_text)
  entries.each do |entry|
    entry.replace_contents!(pattern, with_text) unless entry.dir?
  end
end

#search(pattern) ⇒ Object

Search file contents for a regular expression. A Rush::SearchResults object is returned.



19
20
21
22
23
24
25
26
# File 'lib/rush/commands.rb', line 19

def search(pattern)
  entries.inject(Rush::SearchResults.new(pattern)) do |results, entry|
    if !entry.dir? and matches = entry.search(pattern)
      results.add(entry, matches)
    end
    results
  end
end

#vi(*args) ⇒ Object Also known as: vim

Invoke vi on one or more files - only works locally.



43
44
45
46
47
48
49
# File 'lib/rush/commands.rb', line 43

def vi(*args)
  if self.class == Rush::Dir
    system "cd #{full_path}; vim"
  else
    open_with('vim', *args)
  end
end