Created
October 5, 2010 17:57
-
-
Save reactormonk/612006 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module FastChoice | |
| class Choice | |
| include Comparable | |
| INTERESSTING = 0.2 | |
| def initialize(element, action) | |
| @element, @action = element, action | |
| self.score = 0.0 | |
| end | |
| attr_reader :value, :action | |
| attr_accessor :score | |
| def <=>(other) | |
| return super unless other.is_a?(Choice) | |
| self.score <=> other.score | |
| end | |
| def interessting? | |
| score > INTERESSTING | |
| end | |
| def match(matcher) | |
| self.score = matcher.match(element) | |
| end | |
| # @abstract | |
| def call | |
| raise NotImplementedError | |
| end | |
| # @abstract | |
| def on_select | |
| end | |
| # @abstract | |
| def on_unselect | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module FastChoice | |
| class Entry < Tk::Tile::Entry | |
| def initialize(*args) | |
| super | |
| setup! | |
| end | |
| def setup! | |
| bind '<Enter>', @tree.method(:action) | |
| bind '<Tab>', @tree.method(:on_tab) | |
| end | |
| end | |
| class Choice | |
| UNINTERESSTING = 0.1 | |
| def initialize(element, action) | |
| @element, @action = element, action | |
| self.score = 0.0 | |
| end | |
| attr :score | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'amatch' | |
| module FastChoice | |
| class Tree < Tk::Tile::Treeview | |
| MAX_ENTRIES = 20 | |
| MATCHER = Amatch::PairDistance | |
| def initialize(*args) | |
| super | |
| setup! | |
| reset! | |
| end | |
| # @param [Array<Choice>] choices Array of possible choices @see Choice | |
| attr_accessor :choices | |
| private | |
| def setup! | |
| configure columns: %w(action value), displaycolumns: %w(action value) | |
| heading 'action', 'Action' | |
| heading 'value', 'Value' | |
| column 'action', stretch: 0, width: 80 | |
| end | |
| def reset! | |
| @choices = [] | |
| @last_needles = [] | |
| clear | |
| end | |
| # @param [String] needle the needle to create matches for. | |
| def completion(needle) | |
| matcher = MATCHER.new(needle) | |
| items = [] | |
| choices = @choices.dup | |
| choices.each do |choice| | |
| choice.match(matcher) | |
| items << choice if choice.interresting? | |
| end | |
| items.sort! | |
| items = items.take(MAX_ENTRIES) | |
| @last_needles.push(items) | |
| items | |
| end | |
| # Generates @item_hash as well, for mapping id => ruby object | |
| # @param [Array<Choice>] choices to insert into the tree. | |
| def display(choices) | |
| clear | |
| @item_hash = {} | |
| choices.each do |choice| | |
| id = insert('', :end, values: [choice.action, choice.value]).id | |
| @item_hash[id] = choice | |
| end | |
| end | |
| def action | |
| @item_hash[selected.id].call | |
| end | |
| def on_char_delete | |
| display(@last_needles.pop) | |
| end | |
| def on_tab | |
| @item_hash[selected.id].on_unselect | |
| next_item = selected.next | |
| selection_set(next_item) | |
| @item_hash[next_item].on_select | |
| end | |
| def on_new_char(needle) | |
| display(completion(needle)) | |
| end | |
| def selected | |
| selection.first | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment