television_screen/
preview.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use crate::{
    cache::RenderedPreviewCache,
    colors::{Colorscheme, PreviewColorscheme},
};
use color_eyre::eyre::Result;
use devicons::FileIcon;
use ratatui::widgets::{Block, BorderType, Borders, Padding, Paragraph, Wrap};
use ratatui::Frame;
use ratatui::{
    layout::{Alignment, Rect},
    prelude::{Color, Line, Modifier, Span, Style, Stylize, Text},
};
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use television_channels::entry::Entry;
use television_previewers::{
    ansi::IntoText,
    previewers::{
        Preview, PreviewContent, FILE_TOO_LARGE_MSG, LOADING_MSG,
        PREVIEW_NOT_SUPPORTED_MSG, TIMEOUT_MSG,
    },
};
use television_utils::strings::{
    replace_non_printable, shrink_with_ellipsis, ReplaceNonPrintableConfig,
    EMPTY_STRING,
};

#[allow(dead_code)]
const FILL_CHAR_SLANTED: char = '╱';
const FILL_CHAR_EMPTY: char = ' ';

#[allow(clippy::needless_pass_by_value)]
pub fn build_preview_paragraph<'a>(
    inner: Rect,
    preview_content: PreviewContent,
    target_line: Option<u16>,
    preview_scroll: u16,
    colorscheme: Colorscheme,
) -> Paragraph<'a> {
    let preview_block =
        Block::default().style(Style::default()).padding(Padding {
            top: 0,
            right: 1,
            bottom: 0,
            left: 1,
        });
    match preview_content {
        PreviewContent::AnsiText(text) => {
            build_ansi_text_paragraph(text, preview_block, preview_scroll)
        }
        PreviewContent::PlainText(content) => build_plain_text_paragraph(
            content,
            preview_block,
            target_line,
            preview_scroll,
            colorscheme.preview,
        ),
        PreviewContent::PlainTextWrapped(content) => {
            build_plain_text_wrapped_paragraph(
                content,
                preview_block,
                colorscheme.preview,
            )
        }
        PreviewContent::SyntectHighlightedText(highlighted_lines) => {
            build_syntect_highlighted_paragraph(
                highlighted_lines.lines,
                preview_block,
                target_line,
                preview_scroll,
                colorscheme.preview,
            )
        }
        // meta
        PreviewContent::Loading => {
            build_meta_preview_paragraph(inner, LOADING_MSG, FILL_CHAR_EMPTY)
                .block(preview_block)
                .alignment(Alignment::Left)
                .style(Style::default().add_modifier(Modifier::ITALIC))
        }
        PreviewContent::NotSupported => build_meta_preview_paragraph(
            inner,
            PREVIEW_NOT_SUPPORTED_MSG,
            FILL_CHAR_EMPTY,
        )
        .block(preview_block)
        .alignment(Alignment::Left)
        .style(Style::default().add_modifier(Modifier::ITALIC)),
        PreviewContent::FileTooLarge => build_meta_preview_paragraph(
            inner,
            FILE_TOO_LARGE_MSG,
            FILL_CHAR_EMPTY,
        )
        .block(preview_block)
        .alignment(Alignment::Left)
        .style(Style::default().add_modifier(Modifier::ITALIC)),
        PreviewContent::Timeout => {
            build_meta_preview_paragraph(inner, TIMEOUT_MSG, FILL_CHAR_EMPTY)
        }
        .block(preview_block)
        .alignment(Alignment::Left)
        .style(Style::default().add_modifier(Modifier::ITALIC)),
        PreviewContent::Empty => Paragraph::new(Text::raw(EMPTY_STRING)),
    }
}

const ANSI_BEFORE_CONTEXT_SIZE: u16 = 10;
const ANSI_CONTEXT_SIZE: usize = 150;

