Module: MrT::Selector

Included in:
ActionRegistry::ActionSelector
Defined in:
lib/mrT/selector.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) /(code)



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mrT/selector.rb', line 10

def /(code)
  me = self
  Class.new do
    include me
    extend ActionRegistry
    def self.desc(str = nil)
      @desc = str if str
      @desc
    end
    define_singleton_method(:inherited) { |sub| me.sources[code.to_s] = sub }
  end
end

+ (Object) sources



6
7
8
# File 'lib/mrT/selector.rb', line 6

def sources
  @sources ||= Hash.new
end

Instance Method Details

- (Object) action(ui)



101
102
103
104
105
106
107
108
# File 'lib/mrT/selector.rb', line 101

def action(ui)
  item = selected(ui)
  if (actions = matching_actions(item)).empty?
    Action.new.tap { |a| a.action = lambda { |u,a| item } }
  else
    ActionRegistry::ActionSelector.new(actions).interact(ui)
  end
end

- (Object) actions



110
111
112
# File 'lib/mrT/selector.rb', line 110

def actions
  @actions ||= self.class.actions
end

- (Object) filter(ui)



97
98
99
# File 'lib/mrT/selector.rb', line 97

def filter(ui)
  ui.show matcher.sorted_matches_for(pattern.join, {})
end

- (Object) interact(ui)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mrT/selector.rb', line 60

def interact(ui)
  filter ui
  return selected ui if ui.items.size < 2 && actions.empty?
  loop do
    ui.render_line 0, prompt, pattern.join
    ui.refresh
    case c = ui.getch
    when :escape
      return
    when :enter
      return selected ui
    when :backspace
      pattern.pop
      filter ui
    when :backslash
      chr = ui.getch
      src = Selector.sources[chr.chr] if Fixnum === chr
      if chr == :backslash
        pattern << "\\"
        filter ui
      elsif src
        return src.new.interact ui
      end
    when :tab
      action = action ui.dup
      if action
        return action.execute(ui)
      else
        ui.redraw
      end
    when (0..255)
      pattern << c.chr
      filter ui
    end
  end
end

- (Object) items



46
47
48
# File 'lib/mrT/selector.rb', line 46

def items
  []
end

- (Object) matcher(options = {})



54
55
56
57
58
# File 'lib/mrT/selector.rb', line 54

def matcher(options = {})
  scanner = CommandT::Scanner.new
  scanner.instance_variable_set :@paths, memoized_items
  CommandT::Matcher.new scanner, options
end

- (Object) matching_actions(obj)



114
115
116
# File 'lib/mrT/selector.rb', line 114

def matching_actions(obj)
  actions.map{ |c| c.new obj }.select(&:applies?)
end

- (Object) memoized_items



32
33
34
# File 'lib/mrT/selector.rb', line 32

def memoized_items
  @items ||= items
end

- (Object) pattern



24
25
26
# File 'lib/mrT/selector.rb', line 24

def pattern
  @pattern ||= []
end

- (Object) prepare



36
37
38
39
40
# File 'lib/mrT/selector.rb', line 36

def prepare
  @actions = MrT.cmd.actions if MrT.cmd.actions
  @pattern = MrT.cmd.pattern if MrT.cmd.pattern
  memoized_items
end

- (Boolean) prepared?

Returns:

  • (Boolean)


42
43
44
# File 'lib/mrT/selector.rb', line 42

def prepared?
  !(@items.nil? || @items.empty?)
end

- (Object) prompt



28
29
30
# File 'lib/mrT/selector.rb', line 28

def prompt
  ">> "
end

- (Object) selected(ui)



50
51
52
# File 'lib/mrT/selector.rb', line 50

def selected(ui)
  ui.selected
end