Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

gh clone --search includes project descriptions to help with selection

  • Loading branch information...
commit 8f1b5976ec04fdf8a43c74876e195b6b9704c5e6 1 parent 723558d
@drnic drnic authored
View
7 lib/commands/commands.rb
@@ -133,8 +133,11 @@
query = [user, repo, dir].compact.join(" ")
data = JSON.parse(open("http://github.com/api/v1/json/search/#{URI.escape query}").read)
if (repos = data['repositories']) && !repos.nil? && repos.length > 0
- repos = repos.map { |r| "#{r['username']}/#{r['name']}"}.sort.uniq
- if user_repo = GitHub::UI.display_select_list(repos)
+ repo_list = repos.map do |r|
+ { "name" => "#{r['username']}/#{r['name']}", "description" => r['description'] }
+ end
+ formatted_list = helper.format_list(repo_list).split("\n")
+ if user_repo = GitHub::UI.display_select_list(formatted_list)
user, repo = user_repo.split('/', 2)
end
end
View
19 lib/commands/helpers.rb
@@ -454,6 +454,25 @@
report.join("\n")
end
+# Converts an array of {"name" => "foo", "description" => "some description"} items
+# as a string list like:
+# foo # some description
+# bar-tar # another description
+helper :format_list do |items|
+ longest_name = items.inject("") do |name, item|
+ name = item["name"] if item["name"] && item["name"].size > name.size
+ name
+ end
+ longest = longest_name.size + 1
+ lines = items.map do |item|
+ cmdstr = "%-#{longest}s" % item["name"]
+ if description = item["description"]
+ cmdstr += "# #{description}"
+ end
+ cmdstr
+ end.join("\n")
+end
+
helper :filter_issue do |issue, options|
if options[:after] && ! options[:after].instance_of?(Time)
options[:after] = Time.parse(options[:after]) rescue (puts 'cant parse after date')
View
5 lib/github/ui.rb
@@ -3,12 +3,15 @@
module GitHub
module UI
extend self
+ # Take a list of items, including optional ' # some description' on each and
+ # return the selected item (without the description)
def display_select_list(list)
HighLine.track_eof = false
- HighLine.new.choose do |menu|
+ long_result = HighLine.new.choose do |menu|
list.each { |item| menu.choice item }
menu.header = "Select a repository to clone"
end
+ long_result && long_result.gsub(/\s+#.*$/,'')
end
end
end
View
14 spec/helper_spec.rb
@@ -31,6 +31,20 @@ def self.helper(name, &block)
@helper = GitHub::Helper.new
end
+ helper :format_list do
+ it "should format an array of hashes with name,description keys" do
+ list = [{"name" => "aaa", "description" => "description for aaa"},
+ {"name" => "a long name", "description" => "help"},
+ {"name" => "no desc"}]
+ expected = <<-EOS.gsub(/^ /, '')
+ aaa # description for aaa
+ a long name # help
+ no desc
+ EOS
+ @helper.format_list(list).should == expected.gsub(/\n$/,'')
+ end
+ end
+
helper :print_issues_help do
it "should exist" do
@helper.should respond_to(:print_issues_help)
View
6 spec/ui_spec.rb
@@ -359,8 +359,12 @@
'{"name":"github-gem-builder","size":76,"followers":26,"username":"pjhyett","language":"Ruby","fork":false,"id":"repo-67489","type":"repo","pushed":"2008-11-04T04:54:57Z","forks":3,"description":"The scripts used to build RubyGems on GitHub","score":3.4152448,"created":"2008-10-24T22:29:32Z"}' +
']}'
json.rewind
+ question_list = <<-LIST.gsub(/^ /, '').split("\n").compact
+ defunkt/github-gem # The official `github` command line helper for simplifying your GitHub experience.
+ pjhyett/github-gem-builder # The scripts used to build RubyGems on GitHub
+ LIST
@command.should_receive(:open).with("http://github.com/api/v1/json/search/github-gem").and_return(json)
- GitHub::UI.should_receive(:display_select_list).with(%w[defunkt/github-gem pjhyett/github-gem-builder]).
+ GitHub::UI.should_receive(:display_select_list).with(question_list).
and_return("defunkt/github-gem")
@command.should_receive(:current_user?).and_return(nil)
@command.should_receive(:git_exec).with("clone git://github.com/defunkt/github-gem.git")
Please sign in to comment.
Something went wrong with that request. Please try again.