#[allow(clippy::needless_pass_by_value)]
fn build_ansi_text_paragraph(
    text: String,
    preview_block: Block,
    preview_scroll: u16,
) -> Paragraph {
    let lines = text.lines();
    let skip =
        preview_scroll.saturating_sub(ANSI_BEFORE_CONTEXT_SIZE) as usize;
    let context = lines
        .skip(skip)
        .take(ANSI_CONTEXT_SIZE)
        .collect::<Vec<_>>()
        .join("\n");

    let mut text = "\n".repeat(skip);
    text.push_str(
        &replace_non_printable(
            context.as_bytes(),
            &ReplaceNonPrintableConfig {
                replace_line_feed: false,
                replace_control_characters: false,
                ..Default::default()
            },
        )
        .0,
    );

    Paragraph::new(text.into_text().unwrap())
        .block(preview_block)
        .scroll((preview_scroll, 0))
}

#[allow(clippy::needless_pass_by_value)]
fn build_plain_text_paragraph(
    text: Vec<String>,
    preview_block: Block<'_>,
    target_line: Option<u16>,
    preview_scroll: u16,
    colorscheme: PreviewColorscheme,
) -> Paragraph<'_> {
    let mut lines = Vec::new();
    for (i, line) in text.iter().enumerate() {
        lines.push(Line::from(vec![
            build_line_number_span(i + 1).style(Style::default().fg(
                if matches!(
                        target_line,
                        Some(l) if l == u16::try_from(i).unwrap_or(0) + 1
                    )
                {
                    colorscheme.gutter_selected_fg
                } else {
                    colorscheme.gutter_fg
                },
            )),
            Span::styled(" │ ",
                         Style::default().fg(colorscheme.gutter_fg).dim()),
            Span::styled(
                line.to_string(),
                Style::default().fg(colorscheme.content_fg).bg(
                    if matches!(target_line, Some(l) if l == u16::try_from(i).unwrap() + 1) {
                        colorscheme.highlight_bg
                    } else {
                        Color::Reset
                    },
                ),
            ),
        ]));
    }
    let text = Text::from(lines);
    Paragraph::new(text)
        .block(preview_block)
        .scroll((preview_scroll, 0))
}

#[allow(clippy::needless_pass_by_value)]
fn build_plain_text_wrapped_paragraph(
    text: String,
    preview_block: Block<'_>,
    colorscheme: PreviewColorscheme,
) -> Paragraph<'_> {
    let mut lines = Vec::new();
    for line in text.lines() {
        lines.push(Line::styled(
            line.to_string(),
            Style::default().fg(colorscheme.content_fg),
        ));
    }
    let text = Text::from(lines);
    Paragraph::new(text)
        .block(preview_block)
        .wrap(Wrap { trim: true })
}

#[allow(clippy::needless_pass_by_value)]
fn build_syntect_highlighted_paragraph(
    highlighted_lines: Vec<Vec<(syntect::highlighting::Style, String)>>,
    preview_block: Block,
    target_line: Option<u16>,
    preview_scroll: u16,
    colorscheme: PreviewColorscheme,
) -> Paragraph {
    compute_paragraph_from_highlighted_lines(
        &highlighted_lines,
        target_line.map(|l| l as usize),
        colorscheme,
    )
    .block(preview_block)
    .alignment(Alignment::Left)
    .scroll((preview_scroll, 0))
}

