Skip to content

Instantly share code, notes, and snippets.

@jamonholmgren
Last active August 29, 2015 14:15
Show Gist options
  • Save jamonholmgren/d8a3e87ae1d0159604a9 to your computer and use it in GitHub Desktop.
Save jamonholmgren/d8a3e87ae1d0159604a9 to your computer and use it in GitHub Desktop.
RubyMotion REPL documentation helper.

Lets you check Cocoa Touch documentation from the RubyMotion REPL.

screenshot

License: MIT, 2015 Jamon Holmgren

class ReplRi
attr_reader :class_name
COLORS = {
red: "\e[0;31m",
green: "\e[0;32m",
yellow: "\e[0;33m",
blue: "\e[0;34m",
light_blue: "\e[1;34m",
purple: "\e[0;35m",
light_purple: "\e[1;35m",
cyan: "\e[0;36m",
off: "\e[0m",
}
def initialize(class_name)
@class_name = class_name
end
def help
ri_docs = `HOME=/tmp /Library/RubyMotion/lib/yard/bin/yri --db /Library/RubyMotion/doc/yardoc #{class_name.inspect}`
colorized(ri_docs)
end
private
def colorized(ri_docs)
ri_doc_array = ri_docs.split("\n")
status = :start
ri_doc_array.map do |ri_line|
if ri_line.include?("Class:")
status = :intro
class_title ri_line
elsif ri_line.start_with? "--------------"
nil
elsif ri_line == ""
nil
elsif ["Class methods:", "Instance methods:", "Direct Known Subclasses:"].include? ri_line
status = :methods
section_title ri_line
elsif status == :intro
intro_text ri_line
elsif status == :methods
method_text ri_line
else
ri_line
end
end.compact.join("\n")
end
def class_title(ri_line)
"#{COLORS[:light_blue]}#{ri_line.split("Class:").last.strip}#{COLORS[:off]}"
end
def section_title(ri_line)
"#{COLORS[:light_purple]}#{ri_line}#{COLORS[:off]}"
end
def intro_text(ri_line)
"#{COLORS[:blue]}#{ri_line}#{COLORS[:off]}"
end
def method_text(ri_line)
"#{COLORS[:purple]}#{ri_line}#{COLORS[:off]}"
end
end
module Kernel
def ri(class_name)
puts ReplRi.new(class_name).help
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment