lib/cocoa/sugarcube-repl/repl.rb in sugarcube-2.7.1 vs lib/cocoa/sugarcube-repl/repl.rb in sugarcube-2.8.0

- old
+ new

@@ -2,10 +2,11 @@ module Repl module_function @sugarcube_view = nil @sugarcube_items = nil + @sugarcube_collapsed_items = nil @sugarcube_restore = nil def adjust(view=nil) return @sugarcube_view unless view @@ -21,10 +22,33 @@ view end alias a adjust + def collapse(view) + if view.is_a? Fixnum + @sugarcube_items ||= SugarCube::Repl::build_tree(window, :subviews) + view = @sugarcube_items[view] + end + + @sugarcube_collapsed_items ||= [] + if @sugarcube_collapsed_items.include?(view) + @sugarcube_collapsed_items.delete(view) + else + @sugarcube_collapsed_items << view + end + + retval = tree + + if @sugarcube_collapsed_items + @sugarcube_collapsed_items.keep_if { |v| @sugarcube_items.include? v } + end + + retval + end + alias coll collapse + ##| FRAME def frame(f=nil) return unless check_sugarcube_view return @sugarcube_view.frame unless f @@ -235,10 +259,13 @@ raise "Unable to determine a SugarCube::Repl::tree selector for #{item.class.to_s}" end end @sugarcube_items = SugarCube::Repl::build_tree(item, selector) + if @sugarcube_collapsed_items + @sugarcube_collapsed_items.keep_if { |v| @sugarcube_items.include? v } + end if self.respond_to? :draw_tree draw_tree(item, selector) else SugarCube::Repl::draw_tree(item, selector) end @@ -259,16 +286,20 @@ end print items_index.to_s + ":" + space if tab print tab - if is_last - print '`-- ' - tab += ' ' + if @sugarcube_collapsed_items && @sugarcube_collapsed_items.include?(item) + print '<<< ' else - print '+-- ' - tab += '| ' + if is_last + print '`-- ' + tab += ' ' + else + print '+-- ' + tab += '| ' + end end else print '. ' tab = '' end @@ -287,33 +318,38 @@ else items = item.send(selector) end items ||= [] - items.each_with_index { |subview, index| - items_index += 1 - if self.respond_to? :draw_tree - items_index = draw_tree(subview, selector, tab, index == items.length - 1, items_index) - else - items_index = SugarCube::Repl::draw_tree(subview, selector, tab, index == items.length - 1, items_index) + unless @sugarcube_collapsed_items && @sugarcube_collapsed_items.include?(item) + items.each_with_index do |subview, index| + items_index += 1 + if self.respond_to? :draw_tree + items_index = draw_tree(subview, selector, tab, index == items.length - 1, items_index) + else + items_index = SugarCube::Repl::draw_tree(subview, selector, tab, index == items.length - 1, items_index) + end end - } + end return items_index end def build_tree(item, selector) + if selector.is_a? Proc items = selector.call(item) else items = item.send(selector) end items ||= [] ret = [item] - items.each_with_index { |subview, index| + return ret if @sugarcube_collapsed_items && @sugarcube_collapsed_items.include?(item) + + items.each_with_index do |subview, index| ret.concat SugarCube::Repl::build_tree(subview, selector) - } + end ret end def check_sugarcube_view raise 'no view has been assigned to SugarCube::Repl::adjust' unless @sugarcube_view