changxi 0.3.0

TUI EPUB Reader
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
use crate::app::{App, ViewMode};
use crate::core::{Chapter, ContentElement, Reader};
use crate::ui::{Component, count_lines, get_border_type};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::{
    Frame,
    layout::{Margin, Rect},
    widgets::{Block, Borders, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState, Wrap},
};
use ratatui_image::sliced::{SignedPosition, SlicedImage};
use ratatui_image::{StatefulImage, picker::Picker};

pub struct ReadingView;

impl Component for ReadingView {
    fn render(&self, f: &mut Frame, area: Rect, app: &mut App, picker: &mut Picker) {
        if let Ok(chapter) = app.reader.get_chapter(app.current_chapter) {
            let width = area.width.saturating_sub(2) as usize;
            let viewport_height = area.height.saturating_sub(2) as usize;

            app.update_layout(width);

            // Elements heights calculation
            let mut heights = Vec::new();
            for element in &chapter.elements {
                heights.push(count_lines(element, width));
            }
            let total_height: usize = heights.iter().sum();
            app.total_height = total_height;
            app.viewport_height = viewport_height;

            // Handle go_to_end flag
            if app.scroll_to_end && total_height > viewport_height {
                app.scroll = total_height.saturating_sub(viewport_height);
                app.scroll_to_end = false;
            }

            // Handle go_to_start flag
            if app.scroll_to_start {
                app.scroll = 0;
                app.scroll_to_start = false;
            }

            if app.scroll + viewport_height > total_height && total_height > viewport_height {
                app.scroll = total_height.saturating_sub(viewport_height);
            }

            let chapter_title = format!(" {} ", chapter.title);

            render_chapter_content(
                f,
                area,
                app,
                picker,
                &chapter,
                app.scroll,
                &heights,
                viewport_height,
                Some(chapter_title),
                true,
            );

            let max_scroll = total_height.saturating_sub(viewport_height);
            let scroll_position = app.scroll.min(max_scroll);

            // Render Scrollbar
            let mut scrollbar_state = ScrollbarState::new(total_height)
                .position(scroll_position)
                .viewport_content_length(viewport_height);
            f.render_stateful_widget(
                Scrollbar::new(ScrollbarOrientation::VerticalRight)
                    .begin_symbol(Some(""))
                    .end_symbol(Some("")),
                area.inner(Margin {
                    vertical: 1,
                    horizontal: 0,
                }),
                &mut scrollbar_state,
            );
        }
    }
}

pub struct ContinuousReadingView;

impl Component for ContinuousReadingView {
    fn render(&self, f: &mut Frame, area: Rect, app: &mut App, picker: &mut Picker) {
        let width = area.width.saturating_sub(2) as usize;
        let viewport_height = area.height.saturating_sub(2) as usize;

        app.viewport_height = viewport_height;
        app.update_layout(width);

        // Handle go_to_end flag
        if app.scroll_to_end {
            let total_height = app.chapter_heights[app.current_chapter];
            if total_height > viewport_height {
                app.scroll = total_height.saturating_sub(viewport_height);
            } else {
                app.scroll = 0;
            }
            app.scroll_to_end = false;
        }

        // Handle go_to_start flag
        if app.scroll_to_start {
            app.scroll = 0;
            app.scroll_to_start = false;
        }

        let mut current_y = 0;
        let mut chapter_index = app.current_chapter;

        while current_y < viewport_height && chapter_index < app.reader.chapter_count() {
            if let Ok(chapter) = app.reader.get_chapter(chapter_index) {
                let mut heights = Vec::new();
                for element in &chapter.elements {
                    heights.push(count_lines(element, width));
                }
                let total_chapter_height: usize = heights.iter().sum();

                let render_scroll = if chapter_index == app.current_chapter {
                    app.scroll
                } else {
                    0
                };

                let remaining_viewport = viewport_height.saturating_sub(current_y);
                let content_height = total_chapter_height.saturating_sub(render_scroll);
                let visible_content_height = content_height.min(remaining_viewport);

                if visible_content_height > 0
                    || (chapter_index == app.current_chapter && visible_content_height == 0)
                {
                    let chapter_area = Rect {
                        x: area.x + 1,
                        y: area.y + 1 + current_y as u16,
                        width: area.width.saturating_sub(2),
                        height: visible_content_height as u16,
                    };

                    render_chapter_content(
                        f,
                        chapter_area,
                        app,
                        picker,
                        &chapter,
                        render_scroll,
                        &heights,
                        visible_content_height,
                        None,
                        false,
                    );

                    current_y += visible_content_height;
                }
                chapter_index += 1;
            } else {
                break;
            }
        }

        let border_type = get_border_type(&app.config);

        // Global border for the whole view
        let chapter_title = app
            .chapter_titles
            .get(app.current_chapter)
            .cloned()
            .unwrap_or_default();
        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(border_type)
            .title(format!(" {} ", chapter_title));
        f.render_widget(block, area);

        // Render Global Scrollbar
        let total_book_height = app.get_total_book_height();
        let global_scroll = app.get_global_scroll();
        let max_scroll = total_book_height.saturating_sub(viewport_height);
        let scroll_position = global_scroll.min(max_scroll);

        let mut scrollbar_state = ScrollbarState::new(total_book_height)
            .position(scroll_position)
            .viewport_content_length(viewport_height);
        f.render_stateful_widget(
            Scrollbar::new(ScrollbarOrientation::VerticalRight)
                .begin_symbol(Some(""))
                .end_symbol(Some("")),
            area.inner(Margin {
                vertical: 1,
                horizontal: 0,
            }),
            &mut scrollbar_state,
        );
    }
}

