Class: Vnews::Display
- Inherits:
-
Object
- Object
- Vnews::Display
- Defined in:
- lib/vnews/display.rb
Class Method Summary collapse
Instance Method Summary collapse
- #cat_items(*guids) ⇒ Object
-
#col(string, width) ⇒ Object
returns items as a list, most recent first e.g.
- #delete_items(*guids) ⇒ Object
-
#feed_items(*feed_selection) ⇒ Object
look up feed up idx.
-
#feeds(order) ⇒ Object
returns feeds as a list, sorted alphabetically e.g.
- #folder_items(window_width, folder) ⇒ Object
-
#folders ⇒ Object
returns folders as a list.
- #format_date(d) ⇒ Object
- #format_item(item) ⇒ Object
- #format_item_summary(i, width) ⇒ Object
-
#format_long_date(d) ⇒ Object
for item display.
-
#initialize ⇒ Display
constructor
A new instance of Display.
- #search_items(window_width, term) ⇒ Object
- #show_item(guid, increment_read_count = false) ⇒ Object
- #star_item(guid) ⇒ Object
- #unstar_item(guid) ⇒ Object
Constructor Details
#initialize ⇒ Display
Returns a new instance of Display.
8 9 10 11 |
# File 'lib/vnews/display.rb', line 8 def initialize @sqliteclient = Vnews.sql_client @window_width = 140 end |
Class Method Details
.strip_item_count(folder) ⇒ Object
95 96 97 |
# File 'lib/vnews/display.rb', line 95 def self.strip_item_count(folder) folder.gsub(/\(\d+\)$/, '').strip end |
Instance Method Details
#cat_items(*guids) ⇒ Object
155 156 157 158 159 |
# File 'lib/vnews/display.rb', line 155 def cat_items(*guids) text = guids.map do |guid| show_item(guid, 0) end.join( "\n+" + ('-' * 78) + "+\n" ) end |
#col(string, width) ⇒ Object
returns items as a list, most recent first e.g. 96: Git on Rails”, “guid”=>“git-on-rails”, “feed”=>“feeds.feedburner.com/railscasts”, “feed_title”=>“Railscasts”, “pub_date”=>2008-03-10 00:00:00 -0400, “word_count”=>41
39 40 41 42 |
# File 'lib/vnews/display.rb', line 39 def col(string, width) return unless string string[0,width].ljust(width) end |
#delete_items(*guids) ⇒ Object
142 143 144 145 146 |
# File 'lib/vnews/display.rb', line 142 def delete_items(*guids) guids.each do |guid| @sqliteclient.delete_item guid end end |
#feed_items(*feed_selection) ⇒ Object
look up feed up idx
87 88 89 90 91 92 93 |
# File 'lib/vnews/display.rb', line 87 def feed_items(*feed_selection) window_width = feed_selection[0] feed_title = feed_selection[1].split(' ')[0..-2].join(' ') @sqliteclient.feed_items(feed_title).map do |x| format_item_summary x, window_width end end |
#feeds(order) ⇒ Object
returns feeds as a list, sorted alphabetically e.g. “feed_url”=>“bits.blogs.nytimes.com/feed/”, “link”=>“bits.blogs.nytimes.com/”
25 26 27 28 29 30 |
# File 'lib/vnews/display.rb', line 25 def feeds(order) # '0' is alphabetical, '1' is most popular first @sqliteclient.feeds(order.to_i).map.with_index do |x, idx| "#{ x['title'] } (#{x['item_count']})" end end |
#folder_items(window_width, folder) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/vnews/display.rb', line 99 def folder_items(window_width, folder) # strip off the count summary folder = self.class.strip_item_count(folder) @sqliteclient.folder_items(folder).map do |x| format_item_summary x, window_width end end |
#folders ⇒ Object
returns folders as a list
14 15 16 17 18 |
# File 'lib/vnews/display.rb', line 14 def folders @sqliteclient.folders.map do |x| "#{x["folder"]} (#{x['count']})" end.join("\n") end |
#format_date(d) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/vnews/display.rb', line 44 def format_date(d) if d.nil? "no date" elsif d.year != Time.now.year d.strftime("%b %Y") elsif d.to_date >= Time.now.to_date d.strftime("%I:%M%P") else d.strftime("%b %d") end end |
#format_item(item) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/vnews/display.rb', line 107 def format_item(item) res = <<-END #{item['feed']} #{item['feed_title']} #{format_long_date item['pub_date']} #{item['word_count']} words #{item['title']}#{item['author'] ? ("\n" + item['author']) : '' } #{item['text']} --- #{item['link']} END end |
#format_item_summary(i, width) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/vnews/display.rb', line 65 def format_item_summary(i, width) varwidth = width.to_i - 35 feed_title = col i['feed_title'], varwidth * 0.25 raw_title = i['title'].gsub(/[\n\r\t]/,' ') title = if i['score'] # search result width = varwidth * 0.75 "#{col(raw_title, (width - 6))} | #{"%.3s" % i['score']}" else col raw_title, varwidth * 0.75 end word_count = i['word_count'].to_s.rjust(6) date = format_date(i['pub_date']).rjust(8) spacer = " " * 20 # to push guid all the way off screen guid = i['guid'] flag = i['unread'] == 1 ? '+' : ' ' flag = i['starred'] == 1 ? '*' : flag "%s | %s | %s | %s | %s | %s | %s" % [flag, feed_title, title, word_count, date, spacer, guid] end |
#format_long_date(d) ⇒ Object
for item display
57 58 59 60 61 62 63 |
# File 'lib/vnews/display.rb', line 57 def format_long_date(d) if d.nil? "[no date]" else d.strftime("%a %m/%d/%Y at %I:%M%p %Z") end end |
#search_items(window_width, term) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/vnews/display.rb', line 148 def search_items(window_width, term) res = @sqliteclient.search_items(term).map do |x| format_item_summary x, window_width end res.empty? ? "No matches" : res end |
#show_item(guid, increment_read_count = false) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/vnews/display.rb', line 124 def show_item(guid, increment_read_count=false) inc_read_count = increment_read_count == '1' res = @sqliteclient.show_item(guid.strip, inc_read_count).first if res format_item(res) else "No item found" end end |
#star_item(guid) ⇒ Object
134 135 136 |
# File 'lib/vnews/display.rb', line 134 def star_item(guid) @sqliteclient.star_item guid, true end |
#unstar_item(guid) ⇒ Object
138 139 140 |
# File 'lib/vnews/display.rb', line 138 def unstar_item(guid) @sqliteclient.star_item guid, false end |