/
cli.rb
57 lines (47 loc) · 1.63 KB
/
cli.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Vain
class CLI
require 'rubygems'
require 'columnizer'
require 'hubruby'
# Some colors used in the output
NameColor = "\e[33m"
CommandColor = "\e[36m"
DefaultColor = "\e[0m"
ErrorColor = "\e[31m"
# Get the commands from the command line
# (Somewhat primitive, will be expanded) TODO
def start
ARGV.length == 1 ? status(ARGV[0]) : help
end
private
def status(user_handle)
user = GitHub.user(user_handle)
return failtown("Unknown user: #{user_handle}") if user.login.nil?
puts "#{CommandColor}#{user.login}#{DefaultColor} - #{user.followers_count} followers - #{user.public_repo_count} public repositories"
# get the repositories sorted by watcher count
repositories = user.repositories.sort_by { |r| 1.0 / r.watchers }
data = repositories.map do |repo|
[
NameColor + repo.name + DefaultColor,
' ',
"#{repo.watchers} #{repo.watchers == 1 ? 'watcher' : 'watchers'}",
"#{repo.forks} #{repo.forks == 1 ? 'fork' : 'forks'}",
ErrorColor + (repo.fork ? '(FORK)' : '')
]
end
puts Columnizer.columnize(data, :padding => 3)
end
# Display help section
def help(*args)
puts "#{NameColor}vain#{DefaultColor} by John Crepezzi <john.crepezzi@gmail.com>"
puts 'http://github.com/seejohnrun/vain'
puts
puts "#{CommandColor}vain handle#{DefaultColor} View account info"
end
# Show error message with help below it
def failtown(message = nil)
puts "#{ErrorColor}Uh-oh! #{message}#{DefaultColor}\n" if message
help
end
end
end