bpaf/
buffer.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
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
#[cfg(feature = "docgen")]
use crate::{
    info::Info,
    meta_help::{HelpItem, HelpItems},
};
use crate::{
    item::{Item, ShortLong},
    Meta,
};

mod console;
mod html;
#[cfg(feature = "docgen")]
mod manpage;
mod splitter;

pub(crate) use self::console::Color;
use self::console::MAX_WIDTH;

#[cfg(feature = "docgen")]
pub use manpage::Section;

impl From<&[(&str, Style)]> for Doc {
    fn from(val: &[(&str, Style)]) -> Self {
        let mut res = Doc::default();
        for (text, style) in val {
            res.write_str(text, *style);
        }
        res
    }
}

impl<const N: usize> From<&'static [(&'static str, Style); N]> for Doc {
    fn from(val: &'static [(&'static str, Style); N]) -> Self {
        let mut res = Doc::default();
        for (text, style) in val {
            res.write_str(text, *style);
        }
        res
    }
}

/// Parser metainformation
///
///
/// This is a newtype around internal parser metainfo representation, generated
/// with [`Parser::with_group_help`](crate::Parser::with_group_help) and consumed by
/// [`Doc::meta`](Doc::meta)
#[derive(Copy, Clone)]
pub struct MetaInfo<'a>(pub(crate) &'a Meta);

impl Doc {
    #[inline]
    /// Append a fragment of plain text to [`Doc`]
    ///
    /// See [`Doc`] for usage examples
    pub fn text(&mut self, text: &str) {
        self.write_str(text, Style::Text);
    }

    #[inline]
    /// Append a fragment of literal text to [`Doc`]
    ///
    /// See [`Doc`] for usage examples
    pub fn literal(&mut self, text: &str) {
        self.write_str(text, Style::Literal);
    }

    #[inline]
    /// Append a fragment of text with emphasis to [`Doc`]
    ///
    /// See [`Doc`] for usage examples
    pub fn emphasis(&mut self, text: &str) {
        self.write_str(text, Style::Emphasis);
    }

    #[inline]
    /// Append a fragment of unexpected user input to [`Doc`]
    ///
    /// See [`Doc`] for usage examples
    pub fn invalid(&mut self, text: &str) {
        self.write_str(text, Style::Invalid);
    }

    /// Append a fragment of parser metadata to [`Doc`]
    ///
    /// See [`Doc`] for usage examples
    pub fn meta(&mut self, meta: MetaInfo, for_usage: bool) {
        self.write_meta(meta.0, for_usage);
    }

    /// Append a `Doc` to [`Doc`]
    ///
    /// See [`Doc`] for usage examples
    pub fn doc(&mut self, buf: &Doc) {
        self.tokens.push(Token::BlockStart(Block::InlineBlock));
        self.tokens.extend(&buf.tokens);
        self.payload.push_str(&buf.payload);
        self.tokens.push(Token::BlockEnd(Block::InlineBlock));
    }

    /// Append a `Doc` to [`Doc`] for plaintext documents try to format
    /// first line as a help section header
    pub fn em_doc(&mut self, buf: &Doc) {
        self.tokens.push(Token::BlockStart(Block::InlineBlock));
        if let Some(Token::Text {
            bytes,
            style: Style::Text,
        }) = buf.tokens.first().copied()
        {
            let prefix = &buf.payload[0..bytes];
            if let Some((a, b)) = prefix.split_once('\n') {
                self.emphasis(a);
                self.tokens.push(Token::BlockStart(Block::Section3));
                self.text(b);

                if buf.tokens.len() > 1 {
                    self.tokens.extend(&buf.tokens[1..]);
                    self.payload.push_str(&buf.payload[bytes..]);
                }
                self.tokens.push(Token::BlockEnd(Block::Section3));
            } else {
                self.emphasis(prefix);
            }
        } else {
            self.tokens.extend(&buf.tokens);
            self.payload.push_str(&buf.payload);
        }

        self.tokens.push(Token::BlockEnd(Block::InlineBlock));
    }
}

impl Doc {
    pub(crate) fn write_shortlong(&mut self, name: &ShortLong) {
        match name {
            ShortLong::Short(s) => {
                self.write_char('-', Style::Literal);
                self.write_char(*s, Style::Literal);
            }
            ShortLong::Long(l) | ShortLong::Both(_, l) => {
                self.write_str("--", Style::Literal);
                self.write_str(l, Style::Literal);
            }
        }
    }

