Module: Meshchat::Ui::Command::NodeFinder

Defined in:
lib/meshchat/ui/command/node_finder.rb

Class Method Summary collapse

Class Method Details

.ask_for_specification(nodes, block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/meshchat/ui/command/node_finder.rb', line 20

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/meshchat/ui/command/node_finder.rb', line 40

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



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/meshchat/ui/command/node_finder.rb', line 7

def find_by_target(string, &block)
  search_key = string.start_with?('#') ? :uid : :alias_name
  nodes = Node.where(search_key => string)
  if nodes.length == 0
    return Display.warning('No node by: ' + string)
  end

  return block.call(nodes.first) if nodes.length == 1

  Display.warning I18n.t('node.multiple_with_alias', name: string)
  ask_for_specification(nodes, block)
end