cfait 1.1.6

Powerful, fast and elegant task / TODO manager. (GUI & TUI, CalDAV & local)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// SPDX-License-Identifier: GPL-3.0-or-later
// File: ./src/gui/view/syntax.rs
// Implements syntax highlighting for the smart input editor.
use crate::color_utils;
use crate::model::parser::{SyntaxType, tokenize_smart_input};
use iced::advanced::text::highlighter::{self, Highlighter};
use iced::{Color, Font};
use std::ops::Range;

pub fn get_syntax_style(kind: SyntaxType, text: &str, is_dark: bool) -> (Option<Color>, bool) {
    match kind {
        SyntaxType::Priority => {
            let p = text.trim_start_matches('!').parse::<u8>().unwrap_or(0);
            let (r, g, b) = color_utils::get_priority_rgb(p, is_dark);
            (Some(Color::from_rgb(r, g, b)), true)
        }
        SyntaxType::DueDate => (Some(Color::from_rgb(0.2, 0.6, 1.0)), false),
        SyntaxType::StartDate => (Some(Color::from_rgb(0.4, 0.8, 0.4)), false),
        SyntaxType::Recurrence => (Some(Color::from_rgb(0.8, 0.4, 0.8)), false),
        SyntaxType::Duration => (Some(Color::from_rgb(0.6, 0.6, 0.6)), false),
        SyntaxType::Tag => {
            let tag_name = text.trim_start_matches('#');
            let (r, g, b) = color_utils::generate_color(tag_name);
            (Some(Color::from_rgb(r, g, b)), true)
        }
        SyntaxType::Text => (None, false),
        SyntaxType::Location => (Some(Color::from_rgb(0.8, 0.5, 0.0)), false),
        SyntaxType::Url => (Some(Color::from_rgb(0.2, 0.2, 0.8)), false),
        SyntaxType::WikiLink => (Some(Color::from_rgb(0.2, 0.7, 1.0)), true),
        SyntaxType::Dependency => (Some(Color::from_rgb(0.9, 0.6, 0.2)), true),
        SyntaxType::Relation => (Some(Color::from_rgb(0.4, 0.6, 0.9)), true),
        SyntaxType::Geo => (Some(Color::from_rgb(0.5, 0.5, 0.5)), false),
        SyntaxType::Description => (Some(Color::from_rgb(0.6, 0.0, 0.6)), false),
        SyntaxType::Reminder => (Some(Color::from_rgb(1.0, 0.4, 0.0)), true),
        SyntaxType::Filter => (Some(Color::from_rgb(0.0, 0.8, 0.8)), false),
        SyntaxType::Operator => (Some(Color::from_rgb(1.0, 0.0, 1.0)), true),
        SyntaxType::Goal => (Some(Color::from_rgb(0.2, 0.8, 0.6)), true),
        SyntaxType::Collection => (Some(Color::from_rgb(0.9, 0.4, 0.4)), true),
        SyntaxType::Calendar => (Some(Color::from_rgb(0.91, 0.11, 0.38)), true),
        SyntaxType::Pin => (Some(Color::from_rgb(1.0, 0.4, 0.0)), true),
        SyntaxType::Note => (Some(Color::from_rgb(0.5, 0.5, 0.5)), true),
    }
}

// 1. Add state field
pub struct SmartInputHighlighter {
    is_dark: bool,
    is_search: bool,
}

impl Default for SmartInputHighlighter {
    fn default() -> Self {
        Self {
            is_dark: true,
            is_search: false,
        } // Default: dark=true, search=false
    }
}

impl Highlighter for SmartInputHighlighter {
    // Settings: (is_dark, is_search)
    type Settings = (bool, bool); // (is_dark, is_search)
    type Highlight = highlighter::Format<Font>;
    type Iterator<'a> = std::vec::IntoIter<(Range<usize>, Self::Highlight)>;

    fn new(settings: &Self::Settings) -> Self {
        Self {
            is_dark: settings.0,
            is_search: settings.1,
        }
    }

