Add a $year shortcut for selecting items within some year

This commit is contained in:
Eike Kettner 2021-03-07 13:34:35 +01:00
parent 7638dc5111
commit 31e2e99c36
3 changed files with 18 additions and 1 deletions

View File

@ -131,6 +131,14 @@ object ItemQuery {
and(date(Operator.Gte, attr, left), date(Operator.Lte, attr, right))
}
case class YearMacro(attr: DateAttr, year: Int) extends MacroExpr {
val body =
DateRangeMacro(attr, date(year), date(year + 1))
private def date(y: Int): Date =
Date(y, 1, 1).fold(throw _, identity)
}
def or(expr0: Expr, exprs: Expr*): OrExpr =
OrExpr(Nel.of(expr0, exprs: _*))

View File

@ -43,6 +43,9 @@ object DateParser {
private val dateFromToday: P[Date.DateLiteral] =
P.string("today").as(Date.Today)
val yearOnly: P[Int] =
digits4
val dateLiteral: P[Date.DateLiteral] =
P.oneOf(List(dateFromString, dateFromToday, dateFromMillis))

View File

@ -16,6 +16,9 @@ object MacroParser {
Expr.DateRangeMacro(attr, left, right)
}
private def yearMacroImpl(name: String, attr: Attr.DateAttr): P[Expr.YearMacro] =
(macroDef(name) *> DateParser.yearOnly).map(year => Expr.YearMacro(attr, year))
val namesMacro: P[Expr.NamesMacro] =
(macroDef("names") *> BasicParser.singleString).map(Expr.NamesMacro.apply)
@ -25,9 +28,12 @@ object MacroParser {
val dueDateRangeMacro: P[Expr.DateRangeMacro] =
dateRangeMacroImpl("duein", Attr.DueDate)
val yearDateMacro: P[Expr.YearMacro] =
yearMacroImpl("year", Attr.Date)
// --- all macro parser
val all: P[Expr] =
P.oneOf(List(namesMacro, dateRangeMacro, dueDateRangeMacro))
P.oneOf(List(namesMacro, dateRangeMacro, dueDateRangeMacro, yearDateMacro))
}