rusty-man 0.4.1

Command-line viewer for rustdoc 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
// SPDX-FileCopyrightText: 2020 Robin Krahl <robin.krahl@ireas.org>
// SPDX-License-Identifier: MIT

mod views;

use std::convert;

use anyhow::Context as _;
use cursive::view::{Resizable as _, Scrollable as _};
use cursive::views::{Dialog, LinearLayout, PaddedView, Panel, TextView};
use cursive::{event, theme, utils::markup};
use cursive_markup::MarkupView;

use crate::args;
use crate::doc;
use crate::source;
use crate::viewer::{self, utils, utils::ManRenderer as _};

use views::{CodeView, HtmlRenderer, LinkView};

#[derive(Clone, Debug)]
pub struct TuiViewer {}

impl TuiViewer {
    pub fn new() -> TuiViewer {
        TuiViewer {}
    }

    fn render<F>(
        &self,
        sources: Vec<Box<dyn source::Source>>,
        args: args::ViewerArgs,
        doc: &doc::Doc,
        f: F,
    ) -> anyhow::Result<()>
    where
        F: Fn(&mut TuiManRenderer) -> Result<(), convert::Infallible>,
    {
        let mut s = create_cursive(sources, args)?;
        let mut renderer = context(&mut s).create_renderer(&doc);
        f(&mut renderer)?;
        let view = renderer.into_view();
        s.add_fullscreen_layer(view);
        s.run();
        Ok(())
    }
}

impl viewer::Viewer for TuiViewer {
    fn open(
        &self,
        sources: Vec<Box<dyn source::Source>>,
        args: args::ViewerArgs,
        doc: &doc::Doc,
    ) -> anyhow::Result<()> {
        self.render(sources, args, doc, |renderer| renderer.render_doc(doc))
    }

    fn open_examples(
        &self,
        sources: Vec<Box<dyn source::Source>>,
        args: args::ViewerArgs,
        doc: &doc::Doc,
        examples: Vec<doc::Example>,
    ) -> anyhow::Result<()> {
        self.render(sources, args, doc, |renderer| {
            renderer.render_examples(doc, &examples)
        })
    }
}

pub struct Context {
    pub sources: Vec<Box<dyn source::Source>>,
    pub args: args::ViewerArgs,
    pub highlighter: Option<utils::Highlighter>,
}

impl Context {
    pub fn new(
        sources: Vec<Box<dyn source::Source>>,
        args: args::ViewerArgs,
    ) -> anyhow::Result<Context> {
        let highlighter = utils::get_highlighter(&args)?;
        Ok(Context {
            sources,
            args,
            highlighter,
        })
    }

    pub fn create_renderer(&self, doc: &doc::Doc) -> TuiManRenderer {
        TuiManRenderer::new(
            doc,
            self.args.max_width.unwrap_or(100),
            self.highlighter.as_ref(),
        )
    }
}

pub struct TuiManRenderer<'s> {
    doc_name: doc::Fqn,
    doc_ty: doc::ItemType,
    layout: LinearLayout,
    max_width: usize,
    highlighter: Option<&'s utils::Highlighter>,
}

impl<'s> TuiManRenderer<'s> {
    pub fn new(
        doc: &doc::Doc,
        max_width: usize,
        highlighter: Option<&'s utils::Highlighter>,
    ) -> TuiManRenderer<'s> {
        TuiManRenderer {
            doc_name: doc.name.clone(),
            doc_ty: doc.ty,
            layout: LinearLayout::vertical(),
            max_width,
            highlighter,
        }
    }

    fn into_view(self) -> impl cursive::View {
        let title = format!("{} {}", self.doc_ty.name(), self.doc_name);
        let scroll = self.layout.scrollable().full_screen();
        Panel::new(scroll).title(title)
    }
}

impl<'s> utils::ManRenderer for TuiManRenderer<'s> {
    type Error = convert::Infallible;

    fn print_title(&mut self, _left: &str, _center: &str, _right: &str) -> Result<(), Self::Error> {
        Ok(())
    }

    fn print_heading(
        &mut self,
        indent: u8,
        text: &str,
        link: Option<utils::DocLink>,
    ) -> Result<(), Self::Error> {
        let text = markup::StyledString::styled(text, theme::Effect::Bold);
        if let Some(link) = link {
            // TODO: bold
            let heading = LinkView::new(text, move |s| {
                if let Err(err) = open_link(s, link.clone().into()) {
                    report_error(s, err);
                }
            });
            self.layout.add_child(indent_view(indent, heading));
        } else {
            let heading = TextView::new(text);
            self.layout.add_child(indent_view(indent, heading));
        }
        Ok(())
    }

