def do_stopwatch(self, line): """Creates a new stopwatch for recording time for a particular task. """ parser = ArgumentParser(stdout=self.stdout, prog="stopwatch", add_help=False) parser.add_argument("task", metavar="TASK") parser.add_argument("metric", metavar="METRIC", nargs="?") args = parser.parse_args(line.split()) if not args.task: self._print() self._error("Invalid arguments") return # Get task with specified name and optional metric task = self.current_project.task(args.task, create=False) if not task: self._error("Task {} does not exist.".format(args.task)) return if args.metric: metric = self.current_project.metric(args.metric) if not metric: self._error("Metric {} does not exist.".format(args.metric)) return else: metric = None # Create a stop watch and UI self._print("\n (R)eset | (S)tart | (P)ause | S(t)op\n") self.stdout.write(" Stopped\t--:--:--\r") self.stdout.flush() stopwatch_active = True stopwatch = StopWatch() with cbreak(): while stopwatch_active: user_input_int = ord(self.stdin.read(1)) if 0 <= user_input_int <= 256: user_input = chr(user_input_int).upper() if user_input == "S": stopwatch.start(tick_callback=self._update_printout) elif user_input == "P": stopwatch.pause() elif user_input == "R": stopwatch.reset() elif user_input == "T": stopwatch.stop() stopwatch_active = False # At this point, stopwatch has been stopped, so now attempt to assign # its total duration to the task. if metric: self._assign_time(task, metric, stopwatch.total) else: self._assign_time_interactive(task, stopwatch.total) self.projects.save(self.current_project)
def test_stopwatch_pause(): s = StopWatch() s.start() time.sleep(1) s.pause() time.sleep(1) s.start() time.sleep(1) s.stop() assert timedelta(seconds=1) <= s.total <= timedelta(seconds=3)