pub fn build_meta_preview_paragraph<'a>(
    inner: Rect,
    message: &str,
    fill_char: char,
) -> Paragraph<'a> {
    let message_len = message.len();
    if message_len + 8 > inner.width as usize {
        return Paragraph::new(Text::from(EMPTY_STRING));
    }
    let fill_char_str = fill_char.to_string();
    let fill_line = fill_char_str.repeat(inner.width as usize);

    // Build the paragraph content with slanted lines and center the custom message
    let mut lines = Vec::new();

    // Calculate the vertical center
    let vertical_center = inner.height as usize / 2;
    let horizontal_padding = (inner.width as usize - message_len) / 2 - 4;

    // Fill the paragraph with slanted lines and insert the centered custom message
    for i in 0..inner.height {
        if i as usize == vertical_center {
            // Center the message horizontally in the middle line
            let line = format!(
                "{}  {}  {}",
                fill_char_str.repeat(horizontal_padding),
                message,
                fill_char_str.repeat(
                    inner.width as usize - horizontal_padding - message_len
                )
            );
            lines.push(Line::from(line));
        } else if i as usize + 1 == vertical_center
            || (i as usize).saturating_sub(1) == vertical_center
        {
            let line = format!(
                "{}  {}  {}",
                fill_char_str.repeat(horizontal_padding),
                " ".repeat(message_len),
                fill_char_str.repeat(
                    inner.width as usize - horizontal_padding - message_len
                )
            );
            lines.push(Line::from(line));
        } else {
            lines.push(Line::from(fill_line.clone()));
        }
    }

    // Create a paragraph with the generated content
    Paragraph::new(Text::from(lines))
}

fn draw_content_outer_block(
    f: &mut Frame,
    rect: Rect,
    colorscheme: &Colorscheme,
    icon: Option<FileIcon>,
    title: &str,
    use_nerd_font_icons: bool,
) -> Result<Rect> {
    let mut preview_title_spans = vec![Span::from(" ")];
    // optional icon
    if icon.is_some() && use_nerd_font_icons {
        let icon = icon.as_ref().unwrap();
        preview_title_spans.push(Span::styled(
            {
                let mut icon_str = String::from(icon.icon);
                icon_str.push(' ');
                icon_str
            },
            Style::default().fg(Color::from_str(icon.color)?),
        ));
    }
    // preview title
    preview_title_spans.push(Span::styled(
        shrink_with_ellipsis(
            &replace_non_printable(
                title.as_bytes(),
                &ReplaceNonPrintableConfig::default(),
            )
            .0,
            rect.width.saturating_sub(4) as usize,
        ),
        Style::default().fg(colorscheme.preview.title_fg).bold(),
    ));
    preview_title_spans.push(Span::from(" "));

    // build the preview block
    let preview_outer_block = Block::default()
        .title_top(
            Line::from(preview_title_spans)
                .alignment(Alignment::Center)
                .style(Style::default().fg(colorscheme.preview.title_fg)),
        )
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(Style::default().fg(colorscheme.general.border_fg))
        .style(
            Style::default()
                .bg(colorscheme.general.background.unwrap_or_default()),
        )
        .padding(Padding::new(0, 1, 1, 0));

    let inner = preview_outer_block.inner(rect);
    f.render_widget(preview_outer_block, rect);
    Ok(inner)
}

#[allow(clippy::too_many_arguments)]
pub fn draw_preview_content_block(
    f: &mut Frame,
    rect: Rect,
    entry: &Entry,
    preview: &Option<Arc<Preview>>,
    rendered_preview_cache: &Arc<Mutex<RenderedPreviewCache<'static>>>,
    preview_scroll: u16,
    use_nerd_font_icons: bool,
    colorscheme: &Colorscheme,
) -> Result<()> {
    if let Some(preview) = preview {
        let inner = draw_content_outer_block(
            f,
            rect,
            colorscheme,
            preview.icon,
            &preview.title,
            use_nerd_font_icons,
        )?;

        // check if the rendered preview content is already in the cache
        let cache_key = compute_cache_key(entry);
        if let Some(rp) =
            rendered_preview_cache.lock().unwrap().get(&cache_key)
        {
            // we got a hit, render the cached preview content
            let p = rp.paragraph.as_ref().clone();
            f.render_widget(p.scroll((preview_scroll, 0)), inner);
            return Ok(());
        }
        // render the preview content and cache it
        let rp = build_preview_paragraph(
            //preview_inner_block,
            inner,
            preview.content.clone(),
            entry.line_number.map(|l| u16::try_from(l).unwrap_or(0)),
            preview_scroll,
            colorscheme.clone(),
        );
        // only cache the preview content if it's not a partial preview
        // and the preview title matches the entry name
        if preview.partial_offset.is_none() && preview.title == entry.name {
            rendered_preview_cache.lock().unwrap().insert(
                cache_key,
                preview.icon,
                &preview.title,
                &Arc::new(rp.clone()),
            );
        }
        f.render_widget(rp.scroll((preview_scroll, 0)), inner);
        return Ok(());
    }
    // else if last_preview exists
    if let Some(last_preview) =
        &rendered_preview_cache.lock().unwrap().last_preview
    {
        let inner = draw_content_outer_block(
            f,
            rect,
            colorscheme,
            last_preview.icon,
            &last_preview.title,
            use_nerd_font_icons,
        )?;

        f.render_widget(
            last_preview
                .paragraph
                .as_ref()
                .clone()
                .scroll((preview_scroll, 0)),
            inner,
        );
        return Ok(());
    }
    // otherwise render empty preview
    let inner = draw_content_outer_block(
        f,
        rect,
        colorscheme,
        None,
        "",
        use_nerd_font_icons,
    )?;
    let preview_outer_block = Block::default()
        .title_top(Line::from(Span::styled(
            " Preview ",
            Style::default().fg(colorscheme.preview.title_fg),
        )))
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(Style::default().fg(colorscheme.general.border_fg))
        .style(
            Style::default()
                .bg(colorscheme.general.background.unwrap_or_default()),
        )
        .padding(Padding::new(0, 1, 1, 0));
    f.render_widget(preview_outer_block, inner);

    Ok(())
}

