rat_menu/
menuitem.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
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
//!
//! MenuItem for both MenuLine and PopupMenu.
//!

use crate::_private::NonExhaustive;
use std::borrow::Cow;
use std::ops::Range;
use unicode_segmentation::UnicodeSegmentation;

/// Separator style
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Separator {
    #[default]
    Plain,
    Empty,
    Thick,
    Double,
    Dashed,
    Dotted,
}

/// A menu item.
#[derive(Debug, Clone)]
pub struct MenuItem<'a> {
    /// Menuitem text
    pub item: Cow<'a, str>,
    /// Text range to highlight. This is a byte-range into `item`.
    pub highlight: Option<Range<usize>>,
    /// Navigation key char.
    pub navchar: Option<char>,
    /// Right aligned text. To show the hotkey, or whatever.
    /// Hotkey handling is not included in this crate.
    pub right: Cow<'a, str>,
    /// Disabled item.
    pub disabled: bool,

    /// Separator after the item.
    pub separator: Option<Separator>,

    pub non_exhaustive: NonExhaustive,
}

impl Default for MenuItem<'_> {
    fn default() -> Self {
        Self {
            item: Default::default(),
            highlight: None,
            navchar: None,
            right: Default::default(),
            disabled: false,
            separator: None,
            non_exhaustive: NonExhaustive,
        }
    }
}