    fn update(&mut self, settings: &Self::Settings) {
        self.is_dark = settings.0;
        self.is_search = settings.1;
    }

    fn highlight_line(&mut self, line: &str) -> Self::Iterator<'_> {
        // Pass context to tokenizer
        let tokens = tokenize_smart_input(line, self.is_search);

        let spans: Vec<(Range<usize>, Self::Highlight)> = tokens
            .into_iter()
            .map(|t| {
                let text = &line[t.start..t.end];
                let (opt_color, is_bold) =
                    crate::gui::view::syntax::get_syntax_style(t.kind, text, self.is_dark);
                let format = highlighter::Format {
                    color: opt_color,
                    font: if is_bold {
                        Some(Font {
                            weight: iced::font::Weight::Bold,
                            ..Default::default()
                        })
                    } else {
                        None
                    },
                };
                (t.start..t.end, format)
            })
            .collect();

        spans.into_iter()
    }

    fn change_line(&mut self, _line: usize) {}
    fn current_line(&self) -> usize {
        0
    }
}
pub struct SessionHighlighter {
    is_dark: bool,
}

impl Default for SessionHighlighter {
    fn default() -> Self {
        Self { is_dark: true }
    }
}

impl Highlighter for SessionHighlighter {
    type Settings = bool;
    type Highlight = highlighter::Format<Font>;
    type Iterator<'a> = std::vec::IntoIter<(Range<usize>, Self::Highlight)>;

    fn new(settings: &Self::Settings) -> Self {
        Self { is_dark: *settings }
    }

    fn update(&mut self, settings: &Self::Settings) {
        self.is_dark = *settings;
    }

    fn change_line(&mut self, _line: usize) {}
    fn current_line(&self) -> usize {
        0
    }

    fn highlight_line(&mut self, line: &str) -> Self::Iterator<'_> {
        let mut spans = Vec::new();
        let mut cursor = 0;

        let lex_guard = crate::model::parser::LEXICON.read().unwrap();
        let lex = &*lex_guard;

        for word in line.split_whitespace() {
            let start = line[cursor..].find(word).unwrap() + cursor;
            let end = start + word.len();

            if start > cursor {
                spans.push((
                    cursor..start,
                    highlighter::Format {
                        color: None,
                        font: None,
                    },
                ));
            }

            let lower = word.to_lowercase();
            let format = if crate::model::parser::parse_duration_with_lex(&lower, lex).is_some() {
                // Duration matches
                highlighter::Format {
                    color: Some(Color::from_rgb(0.6, 0.6, 0.6)),
                    font: None,
                }
            } else if crate::model::parser::parse_smart_date_with_lex(&lower, lex).is_some()
                || crate::model::parser::parse_weekday_code_with_lex(&lower, lex).is_some()
            {
                // Date matches
                highlighter::Format {
                    color: Some(Color::from_rgb(0.2, 0.6, 1.0)),
                    font: None,
                }
            } else if lower.contains(':') && (lower.contains('-') || lower.len() <= 5) {
                // Time or Time Range matches
                highlighter::Format {
                    color: Some(Color::from_rgb(0.4, 0.8, 0.4)),
                    font: None,
                }
            } else {
                // Default text
                highlighter::Format {
                    color: None,
                    font: None,
                }
            };

            spans.push((start..end, format));
            cursor = end;
        }

        if cursor < line.len() {
            spans.push((
                cursor..line.len(),
                highlighter::Format {
                    color: None,
                    font: None,
                },
            ));
        }

        spans.into_iter()
    }
}

pub struct MarkdownHighlighter {
    is_dark: bool,
}

impl Default for MarkdownHighlighter {
    fn default() -> Self {
        Self { is_dark: true }
    }
}

impl Highlighter for MarkdownHighlighter {
    type Settings = bool;
    type Highlight = highlighter::Format<Font>;
    type Iterator<'a> = std::vec::IntoIter<(Range<usize>, Self::Highlight)>;

