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.
-
#open(*args) ⇒ Object
Open file with xdg-open.
- #open_command(app, *args) ⇒ Object
-
#open_with(app, *args) ⇒ Object
Open file with any application you like.
- #opt_to_s(k, v) ⇒ Object
- #output_of(app, *args) ⇒ Object
-
#replace_contents!(pattern, with_text) ⇒ Object
Search and replace file contents.
-
#search(pattern) ⇒ Object
Search file contents for a regular expression.
Instance Method Details
#edit(*args) ⇒ Object
Open file with $EDITOR.
46 47 48 |
# File 'lib/rush/commands.rb', line 46 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.
15 16 17 |
# File 'lib/rush/commands.rb', line 15 def entries fail 'must define me in class mixed in to for command use' end |
#line_count ⇒ Object
Count the number of lines in the contained files.
38 39 40 41 42 |
# File 'lib/rush/commands.rb', line 38 def line_count entries.inject(0) do |count, entry| count + (entry.dir? ? 0 : entry.lines.size) end end |
#open(*args) ⇒ Object
Open file with xdg-open. Usage:
home.locate('mai_failz').open
53 54 55 56 |
# File 'lib/rush/commands.rb', line 53 def open(*args) cmd = RUBY_PLATFORM.match(/darwin/) ? 'open' : 'xdg-open' open_with(cmd, *args) end |
#open_command(app, *args) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rush/commands.rb', line 80 def open_command(app, *args) opts = args.last.is_a?(Hash) ? args.pop : {} names = dir? ? '' : entries.map { |x| Rush.quote x.to_s }.join(' ') = opts .reject { |k, _| k == :env } .map { |k, v| opt_to_s(k, v) } .join(' ') dir = Rush.quote dirname.to_s cmd = "cd #{dir}; #{app} #{names} #{} #{args.join(' ')}" if vars = opts[:env] env = vars.inject({}) { |r, (k, v)| r.merge(k.to_s.upcase => v.to_s) } end vars ? [env, cmd] : cmd end |
#open_with(app, *args) ⇒ 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' }
home['my_app'].rails :c, env: { rails_env: 'test' } # environment vars
63 64 65 |
# File 'lib/rush/commands.rb', line 63 def open_with(app, *args) system(*open_command(app, *args)) end |
#opt_to_s(k, v) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/rush/commands.rb', line 71 def opt_to_s(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 |
#output_of(app, *args) ⇒ Object
67 68 69 |
# File 'lib/rush/commands.rb', line 67 def output_of(app, *args) `#{open_command(app, *args)}` end |
#replace_contents!(pattern, with_text) ⇒ Object
Search and replace file contents.
31 32 33 34 35 |
# File 'lib/rush/commands.rb', line 31 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.
21 22 23 24 25 26 27 28 |
# File 'lib/rush/commands.rb', line 21 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 |