Class: YapShellAddonTabCompletion::Addon

Inherits:
Yap::Addon::Base
  • Object
show all
Defined in:
lib/yap-shell-addon-tab-completion.rb

Defined Under Namespace

Classes: CompletionResult

Constant Summary collapse

COMPLETIONS =
[ BasicCompletion ]
Color =
Term::ANSIColor
DISPLAY_PROCS =
Hash.new{ |h,k| h[k] = ->(text){ text } }.merge(
  directory: -> (text){ text + "/" }
)
STYLE_PROCS =
Hash.new{ |h,k| h[k] = ->(text){ text } }.merge(
  alias:     -> (text){ Color.bold(Color.color("#ff00d7"){ text } ) },
  builtin:   -> (text){ Color.bold(Color.color("#d7af00"){ text } ) },
  directory: -> (text){ Color.bold(Color.red(text)) },
  command:   -> (text){ Color.bold(Color.green(text)) },
  shell_command: -> (text){ Color.bold(Color.color("#ffafff"){ text } ) },
  symlink:   -> (text){ Color.bold(Color.cyan(text)) },
  selected:  -> (text){ Color.negative(text) }
)
DECORATION_PROCS =
Hash.new{ |h,k| h[k] = ->(text){ text } }.merge(
  directory: -> (text){ text + "/" },
  command:   -> (text){ text + "@" },
  shell_command: -> (text) { text + "🐚" }
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#editorObject (readonly)

Returns the value of attribute editor.



60
61
62
# File 'lib/yap-shell-addon-tab-completion.rb', line 60

def editor
  @editor
end

#worldObject (readonly)

Returns the value of attribute world.



60
61
62
# File 'lib/yap-shell-addon-tab-completion.rb', line 60

def world
  @world
end

Instance Method Details

#add_completion(name, pattern, &blk) ⇒ Object

Raises:

  • (ArgumentError)


124
125
126
127
128
# File 'lib/yap-shell-addon-tab-completion.rb', line 124

def add_completion(name, pattern, &blk)
  raise ArgumentError, "Must supply block!" unless block_given?
  logger.puts "NO-OP add_completion for name=#{name.inspect} pattern=#{pattern.inspect} block?=#{block_given?}"
  # @completions.push CustomCompletion.new(name:name, pattern:pattern, world:world, &blk)
end

#complete(word, words, word_index) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/yap-shell-addon-tab-completion.rb', line 136

def complete(word, words, word_index)
  logger.puts "complete word=#{word.inspect} words=#{words.inspect} word_index=#{word_index.inspect}"

  matches = @completions.sort_by(&:priority).reverse.map do |completion|
    if completion.respond_to?(:call)
      completion.call
    else
      completions = completion.new(
        world: @world,
        word_break_characters: editor.word_break_characters
      ).completions_for(word, words, word_index)
      completions.each do |completion|
        completion.text = display_text_for_match(completion)
      end
    end
  end.flatten

  logger.puts "complete possible matches are #{matches.inspect}"
  matches
end

#initialize_world(world) ⇒ Object



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/yap-shell-addon-tab-completion.rb', line 62

def initialize_world(world)
  @world = world
  @world.extend YapShellAddonTabCompletion::DslMethods
  @editor = @world.editor
  @editor.completion_proc = -> (word, line, word_index){
    complete(word, line, word_index)
  }
  @editor.bind(:tab){ @editor.complete }
  @completions = COMPLETIONS.dup

  @style_procs = STYLE_PROCS.dup
  @decoration_procs = DECORATION_PROCS.dup
  @display_procs = DISPLAY_PROCS.dup

  editor.on_word_complete do |event|
    logger.puts "on_word_complete event: #{event}"

    sub_word = event[:payload][:sub_word]
    word = event[:payload][:word]
    actual_completion = event[:payload][:completion]
    possible_completions = event[:payload][:possible_completions]

    semi_formatted_possibilities = possible_completions.map.with_index do |completion, i|
      if completion == actual_completion
        style_text_for_selected_match(completion) + "\e[0m"
      else
        style_text_for_nonselected_match(completion) + "\e[0m"
      end
    end

    max_width = @editor.terminal_width
    max_item_width = semi_formatted_possibilities.map(&:length).max + 2
    most_per_line = max_width / max_item_width
    padding_at_the_end = max_width % max_item_width

    formatted_possibilities = semi_formatted_possibilities.map.with_index do |completion, i|
      spaces_to_pad = max_item_width - completion.length
      completion + (" " * spaces_to_pad)
    end

    editor.content_box.children = formatted_possibilities.map do |str|
      TerminalLayout::Box.new(content: str, style: { display: :float, float: :left, height: 1, width: max_item_width })
    end
  end

  editor.on_word_complete_no_match do |event|
    logger.puts "on_word_complete_no_match event: #{event}"

    sub_word = event[:payload][:sub_word]
    word = event[:payload][:word]
    editor.content_box.children = []
    # editor.content_box.content = "Failed to find a match to complete #{sub_word} portion of #{word}"
  end

  editor.on_word_complete_done do |event|
    logger.puts "on_word_complete_done event: #{event}"

    # TODO: add a better way to clear content
    editor.content_box.children = []
  end
end

#set_decoration(type, &blk) ⇒ Object

Raises:

  • (ArgumentError)


130
131
132
133
134
# File 'lib/yap-shell-addon-tab-completion.rb', line 130

def set_decoration(type, &blk)
  raise ArgumentError, "Must supply block!" unless block_given?
  logger.puts "set_decoration for type=#{name.inspect}"
  @style_procs[type] = blk
end