aimcal-cli 0.12.1

AIM - Analyze. Interact. Manage Your Time, with calendar support
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
// SPDX-FileCopyrightText: 2025-2026 Zexin Yuan <aim@yzx9.xyz>
//
// SPDX-License-Identifier: Apache-2.0

use std::{cell::RefCell, rc::Rc};

use ratatui::crossterm::event::{KeyCode, KeyEvent};
use ratatui::prelude::*;
use ratatui::widgets::{Clear, Paragraph};
use unicode_width::UnicodeWidthStr;

use crate::tui::component::{Component, Message};
use crate::tui::dispatcher::{Action, Dispatcher};
use crate::util::{byte_range_of_grapheme_at, unicode_width_of_slice};

pub struct Form<S, C: FormItem<S>> {
    items: Vec<C>,
    item_index: usize,
    _phantom: std::marker::PhantomData<S>,
}

impl<S, C: FormItem<S>> Form<S, C> {
    pub fn new(items: Vec<C>) -> Self {
        Self {
            items,
            item_index: 0,
            _phantom: std::marker::PhantomData,
        }
    }

    fn layout(&self, store: &RefCell<S>) -> Layout {
        Layout::vertical(self.items.iter().map(|item| match item.item_state(store) {
            FormItemState::Invisible => Constraint::Max(0),
            _ => Constraint::Max(3),
        }))
        .margin(1)
    }

    fn navigate(&mut self, dispatcher: &mut Dispatcher, store: &RefCell<S>, offset: isize) {
        // deactivate current item
        if let Some(a) = self.items.get_mut(self.item_index) {
            a.deactivate(dispatcher, store);
        }

        // move to next/previous item, skipping invisible items
        let len = self.items.len();

        // find the next visible item
        let mut new_index = self.item_index;
        let mut steps = offset.unsigned_abs();

        while steps > 0 {
            if offset > 0 {
                new_index = (new_index + 1) % len;
            } else {
                new_index = (new_index + len - 1) % len;
            }

            // Check if the item at new_index is visible
            if let Some(item) = self.items.get(new_index)
                && item_is_visible(item, store)
            {
                steps -= 1; // Found a visible item
            } else if new_index == self.item_index {
                // If we've gone through all items and none are visible, break
                break;
            }
        }

        self.item_index = new_index;

        // activate new item
        if let Some(a) = self.items.get_mut(self.item_index) {
            a.activate(dispatcher, store);
        }
    }
}

impl<S, C: FormItem<S>> Component<S> for Form<S, C> {
    fn render(&self, store: &RefCell<S>, area: Rect, buf: &mut Buffer) {
        let areas = self.layout(store).split(area);
        let mut is_last = true;
        for (item, area) in self.items.iter().zip(areas.iter()).rev() {
            // reverse order to draw the last item first, dont assert if the item is visible
            if item_is_visible(item, store) {
                item_render(is_last, item, store, *area, buf);
                item.render(store, item_inner(*area), buf);
                is_last = false;
            }
        }
    }

    fn get_cursor_position(&self, store: &RefCell<S>, area: Rect) -> Option<(u16, u16)> {
        self.items
            .iter()
            .zip(self.layout(store).split(area).iter())
            .take(self.item_index + 1)
            .last()
            .and_then(|(comp, area)| comp.get_cursor_position(store, *area))
    }

    fn on_key(
        &mut self,
        dispatcher: &mut Dispatcher,
        store: &RefCell<S>,
        area: Rect,
        event: KeyEvent,
    ) -> Option<Message> {
        // Handle key events for the current component
        let areas = self.layout(store).split(area);
        if let Some((comp, subarea)) = self
            .items
            .iter_mut()
            .zip(areas.iter())
            .take(self.item_index + 1)
            .last()
            && let Some(msg) = comp.on_key(dispatcher, store, *subarea, event)
        {
            return Some(msg);
        }

        match event.code {
            KeyCode::Up | KeyCode::BackTab if self.item_index > 0 => {
                self.navigate(dispatcher, store, -1);
                Some(Message::CursorUpdated)
            }
            KeyCode::Down | KeyCode::Tab if self.item_index < self.items.len() - 1 => {
                self.navigate(dispatcher, store, 1);
                Some(Message::CursorUpdated)
            }
            KeyCode::Enter => {
                dispatcher.dispatch(&Action::SubmitChanges);
                Some(Message::Exit)
            }
            _ => None,
        }
    }

    fn activate(&mut self, dispatcher: &mut Dispatcher, store: &RefCell<S>) {
        if let Some(item) = self.items.get_mut(self.item_index) {
            item.activate(dispatcher, store);
        }
    }

    fn deactivate(&mut self, dispatcher: &mut Dispatcher, store: &RefCell<S>) {
        if let Some(item) = self.items.get_mut(self.item_index) {
            item.deactivate(dispatcher, store);
        }
    }
}