fn build_line_number_span<'a>(line_number: usize) -> Span<'a> {
    Span::from(format!("{line_number:5} "))
}

fn compute_paragraph_from_highlighted_lines(
    highlighted_lines: &[Vec<(syntect::highlighting::Style, String)>],
    line_specifier: Option<usize>,
    colorscheme: PreviewColorscheme,
) -> Paragraph<'static> {
    let preview_lines: Vec<Line> = highlighted_lines
        .iter()
        .enumerate()
        .map(|(i, l)| {
            let line_number =
                build_line_number_span(i + 1).style(Style::default().fg(
                    if line_specifier.is_some()
                        && i == line_specifier.unwrap().saturating_sub(1)
                    {
                        colorscheme.gutter_selected_fg
                    } else {
                        colorscheme.gutter_fg
                    },
                ));
            Line::from_iter(
                std::iter::once(line_number)
                    .chain(std::iter::once(Span::styled(
                        " │ ",
                        Style::default().fg(colorscheme.gutter_fg).dim(),
                    )))
                    .chain(l.iter().cloned().map(|sr| {
                        convert_syn_region_to_span(
                            &(sr.0, sr.1),
                            if line_specifier.is_some()
                                && i == line_specifier
                                    .unwrap()
                                    .saturating_sub(1)
                            {
                                Some(colorscheme.highlight_bg)
                            } else {
                                None
                            },
                        )
                    })),
            )
        })
        .collect();

    Paragraph::new(preview_lines)
}

pub fn convert_syn_region_to_span<'a>(
    syn_region: &(syntect::highlighting::Style, String),
    background: Option<Color>,
) -> Span<'a> {
    let mut style = Style::default()
        .fg(convert_syn_color_to_ratatui_color(syn_region.0.foreground));
    if let Some(background) = background {
        style = style.bg(background);
    }
    style = match syn_region.0.font_style {
        syntect::highlighting::FontStyle::BOLD => style.bold(),
        syntect::highlighting::FontStyle::ITALIC => style.italic(),
        syntect::highlighting::FontStyle::UNDERLINE => style.underlined(),
        _ => style,
    };
    Span::styled(syn_region.1.clone(), style)
}

fn convert_syn_color_to_ratatui_color(
    color: syntect::highlighting::Color,
) -> Color {
    Color::Rgb(color.r, color.g, color.b)
}

fn compute_cache_key(entry: &Entry) -> String {
    let mut cache_key = entry.name.clone();
    if let Some(line_number) = entry.line_number {
        cache_key.push_str(&line_number.to_string());
    }
    cache_key
}