    fn new(settings: &Self::Settings) -> Self {
        Self { is_dark: *settings }
    }

    fn update(&mut self, settings: &Self::Settings) {
        self.is_dark = *settings;
    }

    fn change_line(&mut self, _line: usize) {}
    fn current_line(&self) -> usize {
        0
    }

    fn highlight_line(&mut self, line: &str) -> Self::Iterator<'_> {
        let mut spans = Vec::new();

        let is_dark = self.is_dark;
        let header_color = Some(if is_dark {
            Color::from_rgb(0.3, 0.7, 1.0)
        } else {
            Color::from_rgb(0.1, 0.4, 0.8)
        });
        let link_color = Some(if is_dark {
            Color::from_rgb(0.2, 0.7, 1.0)
        } else {
            Color::from_rgb(0.1, 0.5, 0.9)
        });
        let dim_color = Some(Color::from_rgba(0.5, 0.5, 0.5, 0.6));
        let checkbox_color = Some(Color::from_rgb(0.4, 0.8, 0.4));
        let list_marker_color = Some(if is_dark {
            Color::from_rgb(0.8, 0.6, 0.0)
        } else {
            Color::from_rgb(0.7, 0.5, 0.0)
        });
        let quote_color = Some(if is_dark {
            Color::from_rgb(0.5, 0.5, 0.5)
        } else {
            Color::from_rgb(0.4, 0.4, 0.4)
        });
        let table_color = Some(Color::from_rgb(0.3, 0.7, 0.5));
        let code_color = Some(Color::from_rgb(0.8, 0.6, 0.4));

        let trimmed = line.trim_start();
        let indent = line.len() - trimmed.len();

        let mut marker_end = indent;
        let mut is_header = false;
        let mut is_quote = false;
        let mut is_table = false;
        let mut is_code_fence = false;

        if trimmed.starts_with("```") {
            is_code_fence = true;
            marker_end = line.len();
        } else if trimmed.starts_with("# ") {
            marker_end = indent + 2;
            is_header = true;
        } else if trimmed.starts_with("## ") {
            marker_end = indent + 3;
            is_header = true;
        } else if trimmed.starts_with("### ") {
            marker_end = indent + 4;
            is_header = true;
        } else if trimmed.starts_with("#### ") {
            marker_end = indent + 5;
            is_header = true;
        } else if trimmed.starts_with("##### ") {
            marker_end = indent + 6;
            is_header = true;
        } else if trimmed.starts_with("###### ") {
            marker_end = indent + 7;
            is_header = true;
        } else if trimmed.starts_with("> ") {
            marker_end = indent + 2;
            is_quote = true;
        } else if trimmed.starts_with('|') && trimmed[1..].contains('|') {
            is_table = true;
        } else if trimmed.starts_with("- ")
            || trimmed.starts_with("* ")
            || trimmed.starts_with("+ ")
        {
            marker_end = indent + 2;
        } else {
            let mut digit_bytes = 0;
            for c in trimmed.chars() {
                if c.is_ascii_digit() {
                    digit_bytes += c.len_utf8();
                } else {
                    break;
                }
            }
            if digit_bytes > 0 && trimmed[digit_bytes..].starts_with(". ") {
                marker_end = indent + digit_bytes + 2;
            }
        }

        // Check for checkbox right after marker
        let mut checkbox_end = marker_end;
        if marker_end > indent && marker_end < line.len() {
            let remainder = &line[marker_end..];
            if remainder.starts_with("[ ] ")
                || remainder.starts_with("[x] ")
                || remainder.starts_with("[X] ")
                || remainder.starts_with("[/] ")
                || remainder.starts_with("[-] ")
                || remainder.starts_with("[<] ")
                || remainder.starts_with("[>] ")
                || remainder.starts_with("[*] ")
                || remainder.starts_with("[~] ")
            {
                checkbox_end = marker_end + 4;
            }
        }