pub trait FormItem<S>: Component<S> {
    fn item_title(&self, store: &RefCell<S>) -> &str;
    fn item_state(&self, store: &RefCell<S>) -> FormItemState;
}

impl<S> FormItem<S> for Box<dyn FormItem<S>> {
    fn item_title(&self, store: &RefCell<S>) -> &str {
        (**self).item_title(store)
    }

    fn item_state(&self, store: &RefCell<S>) -> FormItemState {
        (**self).item_state(store)
    }
}

pub enum FormItemState {
    // Whether the component is currently active (focused).
    Active,

    // Whether the component is currently inactive (not focused).
    Inactive,

    /// Whether the component is currently visible. By default, all items are visible.
    Invisible,
}

pub trait Access<S, T: ToOwned> {
    fn get(store: &RefCell<S>) -> T;
    fn set(dispatcher: &mut Dispatcher, value: T) -> bool;
}

#[derive(Debug)]
pub struct Input<S, A: Access<S, String>> {
    title: String,
    active: bool,
    character_index: usize,
    _phantom_s: std::marker::PhantomData<S>,
    _phantom_a: std::marker::PhantomData<A>,
}

impl<S, A: Access<S, String>> Input<S, A> {
    pub fn new(title: &(impl ToString + ?Sized)) -> Self {
        Self {
            title: title.to_string(),
            active: false,
            character_index: 0,
            _phantom_a: std::marker::PhantomData,
            _phantom_s: std::marker::PhantomData,
        }
    }
}

impl<S, A: Access<S, String>> Component<S> for Input<S, A> {
    fn render(&self, store: &RefCell<S>, area: Rect, buf: &mut Buffer) {
        let v = A::get(store);
        Paragraph::new(v.as_str()).render(area, buf);
    }

    fn get_cursor_position(&self, store: &RefCell<S>, area: Rect) -> Option<(u16, u16)> {
        if !self.active {
            return None; // No cursor position when not active
        }

        let v = A::get(store);
        let width = unicode_width_of_slice(v.as_str(), self.character_index);
        let x = area.x + u16::try_from(width).unwrap_or_default() + 2; // border 1 + padding 1
        let y = area.y + 1; // title line: 1
        Some((x, y))
    }

    fn on_key(
        &mut self,
        dispatcher: &mut Dispatcher,
        store: &RefCell<S>,
        _area: Rect,
        event: KeyEvent,
    ) -> Option<Message> {
        use KeyCode::{Backspace, Char, Left, Right};
        if !self.active || !matches!(event.code, Left | Right | Backspace | Char(_)) {
            return None;
        }

        match event.code {
            Left if self.character_index > 0 => self.character_index -= 1,
            Right if self.character_index < A::get(store).len() => self.character_index += 1,
            Backspace if self.character_index > 0 => {
                let mut v = A::get(store);
                if let Some(range) = byte_range_of_grapheme_at(&v, self.character_index - 1) {
                    v.replace_range(range, "");
                    if A::set(dispatcher, v) {
                        self.character_index -= 1;
                    }
                }
            }
            Char(c) => {
                let mut v = A::get(store);
                let byte_index = v
                    .char_indices()
                    .nth(self.character_index)
                    .map_or(v.len(), |(i, _)| i);
                v.insert(byte_index, c);
                if A::set(dispatcher, v) {
                    self.character_index += 1;
                }
            }
            _ => {}
        }

        // Always update the cursor position for simplicity
        Some(Message::CursorUpdated)
    }

    fn activate(&mut self, _dispatcher: &mut Dispatcher, _store: &RefCell<S>) {
        self.active = true;
        self.character_index = 0; // Reset character index when activated
    }

    fn deactivate(&mut self, _dispatcher: &mut Dispatcher, _store: &RefCell<S>) {
        self.active = false;
        self.character_index = 0; // Reset character index when deactivated
    }
}

impl<S, A: Access<S, String>> FormItem<S> for Input<S, A> {
    fn item_title(&self, _store: &RefCell<S>) -> &str {
        &self.title
    }

    fn item_state(&self, _store: &RefCell<S>) -> FormItemState {
        if self.active {
            FormItemState::Active
        } else {
            FormItemState::Inactive
        }
    }
}

#[derive(Debug)]
pub struct RadioGroup<S, T: Eq + Clone, A: Access<S, T>> {
    title: String,
    values: Vec<T>,
    options: Vec<String>,
    active: bool,
    _phantom_s: std::marker::PhantomData<S>,
    _phantom_a: std::marker::PhantomData<A>,
}

impl<S, T: Eq + Clone, A: Access<S, T>> RadioGroup<S, T, A> {
    pub fn new(title: &(impl ToString + ?Sized), values: Vec<T>, options: Vec<String>) -> Self {
        Self {
            title: title.to_string(),
            values,
            options,
            active: false,
            _phantom_s: std::marker::PhantomData,
            _phantom_a: std::marker::PhantomData,
        }
    }

    fn selected(&self, store: &RefCell<S>) -> usize {
        let v = A::get(store);
        self.values.iter().position(|s| s == &v).unwrap_or(0)
    }

