finery 0.15.0

Keyboard-first Jira backlog and Composer with optional MCP access
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use std::{
    cell::RefCell,
    rc::Rc,
    sync::{Arc, RwLock},
    time::Duration,
};

use ratatui::{Frame, layout::Rect};
use tuicore::{
    AnimationSettings, EventCtx, EventOutcome, EventRoute, Flex, FlexItem, FocusCtx, FocusId,
    FocusTarget, LayoutCtx, LayoutProposal, LayoutResult, LayoutSizeHint, LifecycleCtx,
    ListControl, PasswordInput, RenderCtx, TextInput, TickResult, Toggle, TuiEvent, TuiNode,
};

use crate::{
    app_settings::AppSettings,
    service::AppService,
    speed_reader_settings::{
        MAX_MARKDOWN_BLOCK_PAUSE_MS, MAX_SPEED_READER_WPM, MIN_SPEED_READER_WPM,
        parse_markdown_block_pause, parse_speed_reader_wpm,
    },
};

enum SettingChange {
    JiraBaseUrl(String),
    JiraEmail(String),
    JiraApiToken(String),
    JiraDefaultProject(String),
    JiraDefaultBoard(String),
    JiraCompanyManagedUrls(bool),
    JiraStoryPointsFieldId(String),
    BacklogUseJiraVelocity(bool),
    BacklogJiraVelocitySprints(String),
    BacklogFixedSprintCapacity(String),
    BacklogUseAverageTicketSize(bool),
    BacklogFixedTicketSize(String),
    BacklogSprintTolerancePercent(String),
    BacklogExcludedSprintNameFragments(String),
    Wpm(String),
    MarkdownBlockPause(String),
    RecentTicketsLimit(String),
}

pub(crate) struct SettingsDialog {
    root: Flex<()>,
    changes: Rc<RefCell<Vec<SettingChange>>>,
    settings: Arc<RwLock<AppSettings>>,
    service: AppService,
}

