edamame 0.1.4

A fast TUI Markdown editor and viewer
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
//! Settings overlay — adapter wrapping [`crate::ui::SettingsState`].
//!
//! Field changes drive [`crate::app::App::save_config_with_flash`].  The "Open config.toml in
//! external editor" row sets a deferred flag the run loop drains, since the editor invocation needs
//! the `&mut Terminal` only the run loop owns.

use std::any::Any;

use crossterm::event::KeyEvent;
use ratatui::layout::Rect;
use ratatui::Frame;

use super::types::{Modal, ModalOutcome, ModalRenderCtx};
use crate::app::App;
use crate::config::sections::VIM_HANDLER;
use crate::config::Config;
use crate::ui::settings_overlay::{
    LABEL_BIG_H1, LABEL_BLINK_CURSOR, LABEL_MATH_PREVIEW, LABEL_REFLOW, LABEL_SCROLL_SPEED,
    LABEL_SHOW_DIAGRAMS, LABEL_SHOW_IMAGES, LABEL_SHOW_REMOTE_IMAGES, LABEL_SYNTAX_HIGHLIGHTING,
    LABEL_VIM_MODE, LABEL_VISUAL_LINE_NAV,
};
use crate::ui::{ModalKind, SettingsResponse, SettingsState, SettingsView};

pub struct SettingsOverlayModal {
    state: SettingsState,
}

impl SettingsOverlayModal {
    pub fn new() -> Self {
        Self {
            state: SettingsState::new(),
        }
    }
}

/// Map a [`SettingsResponse`] to a [`ModalOutcome`], running its App-side effects.  Shared by the
/// key and click paths so a click on a row behaves exactly like operating it from the keyboard.
fn resolve(app: &mut App, response: SettingsResponse) -> ModalOutcome {
    match response {
        SettingsResponse::Continue => ModalOutcome::Continue,
        SettingsResponse::Cancelled => ModalOutcome::Close,
        SettingsResponse::OpenInExternalEditor => {
            // Record intent only; the run loop owns the `Terminal` handle the editor needs.
            ModalOutcome::CloseAnd(Box::new(|app| {
                app.pending_open_config_in_editor = true;
                app.needs_draw = true;
            }))
        }
        SettingsResponse::OpenConfigFolder => ModalOutcome::CloseAnd(Box::new(|app| {
            if let Some(dir) = Config::config_dir() {
                app.spawn_open_worker(dir.display().to_string());
            } else {
                app.notify("No config directory available", ModalKind::Error);
            }
            app.needs_draw = true;
        })),
        SettingsResponse::FieldChanged(label) => {
            app.save_config_with_flash("failed to persist settings overlay change");
            apply_live_update(label, app);
            ModalOutcome::Continue
        }
    }
}

/// Push a single settings-overlay change into App-owned cached state.  Separate from [`resolve`]
/// so the live-update wiring can be unit-tested without the full overlay key dispatch.
pub(crate) fn apply_live_update(label: &str, app: &mut App) {
    match label {
        LABEL_BIG_H1 => app.editor.set_big_h1(app.config.editor.big_h1),
        LABEL_REFLOW => app.editor.set_reflow(app.config.editor.reflow),
        LABEL_SYNTAX_HIGHLIGHTING => app
            .editor
            .set_syntax_highlighting(app.config.editor.syntax_highlighting),
        LABEL_BLINK_CURSOR => app.editor.cursor_blink.apply_config(
            app.config.editor.cursor_blink,
            app.config.editor.cursor_blink_ms,
        ),
        LABEL_SCROLL_SPEED => app
            .mouse
            .set_wheel_step(app.config.editor.mouse_scroll_lines),
        LABEL_VISUAL_LINE_NAV => {
            app.editor.visual_line_nav = app.config.editor.visual_line_nav;
        }
        LABEL_VIM_MODE => {
            // Rebuild the live VimState so vim editing turns on/off without a restart.
            app.set_vim_enabled(app.config.modal.handler == VIM_HANDLER);
        }
        // These rows emit `FieldChanged` only on a real transition, never for a no-op cycle.
        LABEL_SHOW_IMAGES => app.apply_images_setting_change(),
        LABEL_SHOW_REMOTE_IMAGES => app.apply_remote_policy_change(),
        LABEL_SHOW_DIAGRAMS => app.apply_diagrams_setting_change(),
        LABEL_MATH_PREVIEW => app.editor.set_math_preview(app.config.figures.math_preview),
        _ => {}
    }
}

