tui-journal 0.17.0

Tui app allows writing and managing journals/notes from within the terminal With different local back-ends
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
use backend::DataProvider;
use std::fmt::Debug;

use multi_select_cmd::*;

use super::{App, HandleInputReturnType, MsgBoxResult, UIComponents};

use editor_cmd::*;
use entries_list_cmd::*;
use global_cmd::*;

mod editor_cmd;
mod entries_list_cmd;
mod global_cmd;
mod multi_select_cmd;

type CmdResult = anyhow::Result<HandleInputReturnType>;

#[derive(Debug, Clone, Copy)]
pub enum ClipboardOperation {
    Copy,
    Cut,
    // I didn't use it yet but it's an option from clipboard operation and shouldn't be deleted
    #[allow(dead_code)]
    Paste,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum UICommand {
    Quit,
    ShowHelp,
    CycleFocusedControlForward,
    CycleFocusedControlBack,
    SelectedNextEntry,
    SelectedPrevEntry,
    CreateEntry,
    EditCurrentEntry,
    DeleteCurrentEntry,
    StartEditEntryContent,
    BackEditorNormalMode,
    SaveEntryContent,
    DiscardChangesEntryContent,
    ReloadAll,
    ExportEntryContent,
    EditInExternalEditor,
    EnterMultiSelectMode,
    LeaveMultiSelectMode,
    MulSelToggleSelected,
    MulSelSelectAll,
    MulSelSelectNone,
    MulSelInverSelection,
    MulSelDeleteEntries,
    MulSelExportEntries,
    ShowFilter,
    ResetFilter,
    CycleTagFilter,
    ShowFuzzyFind,
    ToggleEditorVisualMode,
    ToggleFullScreenMode,
    CopyOsClipboard,
    CutOsClipboard,
    PasteOsClipboard,
    ShowSortOptions,
    GoToTopEntry,
    GoToBottomEntry,
    PageUpEntries,
    PageDownEntries,
    Undo,
    Redo,
}

#[derive(Debug, Clone)]
pub struct CommandInfo {
    pub name: String,
    pub description: String,
}

impl CommandInfo {
    pub fn new(name: &str, description: &str) -> Self {
        Self {
            name: name.to_owned(),
            description: description.to_owned(),
        }
    }
}

impl UICommand {
    pub fn get_info(&self) -> CommandInfo {
        match self {
            UICommand::Quit => CommandInfo::new("Exit", "Exit the program"),
            UICommand::ShowHelp => CommandInfo::new("Show help", "Show keybindings overview"),
            UICommand::CycleFocusedControlForward => {
                CommandInfo::new("Cycle focus forward", "Move focus to the next control")
            }
            UICommand::CycleFocusedControlBack => {
                CommandInfo::new("Cycle focus backward", "Move focus to the previous control")
            }
            UICommand::SelectedNextEntry => CommandInfo::new(
                "Select next journal",
                "Select next entry in the journals list",
            ),
            UICommand::SelectedPrevEntry => CommandInfo::new(
                "Select previous journal",
                "Select previous entry in the journal list",
            ),
            UICommand::CreateEntry => CommandInfo::new(
                "Create new journal",
                "Opens dialog to add a new journal entry",
            ),
            UICommand::EditCurrentEntry => CommandInfo::new(
                "Edit current journal",
                "Open entry dialog to edit current journal entry if any",
            ),
            UICommand::DeleteCurrentEntry => {
                CommandInfo::new("Delete journal", "Delete current journal entry if any")
            }
            UICommand::StartEditEntryContent => CommandInfo::new(
                "Edit journal content",
                "Start editing current journal entry content in editor",
            ),
            UICommand::BackEditorNormalMode => CommandInfo::new(
                "Back to Editor Normal Mode / Leave Editor",
                "Exit editor special modes (insert, visual) and go back to normal mode. If already in normal mode, leave the editor and return focus to the entries list",
            ),
            UICommand::SaveEntryContent => {
                CommandInfo::new("Save", "Save changes on journal content")
            }
            UICommand::DiscardChangesEntryContent => {
                CommandInfo::new("Discard changes", "Discard changes on journal content")
            }
            UICommand::ReloadAll => CommandInfo::new("Reload all", "Reload all entries"),
            UICommand::ExportEntryContent => {
                CommandInfo::new("Export journal content", "Export current journal content")
            }
            UICommand::EditInExternalEditor => CommandInfo::new(
                "Edit in external editor",
                "Edit current journal content in external editor (The editor can be set in configurations file or via the environment variables VISUAL, EDITOR)",
            ),
            UICommand::EnterMultiSelectMode => CommandInfo::new(
                "Enter journals multi selection mode",
                "Enter multi selection mode for journals when journals list is in focus to work with multi journals at once",
            ),
            UICommand::LeaveMultiSelectMode => CommandInfo::new(
                "Leave journals multi selection mode",
                "Leave multi selection mode for journals and return to normal mode",
            ),
            UICommand::MulSelToggleSelected => CommandInfo::new(
                "Toggle selected",
                "Toggle if the current journal is selected in multi selection mode",
            ),
            UICommand::MulSelSelectAll => CommandInfo::new(
                "Select all journals",
                "Select all journals in multi selection mode",
            ),
            UICommand::MulSelSelectNone => CommandInfo::new(
                "Clear selection",
                "Clear journals selection in multi selection mode",
            ),
            UICommand::MulSelInverSelection => CommandInfo::new(
                "Invert selection",
                "Invert journals selection in multi selection mode",
            ),
            UICommand::MulSelDeleteEntries => CommandInfo::new(
                "Delete selection",
                "Delete selected journals in multi selection mode",
            ),
            UICommand::MulSelExportEntries => CommandInfo::new(
                "Export selection",
                "Export selected journals to a transfer JSON file, which can be imported to other back-end files",
            ),
            UICommand::ShowFilter => {
                CommandInfo::new("Open filter", "Open filter popup for journals")
            }
            UICommand::ResetFilter => {
                CommandInfo::new("Reset filter", "Reset the applied filter on journals")
            }
            UICommand::CycleTagFilter => {
                CommandInfo::new("Cycle Tag Filter", "Cycle through the tag filters")
            }
            UICommand::ShowFuzzyFind => {
                CommandInfo::new("Fuzzy find", "Open fuzzy find popup for journals")
            }
            UICommand::ToggleEditorVisualMode => CommandInfo::new(
                "Toggle Editor Visual Mode",
                "Toggle Editor Visual(Select) Mode when editor is in focus",
            ),
            UICommand::ToggleFullScreenMode => CommandInfo::new(
                "Toggle Full Screen Mode",
                "Maximize the currently selected view",
            ),
            UICommand::CopyOsClipboard => CommandInfo::new(
                "Copy to OS clipboard",
                "Copy selection to operation system clipboard while in editor visual mode",
            ),
            UICommand::CutOsClipboard => CommandInfo::new(
                "Cut to OS clipboard",
                "Cut selection to operation system clipboard while in editor visual mode",
            ),
            UICommand::PasteOsClipboard => CommandInfo::new(
                "Paste OS clipboard Content",
                "Paste the operation system clipboard content to in the editor",
            ),
            UICommand::ShowSortOptions => CommandInfo::new(
                "Open sort options",
                "Open sort popup to set the sorting options of the journals",
            ),
            UICommand::GoToTopEntry => CommandInfo::new(
                "Go to top journal",
                "Go to the top entry in the journals' list",
            ),
            UICommand::GoToBottomEntry => CommandInfo::new(
                "Go to bottom journal",
                "Go to the bottom entry in the journals' list",
            ),
            UICommand::PageUpEntries => {
                CommandInfo::new("Page Up journals", "Go one page up in the journals' list")
            }
            UICommand::PageDownEntries => CommandInfo::new(
                "Page Down journals",
                "Go one page down in the journals' list",
            ),
            UICommand::Undo => CommandInfo::new("Undo", "Undo the latest change on journals"),
            UICommand::Redo => CommandInfo::new("Redo", "Redo the latest change on journals"),
        }
    }

