Method: Rush::Shell#print_result

Defined in:
lib/rush/shell.rb

Nice printing of different return types, particularly Rush::SearchResults.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rush/shell.rb', line 71

def print_result(res)
	return if self.suppress_output
	if res.kind_of? String
		puts res
	elsif res.kind_of? Rush::SearchResults
		widest = res.entries.map { |k| k.full_path.length }.max
		res.entries_with_lines.each do |entry, lines|
			print entry.full_path
			print ' ' * (widest - entry.full_path.length + 2)
			print "=> "
			print res.colorize(lines.first.strip.head(30))
			print "..." if lines.first.strip.length > 30
			if lines.size > 1
				print " (plus #{lines.size - 1} more matches)"
			end
			print "\n"
		end
		puts "#{res.entries.size} matching files with #{res.lines.size} matching lines"
	elsif res.respond_to? :each
		counts = {}
		res.each do |item|
			puts item
			counts[item.class] ||= 0
			counts[item.class] += 1
		end
		if counts == {}
			puts "=> (empty set)"
		else
			count_s = counts.map do |klass, count|
				"#{count} x #{klass}"
			end.join(', ')
			puts "=> #{count_s}"
		end
	else
		puts "=> #{res.inspect}"
	end
end