    fn print_code(&mut self, indent: u8, code: &doc::Code) -> Result<(), Self::Error> {
        if let Some(highlighter) = self.highlighter {
            let code = CodeView::new(&code.to_string(), highlighter);
            self.layout.add_child(indent_view(indent, code));
        } else {
            let text = TextView::new(code.to_string());
            self.layout.add_child(indent_view(indent, text));
        }
        Ok(())
    }

    fn print_text(&mut self, indent: u8, text: &doc::Text) -> Result<(), Self::Error> {
        let indent = usize::from(indent);
        let renderer = HtmlRenderer::new(&text.html, self.highlighter.cloned());
        let mut view = MarkupView::with_renderer(renderer);
        view.set_maximum_width(self.max_width.saturating_sub(indent));
        let doc_name = self.doc_name.clone();
        let doc_ty = self.doc_ty;
        view.on_link_select(move |s, link| handle_link(s, &doc_name, doc_ty, link));
        self.layout.add_child(indent_view(indent, view));
        Ok(())
    }

    fn println(&mut self) -> Result<(), Self::Error> {
        self.layout.add_child(TextView::new(" "));
        Ok(())
    }
}

fn indent_view<V>(indent: impl Into<usize>, view: V) -> PaddedView<V> {
    PaddedView::lrtb(indent.into(), 0, 0, 0, view)
}

fn create_backend() -> anyhow::Result<Box<dyn cursive::backend::Backend>> {
    let termion =
        cursive::backends::termion::Backend::init().context("Could not create termion backend")?;
    let buffered = cursive_buffered_backend::BufferedBackend::new(termion);
    Ok(Box::new(buffered))
}

fn create_cursive(
    sources: Vec<Box<dyn source::Source>>,
    args: args::ViewerArgs,
) -> anyhow::Result<cursive::Cursive> {
    use cursive::event::{Event, Key};

    let mut cursive =
        cursive::Cursive::try_new(create_backend).context("Could not create Cursive instance")?;

    cursive.set_user_data(Context::new(sources, args)?);

    // vim-like keybindings
    cursive.add_global_callback('j', |s| s.on_event(Key::Down.into()));
    cursive.add_global_callback('k', |s| s.on_event(Key::Up.into()));
    cursive.add_global_callback('h', |s| s.on_event(Key::Left.into()));
    cursive.add_global_callback('l', |s| s.on_event(Key::Right.into()));
    cursive.add_global_callback('G', |s| s.on_event(Key::End.into()));
    cursive.add_global_callback('g', |s| s.on_event(Key::Home.into()));
    cursive.add_global_callback(Event::CtrlChar('f'), |s| s.on_event(Key::PageDown.into()));
    cursive.add_global_callback(Event::CtrlChar('b'), |s| s.on_event(Key::PageUp.into()));

    cursive.add_global_callback('q', |s| s.quit());
    cursive.add_global_callback(event::Key::Backspace, |s| {
        let screen = s.screen_mut();
        if screen.len() > 1 {
            screen.pop_layer();
        }
    });

    let mut theme = theme::Theme::default();
    theme.shadow = false;
    theme.palette[theme::PaletteColor::Background] = theme::Color::TerminalDefault;
    theme.palette[theme::PaletteColor::View] = theme::Color::TerminalDefault;
    theme.palette[theme::PaletteColor::Primary] = theme::Color::TerminalDefault;
    cursive.set_theme(theme);

    Ok(cursive)
}

fn context(s: &mut cursive::Cursive) -> &mut Context {
    s.user_data()
        .expect("Missing context in cursive application")
}

fn report_error(s: &mut cursive::Cursive, error: anyhow::Error) {
    let context: Vec<_> = error
        .chain()
        .skip(1)
        .map(|e| format!("    {}", e.to_string()))
        .collect();

    let mut msg = error.to_string();
    if !context.is_empty() {
        msg.push_str("\n\nContext:\n");
        msg.push_str(&context.join("\n"));
    }

    let dialog = Dialog::info(msg).title("Error");
    s.add_layer(dialog);
}

fn handle_link(s: &mut cursive::Cursive, doc_name: &doc::Fqn, doc_ty: doc::ItemType, link: &str) {
    let result = resolve_link(doc_name, doc_ty, link).and_then(|link| open_link(s, link));
    if let Err(err) = result {
        report_error(s, err);
    }
}