    pub(crate) fn write_item(&mut self, item: &Item) {
        match item {
            Item::Positional { metavar, help: _ } => {
                self.metavar(*metavar);
            }
            Item::Command {
                name: _,
                short: _,
                help: _,
                meta: _,
                info: _,
            } => {
                self.write_str("COMMAND ...", Style::Metavar);
            }
            Item::Flag {
                name,
                shorts: _,
                env: _,
                help: _,
            } => self.write_shortlong(name),
            Item::Argument {
                name,
                shorts: _,
                metavar,
                env: _,
                help: _,
            } => {
                self.write_shortlong(name);
                self.write_char('=', Style::Text);
                self.metavar(*metavar);
            }
            Item::Any {
                metavar,
                anywhere: _,
                help: _,
            } => {
                self.doc(metavar);
            }
        }
    }

    pub(crate) fn write_meta(&mut self, meta: &Meta, for_usage: bool) {
        fn go(meta: &Meta, f: &mut Doc) {
            match meta {
                Meta::And(xs) => {
                    for (ix, x) in xs.iter().enumerate() {
                        if ix != 0 {
                            f.write_str(" ", Style::Text);
                        }
                        go(x, f);
                    }
                }
                Meta::Or(xs) => {
                    for (ix, x) in xs.iter().enumerate() {
                        if ix != 0 {
                            f.write_str(" | ", Style::Text);
                        }
                        go(x, f);
                    }
                }
                Meta::Optional(m) => {
                    f.write_str("[", Style::Text);
                    go(m, f);
                    f.write_str("]", Style::Text);
                }
                Meta::Required(m) => {
                    f.write_str("(", Style::Text);
                    go(m, f);
                    f.write_str(")", Style::Text);
                }
                Meta::Item(i) => f.write_item(i),
                Meta::Many(m) => {
                    go(m, f);
                    f.write_str("...", Style::Text);
                }

                Meta::Adjacent(m) | Meta::Subsection(m, _) | Meta::Suffix(m, _) => {
                    go(m, f);
                }
                Meta::Skip => {} // => f.write_str("no parameters expected", Style::Text),
                Meta::CustomUsage(_, u) => {
                    f.doc(u);
                }
                Meta::Strict(m) => {
                    f.write_str("--", Style::Literal);
                    f.write_str(" ", Style::Text);
                    go(m, f);
                }
            }
        }

        let meta = meta.normalized(for_usage);
        self.token(Token::BlockStart(Block::Mono));
        go(&meta, self);
        self.token(Token::BlockEnd(Block::Mono));
    }
}

/// Style of a text fragment inside of [`Doc`]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum Style {
    /// Plain text, no decorations
    Text,

    /// Word with emphasis - things like "Usage", "Available options", etc
    Emphasis,

    /// Something user needs to type literally - command names, etc
    Literal,

    /// Metavavar placeholder - something user needs to replace with own input
    Metavar,

    /// Invalid input given by user - used to display invalid parts of the input
    Invalid,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[allow(dead_code)]
pub(crate) enum Block {
    /// level 1 section header, block for separate command inside manpage, not used in --help
    Header,

    Section2,

    // 2 margin
    /// level 3 section header, "group_help" header, etc.
    Section3,

    // inline, 4 margin, no nesting
    /// -h, --help
    ItemTerm,

    // widest term up below 20 margin margin plus two, but at least 4.
    /// print usage information, but also items inside Numbered/Unnumbered lists
    ItemBody,

    // 0 margin
    /// Definition list,
    DefinitionList,

    /// block of text, blocks are separated by a blank line in man or help
    /// can contain headers or other items inside
    Block,

    /// inserted when block is written into a block. single blank line in the input
    /// fast forward until the end of current inline block
    InlineBlock,

    // inline
    /// displayed with `` in monochrome or not when rendered with colors.
    /// In markdown this becomes a link to a term if one is defined
    TermRef,

    /// Surrounds metavars block in manpage
    ///
    /// used only inside render_manpage at the moment
    Meta,

    /// Monospaced font that goes around [`Meta`]
    Mono,
}

#[derive(Debug, Copy, Clone)]
pub(crate) enum Token {
    Text { bytes: usize, style: Style },
    BlockStart(Block),
    BlockEnd(Block),
}

#[derive(Debug, Clone, Default)]
/// String with styled segments.
///
/// You can add style information to generated documentation and help messages
/// For simpliest possible results you can also pass a string slice in all the places
/// that require `impl Into<Doc>`
pub struct Doc {
    /// string info saved here
    payload: String,