    pub async fn execute<D: DataProvider>(
        &self,
        ui_components: &mut UIComponents<'_>,
        app: &mut App<D>,
    ) -> CmdResult {
        match self {
            UICommand::Quit => exec_quit(ui_components),
            UICommand::ShowHelp => exec_show_help(ui_components),
            UICommand::CycleFocusedControlForward => exec_cycle_forward(ui_components),
            UICommand::CycleFocusedControlBack => exec_cycle_backward(ui_components),
            UICommand::SelectedNextEntry => exec_select_next_entry(ui_components, app),
            UICommand::SelectedPrevEntry => exec_select_prev_entry(ui_components, app),
            UICommand::CreateEntry => exec_create_entry(ui_components, app),
            UICommand::EditCurrentEntry => exec_edit_current_entry(ui_components, app),
            UICommand::DeleteCurrentEntry => exec_delete_current_entry(ui_components, app),
            UICommand::StartEditEntryContent => exec_start_edit_content(ui_components),
            UICommand::BackEditorNormalMode => exec_back_editor_to_normal_mode(ui_components),
            UICommand::SaveEntryContent => exec_save_entry_content(ui_components, app).await,
            UICommand::DiscardChangesEntryContent => exec_discard_content(ui_components),
            UICommand::ReloadAll => exec_reload_all(ui_components, app).await,
            UICommand::ExportEntryContent => exec_export_entry_content(ui_components, app),
            UICommand::EditInExternalEditor => {
                exec_edit_in_external_editor(ui_components, app).await
            }
            UICommand::EnterMultiSelectMode => exec_enter_select_mode(ui_components),
            UICommand::LeaveMultiSelectMode => exec_leave_select_mode(ui_components, app),
            UICommand::MulSelToggleSelected => exec_toggle_selected(app),
            UICommand::MulSelSelectAll => exec_select_all(app),
            UICommand::MulSelSelectNone => exec_select_none(app),
            UICommand::MulSelInverSelection => exec_invert_selection(app),
            UICommand::MulSelDeleteEntries => exec_delete_selected_entries(ui_components, app),
            UICommand::MulSelExportEntries => exec_export_selected_entries(ui_components, app),
            UICommand::ShowFilter => exec_show_filter(ui_components, app),
            UICommand::ResetFilter => exec_reset_filter(app),
            UICommand::CycleTagFilter => exec_cycle_tag_filter(ui_components, app),
            UICommand::ShowFuzzyFind => exec_show_fuzzy_find(ui_components, app),
            UICommand::ToggleEditorVisualMode => exec_toggle_editor_visual_mode(ui_components),
            UICommand::ToggleFullScreenMode => exec_toggle_full_screen_mode(app),
            UICommand::CopyOsClipboard => exec_copy_os_clipboard(ui_components),
            UICommand::CutOsClipboard => exec_cut_os_clipboard(ui_components),
            UICommand::PasteOsClipboard => exec_paste_os_clipboard(ui_components),
            UICommand::ShowSortOptions => exec_show_sort_options(ui_components, app),
            cmd @ UICommand::GoToTopEntry => {
                check_unsaved_then_exec_cmd(*cmd, go_to_top_entry, ui_components, app)
            }
            cmd @ UICommand::GoToBottomEntry => {
                check_unsaved_then_exec_cmd(*cmd, go_to_bottom_entry, ui_components, app)
            }
            cmd @ UICommand::PageUpEntries => {
                check_unsaved_then_exec_cmd(*cmd, page_up_entries, ui_components, app)
            }
            cmd @ UICommand::PageDownEntries => {
                check_unsaved_then_exec_cmd(*cmd, page_down_entries, ui_components, app)
            }
            UICommand::Undo => exec_undo(ui_components, app).await,
            UICommand::Redo => exec_redo(ui_components, app).await,
        }
    }