impl SettingsDialog {
    pub(crate) fn new(settings: Arc<RwLock<AppSettings>>, service: AppService) -> Self {
        let values = settings.read().expect("settings lock poisoned").clone();
        let changes = Rc::new(RefCell::new(Vec::new()));
        let url_changes = Rc::clone(&changes);
        let email_changes = Rc::clone(&changes);
        let token_changes = Rc::clone(&changes);
        let project_changes = Rc::clone(&changes);
        let board_changes = Rc::clone(&changes);
        let company_managed_urls_changes = Rc::clone(&changes);
        let story_points_changes = Rc::clone(&changes);
        let velocity_changes = Rc::clone(&changes);
        let velocity_sprints_changes = Rc::clone(&changes);
        let sprint_capacity_changes = Rc::clone(&changes);
        let average_ticket_size_changes = Rc::clone(&changes);
        let ticket_size_changes = Rc::clone(&changes);
        let tolerance_changes = Rc::clone(&changes);
        let wpm_changes = Rc::clone(&changes);
        let delay_changes = Rc::clone(&changes);
        let recent_tickets_limit_changes = Rc::clone(&changes);
        let root = Flex::column()
            .child(
                "jira-base-url",
                TextInput::new()
                    .value(values.jira_base_url.clone())
                    .panel("Jira URL")
                    .placeholder("https://example.atlassian.net")
                    .focused(true)
                    .on_edit_end(move |value| {
                        url_changes
                            .borrow_mut()
                            .push(SettingChange::JiraBaseUrl(value));
                    }),
                FlexItem::fixed(3),
            )
            .child(
                "jira-email",
                TextInput::new()
                    .value(values.jira_email.clone())
                    .panel("Jira email")
                    .on_edit_end(move |value| {
                        email_changes
                            .borrow_mut()
                            .push(SettingChange::JiraEmail(value));
                    }),
                FlexItem::fixed(3),
            )
            .child(
                "jira-api-token",
                PasswordInput::new()
                    .value(values.jira_api_token.clone())
                    .panel("Jira API token")
                    .on_edit_end(move |value| {
                        token_changes
                            .borrow_mut()
                            .push(SettingChange::JiraApiToken(value));
                    }),
                FlexItem::fixed(3),
            )
            .child(
                "jira-default-project",
                TextInput::new()
                    .value(values.jira_default_project.clone())
                    .panel("Default Jira project")
                    .placeholder("FIN")
                    .on_edit_end(move |value| {
                        project_changes
                            .borrow_mut()
                            .push(SettingChange::JiraDefaultProject(value));
                    }),
                FlexItem::fixed(3),
            )
            .child(
                "jira-default-board",
                TextInput::new()
                    .value(values.jira_default_board.clone())
                    .numbers_only(true)
                    .panel("Default Jira board ID")
                    .placeholder("Auto-select from project")
                    .on_edit_end(move |value| {
                        board_changes
                            .borrow_mut()
                            .push(SettingChange::JiraDefaultBoard(value));
                    }),
                FlexItem::fixed(3),
            )
            .child(
                "jira-company-managed-urls",
                Toggle::new("Jira project is company-managed")
                    .checked(values.jira_company_managed_urls)
                    .on_change(move |value| {
                        company_managed_urls_changes
                            .borrow_mut()
                            .push(SettingChange::JiraCompanyManagedUrls(value));
                    }),
                FlexItem::fixed(1),
            )
            .child(
                "jira-story-points-field-id",
                TextInput::new()
                    .value(values.jira_story_points_field_id.clone())
                    .panel("Jira story-points custom-field ID")
                    .placeholder("Custom field ID")
                    .on_edit_end(move |value| {
                        story_points_changes
                            .borrow_mut()
                            .push(SettingChange::JiraStoryPointsFieldId(value));
                    }),
                FlexItem::fixed(3),
            )
            .child(
                "backlog-use-jira-velocity",
                Toggle::new("Backlog capacity: use Jira velocity")
                    .checked(values.backlog_runway.use_jira_velocity)
                    .on_change(move |value| {
                        velocity_changes
                            .borrow_mut()
                            .push(SettingChange::BacklogUseJiraVelocity(value));
                    }),
                FlexItem::fixed(1),
            )
            .child(
                "backlog-jira-velocity-sprints",
                TextInput::new()
                    .value(values.backlog_runway.jira_velocity_sprints.to_string())
                    .numbers_only(true)
                    .panel("Jira velocity sprints to average")
                    .placeholder("4")
                    .on_edit_end(move |value| {
                        velocity_sprints_changes
                            .borrow_mut()
                            .push(SettingChange::BacklogJiraVelocitySprints(value));
                    }),
                FlexItem::fixed(3),
            )
            .child(
                "backlog-fixed-sprint-capacity",
                TextInput::new()
                    .value(values.backlog_runway.fixed_sprint_capacity.to_string())
                    .panel("Backlog fixed / fallback sprint capacity")
                    .placeholder("20")
                    .on_edit_end(move |value| {
                        sprint_capacity_changes
                            .borrow_mut()
                            .push(SettingChange::BacklogFixedSprintCapacity(value));
                    }),
                FlexItem::fixed(3),
            )
            .child(
                "backlog-use-average-ticket-size",
                Toggle::new("Backlog assumptions: average loaded estimates")
                    .checked(values.backlog_runway.use_average_ticket_size)
                    .on_change(move |value| {
                        average_ticket_size_changes
                            .borrow_mut()
                            .push(SettingChange::BacklogUseAverageTicketSize(value));
                    }),
                FlexItem::fixed(1),
            )
            .child(
                "backlog-fixed-ticket-size",
                TextInput::new()
                    .value(values.backlog_runway.fixed_ticket_size.to_string())
                    .panel("Backlog fixed assumed ticket size")
                    .placeholder("3")
                    .on_edit_end(move |value| {
                        ticket_size_changes
                            .borrow_mut()
                            .push(SettingChange::BacklogFixedTicketSize(value));
                    }),
                FlexItem::fixed(3),
            )
            .child(
                "backlog-sprint-tolerance-percent",
                TextInput::new()
                    .value(values.backlog_runway.sprint_tolerance_percent.to_string())
                    .numbers_only(true)
                    .panel("Sprint target tolerance (%)")
                    .placeholder("20")
                    .on_edit_end(move |value| {
                        tolerance_changes
                            .borrow_mut()
                            .push(SettingChange::BacklogSprintTolerancePercent(value));
                    }),
                FlexItem::fixed(3),
            )
            .child(
                "backlog-excluded-sprint-name-fragments",
                SprintExclusionList::new(
                    values.excluded_sprint_name_fragments.clone(),
                    Rc::clone(&changes),
                ),
                FlexItem::fixed(7),
            )
            .child(
                "speed-reader-wpm",
                TextInput::new()
                    .value(values.speed_reader.wpm.to_string())
                    .numbers_only(true)
                    .panel("Reader WPM")
                    .on_edit_end(move |value| {
                        wpm_changes.borrow_mut().push(SettingChange::Wpm(value));
                    }),
                FlexItem::fixed(3),
            )
            .child(
                "markdown-block-pause",
                TextInput::new()
                    .value(values.block_delay().as_millis().to_string())
                    .numbers_only(true)
                    .panel("Reader block delay (ms)")
                    .on_edit_end(move |value| {
                        delay_changes
                            .borrow_mut()
                            .push(SettingChange::MarkdownBlockPause(value));
                    }),
                FlexItem::fixed(3),
            )
            .child(
                "recent-tickets-limit",
                TextInput::new()
                    .value(values.recent_tickets_limit.to_string())
                    .numbers_only(true)
                    .panel("Recent tickets to remember")
                    .placeholder("15")
                    .on_edit_end(move |value| {
                        recent_tickets_limit_changes
                            .borrow_mut()
                            .push(SettingChange::RecentTicketsLimit(value));
                    }),
                FlexItem::fixed(3),
            );
        Self {
            root,
            changes,
            settings,
            service,
        }
    }