    /// string meta info tokens
    tokens: Vec<Token>,
}

impl std::fmt::Display for Doc {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let width = f.width().unwrap_or(MAX_WIDTH);
        f.write_str(&self.render_console(true, Color::Monochrome, width))
    }
}

#[derive(Debug, Clone, Copy, Default)]
struct Skip(usize);
impl Skip {
    fn enabled(self) -> bool {
        self.0 > 0
    }
    fn enable(&mut self) {
        self.0 = 1;
    }
    fn push(&mut self) {
        if self.0 > 0 {
            self.0 += 1;
        }
    }
    fn pop(&mut self) {
        self.0 = self.0.saturating_sub(1);
    }
}

impl Doc {
    pub(crate) fn is_empty(&self) -> bool {
        self.tokens.is_empty()
    }

    pub(crate) fn first_line(&self) -> Option<Doc> {
        if self.tokens.is_empty() {
            return None;
        }

        let mut res = Doc::default();
        let mut cur = 0;

        for &t in &self.tokens {
            match t {
                Token::Text { bytes, style } => {
                    let payload = &self.payload[cur..cur + bytes];
                    if let Some((first, _)) = payload.split_once('\n') {
                        res.tokens.push(Token::Text {
                            bytes: first.len(),
                            style,
                        });
                        res.payload.push_str(first);
                    } else {
                        res.tokens.push(t);
                        res.payload.push_str(&self.payload[cur..cur + bytes]);
                        cur += bytes;
                    }
                }
                _ => break,
            }
        }
        Some(res)
    }

    #[cfg(feature = "autocomplete")]
    pub(crate) fn to_completion(&self) -> Option<String> {
        let mut s = self.first_line()?.monochrome(false);
        s.truncate(s.trim_end().len());
        Some(s)
    }
}

impl From<&str> for Doc {
    fn from(value: &str) -> Self {
        let mut buf = Doc::default();
        buf.write_str(value, Style::Text);
        buf
    }
}

impl Doc {
    //    #[cfg(test)]
    //    pub(crate) fn clear(&mut self) {
    //        self.payload.clear();
    //        self.tokens.clear();
    //    }

    #[inline(never)]
    pub(crate) fn token(&mut self, token: Token) {
        self.tokens.push(token);
    }

    pub(crate) fn write<T>(&mut self, input: T, style: Style)
    where
        T: std::fmt::Display,
    {
        use std::fmt::Write;
        let old_len = self.payload.len();
        let _ = write!(self.payload, "{}", input);
        self.set_style(self.payload.len() - old_len, style);
    }

    #[inline(never)]
    fn set_style(&mut self, len: usize, style: Style) {
        // buffer chunks are unified with previous chunks of the same type
        // [metavar]"foo" + [metavar]"bar" => [metavar]"foobar"
        match self.tokens.last_mut() {
            Some(Token::Text {
                bytes: prev_bytes,
                style: prev_style,
            }) if *prev_style == style => {
                *prev_bytes += len;
            }
            _ => {
                self.tokens.push(Token::Text { bytes: len, style });
            }
        }
    }

    #[inline(never)]
    pub(crate) fn write_str(&mut self, input: &str, style: Style) {
        self.payload.push_str(input);
        self.set_style(input.len(), style);
    }

    #[inline(never)]
    pub(crate) fn write_char(&mut self, c: char, style: Style) {
        self.write_str(c.encode_utf8(&mut [0; 4]), style);
    }
}

#[cfg(feature = "docgen")]
#[derive(Debug, Clone)]
struct DocSection<'a> {
    /// Path name to the command name starting from the application
    path: Vec<String>,
    info: &'a Info,
    meta: &'a Meta,
}

#[cfg(feature = "docgen")]
fn extract_sections<'a>(
    meta: &'a Meta,
    info: &'a Info,
    path: &mut Vec<String>,
    sections: &mut Vec<DocSection<'a>>,
) {
    sections.push(DocSection {
        path: path.clone(),
        info,
        meta,
    });
    let mut hi = HelpItems::default();
    hi.append_meta(meta);
    for item in &hi.items {
        if let HelpItem::Command {
            name,
            short: _,
            help: _,
            meta,
            info,
        } = item
        {
            path.push((*name).to_string());
            extract_sections(meta, info, path, sections);
            path.pop();
        }
    }
}