|
| 1 | +import type { Task } from '../../Task/Task'; |
| 2 | +import { Duration } from '../../Task/Duration'; |
| 3 | +import { Explanation } from '../Explain/Explanation'; |
| 4 | +import type { Comparator } from '../Sort/Sorter'; |
| 5 | +import type { GrouperFunction } from '../Group/Grouper'; |
| 6 | +import { TemplatingPluginTools } from '../../lib/TemplatingPluginTools'; |
| 7 | +import { Field } from './Field'; |
| 8 | +import { Filter, type FilterFunction } from './Filter'; |
| 9 | +import { FilterInstructions } from './FilterInstructions'; |
| 10 | +import { FilterOrErrorMessage } from './FilterOrErrorMessage'; |
| 11 | + |
| 12 | +export type DurationFilterFunction = (duration: Duration) => boolean; |
| 13 | + |
| 14 | +export class DurationField extends Field { |
| 15 | + protected readonly filterInstructions: FilterInstructions; |
| 16 | + |
| 17 | + constructor(filterInstructions: FilterInstructions | null = null) { |
| 18 | + super(); |
| 19 | + if (filterInstructions !== null) { |
| 20 | + this.filterInstructions = filterInstructions; |
| 21 | + } else { |
| 22 | + this.filterInstructions = new FilterInstructions(); |
| 23 | + this.filterInstructions.add(`has ${this.fieldName()}`, (task: Task) => task.duration !== Duration.None); |
| 24 | + this.filterInstructions.add(`no ${this.fieldName()}`, (task: Task) => task.duration === Duration.None); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public fieldName(): string { |
| 29 | + return 'duration'; |
| 30 | + } |
| 31 | + |
| 32 | + public canCreateFilterForLine(line: string): boolean { |
| 33 | + if (this.filterInstructions.canCreateFilterForLine(line)) { |
| 34 | + return true; |
| 35 | + } |
| 36 | + |
| 37 | + return super.canCreateFilterForLine(line); |
| 38 | + } |
| 39 | + |
| 40 | + public createFilterOrErrorMessage(line: string): FilterOrErrorMessage { |
| 41 | + // There have been multiple "bug reports", where the query had un-expanded |
| 42 | + // template text to signify the search duration. |
| 43 | + // Enough to explicitly trap any such text for duration searches: |
| 44 | + const errorText = this.checkForUnexpandedTemplateText(line); |
| 45 | + if (errorText) { |
| 46 | + return FilterOrErrorMessage.fromError(line, errorText); |
| 47 | + } |
| 48 | + |
| 49 | + const filterResult = this.filterInstructions.createFilterOrErrorMessage(line); |
| 50 | + if (filterResult.isValid()) { |
| 51 | + return filterResult; |
| 52 | + } |
| 53 | + |
| 54 | + const fieldNameKeywordDuration = Field.getMatch(this.filterRegExp(), line); |
| 55 | + if (fieldNameKeywordDuration === null) { |
| 56 | + return FilterOrErrorMessage.fromError(line, 'do not understand query filter (' + this.fieldName() + ')'); |
| 57 | + } |
| 58 | + |
| 59 | + const fieldKeyword = fieldNameKeywordDuration[2]?.toLowerCase(); // 'is', 'above', 'under' |
| 60 | + const fieldDurationString = fieldNameKeywordDuration[3]; // The remainder of the instruction |
| 61 | + |
| 62 | + // Try interpreting everything after the keyword as a duration: |
| 63 | + const fieldDuration = Duration.fromText(fieldDurationString); |
| 64 | + |
| 65 | + if (!fieldDuration) { |
| 66 | + return FilterOrErrorMessage.fromError(line, 'do not understand ' + this.fieldName()); |
| 67 | + } |
| 68 | + |
| 69 | + const filterFunction = this.buildFilterFunction(fieldKeyword, fieldDuration); |
| 70 | + |
| 71 | + const explanation = DurationField.buildExplanation( |
| 72 | + this.fieldNameForExplanation(), |
| 73 | + fieldKeyword, |
| 74 | + this.filterResultIfFieldMissing(), |
| 75 | + fieldDuration, |
| 76 | + ); |
| 77 | + return FilterOrErrorMessage.fromFilter(new Filter(line, filterFunction, explanation)); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Builds function that actually filters the tasks depending on the duration |
| 82 | + * @param fieldKeyword relationship to be held with the duration 'under', 'is', 'above' |
| 83 | + * @param fieldDuration the duration to be used by the filter function |
| 84 | + * @returns the function that filters the tasks |
| 85 | + */ |
| 86 | + protected buildFilterFunction(fieldKeyword: string, fieldDuration: Duration): FilterFunction { |
| 87 | + let durationFilter: DurationFilterFunction; |
| 88 | + switch (fieldKeyword) { |
| 89 | + case 'under': |
| 90 | + durationFilter = (duration) => this.compare(duration, fieldDuration) < 0; |
| 91 | + break; |
| 92 | + case 'above': |
| 93 | + durationFilter = (duration) => this.compare(duration, fieldDuration) > 0; |
| 94 | + break; |
| 95 | + case 'is': |
| 96 | + default: |
| 97 | + durationFilter = (duration) => this.compare(duration, fieldDuration) === 0; |
| 98 | + break; |
| 99 | + } |
| 100 | + return this.getFilter(durationFilter); |
| 101 | + } |
| 102 | + |
| 103 | + protected getFilter(durationFilterFunction: DurationFilterFunction): FilterFunction { |
| 104 | + return (task: Task) => { |
| 105 | + return durationFilterFunction(task.duration); |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + protected filterRegExp(): RegExp { |
| 110 | + return new RegExp('^duration( expectation)? (is|above|under) ?(.*)', 'i'); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Constructs an Explanation for a duration-based filter |
| 115 | + * @param fieldName - for example, 'due' |
| 116 | + * @param fieldKeyword - one of the keywords like 'before' or 'after' |
| 117 | + * @param filterResultIfFieldMissing - whether the search matches tasks without the requested duration value |
| 118 | + * @param filterDurations - the duration range used in the filter |
| 119 | + */ |
| 120 | + public static buildExplanation( |
| 121 | + fieldName: string, |
| 122 | + fieldKeyword: string, |
| 123 | + filterResultIfFieldMissing: boolean, |
| 124 | + filterDurations: Duration, |
| 125 | + ): Explanation { |
| 126 | + const fieldKeywordVerbose = fieldKeyword === 'is' ? 'is' : 'is ' + fieldKeyword; |
| 127 | + let oneLineExplanation = `${fieldName} ${fieldKeywordVerbose} ${filterDurations.toText()}`; |
| 128 | + if (filterResultIfFieldMissing) { |
| 129 | + oneLineExplanation += ` OR no ${fieldName}`; |
| 130 | + } |
| 131 | + return new Explanation(oneLineExplanation); |
| 132 | + } |
| 133 | + |
| 134 | + protected fieldNameForExplanation() { |
| 135 | + return this.fieldName(); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Determine whether a task that does not have a duration value |
| 140 | + * should be treated as a match. |
| 141 | + * @protected |
| 142 | + */ |
| 143 | + protected filterResultIfFieldMissing(): boolean { |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + public supportsSorting(): boolean { |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + public compare(a: Duration, b: Duration): number { |
| 152 | + if (a === Duration.None || b === Duration.None) { |
| 153 | + return 0; |
| 154 | + } |
| 155 | + return a.hours * 60 + a.minutes - (b.hours * 60 + b.minutes); |
| 156 | + } |
| 157 | + |
| 158 | + public comparator(): Comparator { |
| 159 | + return (a: Task, b: Task) => { |
| 160 | + return this.compare(a.duration, b.duration); |
| 161 | + }; |
| 162 | + } |
| 163 | + |
| 164 | + public supportsGrouping(): boolean { |
| 165 | + return true; |
| 166 | + } |
| 167 | + |
| 168 | + public grouper(): GrouperFunction { |
| 169 | + return (task: Task) => { |
| 170 | + const duration = task.duration; |
| 171 | + if (!duration || duration === Duration.None) { |
| 172 | + return ['No ' + this.fieldName()]; |
| 173 | + } |
| 174 | + return [duration.toText()]; |
| 175 | + }; |
| 176 | + } |
| 177 | + |
| 178 | + private checkForUnexpandedTemplateText(line: string): null | string { |
| 179 | + return new TemplatingPluginTools().findUnexpandedDateText(line); |
| 180 | + } |
| 181 | +} |
0 commit comments