Class: Teletype::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/teletype/pager.rb

Overview

Pager divides lines of text into proper screen size and provides page suggestion based on statistics of click accuracy.

Constant Summary collapse

SUGGEST =
1

Instance Method Summary collapse

Constructor Details

#initialize(lines, stats, height) ⇒ Pager

Returns a new instance of Pager.



9
10
11
12
13
# File 'lib/teletype/pager.rb', line 9

def initialize(lines, stats, height)
  @lines = lines
  @stats = stats
  @height = height
end

Instance Method Details

#eachObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/teletype/pager.rb', line 15

def each
  @lines.each_slice(@height).map do |lines|
    yield lines

    loop do
      suggestions = @stats.suggestions
      break if suggestions.empty?

      yield pick(suggestions)
    end
  end
end

#pick(suggestions) ⇒ Object



28
29
30
31
# File 'lib/teletype/pager.rb', line 28

def pick(suggestions)
  index = 0.0
  @lines.sort_by { |line| suggestions.map { |keys| -line.scan(keys).count }.sum + (index += 0.0001) }.first(SUGGEST)
end