Module: SimpleFeed::DSL::Formatter

Includes:
SimpleFeed::DSL
Included in:
Activities
Defined in:
lib/simplefeed/dsl/formatter.rb

Overview

This module exports method #color_dump which receives an activity and then prints out a report about the activity, including the event data found for a given user.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SimpleFeed::DSL

#_puts, debug?, #event, #feed_header, #field, #field_label, #field_value, #header, #hr, #hr_string, #output, #width, #with_activity

Instance Attribute Details

#activityObject

Returns the value of attribute activity.



18
19
20
# File 'lib/simplefeed/dsl/formatter.rb', line 18

def activity
  @activity
end

#feedObject

Returns the value of attribute feed.



18
19
20
# File 'lib/simplefeed/dsl/formatter.rb', line 18

def feed
  @feed
end

Instance Method Details

#color_dump(this_activity = activity) ⇒ Object



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
67
# File 'lib/simplefeed/dsl/formatter.rb', line 20

def color_dump(this_activity = activity)
  this_activity = if this_activity.is_a?(SimpleFeed::Activity::SingleUser)
                    this_activity.feed.activity([this_activity.user_id])
                  else
                    this_activity
                  end
  _puts

  feed_header(feed) do
    [
      field('Feed Name', feed.name, "\n"),
      field('Provider', feed.provider.provider.class, "\n"),
      field('Max Size', feed.max_size, "\n")
    ]
  end

  with_activity(this_activity) do
    this_activity.each_with_index do |user_id, index|
      this_last_event_at = nil
      this_last_read     = (last_read[user_id] || 0.0).to_f

      fields = []
      [['User ID', user_id, "\n"],
       ['Activities', sprintf('%d total, %d unread', total_count[user_id], unread_count[user_id]), "\n"],
       ['Last Read', this_last_read ? Time.at(this_last_read) : 'N/A'],].each do |field, value, *args|
        fields << field(field, value, *args)
      end

      header(title: { top_center: " « User Activity #{index + 1} » " }, style: { fg: :green }) { fields }

      this_events       = fetch[user_id]
      this_events_count = this_events.size
      this_events.each_with_index do |evt, idx|
        if this_last_event_at.nil? && evt.at < this_last_read
          print_last_read_separator(this_last_read)
        elsif this_last_event_at && this_last_read < this_last_event_at && this_last_read > evt.at
          print_last_read_separator(this_last_read)
        end

        this_last_event_at = evt.at # float
        output "[%2d] %16s %s\n", idx, evt.time.strftime(TIME_FORMAT).blue.bold, evt.value
        if idx == this_events_count - 1 && this_last_read < evt.at
          print_last_read_separator(this_last_read)
        end
      end
    end
  end
end


69
70
71
# File 'lib/simplefeed/dsl/formatter.rb', line 69

def print_last_read_separator(lr)
  output "———— %16s [last read] ———————————— \n", Time.at(lr).strftime(TIME_FORMAT).red.bold
end