    fn apply_changes(&self, ctx: &mut EventCtx<()>) {
        let mut settings = self
            .settings
            .read()
            .expect("settings lock poisoned")
            .clone();
        let mut changed = false;
        for change in self.changes.borrow_mut().drain(..) {
            match change {
                SettingChange::JiraBaseUrl(value) => {
                    let value = value.trim().trim_end_matches('/').to_owned();
                    if settings.jira_base_url != value {
                        settings.jira_base_url = value;
                        settings.invalidate_discovered_story_points();
                    }
                    changed = true;
                }
                SettingChange::JiraEmail(value) => {
                    settings.jira_email = value.trim().into();
                    changed = true;
                }
                SettingChange::JiraApiToken(value) => {
                    settings.jira_api_token = value.trim().into();
                    changed = true;
                }
                SettingChange::JiraDefaultProject(value) => {
                    settings.jira_default_project = value.trim().to_ascii_uppercase();
                    changed = true;
                }
                SettingChange::JiraDefaultBoard(value) => {
                    let value = value.trim().to_owned();
                    if settings.jira_default_board != value {
                        settings.jira_default_board = value;
                        settings.invalidate_discovered_story_points();
                    }
                    changed = true;
                }
                SettingChange::JiraCompanyManagedUrls(value) => {
                    settings.jira_company_managed_urls = value;
                    changed = true;
                }
                SettingChange::JiraStoryPointsFieldId(value) => {
                    settings.set_manual_story_points_field(value.trim().into());
                    changed = true;
                }
                SettingChange::BacklogUseJiraVelocity(value) => {
                    settings.backlog_runway.use_jira_velocity = value;
                    changed = true;
                }
                SettingChange::BacklogJiraVelocitySprints(value) => {
                    let Some(value) = value
                        .trim()
                        .parse::<usize>()
                        .ok()
                        .filter(|value| *value > 0)
                    else {
                        ctx.notify(tuicore::Notification::warning(
                            "Invalid velocity sprint count",
                            "Enter a whole number greater than zero.",
                        ));
                        continue;
                    };
                    settings.backlog_runway.jira_velocity_sprints = value;
                    changed = true;
                }
                SettingChange::BacklogFixedSprintCapacity(value) => {
                    let Some(value) = parse_positive_number(&value) else {
                        ctx.notify(tuicore::Notification::warning(
                            "Invalid sprint capacity",
                            "Enter a positive number of story points.",
                        ));
                        continue;
                    };
                    settings.backlog_runway.fixed_sprint_capacity = value;
                    changed = true;
                }
                SettingChange::BacklogUseAverageTicketSize(value) => {
                    settings.backlog_runway.use_average_ticket_size = value;
                    changed = true;
                }
                SettingChange::BacklogFixedTicketSize(value) => {
                    let Some(value) = parse_nonnegative_number(&value) else {
                        ctx.notify(tuicore::Notification::warning(
                            "Invalid assumed ticket size",
                            "Enter zero or a positive number of story points.",
                        ));
                        continue;
                    };
                    settings.backlog_runway.fixed_ticket_size = value;
                    changed = true;
                }
                SettingChange::BacklogSprintTolerancePercent(value) => {
                    let Some(value) = value
                        .trim()
                        .parse::<u8>()
                        .ok()
                        .filter(|value| *value <= 100)
                    else {
                        ctx.notify(tuicore::Notification::warning(
                            "Invalid sprint tolerance",
                            "Enter a whole percentage from 0 to 100.",
                        ));
                        continue;
                    };
                    settings.backlog_runway.sprint_tolerance_percent = value;
                    changed = true;
                }
                SettingChange::BacklogExcludedSprintNameFragments(value) => {
                    settings.excluded_sprint_name_fragments = sprint_name_fragments(&value);
                    changed = true;
                }
                SettingChange::Wpm(value) => {
                    let Some(wpm) = parse_speed_reader_wpm(&value) else {
                        ctx.notify(tuicore::Notification::warning(
                            "Invalid speed reader WPM",
                            format!(
                                "Enter a whole number from {MIN_SPEED_READER_WPM} to {MAX_SPEED_READER_WPM}."
                            ),
                        ));
                        continue;
                    };
                    settings.speed_reader.wpm = wpm;
                    changed = true;
                }
                SettingChange::MarkdownBlockPause(value) => {
                    let Some(markdown_block_pause) = parse_markdown_block_pause(&value) else {
                        ctx.notify(tuicore::Notification::warning(
                            "Invalid block delay",
                            format!(
                                "Enter a whole number from 0 to {MAX_MARKDOWN_BLOCK_PAUSE_MS} ms."
                            ),
                        ));
                        continue;
                    };
                    settings.speed_reader.markdown_block_pause = markdown_block_pause;
                    changed = true;
                }
                SettingChange::RecentTicketsLimit(value) => {
                    let Some(value) = value
                        .trim()
                        .parse::<usize>()
                        .ok()
                        .filter(|value| (1..=100).contains(value))
                    else {
                        ctx.notify(tuicore::Notification::warning(
                            "Invalid recent ticket limit",
                            "Enter a whole number from 1 to 100.",
                        ));
                        continue;
                    };
                    settings.recent_tickets_limit = value;
                    changed = true;
                }
            }
        }
        if changed {
            self.service.save_settings(settings);
        }
    }
}

