Class: EntryWindow

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CharacterCleaner

#process_entities_and_utf

Constructor Details

#initialize(scr, feed_title, title, content, item, search_string = nil) ⇒ EntryWindow

Returns a new instance of EntryWindow.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/entry_window.rb', line 8

def initialize(scr, feed_title, title, content, item, search_string=nil)
  @item = item
  @height = scr.maxy - 10  # This is the height on the content area. The title box is separate
  @width = scr.maxx - 2
  
  @feed_title = feed_title
  @search_string = search_string

  @title = title
  @title_box = Curses::Window.new(3, @width, 2, 5) 

  @window = Curses::Window.new(@height, @width, 7, 5) 

  @content = content

  @lines = content.split("\n")
  @page_index = 0
  @num_pages = (@lines.size / @height) + 1
  #
  #scr.clear
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/entry_window.rb', line 6

def content
  @content
end

Instance Method Details

#addstr_with_highlighting(string, highlighted_string) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/entry_window.rb', line 62

def addstr_with_highlighting(string, highlighted_string)
  text = string
  text = text.gsub(/(#{highlighted_string})/i, '%%%\1%%%') 
  text.split(/%%%/).each_with_index do |chunk, index|
    if index % 2 == 0
      @window.addstr(chunk)
    else 
      @window.addstr(chunk, :yellow)
    end
  end

end

#closeObject



115
116
117
# File 'lib/entry_window.rb', line 115

def close
  @window.close
end

#drawObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/entry_window.rb', line 75

def draw
  draw_title
  @window.setpos(0, 0)
  @window.clear
  if @search_string
    page_content.each do |line|
      addstr_with_highlighting(line + "\n", @search_string)
    end
  else
    page_content.each do |line|
      @window.addstr(line + "\n")
    end
  end

  @window.refresh
end

#draw_titleObject

TODO add a page range



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
# File 'lib/entry_window.rb', line 32

def draw_title
  @title_box.clear
  
  @title_box.addstr(@feed_title)
  @title_box.setpos(2, 0)
  color = case 
            when @item.flagged
              :red
            else
              nil
            end

  if @search_string && @title =~ /#{@search_string}/i
    text = @title.gsub(/(#{@search_string})/i, '%%%\1%%%') 
    text.split(/%%%/).each_with_index do |chunk, index|
      if index % 2 == 0
        @title_box.addstr(chunk, color)
      else 
        @title_box.addstr(chunk, :yellow)
      end
    end
  else
    @title_box.addstr(@title, color)
  end

  @title_box.addstr(" (%s/%s)" % [@page_index + 1 ,  @num_pages], :cyan)
  @title_box.refresh
end

#next_pageObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/entry_window.rb', line 92

def next_page
  LOGGER.debug("Trying to next_page")
  LOGGER.debug("Page index: #{@page_index}; lines: #{@lines.size}; height: #{@height}")
  if (@page_index + 1) * @height >= @lines.size
    LOGGER.debug('exiting')
    # Go back to entries menu
    return :exit
  else
    @page_index += 1
    draw
  end
end

#page_contentObject



119
120
121
122
# File 'lib/entry_window.rb', line 119

def page_content
  line_offset = @page_index * @height
  @lines[line_offset, @height]
end

#prev_pageObject



105
106
107
108
109
110
111
112
113
# File 'lib/entry_window.rb', line 105

def prev_page
  LOGGER.debug("Trying to prev_page")
  if @page_index == 0
    return :exit
  else
    @page_index -= 1
    draw
  end
end

#teletypeObject

turned off for now



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/entry_window.rb', line 133

def teletype
  
  # used to calibrate the speed of typing
  interval = 3.0 / (height * 80)
  sleep 0.4

  @content.chars.length.times do |x|
    character = @content.chars[x,1]
    if character !~ /\S/
      # add to a buffer, so you output all the spaces at once
      @window.addstr(character)
    else
      @window.addstr(character.chars)
      @window.refresh
      sleep interval
    end
  end

end

#toggle_flagObject



124
125
126
127
128
129
130
# File 'lib/entry_window.rb', line 124

def toggle_flag
  if @item.flagged 
    @item.update_attribute(:flagged, nil)
  else
    @item.update_attribute(:flagged, Time.now)
  end
end