Class: MenuPager

Inherits:
Object
  • Object
show all
Includes:
CharacterCleaner
Defined in:
lib/menu_pager.rb

Overview

This is initialized with the screen area of the window of screen, and then handles showing the visible area of content for menus.

Instance Method Summary collapse

Methods included from CharacterCleaner

#process_entities_and_utf

Constructor Details

#initialize(window_height, window_width) ⇒ MenuPager

Returns a new instance of MenuPager.



7
8
9
10
# File 'lib/menu_pager.rb', line 7

def initialize(window_height, window_width)
  @window_height = window_height
  @window_width = window_width
end

Instance Method Details

#calculate_page_for_index(index) ⇒ Object



68
69
70
# File 'lib/menu_pager.rb', line 68

def calculate_page_for_index( index )
  index / @window_height 
end

#create_page(items, current_index, matches = nil, options = {}) ⇒ Object

Pass in an array of lines or a string. Returns an array of hashes. Each hash has a key :item that holds the item object and a key :text that holds the text for the item.

We don’t do anything with the matches parameters yet. May use it later for highlighting. But then we would also need to pass in the search term.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/menu_pager.rb', line 20

def create_page(items, current_index, matches=nil, options={})
  out = []
  pages = []
  items.each_slice(@window_height) do |slice|
    pages << slice
  end
  page = calculate_page_for_index( current_index ) 
  items_on_page = pages[page] 

  items_on_page.each_with_index do |x, i|
    # Space on the left for the arrow
    # Truncate title if it is longer than the width of the window

    if x.is_a?(Entry)
      title = process_entities_and_utf(x.title).strip
    else
      # Put in a line number if this is a Feed in the Feed Menu
      index = items.index(x)
      title = "%2d " % (index + 1) + x.title 
      title = process_entities_and_utf(title)
    end

    title = "   " + title
    if x.is_a?(Feed) # put the feed count 
      title += " (#{x.entries.count})" 

    elsif x.is_a?(VirtualFeed) # put the feed count 
      title += " (#{x.entry_count})"
    end
#      if x.respond_to?(:categories) and !x.categories.empty?
#        title += " - [#{x.categories.join(', ')}]"
#      end

    maxwidth = @window_width - 18 
    full_title = [title]
    full_title << x.feed.title if options[:show_feed_titles]
    full_title << time_ago_in_words(x.date_published) if x.is_a?(Entry)
    excess = full_title.join(" ").length - maxwidth
    if excess > 0
      title = title[0, title.length - excess] + "..."
    end
    out << {:item => x, :text => title}
  end

  # page 0 is the first page
  [page, out]
end