fn find_doc(
    sources: &[Box<dyn source::Source>],
    ty: Option<doc::ItemType>,
    name: &doc::Fqn,
) -> anyhow::Result<doc::Doc> {
    for source in sources {
        if let Some(doc) = source.find_doc(name, ty)? {
            return Ok(doc);
        }
    }
    Err(anyhow::anyhow!(
        "Could not find documentation for item: {}",
        name
    ))
}

fn open_link(s: &mut cursive::Cursive, link: ResolvedLink) -> anyhow::Result<()> {
    match link {
        ResolvedLink::Doc(ty, name) => {
            let doc = find_doc(&context(s).sources, ty, &name)?;
            let mut renderer = context(s).create_renderer(&doc);
            renderer.render_doc(&doc).unwrap();
            let view = renderer.into_view();
            s.add_fullscreen_layer(view);
            Ok(())
        }
        ResolvedLink::External(link) => webbrowser::open(&link)
            .map(|_| {})
            .context("Failed to open web browser"),
    }
}

enum ResolvedLink {
    Doc(Option<doc::ItemType>, doc::Fqn),
    External(String),
}

impl From<utils::DocLink> for ResolvedLink {
    fn from(link: utils::DocLink) -> ResolvedLink {
        ResolvedLink::Doc(link.ty, link.name)
    }
}

fn resolve_link(
    doc_name: &doc::Fqn,
    doc_ty: doc::ItemType,
    link: &str,
) -> anyhow::Result<ResolvedLink> {
    // TODO: support docs.rs and doc.rust-lang.org links
    match url::Url::parse(link) {
        Ok(_) => Ok(ResolvedLink::External(link.to_owned())),
        Err(url::ParseError::RelativeUrlWithoutBase) => resolve_doc_link(doc_name, doc_ty, link)
            .with_context(|| format!("Could not parse relative link URL: {}", link)),
        Err(e) => Err(anyhow::Error::new(e).context(format!("Could not parse link URL: {}", link))),
    }
}

fn resolve_doc_link(
    doc_name: &doc::Fqn,
    doc_ty: doc::ItemType,
    link: &str,
) -> anyhow::Result<ResolvedLink> {
    // TODO: use a proper URL parser instead of manually parsing the URL
    let (link, fragment) = {
        let parts: Vec<_> = link.splitn(2, '#').collect();
        if parts.len() > 1 {
            (parts[0], Some(parts[1]))
        } else {
            (parts[0], None)
        }
    };
    let parts: Vec<_> = link
        .split('/')
        .filter(|s| !s.is_empty())
        .filter(|s| *s != ".")
        .collect();

    let (mut ty, mut name) = if doc_ty != doc::ItemType::Module && !parts.is_empty() {
        (None, doc_name.parent())
    } else {
        (Some(doc_ty), Some(doc_name.to_owned()))
    };

    for part in parts {
        // We support "..", "index.html", "<module>" and "<type>.<name>.html".
        match part {
            ".." => {
                ty = None;
                name = name.context("Exceeded root level")?.parent();
            }
            "index.html" => {}
            _ => {
                if let Some((part_ty, part_name)) = parse_url_part(part, Some(".html")) {
                    // part == "type.name.html"
                    ty = Some(part_ty.parse()?);
                    name = if let Some(name) = name {
                        Some(name.child(&part_name))
                    } else {
                        Some(part_name.to_owned().into())
                    };
                } else {
                    // part == "<module>"
                    ty = Some(doc::ItemType::Module);
                    name = if let Some(name) = name {
                        Some(name.child(&part))
                    } else {
                        Some(part.to_owned().into())
                    };
                }
            }
        }
    }

    if let Some(fragment) = fragment {
        // If the fragment is "<type>.:name>", we add it to the name, otherwise we ignore it
        // because it just points to some other element on the page.
        if let Some((fragment_ty, fragment_name)) = parse_url_part(fragment, None) {
            ty = Some(fragment_ty.parse()?);
            name = if let Some(name) = name {
                Some(name.child(&fragment_name))
            } else {
                Some(fragment_name.to_owned().into())
            };
        }
    }

    Ok(ResolvedLink::Doc(
        ty,
        name.context("Cannot handle link to root")?,
    ))
}

fn parse_url_part<'s>(s: &'s str, suffix: Option<&str>) -> Option<(&'s str, &'s str)> {
    let s = if let Some(suffix) = suffix {
        if s.ends_with(suffix) {
            &s[..s.len() - suffix.len()]
        } else {
            return None;
        }
    } else {
        s
    };
    let parts: Vec<_> = s.split('.').collect();
    if parts.len() == 2 {
        Some((parts[0], parts[1]))
    } else {
        None
    }
}