module Rush::Commands
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
Public Instance Methods
edit(*args)
click to toggle source
Open file with $EDITOR.
# File lib/rush/commands.rb, line 46 def edit(*args) open_with ENV['EDITOR'], *args end
entries()
click to toggle source
The entries command must return an array of Rush::Entry
items. This varies by class that it is mixed in to.
# File lib/rush/commands.rb, line 15 def entries fail 'must define me in class mixed in to for command use' end
line_count()
click to toggle source
Count the number of lines in the contained files.
# 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)
click to toggle source
Open file with xdg-open. Usage:
home.locate('mai_failz').open
# 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)
click to toggle source
# 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(' ') options = 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} #{options} #{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)
click to toggle source
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
# File lib/rush/commands.rb, line 63 def open_with(app, *args) system(*open_command(app, *args)) end
opt_to_s(k, v)
click to toggle source
# 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)
click to toggle source
# File lib/rush/commands.rb, line 67 def output_of(app, *args) `#{open_command(app, *args)}` end
replace_contents!(pattern, with_text)
click to toggle source
Search and replace file contents.
# 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)
click to toggle source
Search file contents for a regular expression. A Rush::SearchResults
object is returned.
# 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