    fn split(&self, area: Rect) -> Rc<[Rect]> {
        self.layout().split(area)
    }

    fn layout(&self) -> Layout {
        let constraints = self
            .options
            .iter()
            // 6 = border left (1) + active marker [ ] (3) + space (1) + border right (1)
            .map(|s| Constraint::Min(6 + u16::try_from(s.width()).unwrap_or_default()));

        Layout::horizontal(constraints)
    }
}

impl<S, T: Eq + Clone, A: Access<S, T>> Component<S> for RadioGroup<S, T, A> {
    fn render(&self, store: &RefCell<S>, area: Rect, buf: &mut Buffer) {
        let options = self.split(area);
        for (i, (value, area)) in self.options.iter().zip(options.iter()).enumerate() {
            let icon = if self.selected(store) == i { 'x' } else { ' ' };
            let label = format!("[{icon}] {value}");
            Paragraph::new(label).render(*area, buf);
        }
    }

    fn get_cursor_position(&self, store: &RefCell<S>, area: Rect) -> Option<(u16, u16)> {
        self.split(item_inner(area))
            .get(self.selected(store))
            .map(|area| (area.x + 1, area.y))
    }

    fn on_key(
        &mut self,
        dispatcher: &mut Dispatcher,
        store: &RefCell<S>,
        _area: Rect,
        event: KeyEvent,
    ) -> Option<Message> {
        if !self.active {
            return None; // Only handle keys when the field is active
        }

        match event.code {
            KeyCode::Left | KeyCode::Right => {
                let offset = match event.code {
                    KeyCode::Left => self.values.len() - 1,
                    KeyCode::Right => 1,
                    _ => 0,
                };
                let index = (self.selected(store) + offset) % self.values.len();
                match self.values.get(index) {
                    Some(a) => {
                        A::set(dispatcher, a.to_owned());
                        Some(Message::CursorUpdated)
                    }
                    None => Some(Message::Handled),
                }
            }
            _ => None,
        }
    }

    fn activate(&mut self, _: &mut Dispatcher, _store: &RefCell<S>) {
        self.active = true;
    }

    fn deactivate(&mut self, _: &mut Dispatcher, _store: &RefCell<S>) {
        self.active = false;
    }
}

impl<S, T: Eq + Clone, A: Access<S, T>> FormItem<S> for RadioGroup<S, T, A> {
    fn item_title(&self, _store: &RefCell<S>) -> &str {
        &self.title
    }

    fn item_state(&self, _store: &RefCell<S>) -> FormItemState {
        if self.active {
            FormItemState::Active
        } else {
            FormItemState::Inactive
        }
    }
}

const S_STEP_ACTIVE: &str = "";
const S_STEP_INACTIVE: &str = "";
// const S_STEP_CANCEL: &str = "■";
// const S_STEP_ERROR: &str = "▲";

// const S_SIDER_TOP: &str = "┌";
const S_SIDER_CONNECTOR: &str = "";
const S_SIDER_BOTTOM: &str = "";

fn item_render<S>(
    is_last: bool,
    item: &impl FormItem<S>,
    store: &RefCell<S>,
    area: Rect,
    buf: &mut Buffer,
) {
    let color = match item.item_state(store) {
        FormItemState::Active => Color::Blue,
        FormItemState::Inactive => Color::Gray,
        FormItemState::Invisible => return,
    };

    let area_title = Rect::new(area.x + 2, area.y, area.width.saturating_sub(2), 1);
    Clear.render(area_title, buf);
    Paragraph::new(item.item_title(store))
        .bold()
        .fg(color)
        .render(area_title, buf);

    if let Some(c) = buf.cell_mut((area.x, area.y)) {
        let symbol = match item.item_state(store) {
            FormItemState::Active => S_STEP_ACTIVE,
            FormItemState::Inactive => S_STEP_INACTIVE,
            FormItemState::Invisible => unreachable!(),
        };
        c.set_symbol(symbol);
        c.set_fg(color);
    }

    for y in 1..area.height.saturating_sub(1) {
        if let Some(c) = buf.cell_mut((area.x, area.y + y)) {
            c.set_symbol(S_SIDER_CONNECTOR);
            c.set_fg(color);
        }
    }

    if let Some(c) = buf.cell_mut((area.x, area.y + area.height.saturating_sub(1))) {
        let symbol = if is_last {
            S_SIDER_BOTTOM
        } else {
            S_SIDER_CONNECTOR
        };
        c.set_symbol(symbol);
        c.set_fg(color);
    }
}

fn item_inner(area: Rect) -> Rect {
    Rect {
        x: area.x + 2,
        y: area.y + 1,
        width: area.width.saturating_sub(2),
        height: area.height.saturating_sub(2),
    }
}

fn item_is_visible<S>(item: &impl FormItem<S>, store: &RefCell<S>) -> bool {
    !matches!(item.item_state(store), FormItemState::Invisible)
}