impl<'a> MenuItem<'a> {
    pub fn new() -> Self {
        Self {
            item: Default::default(),
            highlight: None,
            navchar: None,
            right: Default::default(),
            disabled: false,
            separator: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }

    /// Uses '_' as special character.
    ///
    /// __Item__
    ///
    /// The first '_' marks the navigation-char.
    /// Pipe '|' separates the item text and the right text.
    ///
    /// __Separator__
    ///
    /// `\\` (underscore) is used as prefix and then
    /// a fixed string to identify the separator:
    ///
    /// * `\\   ` - three blanks -> empty separator
    /// * `\\___` - three underscores -> plain line
    /// * `\\______` - six underscore -> thick line
    /// * `\\===` - three equals -> double line
    /// * `\\---` - three hyphen -> dashed line
    /// * `\\...` - three dots -> dotted line
    ///
    pub fn new_parsed(s: &'a str) -> Self {
        if is_separator_str(s) {
            Self::new_sep(separator_str(s))
        } else {
            item_str(s)
        }
    }

    /// New borrowed string as item text.
    pub fn new_str(text: &'a str) -> Self {
        Self {
            item: Cow::Borrowed(text),
            highlight: None,
            navchar: None,
            right: Cow::Borrowed(""),
            disabled: false,
            separator: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }

    /// New with owned string as item text.
    pub fn new_string(text: String) -> Self {
        Self {
            item: Cow::Owned(text),
            highlight: None,
            navchar: None,
            right: Default::default(),
            disabled: false,
            separator: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }

    /// New with navigation char and highlight.
    /// Highlight here is a byte range into the text.
    pub fn new_nav_str(text: &'a str, highlight: Range<usize>, navchar: char) -> Self {
        Self {
            item: Cow::Borrowed(text),
            highlight: Some(highlight),
            navchar: Some(navchar.to_ascii_lowercase()),
            right: Cow::Borrowed(""),
            disabled: false,
            separator: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }

    /// New with navigation char and highlight.
    /// Highlight here is a byte range into the text.
    pub fn new_nav_string(text: String, highlight: Range<usize>, navchar: char) -> Self {
        Self {
            item: Cow::Owned(text),
            highlight: Some(highlight),
            navchar: Some(navchar.to_ascii_lowercase()),
            right: Cow::Borrowed(""),
            disabled: false,
            separator: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }

    /// New separator.
    ///
    /// Such a menu item will be merged with the one before, unless
    /// you set some item-text later.
    pub fn new_sep(separator: Separator) -> Self {
        Self {
            item: Default::default(),
            highlight: None,
            navchar: None,
            right: Default::default(),
            disabled: false,
            separator: Some(separator),
            non_exhaustive: NonExhaustive,
        }
    }

    /// Set the right text.
    pub fn right(mut self, right: &'a str) -> Self {
        self.right = Cow::Borrowed(right);
        self
    }

    /// Set disabled.
    pub fn disabled(mut self) -> Self {
        self.disabled = true;
        self
    }

    /// Adds a separator after the menuitem.
    pub fn separator(mut self, separator: Separator) -> Self {
        self.separator = Some(separator);
        self
    }

    /// Text-width in graphemes for item.
    pub fn item_width(&self) -> u16 {
        self.item.graphemes(true).count() as u16 - if self.navchar.is_some() { 1 } else { 0 }
    }

    /// Text-width in graphemes for right.
    pub fn right_width(&self) -> u16 {
        self.right.graphemes(true).count() as u16
    }

    /// Text-height.
    pub fn height(&self) -> u16 {
        if self.separator.is_none() {
            1
        } else {
            2
        }
    }
}

#[allow(clippy::needless_bool)]
#[allow(clippy::if_same_then_else)]
fn is_separator_str(s: &str) -> bool {
    if s == "\\   " {
        true
    } else if s == "\\___" {
        true
    } else if s == "\\______" {
        true
    } else if s == "\\===" {
        true
    } else if s == "\\---" {
        true
    } else if s == "\\..." {
        true
    } else {
        false
    }
}

/// This uses `\\` (underscore) as prefix and
/// a fixed string to identify the separator:
///
/// * `\\   ` - three blanks -> empty separator
/// * `\\___` - three underscores -> plain line
/// * `\\______` - six underscore -> thick line
/// * `\\===` - three equals -> double line
/// * `\\---` - three hyphen -> dashed line
/// * `\\...` - three dots -> dotted line
fn separator_str(s: &str) -> Separator {
    if s == "\\   " {
        Separator::Empty
    } else if s == "\\___" {
        Separator::Plain
    } else if s == "\\______" {
        Separator::Thick
    } else if s == "\\===" {
        Separator::Double
    } else if s == "\\---" {
        Separator::Dashed
    } else if s == "\\..." {
        Separator::Dotted
    } else {
        unreachable!()
    }
}

/// Create a Line from the given text.
/// The first '_' marks the navigation-char.
/// Pipe '|' separates the item text and the right text.
fn item_str(txt: &str) -> MenuItem<'_> {
    let mut idx_underscore = None;
    let mut idx_navchar_start = None;
    let mut idx_navchar_end = None;
    let mut idx_pipe = None;
    let cit = txt.char_indices();
    for (idx, c) in cit {
        if idx_underscore.is_none() && c == '_' {
            idx_underscore = Some(idx);
        } else if idx_underscore.is_some() && idx_navchar_start.is_none() {
            idx_navchar_start = Some(idx);
        } else if idx_navchar_start.is_some() && idx_navchar_end.is_none() {
            idx_navchar_end = Some(idx);
        }
        if c == '|' {
            idx_pipe = Some(idx);
        }
    }
    if idx_navchar_start.is_some() && idx_navchar_end.is_none() {
        idx_navchar_end = Some(txt.len());
    }

    if let Some(pipe) = idx_pipe {
        if let Some(navchar_end) = idx_navchar_end {
            if navchar_end > pipe {
                idx_pipe = None;
            }
        }
    }

    let (text, right) = if let Some(idx_pipe) = idx_pipe {
        (&txt[..idx_pipe], &txt[idx_pipe + 1..])
    } else {
        (txt, "")
    };

    if let Some(idx_navchar_start) = idx_navchar_start {
        if let Some(idx_navchar_end) = idx_navchar_end {
            MenuItem {
                item: Cow::Borrowed(text),
                highlight: Some(idx_navchar_start..idx_navchar_end),
                navchar: Some(
                    text[idx_navchar_start..idx_navchar_end]
                        .chars()
                        .next()
                        .expect("char")
                        .to_ascii_lowercase(),
                ),
                right: Cow::Borrowed(right),
                ..Default::default()
            }
        } else {
            unreachable!();
        }
    } else {
        MenuItem {
            item: Cow::Borrowed(text),
            right: Cow::Borrowed(right),
            ..Default::default()
        }
    }
}