Module: Meshchat::Ui::Command::NodeFinder
- Defined in:
- lib/meshchat/ui/command/node_finder.rb
Class Method Summary collapse
- .ask_for_specification(nodes, block) ⇒ Object
- .display_nodes(nodes) ⇒ Object
- .find_by_target(string, &block) ⇒ Object
Class Method Details
.ask_for_specification(nodes, block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/meshchat/ui/command/node_finder.rb', line 19 def ask_for_specification(nodes, block) # there are now more than 1 nodes display_nodes(nodes) # insert a callback into the input handler to run the next # time a line is received # # TODO: this feels gross, is there a better way? # TODO: move this callback from ReadlineInput to the Input Base CLI::ReadlineInput.input_handler.callback_on_next_tick = lambda do |line| answer = line.to_i node = nodes[answer] # finally, send the mesasge # (or do whatever this is) # but usually, it sending the message block.call(node) end end |
.display_nodes(nodes) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/meshchat/ui/command/node_finder.rb', line 38 def display_nodes(nodes) # write header Display.info "\t\t UID Last Seen" Display.info '-' * 60 # write nodes nodes.each_with_index do |node, index| i = index.to_s alias_name = node.alias_name uid = node.uid[0..5] last_seen = node.updated_at&.strftime('%B %e, %Y %H:%M:%S') || 'never' line = '%-2s | %-15s %-8s %s' % [i, alias_name, uid, last_seen] Display.info line end end |
.find_by_target(string, &block) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/meshchat/ui/command/node_finder.rb', line 8 def find_by_target(string, &block) search_key = string.start_with?('#') ? :uid : :alias_name nodes = Node.where(search_key => string) return Display.warning('No node by: ' + string) if nodes.empty? return yield(nodes.first) if nodes.length == 1 Display.warning I18n.t('node.multiple_with_alias', name: string) ask_for_specification(nodes, block) end |