提交 c6c1f371 编写于 作者: C Chris Wanstrath

`hub help hub`

上级 389a0665
......@@ -145,9 +145,13 @@ module Hub
# $ hub help
# (print improved help text)
def help(args)
return if args.size > 1
puts improved_help_text
exit
if args[1] == 'hub'
puts hub_manpage
exit
elsif args.size == 1
puts improved_help_text
exit
end
end
# The text print when `hub help` is run, kept in its own method
......@@ -208,6 +212,49 @@ help
end
end
# Returns the terminal-formatted manpage, ready to be printed to
# the screen.
def hub_manpage
return "** Can't find groff(1)" unless groff?
require 'open3'
out = nil
Open3.popen3(groff_command) do |stdin, stdout, _|
stdin.puts hub_raw_manpage
stdin.close
out = stdout.read.strip
end
out
end
# Returns true if groff is installed and in our path, false if
# not.
def groff?
system("which groff")
end
# The groff command complete with crazy arguments we need to run
# in order to turn our raw roff (manpage markup) into something
# readable on the terminal.
def groff_command
"groff -Wall -mtty-char -mandoc -Tascii"
end
# Returns the raw hub manpage. If we're not running in standalone
# mode, it's a file sitting at the root under the `man`
# directory.
#
# If we are running in standalone mode the manpage will be
# included after the __END__ of the file so we can grab it using
# DATA.
def hub_raw_manpage
if File.exists? file = File.dirname(__FILE__) + '/../../man/hub.1'
File.read(file)
else
DATA.read
end
end
# All calls to `puts` in after hooks or commands are paged,
# git-style.
def puts(*args)
......
......@@ -97,4 +97,26 @@ class HubTest < Test::Unit::TestCase
def test_help_by_default
assert_equal Hub::Commands.improved_help_text, hub("")
end
def test_help_hub
help_manpage = hub("help hub")
assert_includes "git + hub = github", help_manpage
assert_includes "Writes shell aliasing code for", help_manpage
assert_includes "Chris Wanstrath :: chris@ozmm.org", help_manpage
puts help_manpage
assert_includes <<-config, help_manpage
Use git-config(1) to display the currently configured GitHub username:
$ git config --global github.user
config
end
def test_help_hub_no_groff
help_manpage = hub("help hub") do
Hub::Commands.class_eval do
def groff?; false end
end
end
assert_equal "** Can't find groff(1)\n", help_manpage
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册