Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Simple time/task logging on the command line. Plain-text log, no dependencies.

```bash
tl add [-t|--time <value>] <description> # start a task now, or at <value> (14:30, 1h5m, 1h, 15m)
tl note <one-line note> # add a note to the current task
tl note [-t|--time <value>] <note> # add a note to the current task, now or at <value>
tl help # overview of all commands
tl version # show the version

Expand Down
39 changes: 34 additions & 5 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def test_missing_value(self):
with contextlib.redirect_stderr(io.StringIO()):
self.assertEqual(commands._get_time_flag(["-t"]), (None, None))

def test_missing_value_uses_prefix(self):
err = io.StringIO()
with contextlib.redirect_stderr(err):
commands._get_time_flag(["-t"], "tl note")
self.assertTrue(err.getvalue().startswith("tl note: -t needs a value"))


class AddTest(CommandTestCase):
def test_add_now(self):
Expand Down Expand Up @@ -137,12 +143,35 @@ def test_note(self):
self.assertEqual(out, "Note added: PR created (12:00)\n")
self.assertEqual(self.lines(), ["2026-09-23 12:00\tnote\tPR created"])

def test_time_flag_rejected(self):
for flag in ("-t", "--time"):
with self.subTest(flag=flag):
code, _, err = self.run_cmd(commands.note, [flag, "09:00", "x"])
def test_note_with_clock_time(self):
code, out, _ = self.run_cmd(commands.note, ["-t", "09:30", "Decided"])
self.assertEqual(code, 0)
self.assertEqual(out, "Note added: Decided (09:30)\n")
self.assertEqual(self.lines(), ["2026-09-23 09:30\tnote\tDecided"])

def test_note_with_duration(self):
self.run_cmd(commands.note, ["--time", "20m", "Called"])
self.assertEqual(self.lines(), ["2026-09-23 11:40\tnote\tCalled"])

def test_bad_time_flag(self):
code, out, err = self.run_cmd(commands.note, ["-t", "nope", "x"])
self.assertEqual(code, 1)
self.assertEqual(out, "")
self.assertIn("tl note: can't parse time flag 'nope'", err)
self.assertFalse(self.day_file().exists())

def test_flag_without_value(self):
code, _, err = self.run_cmd(commands.note, ["-t"])
self.assertEqual(code, 1)
self.assertIn("tl note: -t needs a value", err)

def test_flag_without_text(self):
for args in (["-t", "5m"], ["-t", "5m", " "]):
with self.subTest(args=args):
code, out, err = self.run_cmd(commands.note, args)
self.assertEqual(code, 1)
self.assertIn("not allowed", err)
self.assertEqual(out, "")
self.assertIn("description is missing", err)
self.assertFalse(self.day_file().exists())

def test_missing_text(self):
Expand Down
32 changes: 24 additions & 8 deletions timelog/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ def _now() -> datetime:
return datetime.now().replace(second=0, microsecond=0)


def _get_time_flag(args):
def _get_time_flag(args, prefix="tl add"):
"""Return (time_string_or_None, remaining_args), pulling out -t/--time and its value.
The flag is recognized only as the first argument"""
if args and args[0] in (TIME_FLAGS):
if len(args) < 2:
print(f"tl add: {args[0]} needs a value, e.g. {args[0]} 14:30", file=sys.stderr)
print(f"{prefix}: {args[0]} needs a value, e.g. {args[0]} 14:30", file=sys.stderr)
return None, None
return args[1], args[2:]
return None, args
Expand Down Expand Up @@ -56,7 +56,7 @@ def add(args: List[str]) -> int:
tl add -t 14:30 Meeting
tl add --time 1h30m Long lunch
"""
time_str, rest = _get_time_flag(args)
time_str, rest = _get_time_flag(args, "tl add")
if time_str is None and rest is None:
return 1

Expand Down Expand Up @@ -86,16 +86,32 @@ def add(args: List[str]) -> int:


def note(args: List[str]) -> int:
"""tl note <note>: add a note."""
if args and args[0] in (TIME_FLAGS):
print(render.flag_not_allowed("tl note", args[0]), file=sys.stderr)
"""tl note [-t|--time <value>] <note>: add a note.

With the flag, value is either a time (12:00) or how long ago it happened (1h30m).

Examples:
tl note PR created
tl note -t 14:30 Decided to postpone
tl note --time 20m Called the client
"""
time_str, rest = _get_time_flag(args, "tl note")
if time_str is None and rest is None:
return 1

entry = Entry(time=_now(), type="note", text=" ".join(args[0:]))
when = _now()
if time_str:
try:
when = _parse_time_flag(time_str, now=when)
except ValueError:
print(f"tl note: can't parse time flag '{time_str}'. Use HH:MM or e.g. 1h5m, 15m", file=sys.stderr)
return 1

entry = Entry(time=when, type="note", text=" ".join(rest))

if not entry.text:
print(
render.missing_argument_error("tl note", "description", "tl note <description>"),
render.missing_argument_error("tl note", "description", "tl note [-t <time>] <description>"),
file=sys.stderr
)
return 1
Expand Down
4 changes: 2 additions & 2 deletions tl.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

Commands:
add [-t | --time <value>] <description> start a task now, or at <value>
note <text> add a note to today's log
note [-t | --time <value>] <text> add a note now, or at <value>
version show the version
help show this help

Time values for -t / --time:
14:30 today at that time
1h30m, 1h, 15m how long ago the task started
1h30m, 1h, 15m how long ago it happened
""".format(version=__version__)

COMMANDS = {
Expand Down