adding line numbers to tree log

This commit is contained in:
Ken Miller
2013-10-02 19:54:23 -07:00
parent 9a6bdb315e
commit 451823e1f6
3 changed files with 45 additions and 21 deletions

View File

@@ -5,7 +5,7 @@ module RubyMotionQuery
# What's an rmq instance?
# - an rmq instance is an array-like object containing UIViews
# - rmq() never returns nil. If nothing is selected, it's an empty [ ] array-like
# object
# object
# - an rmq object always (almost always) returns either itself or a new
# rmq object. This is how chaining works. You do not need to worry if
# an rmq is blank or not, everything always works without throwing a
@@ -63,7 +63,7 @@ module RubyMotionQuery
# Wraps 1 or more views in an rmq instance.
#
# Normally you select a view or views using rmq(my_view). Which doesn't
# Normally you select a view or views using rmq(my_view). Which doesn't
# work if you have an instance of a RMQ, in which case it isn't a method.
# In some cases you want to save an instance of rmq and use it like the rmq
# method, for this you'd use #wrap
@@ -72,7 +72,7 @@ module RubyMotionQuery
# q = RubyMotionQuery::RMQ.new
#
# # Bad
# q(my_view).view_controller
# q(my_view).view_controller
#
# # Good
# q.wrap(my_view).view_controller
@@ -83,7 +83,7 @@ module RubyMotionQuery
end
# Untested and I don' think it's getting set right in some situations
# Untested and I don' think it's getting set right in some situations
#
# @return [RMQ] rmq that created this rmq, if any
def parent_rmq
@@ -104,7 +104,7 @@ module RubyMotionQuery
end
end
# Returns a view or array of views.
# Returns a view or array of views.
# Normally used to get the only view in selected.
#
# @example
@@ -126,19 +126,19 @@ module RubyMotionQuery
end
end
# Is this rmq a root instance?
# Is this rmq a root instance?
#
# Which means it only has 1 selected view, and that view
# is the view you called rmq in. Which is a tad confusing, but if you call *just* rmq inside a
# is the view you called rmq in. Which is a tad confusing, but if you call *just* rmq inside a
# view, then only that view will be *selected* and this rmq will be *root*. If you call rmq
# inside a controller, only controller.view will be selected and the rma instance will be a root.
def root?
(selected.length == 1) && (selected.first == @context)
end
# The context is where rmq was created (not the selectors).
# The context is where rmq was created (not the selectors).
#
# Normally you are inside a controller or a UIView when you execute the rmq method.
# Normally you are inside a controller or a UIView when you execute the rmq method.
#
# @return [UIView] the controller's view or the view you are in when calling rmq
def context_or_context_view
@@ -164,7 +164,7 @@ module RubyMotionQuery
# Super useful in the console. log outputs to the console a table of the selected views
#
# @param :wide outputs wide format (really wide, but awesome: rmq.all.log :wide). :tree outputs the
# @param :wide outputs wide format (really wide, but awesome: rmq.all.log :wide). :tree outputs the
# log in a tree form
#
# @return [String]
@@ -202,7 +202,7 @@ module RubyMotionQuery
# rmq.all.log :wide
def log(opt = nil)
if opt == :tree
puts tree_to_s(selected)
puts tree_to_s(selected)
return
end
@@ -215,7 +215,7 @@ module RubyMotionQuery
out << line.chop if wide
out << line
selected.each do |view|
selected.each do |view|
out << " #{view.object_id.to_s.ljust(12)}|"
out << " #{view.class.name[0..21].ljust(22)}|"
out << " #{(view.rmq_data.style_name || '')[0..23].ljust(24)}|"
@@ -246,10 +246,12 @@ module RubyMotionQuery
puts out
end
def tree_to_s(selected_views, depth = 0)
out = ""
def tree_to_s(selected_views, depth = 0, lines = [], maxlength = [0])
selected_views.each do |view|
out = ""
selected_views.each do |view|
first = (view == selected_views.first)
last = (view == selected_views.last)
@@ -277,11 +279,28 @@ module RubyMotionQuery
out << s
end
out << "\n"
out << tree_to_s(view.subviews, depth + 1)
if out.size > maxlength[0]
lines.each do |l|
l.gsub!('%PADDING%', " " * (out.size - maxlength[0]) + '%PADDING%')
end
maxlength[0] = out.size
end
if view.rmq_data.source
pad = " " * (maxlength[0] - out.size)
out << pad << "%PADDING%: "
out << view.rmq_data.source
end
lines << out
tree_to_s(view.subviews, depth + 1, lines, maxlength)
end
out
if depth == 0
pad = " " * (100 - maxlength[0])
lines.join("\n").gsub!("%PADDING%", " ")
end
end
class << self
@@ -289,7 +308,7 @@ module RubyMotionQuery
@cache_controller_rmqs = true
end
protected
protected
def extract_views_from_selectors(view_container, working_selectors)
unless RMQ.is_blank?(working_selectors)
working_selectors.each do |selector|
@@ -319,7 +338,7 @@ module RubyMotionQuery
if (nr = view.nextResponder) && nr.is_a?(UIView)
out << nr
all_superviews_for(nr, out)
end
end
out
end

View File

@@ -1,6 +1,6 @@
module RubyMotionQuery
class ViewData
attr_accessor :events, :style_name
attr_accessor :events, :style_name, :source
# @return [Hash] Array of tag names assigned to to this view
def tags

View File

@@ -13,6 +13,8 @@ module RubyMotionQuery
subviews_added = []
style = opts[:style]
opts[:caller] ||= caller.first
selected.each do |selected_view|
created = false
@@ -39,6 +41,7 @@ module RubyMotionQuery
apply_style_to_view(new_view, style) if style
end
new_view.rmq_data.source = opts[:caller]
new_view.rmq_did_create(self.wrap(new_view)) if created
end
@@ -49,6 +52,7 @@ module RubyMotionQuery
# @return [RMQ]
def append(view_or_constant, style=nil, opts = {})
opts[:style] = style
opts[:caller] = caller.first
add_subview(view_or_constant, opts)
end
@@ -56,6 +60,7 @@ module RubyMotionQuery
def unshift(view_or_constant, style=nil, opts = {})
opts[:at_index] = 0
opts[:style] = style
opts[:caller] = caller.first
add_subview view_or_constant, opts
end
alias :prepend :unshift