Skip to content

Commit

Permalink
Add elapsed property
Browse files Browse the repository at this point in the history
  • Loading branch information
avylove committed Oct 13, 2017
1 parent 8f3e979 commit f02669c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
20 changes: 15 additions & 5 deletions enlighten.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,20 @@ def position(self):

return self.manager.counters.get(self, 0)

@property
def elapsed(self):
"""
Get elapsed time is seconds (float)
"""

# Clock stops running when total is reached
if self.count == self.total:
elapsed = self.last_update - self.start
else:
elapsed = time.time() - self.start

return elapsed

def clear(self, flush=True):
"""
Args:
Expand Down Expand Up @@ -338,11 +352,7 @@ def format(self, width=None, elapsed=None):

# Get elapsed time
if elapsed is None:
# Clock stops running when total is reached
if self.count == self.total:
elapsed = self.last_update - self.start
else:
elapsed = time.time() - self.start
elapsed = self.elapsed

fields['elapsed'] = _format_time(elapsed)

Expand Down
11 changes: 11 additions & 0 deletions tests/test_enlighten.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ def test_refresh_total(self):
def test_position(self):
self.assertEqual(self.ctr.position, 3)

def test_elapsed(self):
ctr = self.ctr
ctr.start = time.time() - 5.0
ctr.last_update = ctr.start + 3.0

self.assertEqual(int(ctr.elapsed), 5)

# Clock stops running when total is reached
ctr.count = ctr.total
self.assertEqual(int(ctr.elapsed), 3)

def test_refresh(self):
self.ctr.refresh()
self.assertRegex(self.manager.output[0],
Expand Down

0 comments on commit f02669c

Please sign in to comment.