#[derive(Clone)]
struct SprintExclusionRow {
    id: u64,
    fragment: String,
}

struct SprintExclusionList {
    list: ListControl<SprintExclusionRow, u64>,
    changes: Rc<RefCell<Vec<SettingChange>>>,
}

impl SprintExclusionList {
    fn new(fragments: Vec<String>, changes: Rc<RefCell<Vec<SettingChange>>>) -> Self {
        let rows = fragments
            .into_iter()
            .enumerate()
            .map(|(index, fragment)| SprintExclusionRow {
                id: u64::try_from(index).unwrap_or(u64::MAX),
                fragment,
            })
            .collect::<Vec<_>>();
        let mut next_id = u64::try_from(rows.len()).unwrap_or(u64::MAX);
        let list = ListControl::list(
            rows,
            |row: &SprintExclusionRow| row.id,
            |row: &SprintExclusionRow| row.fragment.clone(),
            move |fragment, _| {
                let row = SprintExclusionRow {
                    id: next_id,
                    fragment,
                };
                next_id = next_id.saturating_add(1);
                row
            },
        )
        .title("Excluded sprint name fragments")
        .empty_message("No sprint names excluded.")
        .max_rows(100)
        .editable(
            |row| vec![row.fragment.clone()],
            |row, values| row.fragment.clone_from(&values[0]),
        );
        Self { list, changes }
    }

    fn sync(&mut self) {
        if self.list.take_events().is_empty() {
            return;
        }
        let fragments = self
            .list
            .items()
            .iter()
            .map(|row| row.fragment.trim())
            .filter(|fragment| !fragment.is_empty())
            .collect::<Vec<_>>()
            .join(",");
        self.changes
            .borrow_mut()
            .push(SettingChange::BacklogExcludedSprintNameFragments(fragments));
    }
}

impl TuiNode for SprintExclusionList {
    fn measure(&self, proposal: LayoutProposal) -> LayoutSizeHint {
        self.list.measure(proposal)
    }

    fn layout(&mut self, area: Rect, ctx: &mut LayoutCtx) -> LayoutResult {
        self.list.layout(area, ctx)
    }

