class CLI
Attributes
inspector[R]
stdin[R]
stdout[R]
Public Class Methods
new(stdin = STDIN, stdout = STDOUT, path_to_histfile='history.txt')
click to toggle source
# File lib/cli.rb, line 7 def initialize(stdin = STDIN, stdout = STDOUT, path_to_histfile='history.txt') @stdin = stdin @stdout = stdout @inspector ||= Inspector.new(read_and_encode(path_to_histfile)) end
Public Instance Methods
ascii_chart(stats)
click to toggle source
# File lib/cli.rb, line 20 def ascii_chart(stats) AsciiCharts::Cartesian.new(stats).draw end
bar_graph(stats)
click to toggle source
# File lib/cli.rb, line 24 def bar_graph(stats) out_of_100(stats).reduce("") do |memo, (command, occurences)| memo += "#{'#'*occurences} #{occurences.to_s}% -- #{command.to_s}\n" end end
format_results(results)
click to toggle source
# File lib/cli.rb, line 39 def format_results(results) results.map { |command, count| "#{count} #{command}" } end
get_input()
click to toggle source
# File lib/cli.rb, line 43 def get_input command = stdin.gets.chomp end
out_of_100(stats)
click to toggle source
# File lib/cli.rb, line 30 def out_of_100(stats) total = stats.reduce(0) {|memo, (command, occurences)| memo += occurences} stats.map { |command, occurences| [command, occurences = (occurences.to_f/total.to_f*100).round] } end
print_top_ten()
click to toggle source
# File lib/cli.rb, line 13 def print_top_ten stats = inspector.top_ten stdout.puts format_results(stats) stdout.puts ascii_chart(stats) stdout.puts bar_graph(stats) end
read_and_encode(history)
click to toggle source
# File lib/cli.rb, line 47 def read_and_encode(history) File.read(history).encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8') end
show_count_for_command()
click to toggle source
# File lib/cli.rb, line 35 def show_count_for_command stdout.puts(inspector.count(get_input)) end