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
-
#edit(*args) ⇒ Object
Open file with $EDITOR.
-
#entries ⇒ Object
The entries command must return an array of Rush::Entry items.
-
#line_count ⇒ Object
Count the number of lines in the contained files.
-
#mate(*args) ⇒ Object
Invoke TextMate on one or more files - only works locally.
-
#open(*args) ⇒ Object
Open file with xdg-open.
- #open_command(app, *args, **opts) ⇒ Object
-
#open_with(app, *args, **opts) ⇒ Object
Open file with any application you like.
- #output_of(app, *args, **opts) ⇒ Object
-
#replace_contents!(pattern, with_text) ⇒ Object
Search and replace file contents.
-
#search(pattern) ⇒ Object
Search file contents for a regular expression.
-
#vi(*args) ⇒ Object
(also: #vim)
Invoke vi on one or more files - only works locally.
Instance Method Details
#edit(*args) ⇒ Object
Open file with $EDITOR.
44 45 46 |
# File 'lib/rush/commands.rb', line 44 def edit(*args) open_with ENV['EDITOR'], *args end |
#entries ⇒ Object
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_count ⇒ Object
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.
59 60 61 |
# File 'lib/rush/commands.rb', line 59 def mate(*args) open_with('mate', *args) end |
#open(*args) ⇒ Object
Open file with xdg-open. Usage:
home.locate('mai_failz').open
66 67 68 |
# File 'lib/rush/commands.rb', line 66 def open(*args) open_with('xdg-open', *args) end |
#open_command(app, *args, **opts) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rush/commands.rb', line 82 def open_command(app, *args, **opts) names = dir? ? '' : entries.map(&:to_s).join(' ') = opts.map do |k, v| key = k.size == 1 ? "-#{k}" : "--#{k}" case when v == true then key when k == 'other' || k == :other then v else "#{key} #{v}" end end.join(' ') "cd #{dirname}; #{app.to_s} #{names} #{options} #{args.join(' ')}" end |
#open_with(app, *args, **opts) ⇒ Object
Open file with any application you like. Usage:
home.locate('timetable').open_with :vim
home['.vimrc'].vim { other: '+55', x: true, u: 'other_vimrc', cmd: 'ls' }
74 75 76 |
# File 'lib/rush/commands.rb', line 74 def open_with(app, *args, **opts) system open_command(app, *args, opts) end |
#output_of(app, *args, **opts) ⇒ Object
78 79 80 |
# File 'lib/rush/commands.rb', line 78 def output_of(app, *args, **opts) `#{open_command(app, *args, opts)}` 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.
49 50 51 52 53 54 55 |
# File 'lib/rush/commands.rb', line 49 def vi(*args) if self.class == Rush::Dir system "cd #{full_path}; vim" else open_with('vim', *args) end end |