        let base_format = if is_header {
            highlighter::Format {
                color: header_color,
                font: Some(Font {
                    weight: iced::font::Weight::Bold,
                    ..Default::default()
                }),
            }
        } else if is_quote {
            highlighter::Format {
                color: quote_color,
                font: Some(Font {
                    style: iced::font::Style::Italic,
                    ..Default::default()
                }),
            }
        } else if is_table {
            highlighter::Format {
                color: table_color,
                font: Some(Font::MONOSPACE),
            }
        } else if is_code_fence {
            highlighter::Format {
                color: code_color,
                font: Some(Font::MONOSPACE),
            }
        } else {
            highlighter::Format {
                color: None,
                font: None,
            }
        };

        // 1. Indent
        if indent > 0 {
            spans.push((
                0..indent,
                highlighter::Format {
                    color: None,
                    font: None,
                },
            ));
        }

        // 2. Marker (Header #, Quote >, List bullet/number)
        if marker_end > indent {
            let fmt = if is_header || is_quote || is_code_fence {
                base_format
            } else {
                highlighter::Format {
                    color: list_marker_color,
                    font: Some(Font {
                        weight: iced::font::Weight::Bold,
                        ..Default::default()
                    }),
                }
            };
            spans.push((indent..marker_end, fmt));
        }

        // 3. Checkbox
        if checkbox_end > marker_end {
            spans.push((
                marker_end..checkbox_end,
                highlighter::Format {
                    color: checkbox_color,
                    font: None,
                },
            ));
        }

        let rest_start = checkbox_end;
        if rest_start >= line.len() {
            return spans.into_iter();
        }

        let rest_of_line = &line[rest_start..];

        // 4. Apply Markdown inline formatting to rest_of_line using central parser
        let inline_elements = crate::model::parser::parse_inline_markdown(rest_of_line);
        let mut elem_cursor = rest_start;
        for el in inline_elements {
            use crate::model::parser::InlineElement;
            let (raw, is_plain) = match &el {
                InlineElement::Text(r) => (*r, true),
                InlineElement::Bold { raw: r, .. } => (*r, false),
                InlineElement::Italic { raw: r, .. } => (*r, false),
                InlineElement::Strikethrough { raw: r, .. } => (*r, false),
                InlineElement::Code { raw: r, .. } => (*r, false),
                InlineElement::Link { raw: r, .. } => (*r, false),
            };

            if is_plain {
                let tokens = crate::model::parser::tokenize_smart_input(raw, false);
                for t in tokens {
                    let text = &raw[t.start..t.end];
                    let (opt_color, is_bold) = get_syntax_style(t.kind, text, self.is_dark);
                    let mut format = base_format;
                    if opt_color.is_some() {
                        format.color = opt_color;
                    }
                    if is_bold {
                        format.font = Some(Font {
                            weight: iced::font::Weight::Bold,
                            ..Default::default()
                        });
                    }
                    spans.push((elem_cursor + t.start..elem_cursor + t.end, format));
                }
            } else {
                let mut format = base_format;
                match el {
                    InlineElement::Bold { .. } => {
                        format.font = Some(Font {
                            weight: iced::font::Weight::Bold,
                            ..Default::default()
                        })
                    }
                    InlineElement::Italic { .. } => {
                        format.font = Some(Font {
                            style: iced::font::Style::Italic,
                            ..Default::default()
                        })
                    }
                    InlineElement::Strikethrough { .. } => {
                        format.color = dim_color;
                        format.font = Some(Font {
                            style: iced::font::Style::Italic,
                            ..Default::default()
                        });
                    }
                    InlineElement::Code { .. } => {
                        format.color = code_color;
                        format.font = Some(Font::MONOSPACE);
                    }
                    InlineElement::Link { .. } => {
                        format.color = link_color;
                        format.font = Some(Font {
                            weight: iced::font::Weight::Bold,
                            ..Default::default()
                        });
                    }
                    _ => {}
                }
                spans.push((elem_cursor..elem_cursor + raw.len(), format));
            }
            elem_cursor += raw.len();
        }
        spans.into_iter()
    }
}