impl Default for SettingsOverlayModal {
    fn default() -> Self {
        Self::new()
    }
}

impl Modal for SettingsOverlayModal {
    fn render(&mut self, frame: &mut Frame<'_>, area: Rect, ctx: &ModalRenderCtx<'_>) {
        let view = SettingsView {
            theme: ctx.theme,
            config: ctx.config,
            cursor_visible: ctx.cursor_visible,
        };
        frame.render_stateful_widget(view, area, &mut self.state);
    }

    fn handle_key(
        &mut self,
        key: KeyEvent,
        app: &mut App,
        _doc_height: usize,
        _doc_width: usize,
    ) -> ModalOutcome {
        let response = self.state.handle_key(&key, &mut app.config);
        resolve(app, response)
    }

    fn handle_paste(&mut self, text: &str) -> ModalOutcome {
        self.state.paste(text);
        ModalOutcome::Continue
    }

    fn handle_wheel(&mut self, delta: i32) {
        self.state.scroll_state.scroll_by(delta);
    }

    fn handle_click(&mut self, col: u16, row: u16, app: &mut App) -> ModalOutcome {
        if super::types::esc_rect_hit(self.state.esc_button_rect, col, row) {
            return ModalOutcome::Close;
        }
        let response = self.state.handle_click(col, row, &mut app.config);
        resolve(app, response)
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    use super::*;
    use crate::app::test_utils::{app_with_buffer, make_app};
    use crate::ui::settings_overlay::all_row_labels;

    /// Labels whose config field is also cached on `App` and so needs an explicit push to take
    /// effect without a restart.  Kept in sync by [`live_update_coverage_is_exhaustive`].
    const LIVE_UPDATE_LABELS: &[&str] = &[
        LABEL_BIG_H1,
        LABEL_REFLOW,
        LABEL_BLINK_CURSOR,
        LABEL_MATH_PREVIEW,
        LABEL_SCROLL_SPEED,
        LABEL_SHOW_DIAGRAMS,
        LABEL_SHOW_IMAGES,
        LABEL_SHOW_REMOTE_IMAGES,
        LABEL_SYNTAX_HIGHLIGHTING,
        LABEL_VISUAL_LINE_NAV,
        LABEL_VIM_MODE,
    ];

    /// Labels read live, with no arm in [`apply_live_update`].  Together with
    /// [`LIVE_UPDATE_LABELS`] this must account for every row in the overlay.
    const NON_LIVE_UPDATE_LABELS: &[&str] = &[
        crate::ui::settings_overlay::HEADER_NOTE,
        "",
        "Open config folder",
        "Open config.toml",
        "",
        "Autosave",
        "  Char limit",
        "Check for updates",
        "Diff when file changes",
        "Limit editor width",
        "Show line numbers",
        "Show table buttons",
    ];

    #[test]
    fn live_update_coverage_is_exhaustive() {
        // A new row in `build_rows` trips this until it is classified as live-update or not.
        let actual = all_row_labels();
        let mut classified: Vec<&str> = LIVE_UPDATE_LABELS
            .iter()
            .copied()
            .chain(NON_LIVE_UPDATE_LABELS.iter().copied())
            .collect();
        classified.sort();
        let mut sorted_actual = actual.clone();
        sorted_actual.sort();
        assert_eq!(
            sorted_actual, classified,
            "settings overlay rows changed; update LIVE_UPDATE_LABELS \
             and/or NON_LIVE_UPDATE_LABELS in src/app/modal/settings.rs"
        );
        for label in LIVE_UPDATE_LABELS {
            assert!(
                !NON_LIVE_UPDATE_LABELS.contains(label),
                "{label:?} is in both LIVE_UPDATE_LABELS and NON_LIVE_UPDATE_LABELS"
            );
        }
    }

    #[test]
    fn live_update_pushes_big_h1_into_editor_cache() {
        let mut app = make_app();
        let original = app.editor.big_h1;
        app.config.editor.big_h1 = !original;
        apply_live_update(LABEL_BIG_H1, &mut app);
        assert_eq!(app.editor.big_h1, app.config.editor.big_h1);
        assert_ne!(app.editor.big_h1, original);
    }

    #[test]
    fn live_update_pushes_math_preview_into_editor_cache() {
        let mut app = make_app();
        let original = app.editor.math_preview;
        app.config.figures.math_preview = !original;
        apply_live_update(LABEL_MATH_PREVIEW, &mut app);
        assert_eq!(app.editor.math_preview, app.config.figures.math_preview);
        assert_ne!(app.editor.math_preview, original);
    }

    #[test]
    fn live_update_pushes_visual_line_nav_into_editor_cache() {
        let mut app = make_app();
        let original = app.editor.visual_line_nav;
        app.config.editor.visual_line_nav = !original;
        apply_live_update(LABEL_VISUAL_LINE_NAV, &mut app);
        assert_eq!(
            app.editor.visual_line_nav,
            app.config.editor.visual_line_nav
        );
        assert_ne!(app.editor.visual_line_nav, original);
    }

    #[test]
    fn live_update_pushes_scroll_speed_into_mouse_dispatcher() {
        let mut app = make_app();
        let new_step = app.config.editor.mouse_scroll_lines + 7;
        app.config.editor.mouse_scroll_lines = new_step;
        apply_live_update(LABEL_SCROLL_SPEED, &mut app);
        assert_eq!(app.mouse.wheel_step(), new_step);
    }

    #[test]
    fn live_update_toggles_vim_state_on_and_off() {
        let mut app = make_app();
        // make_app starts with the default handler → no vim state.
        assert!(app.vim.is_none());

        app.config.modal.handler = "vim".into();
        apply_live_update(LABEL_VIM_MODE, &mut app);
        assert!(app.vim.is_some(), "vim mode on builds VimState");

        app.config.modal.handler = "default".into();
        apply_live_update(LABEL_VIM_MODE, &mut app);
        assert!(app.vim.is_none(), "vim mode off clears VimState");
    }

    #[test]
    fn live_update_images_ask_queues_prompt_and_resets_session_answer() {
        let mut app = app_with_buffer("![a](img.png)\n", 0);
        // An earlier session-level decline, then a settings change to `Ask`.
        app.session_images_enabled = Some(false);
        app.editor.images_enabled = false;
        app.config.images.enabled = crate::config::ImagesEnabled::Ask;
        apply_live_update(LABEL_SHOW_IMAGES, &mut app);
        assert_eq!(app.session_images_enabled, None);
        assert!(app.editor.images_enabled, "Ask reserves image rows again");
        assert!(app
            .modal_stack
            .contains::<crate::app::modal::ImagesEnabledPromptModal>());
    }

    #[test]
    fn live_update_images_never_collapses_layout_and_drops_prompts() {
        let mut app = app_with_buffer("![a](img.png)\n", 0);
        app.config.images.enabled = crate::config::ImagesEnabled::Ask;
        apply_live_update(LABEL_SHOW_IMAGES, &mut app);
        assert!(app
            .modal_stack
            .contains::<crate::app::modal::ImagesEnabledPromptModal>());
        app.config.images.enabled = crate::config::ImagesEnabled::Never;
        apply_live_update(LABEL_SHOW_IMAGES, &mut app);
        assert!(!app.editor.images_enabled);
        assert!(!app
            .modal_stack
            .contains::<crate::app::modal::ImagesEnabledPromptModal>());
    }

    #[test]
    fn live_update_images_always_enables_layout_without_prompt() {
        let mut app = app_with_buffer("![a](img.png)\n", 0);
        app.session_images_enabled = Some(false);
        app.editor.images_enabled = false;
        app.config.images.enabled = crate::config::ImagesEnabled::Always;
        apply_live_update(LABEL_SHOW_IMAGES, &mut app);
        assert!(app.editor.images_enabled);
        assert_eq!(app.session_images_enabled, None);
        assert!(!app
            .modal_stack
            .contains::<crate::app::modal::ImagesEnabledPromptModal>());
    }

    #[test]
    fn live_update_diagrams_ask_queues_prompt_and_resets_session_answer() {
        let mut app = app_with_buffer("```mermaid\ngraph TD;\n```\n", 0);
        app.session_diagrams_enabled = Some(false);
        app.editor.diagrams_enabled = false;
        app.config.figures.enabled = crate::config::FiguresEnabled::Ask;
        apply_live_update(LABEL_SHOW_DIAGRAMS, &mut app);
        assert_eq!(app.session_diagrams_enabled, None);
        assert!(
            app.editor.diagrams_enabled,
            "Ask reserves diagram rows again"
        );
        assert!(app
            .modal_stack
            .contains::<crate::app::modal::FiguresEnabledPromptModal>());
    }

    #[test]
    fn live_update_diagrams_never_collapses_layout_and_drops_prompt() {
        let mut app = app_with_buffer("```mermaid\ngraph TD;\n```\n", 0);
        app.config.figures.enabled = crate::config::FiguresEnabled::Ask;
        apply_live_update(LABEL_SHOW_DIAGRAMS, &mut app);
        assert!(app
            .modal_stack
            .contains::<crate::app::modal::FiguresEnabledPromptModal>());
        app.config.figures.enabled = crate::config::FiguresEnabled::Never;
        apply_live_update(LABEL_SHOW_DIAGRAMS, &mut app);
        assert!(!app.editor.diagrams_enabled);
        assert!(!app
            .modal_stack
            .contains::<crate::app::modal::FiguresEnabledPromptModal>());
    }

    #[test]
    fn live_update_diagrams_always_enables_layout_without_prompt() {
        let mut app = app_with_buffer("```mermaid\ngraph TD;\n```\n", 0);
        app.session_diagrams_enabled = Some(false);
        app.editor.diagrams_enabled = false;
        app.config.figures.enabled = crate::config::FiguresEnabled::Always;
        apply_live_update(LABEL_SHOW_DIAGRAMS, &mut app);
        assert!(app.editor.diagrams_enabled);
        assert_eq!(app.session_diagrams_enabled, None);
        assert!(!app
            .modal_stack
            .contains::<crate::app::modal::FiguresEnabledPromptModal>());
    }

    #[test]
    fn live_update_remote_ask_queues_prompt_and_resets_session_allow() {
        let mut app = app_with_buffer("![a](https://example.com/a.png)\n", 0);
        app.config.images.enabled = crate::config::ImagesEnabled::Always;
        app.session_allow_remote = true;
        app.config.images.remote_policy = crate::config::RemoteImagePolicy::Ask;
        apply_live_update(LABEL_SHOW_REMOTE_IMAGES, &mut app);
        assert!(!app.session_allow_remote);
        assert!(app
            .modal_stack
            .contains::<crate::app::modal::RemoteImagePromptModal>());
    }

    #[test]
    fn live_update_remote_never_evicts_cached_remote_decodes() {
        let mut app = app_with_buffer("![a](https://example.com/a.png)\n", 0);
        app.editor.images.set_decoded(
            "https://example.com/a.png",
            image::DynamicImage::new_rgba8(1, 1),
        );
        app.session_allow_remote = true;
        app.config.images.remote_policy = crate::config::RemoteImagePolicy::Never;
        apply_live_update(LABEL_SHOW_REMOTE_IMAGES, &mut app);
        assert!(!app.session_allow_remote);
        assert!(
            app.editor
                .images
                .status("https://example.com/a.png")
                .is_none(),
            "remote decode evicted so the new policy re-resolves it"
        );
        assert!(!app
            .modal_stack
            .contains::<crate::app::modal::RemoteImagePromptModal>());
    }

    #[test]
    fn settings_overlay_open_external_sets_pending_flag_and_closes_overlay() {
        let mut app = make_app();
        app.open_settings_overlay();
        assert!(app.modal_stack.contains::<SettingsOverlayModal>());
        // Default focus is the first editable row; one Up skips the divider to the editor row.
        app.dispatch_modal_key(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE), 40, 80);
        app.dispatch_modal_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), 40, 80);
        assert!(app.pending_open_config_in_editor);
        assert!(!app.modal_stack.contains::<SettingsOverlayModal>());
    }

    #[test]
    fn settings_overlay_open_config_folder_closes_overlay() {
        // Two Up presses from the default focus (skipping the divider) reach the folder row.
        // It opens the OS file manager, so no `pending_open_config_in_editor` flag is set.
        let mut app = make_app();
        app.open_settings_overlay();
        app.dispatch_modal_key(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE), 40, 80);
        app.dispatch_modal_key(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE), 40, 80);
        app.dispatch_modal_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), 40, 80);
        assert!(!app.pending_open_config_in_editor);
        assert!(!app.modal_stack.contains::<SettingsOverlayModal>());
    }
}