/
v2ex
executable file
·103 lines (87 loc) · 2.75 KB
/
v2ex
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
58
59
60
61
62
63
64
65
66
67
68
69
70
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
#!/usr/bin/env ruby
require "rubygems"
require "v2ex_cli"
require 'commander/import'
program :version, V2exCli::VERSION
program :description, 'command line interface to v2ex.com'
global_option('--succinct', '-s') { $succinct = true }
def info(message)
print "#{message}\n" unless $succinct
end
command :latest do |c|
c.syntax = 'v2ex latest'
c.description = 'list latest topics'
c.action do |args, options|
info "latest topics:"
V2exCli::Engine.latest.each { |topic| print_topic(topic) }
end
end
command :topic do |c|
c.syntax = 'v2ex topic TOPIC_ID'
c.description = 'show a topic and its replies'
c.action do |args, options|
topic_id = args.first.to_i
print_topic V2exCli::Engine.topic(topic_id), :show_content => true
print_topic_replies V2exCli::Engine.replies(topic_id)
end
end
command :user_topics do |c|
c.syntax = 'v2ex user_topics OPTIONS'
c.description = 'show topics of a user'
c.action do |args, options|
info "latest topics by @#{args.first}:\n"
V2exCli::Engine.user_topics(args.first).each { |topic| print_topic(topic) }
end
end
command :node_topics do |c|
c.syntax = 'v2ex node_topics OPTIONS'
c.description = 'show topics of a node'
c.action do |args, options|
info "latest topics under #{args.first}:\n"
V2exCli::Engine.node_topics(args.first).each { |topic| print_topic(topic) }
end
end
command :user do |c|
c.syntax = 'v2ex user USERNAME'
c.description = 'show user info'
c.action do |args, options|
print_user_meta V2exCli::Engine.user_info(args.first)
end
end
command :nodes do |c|
c.syntax = 'v2ex nodes'
c.description = 'list all nodes'
c.action do |args, options|
info "listing all nodes:\n"
V2exCli::Engine.nodes.each { |node| puts node[:name].ljust(12) + node[:title] }
end
end
def print_topic_replies(replies)
info "\nreplies:\n"
if replies.empty?
puts "No replies yet."
else
replies.each do |reply|
puts "#{reply[:member][:username]} ".ljust(25, '-')
print "#{reply[:content]}\n\n"
end
end
end
def print_user_meta(user)
puts "#{user[:username]} - V2EX Member ##{user[:id]}"
%w{twitter website location tagline}.map(&:to_sym).each do |key|
puts "#{key}:".ljust(11) + user[key] if user.has_key?(key) && user[key].length > 0
end
print "Bio:\n#{user[:bio]}\n" if user.has_key?(:bio) && user[:bio].length > 0
end
def print_topic(topic, options={ :show_content => false })
puts topic[:id].to_s.ljust(7) + topic[:member][:username].ljust(13) + topic[:replies].to_s.ljust(4) + topic[:title]
puts "\n#{topic[:content]}" if options[:show_content]
end
alias_command :l, :latest
alias_command :t, :topic
alias_command :ut, :user_topics
alias_command :nt, :node_topics
alias_command :u, :user
alias_command :n, :nodes
default_command :latest