rat_markdown/
lib.rs

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
#![doc = include_str!("../readme.md")]

use crate::op::{md_backtab, md_format, md_line_break, md_make_header, md_tab};
use rat_event::{ct_event, flow, HandleEvent, Regular};
use rat_focus::HasFocus;
use rat_text::event::TextOutcome;
use rat_text::text_area::TextAreaState;

mod format;
mod operations;
mod parser;
mod styles;
mod util;

pub use styles::{parse_md_styles, MDStyle};
pub mod op {
    pub use crate::format::{md_format, reformat};
    pub use crate::operations::{md_backtab, md_line_break, md_make_header, md_tab};
}

/// Event qualifier.
#[derive(Debug)]
pub struct MarkDown {
    text_width: u16,
}

impl Default for MarkDown {
    fn default() -> Self {
        Self { text_width: 65 }
    }
}

impl MarkDown {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn text_width(mut self, width: u16) -> Self {
        self.text_width = width;
        self
    }
}

impl HandleEvent<crossterm::event::Event, MarkDown, TextOutcome> for TextAreaState {
    fn handle(&mut self, event: &crossterm::event::Event, qualifier: MarkDown) -> TextOutcome {
        if self.is_focused() {
            flow!(match event {
                ct_event!(key press CONTROL-'f') =>
                    md_format(self, qualifier.text_width as usize, false),
                ct_event!(key press CONTROL-'g') =>
                    md_format(self, qualifier.text_width as usize, true),

                ct_event!(key press ALT-'1') => md_make_header(self, 1),
                ct_event!(key press ALT-'2') => md_make_header(self, 2),
                ct_event!(key press ALT-'3') => md_make_header(self, 3),
                ct_event!(key press ALT-'4') => md_make_header(self, 4),
                ct_event!(key press ALT-'5') => md_make_header(self, 5),
                ct_event!(key press ALT-'6') => md_make_header(self, 6),
                // todo: more
                ct_event!(keycode press Enter) => md_line_break(self),
                ct_event!(keycode press Tab) => md_tab(self),
                ct_event!(keycode press SHIFT-BackTab) => md_backtab(self),
                _ => TextOutcome::Continue,
            });
        }

        self.handle(event, Regular)
    }
}