Experience
Projects
My Stack
Case studies
Contact me
Open my CV

·5 min read

TaskPad deadlines: why due:+3d becomes a date as you type, and date-only means 23:59:59

A deadline in a plain-text task note looks trivial until you decide what the text actually stores. If I type due:+3d and the file keeps those characters, then tomorrow the same line means a different day, and so does every line in every old note. TaskPad's deadline feature, src/Due.cs, is built around refusing that: the text must hold a fixed moment, and the countdown next to it must be computed fresh.

Project
TaskPad
Date
Oct 2026
Stack
C#, WPF, AvalonEdit
Commit
e6563be

What the token accepts

The grammar is one compiled regex. A token is due: followed by a date, a date with a time, or only a time, or the wrapped form @due(...):

csharp
public static readonly Regex Token = new Regex(
    @"(?<![\w@])(?:due:(?<v>\d{4}-\d{2}-\d{2}(?:[ T]\d{1,2}:\d{2})?|\d{1,2}:\d{2})|@due\((?<v>[^)\r\n]*)\))",
    RegexOptions.Compiled | RegexOptions.IgnoreCase);

The (?<![\w@]) lookbehind means due: must not be glued to the end of a word, so something like overdue:2026-01-01 in prose is not treated as a deadline. Parsing then happens separately in TryParse, which tries four exact formats: yyyy-MM-dd HH:mm, yyyy-MM-dd H:mm, yyyy-MM-ddTHH:mm and yyyy-MM-dd, all with the invariant culture so a machine with a Georgian or German regional setting parses the same text the same way.

A date without a time

The decision I thought about longest is what due:2026-10-10 means. Midnight at the start of the day would mark the task overdue while the person still has the whole day. So a date-only value becomes the last second of that date:

csharp
if (!hasTime) due = due.Date.AddDays(1).AddSeconds(-1);

The consequence is visible in the pill: on the due day it reads "in 14h 22m" or similar until the evening, and it only turns to "overdue" after 23:59:59. A time-only token, due:18:00, resolves to today at that time at the moment it is parsed.

Relative words are expanded, not stored

This is the part that gives the feature its shape. While you type, ExpandBeforeCaret runs after a space or Enter. It looks at the text before the caret, matches a second regex for relative forms, and replaces the match with a concrete date:

csharp
var when = Resolve(m.Groups["v"].Value, out bool withTime);
var value = withTime ? when.Value.ToString("yyyy-MM-dd HH:mm") : when.Value.ToString("yyyy-MM-dd");
var text = m.Groups["k"].Value.StartsWith("@") ? "@due(" + value + ")" : "due:" + value;
doc.Replace(line.Offset + m.Index, m.Length, text);

The forms it understands are +N followed by a unit (minutes, hours, days, weeks, months, years), today, tonight, tomorrow and tmr, weekday names, next week and next month. Two choices inside Resolve are worth knowing. tonight means 20:00 on the current day. A weekday name that matches today's weekday means next week, not today, because the formula (idx - (int)now.DayOfWeek + 7) % 7 yields 0 and the code turns 0 into 7. Typing due:friday on a Friday therefore gives a week from now. Hour and minute offsets are rounded up to the next whole minute, so +30min does not land on 14:07:41.

The result is that the file on disk only ever contains due:2026-10-13-style text, which any other editor can read and which will mean the same thing next year.

Drawing the countdown

The visible part is DueGenerator, an AvalonEdit VisualLineElementGenerator. For each due token on a line it constructs an inline element: a bordered pill with a clock glyph, the pretty date ("Today", "Tomorrow", "Fri 10 Oct", or a full date for another year), and a bold countdown such as in 3d 4h, in 42m or 2d overdue. Border and countdown colour come from StateBrush: done is its own colour, overdue is the alert colour, under 24 hours is the "todo" colour, under 7 days the accent, and anything further is dim.

The countdown text itself comes from Countdown(due, now), a pure function of two dates. It picks units by magnitude: months from 60 days up, weeks and days from 14 days, days and hours from one day, hours and minutes from one hour, then minutes, then <1m left or due now. Because it is pure and takes now as a parameter, the formatting is easy to reason about without a running UI.

The pill replaces the raw text only when the caret is elsewhere. The generator returns nothing for the caret's own line, which is how you can still edit due:2026-10-10 18:00 as plain text. A click on the pill opens a picker instead.

Keeping it cheap

Every visible line passes through the generator on every redraw, so the per-line check has to be nearly free. Next first calls text.IndexOf("due", rel, StringComparison.OrdinalIgnoreCase) and returns immediately when there is no hit. Only lines that contain the three letters get the regex. Scan, which collects deadlines for other features, adds two more early exits: lines shorter than 8 characters are skipped, and lines inside fenced code are skipped, as are whole code files.

Time moves on without any edit, so there is a timer. Due.Start() creates a DispatcherTimer with a 30 second interval. On each tick it checks tray reminders, forces a redraw of every editor view, and updates the status bar of every window. I picked 30 seconds because the smallest unit the countdown shows is a minute, so a faster tick would repaint with no visible change.

What I took from it

Three rules came out of this. Store the thing that cannot change, and compute the thing that changes. Convert human shorthand at the moment of input, while the context that gives it meaning, today's date, is still true. And when a UI is drawn from text, make the cheap rejection test the first line of the hot path. None of it is clever. It is the difference between a deadline that is still right next week and one that quietly moves.

#csharp#wpf#avalonedit#datetime#text-editor

Keep reading

Have a project in mind?