    fn render<'a>(&'a self, frame: &mut Frame, area: Rect, ctx: &mut RenderCtx<'a>) {
        self.list.render(frame, area, ctx);
    }

    fn event(&mut self, event: &TuiEvent, ctx: &mut EventCtx<()>) -> EventOutcome {
        let outcome = self.list.event(event, ctx);
        self.sync();
        outcome
    }

    fn dispatch_event(
        &mut self,
        route: &EventRoute,
        event: &TuiEvent,
        ctx: &mut EventCtx<()>,
    ) -> EventOutcome {
        let outcome = self.list.dispatch_event(route, event, ctx);
        self.sync();
        outcome
    }

    fn dispatch_focus(&mut self, target: &FocusTarget, focused: bool, ctx: &mut FocusCtx<()>) {
        self.list.dispatch_focus(target, focused, ctx);
    }

    fn focus(&mut self, target: Option<&FocusId>, focused: bool, ctx: &mut FocusCtx<()>) {
        self.list.focus(target, focused, ctx);
    }

    fn tick(&mut self, dt: Duration, settings: AnimationSettings) -> TickResult {
        self.list.tick(dt, settings)
    }

    fn init(&mut self, ctx: &mut LifecycleCtx<()>) {
        self.list.init(ctx);
    }

    fn mount(&mut self, ctx: &mut LifecycleCtx<()>) {
        self.list.mount(ctx);
    }

    fn unmount(&mut self, ctx: &mut LifecycleCtx<()>) {
        self.list.unmount(ctx);
    }

    fn destroy(&mut self, ctx: &mut LifecycleCtx<()>) {
        self.list.destroy(ctx);
    }
}

fn sprint_name_fragments(value: &str) -> Vec<String> {
    value
        .split(',')
        .map(str::trim)
        .filter(|fragment| !fragment.is_empty())
        .fold(Vec::new(), |mut fragments, fragment| {
            if !fragments
                .iter()
                .any(|existing: &String| existing.eq_ignore_ascii_case(fragment))
            {
                fragments.push(fragment.to_owned());
            }
            fragments
        })
}

fn parse_positive_number(value: &str) -> Option<f64> {
    parse_nonnegative_number(value).filter(|value| *value > 0.0)
}

fn parse_nonnegative_number(value: &str) -> Option<f64> {
    value
        .trim()
        .parse::<f64>()
        .ok()
        .filter(|value| value.is_finite() && *value >= 0.0)
}

impl TuiNode for SettingsDialog {
    fn measure(&self, proposal: LayoutProposal) -> LayoutSizeHint {
        self.root.measure(proposal)
    }

    fn layout(&mut self, area: Rect, ctx: &mut LayoutCtx) -> LayoutResult {
        self.root.layout(area, ctx)
    }

    fn render<'a>(&'a self, frame: &mut Frame, area: Rect, ctx: &mut RenderCtx<'a>) {
        self.root.render(frame, area, ctx);
    }

    fn event(&mut self, event: &TuiEvent, ctx: &mut EventCtx<()>) -> EventOutcome {
        let outcome = self.root.event(event, ctx);
        self.apply_changes(ctx);
        outcome
    }

    fn dispatch_event(
        &mut self,
        route: &EventRoute,
        event: &TuiEvent,
        ctx: &mut EventCtx<()>,
    ) -> EventOutcome {
        let outcome = self.root.dispatch_event(route, event, ctx);
        self.apply_changes(ctx);
        outcome
    }

    fn dispatch_focus(&mut self, target: &FocusTarget, focused: bool, ctx: &mut FocusCtx<()>) {
        self.root.dispatch_focus(target, focused, ctx);
    }

    fn focus(&mut self, target: Option<&FocusId>, focused: bool, ctx: &mut FocusCtx<()>) {
        self.root.focus(target, focused, ctx);
    }

    fn tick(&mut self, dt: Duration, settings: AnimationSettings) -> TickResult {
        self.root.tick(dt, settings)
    }

    fn init(&mut self, ctx: &mut LifecycleCtx<()>) {
        self.root.init(ctx);
    }

    fn mount(&mut self, ctx: &mut LifecycleCtx<()>) {
        self.root.mount(ctx);
    }

    fn unmount(&mut self, ctx: &mut LifecycleCtx<()>) {
        self.root.unmount(ctx);
    }

    fn destroy(&mut self, ctx: &mut LifecycleCtx<()>) {
        self.root.destroy(ctx);
    }
}

#[cfg(test)]
mod tests;