Class: UserStatsDisplay

Inherits:
Object
  • Object
show all
Defined in:
lib/tng/ui/user_stats_display.rb

Instance Method Summary collapse

Constructor Details

#initialize(pastel, prompt) ⇒ UserStatsDisplay

Returns a new instance of UserStatsDisplay.



10
11
12
13
14
15
16
17
18
# File 'lib/tng/ui/user_stats_display.rb', line 10

def initialize(pastel, prompt)
  @pastel = pastel
  @prompt = prompt
  @terminal_width = begin
    TTY::Screen.width
  rescue StandardError
    80
  end
end

Instance Method Details

#determine_bar_color(usage_percent) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/tng/ui/user_stats_display.rb', line 127

def determine_bar_color(usage_percent)
  case usage_percent
  when 0..50
    @pastel.public_send(Tng::UI::Theme.color(:success), "█")
  when 51..80
    @pastel.public_send(Tng::UI::Theme.color(:warning), "█")
  when 81..95
    @pastel.public_send(Tng::UI::Theme.color(:info), "█")
  else
    @pastel.public_send(Tng::UI::Theme.color(:error), "█")
  end
end

#display(stats_data) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tng/ui/user_stats_display.rb', line 20

def display(stats_data)
  header = @pastel.public_send(Tng::UI::Theme.color(:primary)).bold("#{Tng::UI::Theme.icon(:stats)} User Statistics")
  puts Tng::UI::Theme.center_text(header, @terminal_width)

  if stats_data
    display_stats_box(stats_data)
    display_usage_progress(stats_data)
    display_usage_info(stats_data)
  else
    display_error
  end

  puts
  tip_msg = @pastel.public_send(Tng::UI::Theme.color(:muted),
                                "#{Tng::UI::Theme.icon(:lightbulb)} Tip: Contact support if you need more test generations")
  puts Tng::UI::Theme.center_text(tip_msg, @terminal_width)
  @prompt.keypress(Tng::UI::Theme.center_text(
                     @pastel.public_send(Tng::UI::Theme.color(:muted),
                                         "Press any key to continue..."), @terminal_width
                   ))
end

#display_errorObject



152
153
154
155
156
157
158
159
# File 'lib/tng/ui/user_stats_display.rb', line 152

def display_error
  error_msg = @pastel.public_send(Tng::UI::Theme.color(:error)).bold("#{Tng::UI::Theme.icon(:error)} Failed to fetch user statistics")
  puts Tng::UI::Theme.center_text(error_msg, @terminal_width)
  puts Tng::UI::Theme.center_text(
    @pastel.public_send(Tng::UI::Theme.color(:muted),
                        "Please check your API key and internet connection"), @terminal_width
  )
end

#display_stats_box(stats_data) ⇒ Object



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/tng/ui/user_stats_display.rb', line 42

def display_stats_box(stats_data)
  stats_content = [
    @pastel.public_send(Tng::UI::Theme.color(:secondary)).bold("Account Information"),
    "",
    @pastel.public_send(Tng::UI::Theme.color(:primary), "Test Runs: ") + @pastel.public_send(Tng::UI::Theme.color(:success)).bold(stats_data["runs"].to_s),
    @pastel.public_send(Tng::UI::Theme.color(:primary), "Max Runs: ") + @pastel.public_send(Tng::UI::Theme.color(:warning)).bold(stats_data["max_runs"].to_s),
    @pastel.public_send(Tng::UI::Theme.color(:primary),
                        "Gem Version: ") + @pastel.public_send(Tng::UI::Theme.color(:accent),
                                                               stats_data["gem_version"] || "N/A"),
    "",
    @pastel.public_send(Tng::UI::Theme.color(:muted), "Request ID: #{stats_data["request_id"]}")
  ].join("\n")

  box_width = Tng::UI::Theme.calculate_box_width(@terminal_width)
  style = Tng::UI::Theme.box_style(:success)

  box = TTY::Box.frame(
    title: { top_left: " Your TNG Stats " },
    style: style[:style],
    padding: style[:padding],
    align: :left,
    width: box_width
  ) { stats_content }

  puts Tng::UI::Theme.center_box(box, box_width, @terminal_width)
end

#display_usage_info(stats_data) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/tng/ui/user_stats_display.rb', line 140

def display_usage_info(stats_data)
  usage_remaining = stats_data["max_runs"] - stats_data["runs"]
  usage_msg = if usage_remaining.positive?
                @pastel.public_send(Tng::UI::Theme.color(:success),
                                    "#{Tng::UI::Theme.icon(:success)} You have #{usage_remaining} test generations remaining")
              else
                @pastel.public_send(Tng::UI::Theme.color(:error),
                                    "#{Tng::UI::Theme.icon(:warning)} You have reached your test generation limit")
              end
  puts Tng::UI::Theme.center_text(usage_msg, @terminal_width)
end

#display_usage_progress(stats_data) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/tng/ui/user_stats_display.rb', line 69

def display_usage_progress(stats_data)
  runs = stats_data["runs"]
  max_runs = stats_data["max_runs"]

  usage_percent = max_runs > 0 ? (runs.to_f / max_runs * 100).round(1) : 0

  bar_color = determine_bar_color(usage_percent)

  puts
  puts Tng::UI::Theme.center_text(@pastel.public_send(Tng::UI::Theme.color(:primary)).bold("Usage Overview"),
                                  @terminal_width)
  puts

  progress_width = 40
  padding = (@terminal_width - progress_width - 20) / 2
  padding = 0 if padding.negative?

  bar_format = "#{" " * padding}Usage: [:bar] :percent (:current/:total)"

  bar = TTY::ProgressBar.new(bar_format,
                             total: max_runs,
                             width: progress_width,
                             complete: bar_color,
                             incomplete: @pastel.public_send(Tng::UI::Theme.color(:muted), "░"),
                             head: bar_color)

  current_progress = 0
  while current_progress < runs
    step = [runs / 10, 1].max
    current_progress = [current_progress + step, runs].min
    bar.current = current_progress
    bar.render
    sleep(0.05) if runs > 10
  end

  bar.current = runs
  bar.render

  puts

  status_msg = case usage_percent
               when 0..50
                 @pastel.public_send(Tng::UI::Theme.color(:success),
                                     "#{Tng::UI::Theme.icon(:success)} Good usage - plenty of runs remaining")
               when 51..80
                 @pastel.public_send(Tng::UI::Theme.color(:warning),
                                     "#{Tng::UI::Theme.icon(:warning)} Moderate usage - consider monitoring")
               when 81..95
                 @pastel.public_send(Tng::UI::Theme.color(:info),
                                     "#{Tng::UI::Theme.icon(:warning)} High usage - approaching limit")
               else
                 @pastel.public_send(Tng::UI::Theme.color(:error),
                                     "#{Tng::UI::Theme.icon(:error)} Limit reached - contact support for more runs")
               end

  puts Tng::UI::Theme.center_text(status_msg, @terminal_width)
end