sus 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sus/progress.rb DELETED
@@ -1,144 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
22
-
23
- require_relative 'output/bar'
24
- require_relative 'output/status'
25
- require_relative 'output/lines'
26
-
27
- module Sus
28
- class Progress
29
- def self.now
30
- ::Process.clock_gettime(Process::CLOCK_MONOTONIC)
31
- end
32
-
33
- def initialize(output, total = 0, minimum_output_duration: 1.0)
34
- @output = output
35
- @subject = subject
36
-
37
- @start_time = Progress.now
38
-
39
- if @output.interactive?
40
- @bar = Output::Bar.new
41
- @lines = Output::Lines.new(@output)
42
- @lines[0] = @bar
43
- end
44
-
45
- @current = 0
46
- @total = total
47
- end
48
-
49
- attr :subject
50
- attr :current
51
- attr :total
52
-
53
- def duration
54
- Progress.now - @start_time
55
- end
56
-
57
- def progress
58
- @current.to_f / @total.to_f
59
- end
60
-
61
- def remaining
62
- @total - @current
63
- end
64
-
65
- def average_duration
66
- if @current > 0
67
- duration / @current
68
- end
69
- end
70
-
71
- def estimated_remaining_time
72
- if average_duration = self.average_duration
73
- average_duration * remaining
74
- end
75
- end
76
-
77
- # Increase the amont of work done.
78
- def increment(amount = 1)
79
- @current += amount
80
-
81
- @bar&.update(@current, @total, self.to_s)
82
- @lines&.redraw(0)
83
-
84
- return self
85
- end
86
-
87
- # Increase the total size of the progress.
88
- def expand(amount = 1)
89
- @total += amount
90
-
91
- @bar&.update(@current, @total, self.to_s)
92
- @lines&.redraw(0)
93
-
94
- return self
95
- end
96
-
97
- def report(index, context, state)
98
- @lines&.[]=(index+1, Output::Status.new(state, context))
99
-
100
- return self
101
- end
102
-
103
- def clear
104
- @lines&.clear
105
- end
106
-
107
- def to_s
108
- if estimated_remaining_time = self.estimated_remaining_time
109
- "#{@current}/#{@total} completed in #{formatted_duration(self.duration)}, #{formatted_duration(estimated_remaining_time)} remaining"
110
- else
111
- "#{@current}/#{@total} completed"
112
- end
113
- end
114
-
115
- private
116
-
117
- def formatted_duration(duration)
118
- seconds = duration.floor
119
-
120
- if seconds < 60.0
121
- return "#{seconds}s"
122
- end
123
-
124
- minutes = (duration / 60.0).floor
125
- seconds = (seconds - (minutes * 60)).round
126
-
127
- if minutes < 60.0
128
- return "#{minutes}m#{seconds}s"
129
- end
130
-
131
- hours = (minutes / 60.0).floor
132
- minutes = (minutes - (hours * 60)).round
133
-
134
- if hours < 24.0
135
- return "#{hours}h#{minutes}m"
136
- end
137
-
138
- days = (hours / 24.0).floor
139
- hours = (hours - (days * 24)).round
140
-
141
- return "#{days}d#{hours}h"
142
- end
143
- end
144
- end