    pub async fn continue_executing<D: DataProvider>(
        &self,
        ui_components: &mut UIComponents<'_>,
        app: &mut App<D>,
        msg_box_result: MsgBoxResult,
    ) -> CmdResult {
        let not_implemented = || unreachable!("continue exec isn't implemented for {:?}", self);
        match self {
            UICommand::Quit => continue_quit(ui_components, app, msg_box_result).await,
            UICommand::ShowHelp => not_implemented(),
            UICommand::CycleFocusedControlForward => not_implemented(),
            UICommand::CycleFocusedControlBack => not_implemented(),
            UICommand::SelectedNextEntry => {
                continue_select_next_entry(ui_components, app, msg_box_result).await
            }
            UICommand::SelectedPrevEntry => {
                continue_select_prev_entry(ui_components, app, msg_box_result).await
            }
            UICommand::CreateEntry => {
                continue_create_entry(ui_components, app, msg_box_result).await
            }
            UICommand::EditCurrentEntry => {
                continue_edit_current_entry(ui_components, app, msg_box_result).await
            }
            UICommand::DeleteCurrentEntry => {
                continue_delete_current_entry(app, msg_box_result).await
            }
            UICommand::StartEditEntryContent => not_implemented(),
            UICommand::BackEditorNormalMode => not_implemented(),
            UICommand::SaveEntryContent => not_implemented(),
            UICommand::DiscardChangesEntryContent => {
                continue_discard_content(ui_components, app, msg_box_result)
            }
            UICommand::ReloadAll => continue_reload_all(ui_components, app, msg_box_result).await,
            UICommand::ExportEntryContent => {
                continue_export_entry_content(ui_components, app, msg_box_result).await
            }
            UICommand::EditInExternalEditor => {
                continue_edit_in_external_editor(ui_components, app, msg_box_result).await
            }
            UICommand::EnterMultiSelectMode => {
                continue_enter_select_mode(ui_components, app, msg_box_result).await
            }
            UICommand::LeaveMultiSelectMode => not_implemented(),
            UICommand::MulSelToggleSelected => not_implemented(),
            UICommand::MulSelSelectAll => not_implemented(),
            UICommand::MulSelSelectNone => not_implemented(),
            UICommand::MulSelInverSelection => not_implemented(),
            UICommand::MulSelDeleteEntries => {
                continue_delete_selected_entries(app, msg_box_result).await
            }
            UICommand::MulSelExportEntries => not_implemented(),
            UICommand::ShowFilter => continue_show_filter(ui_components, app, msg_box_result).await,
            UICommand::ResetFilter => not_implemented(),
            UICommand::CycleTagFilter => {
                continue_cycle_tag_filter(ui_components, app, msg_box_result).await
            }
            UICommand::ShowFuzzyFind => {
                continue_fuzzy_find(ui_components, app, msg_box_result).await
            }
            UICommand::ToggleEditorVisualMode => not_implemented(),
            UICommand::ToggleFullScreenMode => not_implemented(),
            UICommand::CopyOsClipboard => not_implemented(),
            UICommand::CutOsClipboard => not_implemented(),
            UICommand::PasteOsClipboard => not_implemented(),
            UICommand::ShowSortOptions => {
                continue_show_sort_options(ui_components, app, msg_box_result).await
            }
            UICommand::GoToTopEntry => {
                continue_cmd_after_check_unsaved(
                    go_to_top_entry,
                    ui_components,
                    app,
                    msg_box_result,
                )
                .await
            }
            UICommand::GoToBottomEntry => {
                continue_cmd_after_check_unsaved(
                    go_to_bottom_entry,
                    ui_components,
                    app,
                    msg_box_result,
                )
                .await
            }
            UICommand::PageUpEntries => {
                continue_cmd_after_check_unsaved(
                    page_up_entries,
                    ui_components,
                    app,
                    msg_box_result,
                )
                .await
            }
            UICommand::PageDownEntries => {
                continue_cmd_after_check_unsaved(
                    page_down_entries,
                    ui_components,
                    app,
                    msg_box_result,
                )
                .await
            }
            UICommand::Undo => continue_undo(ui_components, app, msg_box_result).await,
            UICommand::Redo => continue_redo(ui_components, app, msg_box_result).await,
        }
    }
}

/// Checks for unsaved and show dialog of there is any, other way it calls the `cmd_func`
pub fn check_unsaved_then_exec_cmd<D, F>(
    cmd: UICommand,
    cmd_func: F,
    ui_components: &mut UIComponents,
    app: &mut App<D>,
) -> CmdResult
where
    D: DataProvider,
    F: Fn(&mut UIComponents, &mut App<D>),
{
    if ui_components.has_unsaved() {
        ui_components.show_unsaved_msg_box(Some(cmd));
    } else {
        cmd_func(ui_components, app);
    }

    Ok(HandleInputReturnType::Handled)
}

/// Calls save entry content if wanted then calls `cmd_func`
pub async fn continue_cmd_after_check_unsaved<D, F>(
    cmd_func: F,
    ui_components: &mut UIComponents<'_>,
    app: &mut App<D>,
    msg_box_result: MsgBoxResult,
) -> CmdResult
where
    D: DataProvider,
    F: Fn(&mut UIComponents, &mut App<D>),
{
    match msg_box_result {
        MsgBoxResult::Ok | MsgBoxResult::Cancel => {}
        MsgBoxResult::Yes => {
            exec_save_entry_content(ui_components, app).await?;
            cmd_func(ui_components, app);
        }
        MsgBoxResult::No => cmd_func(ui_components, app),
    }

    Ok(HandleInputReturnType::Handled)
}