Module: NineteenNinetynine::Output

Included in:
NineteenNinetynine
Defined in:
lib/nineteen_ninetynine/output.rb

Instance Method Summary collapse

Instance Method Details

#color_of(name) ⇒ Object



79
80
81
82
# File 'lib/nineteen_ninetynine/output.rb', line 79

def color_of(name)
  colors =  (31..36).to_a + (91..96).to_a
  colors[name.delete("^0-9A-Za-z_").to_i(36) % colors.size]
end

#insert(*messages) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nineteen_ninetynine/output.rb', line 56

def insert(*messages)
  monitor.synchronize do
    begin
      try_swap = !$stdout.is_a?(StringIO)
      $stdout = StringIO.new if try_swap

      puts messages
      yield if block_given?

      unless $stdout.string.empty?
        STDOUT.print "\e[0G\e[K#{$stdout.string}"
        Readline.refresh_line
      end
    ensure
      $stdout = STDOUT if try_swap
    end
  end
end

#monitorObject



75
76
77
# File 'lib/nineteen_ninetynine/output.rb', line 75

def monitor
  @monitor ||= Monitor.new
end

#output(name = nil, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nineteen_ninetynine/output.rb', line 3

def output(name = nil, &block)
  if block
    outputs.delete_if { |o| o[:name] == name } if name
    outputs << { name: name, block: block }
  else
    insert do
      while note = @notes.shift
        if note.user.nil?
          sleep 1
          # users = $cache.read("users")
          user = @users.find { |u| u.pubkey == note.raw["pubkey"] }
          note.user = user
          puts_items(note)
        else
          puts_items(note)
        end
      end
    end
  end
end

#output_filter(&block) ⇒ Object



28
29
30
# File 'lib/nineteen_ninetynine/output.rb', line 28

def output_filter(&block)
  output_filters << block
end

#output_filtersObject



24
25
26
# File 'lib/nineteen_ninetynine/output.rb', line 24

def output_filters
  @output_filters ||= []
end

#outputsObject



32
33
34
# File 'lib/nineteen_ninetynine/output.rb', line 32

def outputs
  @outputs ||= []
end

#puts_items(items) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nineteen_ninetynine/output.rb', line 36

def puts_items(items)
  mark_color = colors.sample + 10

  [items].flatten.reverse_each do |item|
    next if output_filters.any? { |f| f.call(item) == false }

    if item.body && !item._stream
      item._mark = ' '.c(mark_color) + item._mark.to_s
    end

    outputs.each do |o|
      begin
        o[:block].call(item)
      rescue => e
        error e
      end
    end
  end
end