pub fn render_chapter_content(
    f: &mut Frame,
    area: Rect,
    app: &mut App,
    picker: &mut Picker,
    chapter: &Chapter,
    scroll: usize,
    heights: &[usize],
    viewport_height: usize,
    title: Option<String>,
    show_borders: bool,
) {
    // Render elements
    let mut current_y_offset = 0;
    let content_rect = if show_borders {
        area.inner(Margin {
            vertical: 1,
            horizontal: 1,
        })
    } else {
        area
    };
    let border_type = get_border_type(&app.config);

    for (i, element) in chapter.elements.iter().enumerate() {
        let h = heights[i];

        // Check if element is at least partially visible
        if current_y_offset + h > scroll && current_y_offset < scroll + viewport_height {
            let rel_y = current_y_offset as i32 - scroll as i32;

            match element {
                ContentElement::Text(spans) => {
                    let start_line = if rel_y < 0 { (-rel_y) as u16 } else { 0 };
                    let draw_y = if rel_y < 0 { 0 } else { rel_y as u16 };
                    let draw_h = (h as u16)
                        .saturating_sub(start_line)
                        .min((viewport_height as u16).saturating_sub(draw_y));

                    if draw_h > 0 {
                        let area = Rect {
                            x: content_rect.x,
                            y: content_rect.y + draw_y,
                            width: content_rect.width,
                            height: draw_h,
                        };

                        let mut ratatui_spans = Vec::new();
                        let search_query = if !app.search_query.is_empty() {
                            Some(if app.search_case_sensitive {
                                app.search_query.clone()
                            } else {
                                app.search_query.to_lowercase()
                            })
                        } else {
                            None
                        };

                        let mut cumulative_char_idx = 0;
                        for s in spans {
                            let mut style = Style::default();
                            if s.style.bold {
                                style = style.add_modifier(Modifier::BOLD);
                            }
                            if s.style.italic {
                                style = style.add_modifier(Modifier::ITALIC);
                            }
                            if s.style.underline {
                                style = style.add_modifier(Modifier::UNDERLINED);
                            }
                            if s.style.strikethrough {
                                style = style.add_modifier(Modifier::CROSSED_OUT);
                            }

                            // Calculate if this span is within visual selection
                            let mut char_spans = Vec::new();
                            let text_chars: Vec<char> = s.text.chars().collect();

                            for (c_idx, &c) in text_chars.iter().enumerate() {
                                let mut char_style = style;
                                let global_c_idx = cumulative_char_idx + c_idx;

                                if app.mode == ViewMode::Visual {
                                    let (v_ei, v_ci) = app.visual_cursor;
                                    if i == v_ei && global_c_idx == v_ci {
                                        char_style = char_style.bg(Color::White).fg(Color::Black);
                                    } else if let Some(anchor) = app.visual_anchor {
                                        let (start, end) = if anchor < (v_ei, v_ci) {
                                            (anchor, (v_ei, v_ci))
                                        } else {
                                            ((v_ei, v_ci), anchor)
                                        };
                                        if (i > start.0
                                            || (i == start.0 && global_c_idx >= start.1))
                                            && (i < end.0 || (i == end.0 && global_c_idx < end.1))
                                        {
                                            char_style =
                                                char_style.bg(Color::Blue).fg(Color::White);
                                        }
                                    }
                                }
                                char_spans.push(Span::styled(c.to_string(), char_style));
                            }

                            if let Some(ref query) = search_query {
                                if app.mode != ViewMode::Visual {
                                    let text_to_search = if app.search_case_sensitive {
                                        s.text.clone()
                                    } else {
                                        s.text.to_lowercase()
                                    };

                                    let mut last_pos = 0;
                                    for (pos, _) in text_to_search.match_indices(query) {
                                        if pos > last_pos {
                                            ratatui_spans
                                                .push(Span::styled(&s.text[last_pos..pos], style));
                                        }
                                        let match_end = pos + query.len();
                                        ratatui_spans.push(Span::styled(
                                            &s.text[pos..match_end],
                                            style.bg(Color::Yellow).fg(Color::Black),
                                        ));
                                        last_pos = match_end;
                                    }
                                    if last_pos < s.text.len() {
                                        ratatui_spans
                                            .push(Span::styled(&s.text[last_pos..], style));
                                    }
                                } else {
                                    ratatui_spans.extend(char_spans);
                                }
                            } else {
                                if app.mode == ViewMode::Visual {
                                    ratatui_spans.extend(char_spans);
                                } else {
                                    ratatui_spans.push(Span::styled(&s.text, style));
                                }
                            }
                            cumulative_char_idx += text_chars.len();
                        }

                        let p = Paragraph::new(Line::from(ratatui_spans))
                            .wrap(Wrap { trim: true })
                            .scroll((start_line, 0));
                        f.render_widget(p, area);
                    }
                }
                ContentElement::Image(path) => {
                    if app.config.image_type == "resize" {
                        let draw_y = if rel_y < 0 { 0 } else { rel_y as u16 };
                        let visible_h = if rel_y < 0 {
                            (h as i32 + rel_y).max(0) as u16
                        } else {
                            (h as u16).min((viewport_height as u16).saturating_sub(draw_y))
                        };

                        if visible_h > 0
                            && let Some(protocol) = app.get_image_protocol(path, picker)
                        {
                            let area = Rect {
                                x: content_rect.x,
                                y: content_rect.y + draw_y,
                                width: content_rect.width,
                                height: visible_h,
                            };
                            f.render_stateful_widget(StatefulImage::default(), area, protocol);
                        }
                    } else if app.config.image_type == "sliced" {
                        // Calculate visible portion
                        let element_top = current_y_offset;
                        let element_bottom = current_y_offset + h;
                        let viewport_top = scroll;
                        let viewport_bottom = scroll + viewport_height;

                        if element_bottom > viewport_top && element_top < viewport_bottom {
                            // Calculate visible vertical range in viewport coordinates
                            let start_y = element_top.saturating_sub(viewport_top);
                            let end_y = (element_bottom - viewport_top).min(viewport_height);
                            let visible_h = end_y.saturating_sub(start_y);

                            if visible_h > 0 {
                                let image_y_offset =
                                    (element_top as i32 - viewport_top as i32) as i16;

                                if let Some(sliced) = app.get_sliced_protocol(
                                    path,
                                    content_rect.width,
                                    h as u16,
                                    picker,
                                ) {
                                    let area = Rect {
                                        x: content_rect.x,
                                        y: content_rect.y + start_y as u16,
                                        width: content_rect.width,
                                        height: visible_h as u16,
                                    };

                                    let position =
                                        SignedPosition::from((0, image_y_offset - start_y as i16));

                                    f.render_widget(SlicedImage::new(sliced, position), area);
                                }
                            }
                        }
                    }
                }
                ContentElement::BlankLine => {
                    // Render a blank line
                    let draw_y = if rel_y < 0 { 0 } else { rel_y as u16 };
                    let visible_h = if rel_y < 0 {
                        (h as i32 + rel_y).max(0) as u16
                    } else {
                        (h as u16).min((viewport_height as u16).saturating_sub(draw_y))
                    };

                    if visible_h > 0 {
                        let area = Rect {
                            x: content_rect.x,
                            y: content_rect.y + draw_y,
                            width: content_rect.width,
                            height: visible_h,
                        };

                        let blank_paragraph =
                            Paragraph::new(Line::from("")).wrap(Wrap { trim: true });
                        f.render_widget(blank_paragraph, area);
                    }
                }
            }
        }
        current_y_offset += h;
    }

    if show_borders {
        let mut block = Block::default()
            .borders(Borders::ALL)
            .border_type(border_type);
        if let Some(t) = title {
            block = block.title(t);
        }
        f.render_widget(block, area);
    }
}