Class: XPGrep::CLI::UI

Inherits:
Object
  • Object
show all
Defined in:
lib/xpgrep.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_count, out = STDOUT, err = STDERR) ⇒ UI

Returns a new instance of UI.



45
46
47
48
# File 'lib/xpgrep.rb', line 45

def initialize(file_count, out = STDOUT, err = STDERR)
  @file_count = file_count
  @out, @err = out, err
end

Instance Method Details

#perror(exception, prefix = $0) ⇒ Object

Prints the error message from an exception, prefixed with the program name.

Parameters:

  • exception

    the exception object

  • prefix (defaults to: $0)

    optional prefix to print. To disable, pass something falsey. Defaults to $0.



80
81
82
83
# File 'lib/xpgrep.rb', line 80

def perror(exception, prefix = $0)
  @err.print "#{prefix}: " if prefix
  @err.puts exception.message
end

Prints a single matched node, possibly prefixed with the filename in which it occurs. If more than one file was given on the command line, or -H was passed, print it.

Parameters:

  • fn

    the filename in which the match occurs

  • match

    an Hpricot node from a set returned by #search

Returns:

  • nil



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/xpgrep.rb', line 61

def print_match(fn, match)
  print_filename = Options.instance.print_filename
  if (@file_count > 1 and print_filename.nil?) or print_filename
    @out.print "#{fn}: "
  end

  if (attribute = Options.instance.attribute)
    @out.puts match[attribute]
  else
    @out.puts match
  end
end

#puts(*args) ⇒ Object



50
51
52
# File 'lib/xpgrep.rb', line 50

def puts(*args)
  @out.puts *args
end