eilmeldung 1.4.2

a feature-rich TUI RSS Reader based on the news-flash library
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
use std::collections::HashSet;

use super::model::FeedOrCategory;
use crate::prelude::*;
use crate::ui::feeds_list::model::FeedListModelData;

use getset::{Getters, MutGetters};
use log::info;
use news_flash::models::{Category, Feed, FeedMapping, NEWSFLASH_TOPLEVEL, UnifiedMapping};
use news_flash::models::{PluginCapabilities, Tag};
use ratatui::text::{Line, Span};
use ratatui::widgets::Scrollbar;
use ratatui::widgets::{Block, Borders};
use ratatui::widgets::{StatefulWidget, Widget};
use strum::IntoEnumIterator;
use tui_tree_widget::{Tree, TreeItem, TreeState};

use super::feed_list_item::FeedListItem;

#[derive(Getters, MutGetters)]
pub struct FeedListViewData {
    #[getset(get = "pub", get_mut = "pub")]
    scope: ArticleScope,

    #[getset(get = "pub", get_mut = "pub")]
    tree_state: TreeState<FeedListItem>,

    #[getset(get = "pub")]
    tree_items: Vec<TreeItem<'static, FeedListItem>>,

    #[getset(get = "pub")]
    yanked_unified_mapping: Option<UnifiedMapping>,

    #[getset(get = "pub")]
    found_items: HashSet<FeedListItem>,

    #[getset(get = "pub")]
    found_paths: HashSet<Vec<FeedListItem>>,

    #[getset(get = "pub")]
    paths: Vec<Vec<FeedListItem>>,
}

impl FeedListViewData {
    pub fn new(config: &Config) -> Self {
        Self {
            scope: config.feed_list_scope,
            tree_state: Default::default(),
            tree_items: Default::default(),
            yanked_unified_mapping: Default::default(),
            found_items: Default::default(),
            found_paths: Default::default(),
            paths: Default::default(),
        }
    }
}

impl Widget for &mut FeedList {
    fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
        let highlight_style = self.config.theme.selected(&Default::default());

        let tree_items = self.view_data.tree_items().clone();

        let tree = Tree::new(&tree_items)
            .unwrap() // TODO error handling
            .block(
                Block::default()
                    .borders(Borders::TOP | Borders::LEFT | Borders::BOTTOM)
                    .border_type(ratatui::widgets::BorderType::Rounded)
                    .border_style(self.config.theme.eff_border(self.is_focused))
                    .title_top(self.view_data.build_title(&self.config)),
            )
            .experimental_scrollbar(Some(
                Scrollbar::new(ratatui::widgets::ScrollbarOrientation::VerticalLeft)
                    .symbols(self.config.scrollbar_set())
                    .style(self.config.theme.eff_border(self.is_focused)),
            ))
            .highlight_style(highlight_style);

        StatefulWidget::render(tree, area, buf, self.view_data.tree_state_mut());
    }
}

impl FeedListViewData {
    pub async fn update(
        &mut self,
        config: &Config,
        model_data: &FeedListModelData,
        search_term: &Option<SearchTerm>,
    ) -> color_eyre::Result<()> {
        self.tree_items = Default::default();
        self.found_items = Default::default();
        self.found_paths = Default::default();
        self.paths = Default::default();

        for item in config.feed_list.iter() {
            use FeedListContentIdentifier::*;
            match item {
                Feeds(item_type) => {
                    self.add_feeds_item(config, model_data, item_type, search_term)?
                }
                Categories(item_type) => {
                    self.add_categories_item(config, model_data, item_type, search_term)?
                }
                Tags(item_type) => {
                    self.add_tags_item(config, model_data, item_type, search_term)
                        .await?
                }
                Query(labeled_query) => self.add_query_item(config, labeled_query, search_term),
            }
        }

        let mut path = Vec::new();
        let tree_items = self.tree_items.to_vec();
        self.build_paths(&tree_items, &mut path);

        Ok(())
    }

    fn build_paths(&mut self, children: &[TreeItem<FeedListItem>], path: &mut Vec<FeedListItem>) {
        for child in children.iter() {
            path.push(child.identifier().to_owned());

            self.paths.push(path.to_vec());

            if self.found_items.contains(child.identifier()) {
                self.found_paths.insert(path.to_vec());
            }

            self.build_paths(child.children(), path);

            path.pop();
        }
    }

    fn add_query_item(
        &mut self,
        config: &Config,
        labeled_query: &LabeledQuery,
        search_term: &Option<SearchTerm>,
    ) {
        // queries
        let query_item = FeedListItem::Query(Box::new(labeled_query.clone()));
        let mut query_item_text = query_item.to_text(config, None, None);

        if let Some(search_term) = search_term.as_ref()
            && search_term.test_text(&query_item_text)
        {
            patch_text_style(
                &mut query_item_text,
                config.theme.highlighted(&Default::default()),
            );
            self.found_items.insert(query_item.to_owned());
        }

        self.tree_items
            .push(TreeItem::new_leaf(query_item, query_item_text));
    }

    fn include_feed_or_category(
        &self,
        model_data: &FeedListModelData,
        feed_or_category: &FeedOrCategory,
    ) -> bool {
        match self.scope {
            ArticleScope::All => true,
            ArticleScope::Unread => model_data
                .unread_count_for_feed_or_category()
                .get(feed_or_category)
                .map(|count| *count > 0)
                .unwrap_or(false),
            ArticleScope::Marked => model_data
                .marked_count_for_feed_or_category()
                .get(feed_or_category)
                .map(|count| *count > 0)
                .unwrap_or(false),
        }
    }

    fn add_categories_item(
        &mut self,
        config: &Config,
        model_data: &FeedListModelData,
        item_type: &FeedListItemType,
        search_term: &Option<SearchTerm>,
    ) -> color_eyre::Result<()> {
        let mut root_items = Vec::new();

        let feeds_or_categories = model_data
            .roots()
            .iter()
            .filter(|feed_or_category| self.include_feed_or_category(model_data, feed_or_category))
            .collect::<Vec<&FeedOrCategory>>();

        for root in feeds_or_categories {
            match root {
                FeedOrCategory::Category(category_id) => {
                    if let Some(category) = model_data.category_map().get(category_id) {
                        root_items.push(self.map_category_to_tree_item(
                            config,
                            category,
                            model_data,
                            search_term,
                        ))
                    }
                }

                FeedOrCategory::Feed(feed_id) => {
                    if let Some(feed) = model_data.feed_map().get(feed_id) {
                        root_items.push(self.map_feed_to_tree_item(
                            config,
                            feed,
                            model_data,
                            search_term,
                        ))
                    }
                }
            }
        }

        match item_type {
            FeedListItemType::Tree => {
                let categories_item = FeedListItem::Categories;
                let mut categories_text = categories_item.to_text(config, None, None);
                if let Some(search_term) = search_term.as_ref()
                    && search_term.test_text(&categories_text)
                {
                    patch_text_style(
                        &mut categories_text,
                        config.theme.highlighted(&Default::default()),
                    );
                    self.found_items.insert(categories_item.to_owned());
                }

                self.tree_items
                    .push(TreeItem::new(categories_item, categories_text, root_items)?);
            }
            FeedListItemType::List => {
                self.tree_items.append(&mut root_items);
            }
        }
        Ok(())
    }

    async fn add_tags_item(
        &mut self,
        config: &Config,
        model_data: &FeedListModelData,
        item_type: &FeedListItemType,
        search_term: &Option<SearchTerm>,
    ) -> Result<(), color_eyre::eyre::Error> {
        if model_data
            .features()
            .await?
            .contains(PluginCapabilities::SUPPORT_TAGS)
        {
            let mut children = model_data
                .tags()
                .iter()
                .filter(|tag| {
                    !(matches!(self.scope, ArticleScope::Unread)
                        && model_data
                            .unread_count_for_tag()
                            .get(&tag.tag_id)
                            .map(|count| *count == 0i64)
                            .unwrap_or(false))
                })
                .cloned()
                .collect::<Vec<Tag>>()
                .into_iter()
                .map(|tag| self.gen_tag_item(config, model_data, tag, search_term))
                .collect::<Vec<TreeItem<_>>>();

            // let children =

            match item_type {
                FeedListItemType::List => self.tree_items.append(&mut children),
                FeedListItemType::Tree => {
                    let tags_item = FeedListItem::Tags;
                    let mut tag_item_text = tags_item.to_text(config, None, None);
                    if let Some(search_term) = search_term.as_ref()
                        && search_term.test_text(&tag_item_text)
                    {
                        patch_text_style(
                            &mut tag_item_text,
                            config.theme.highlighted(&Default::default()),
                        );
                        self.found_items.insert(tags_item.to_owned());
                    }

                    let tags_tree_item = TreeItem::new(tags_item, tag_item_text, children)?;
                    self.tree_items.push(tags_tree_item);
                }
            }
        }
        Ok(())
    }

    fn add_feeds_item(
        &mut self,
        config: &Config,
        model_data: &FeedListModelData,
        item_type: &FeedListItemType,
        search_term: &Option<SearchTerm>,
    ) -> Result<(), color_eyre::eyre::Error> {
        let feeds = model_data
            .feeds()
            .iter()
            .filter(|feed| {
                self.include_feed_or_category(
                    model_data,
                    &FeedOrCategory::Feed(feed.feed_id.to_owned()),
                )
            })
            .collect::<Vec<&Feed>>();
        let mut children = feeds
            .into_iter()
            .map(|feed| self.map_feed_to_tree_item(config, feed, model_data, search_term))
            .collect();

        match item_type {
            FeedListItemType::List => self.tree_items.append(&mut children),
            FeedListItemType::Tree => self.tree_items.push(TreeItem::new(
                FeedListItem::All,
                FeedListItem::All.to_text(config, Some(*model_data.unread_count_all()), None),
                children,
            )?),
        }
        Ok(())
    }

    fn map_feed_to_tree_item<'a>(
        &mut self,
        config: &Config,
        feed: &Feed,
        model_data: &FeedListModelData,
        search_term: &Option<SearchTerm>,
    ) -> TreeItem<'a, FeedListItem> {
        let identifier = FeedListItem::Feed(Box::new(feed.clone()));
        let mut identifier_text = identifier.to_text(
            config,
            Some(
                model_data
                    .unread_count_for_feed_or_category()
                    .get(&FeedOrCategory::Feed(feed.feed_id.clone()))
                    .copied()
                    .unwrap_or(0),
            ),
            Some(
                model_data
                    .marked_count_for_feed_or_category()
                    .get(&FeedOrCategory::Feed(feed.feed_id.clone()))
                    .copied()
                    .unwrap_or(0),
            ),
        );

        if let Some(UnifiedMapping::Feed(feed_mapping)) = self.yanked_unified_mapping()
            && feed_mapping.feed_id == feed.feed_id
        {
            identifier_text = identifier_text.style(config.theme.yanked());
        }

        if let Some(search_term) = search_term.as_ref()
            && search_term.test_text(&identifier_text)
        {
            patch_text_style(
                &mut identifier_text,
                config.theme.highlighted(&Default::default()),
            );
            self.found_items.insert(identifier.to_owned());
        }

        TreeItem::new_leaf(identifier, identifier_text)
    }

    fn map_category_to_tree_item<'a>(
        &mut self,
        config: &Config,
        category: &Category,
        model_data: &FeedListModelData,
        search_term: &Option<SearchTerm>,
    ) -> TreeItem<'a, FeedListItem> {
        let mut children: Vec<TreeItem<'a, FeedListItem>> = Vec::new();

        if let Some(child_categories) = model_data.category_tree().get(&category.category_id) {
            let child_categories = child_categories
                .iter()
                .filter(|feed_or_category| {
                    self.include_feed_or_category(model_data, feed_or_category)
                })
                .collect::<Vec<&FeedOrCategory>>();

            for child in child_categories {
                children.push(match child {
                    FeedOrCategory::Category(category_id) => {
                        let child_category = model_data.category_map().get(category_id).unwrap();
                        self.map_category_to_tree_item(
                            config,
                            child_category,
                            model_data,
                            search_term,
                        )
                    }

                    FeedOrCategory::Feed(feed_id) => {
                        let feed = model_data.feed_map().get(feed_id).unwrap();
                        self.map_feed_to_tree_item(config, feed, model_data, search_term)
                    }
                });
            }
        }

        let identifier = FeedListItem::Category(Box::new(category.clone()));
        let unread_category = model_data
            .unread_count_for_feed_or_category()
            .get(&category.category_id.clone().into())
            .copied();
        let marked_category = model_data
            .marked_count_for_feed_or_category()
            .get(&category.category_id.clone().into())
            .copied();
        let mut identifier_text = identifier.to_text(config, unread_category, marked_category);

        if let Some(search_term) = search_term.as_ref()
            && search_term.test_text(&identifier_text)
        {
            patch_text_style(
                &mut identifier_text,
                config.theme.highlighted(&Default::default()),
            );
            self.found_items.insert(identifier.to_owned());
        }

        if let Some(UnifiedMapping::Category(category_mapping)) = self.yanked_unified_mapping()
            && category_mapping.category_id == category.category_id
        {
            identifier_text = identifier_text.style(config.theme.yanked());
        }

        TreeItem::new(identifier, identifier_text, children).unwrap()
    }

    fn gen_tag_item(
        &mut self,
        config: &Config,
        model_data: &FeedListModelData,
        tag: news_flash::models::Tag,
        search_term: &Option<SearchTerm>,
    ) -> TreeItem<'static, FeedListItem> {
        let count = model_data
            .unread_count_for_tag()
            .get(&tag.tag_id)
            .copied()
            .unwrap_or(0);
        let tag_item = FeedListItem::Tag(Box::new(tag));
        let mut tag_item_text = tag_item.to_text(config, Some(count), None);
        if let Some(search_term) = search_term.as_ref()
            && search_term.test_text(&tag_item_text)
        {
            patch_text_style(
                &mut tag_item_text,
                config.theme.highlighted(&Default::default()),
            );
            self.found_items.insert(tag_item.to_owned());
        }
        TreeItem::new_leaf(tag_item, tag_item_text)
    }

    pub(super) fn take_yanked_unified_mapping(&mut self) -> Option<UnifiedMapping> {
        self.yanked_unified_mapping.take()
    }

    pub(super) fn set_yanked_unified_mapping(&mut self, unified_mapping: Option<UnifiedMapping>) {
        self.yanked_unified_mapping = unified_mapping;
    }

    pub(super) fn yank_feed_or_category(
        &mut self,
        feed_or_category: FeedOrCategory,
        model_data: &FeedListModelData,
    ) {
        info!("yanked {:?}", feed_or_category);
        // self.yanked_feed_or_category = Some(feed_or_category);

        use FeedOrCategory::*;
        self.yanked_unified_mapping = match feed_or_category {
            Feed(feed_id) => {
                let feed_mapping = match model_data.feed_mapping_for_feed().get(&feed_id) {
                    Some(feed_mapping) => feed_mapping.to_owned(),
                    None => FeedMapping {
                        feed_id: feed_id.to_owned(),
                        category_id: (*NEWSFLASH_TOPLEVEL).to_owned(),
                        sort_index: None,
                    },
                };
                Some(UnifiedMapping::Feed(feed_mapping))
            }
            Category(category_id) => model_data
                .category_mapping_for_category()
                .get(&category_id)
                .map(|mapping| UnifiedMapping::Category(mapping.to_owned())),
        };
    }

    pub(super) fn get_selection_with_index(
        &self,
        selection: &[FeedListItem],
    ) -> Vec<(usize, FeedListItem)> {
        selection
            .iter()
            .scan(self.tree_items.as_slice(), |items, id| {
                items
                    .iter()
                    .enumerate()
                    .find(|(_, item)| item.identifier() == id)
                    .map(|(index, next)| {
                        *items = next.children();
                        (index, next.identifier().to_owned())
                    })
                    .or(None)
            })
            .collect::<Vec<(usize, FeedListItem)>>()
    }

    pub(super) fn ensure_sensible_selection(
        &mut self,
        selected_before: &[(usize, FeedListItem)],
    ) -> bool {
        let sensible_selection = selected_before
            .iter()
            .scan(
                (false, self.tree_items.as_slice()),
                |(last, items), (index, id)| {
                    items
                        .iter()
                        .enumerate()
                        .find(|(_, item)| item.identifier() == id)
                        .map(|(_, next)| {
                            *items = next.children();
                            next.identifier().to_owned()
                        })
                        .or_else(|| {
                            if *last {
                                None
                            } else {
                                *last = true;
                                items
                                    .get(*index) // try index
                                    .or(items.get((*index).saturating_sub(1))) // or the one before
                                    .or(items.get((*index).saturating_add(1))) // or the one after
                                    .map(|item| item.identifier().to_owned())
                            }
                        })
                },
            )
            .collect::<Vec<FeedListItem>>();

        if !sensible_selection.is_empty() {
            self.tree_state.select(sensible_selection)
        } else {
            self.tree_state.select(
                self.tree_items
                    .first()
                    .map(|first_item| vec![first_item.identifier().to_owned()])
                    .unwrap_or_default(),
            )
        }
    }

    fn build_title<'a>(&self, config: &Config) -> Line<'a> {
        let mut title = Line::styled("", config.theme.header());
        let spans = &mut title.spans;
        for scope in ArticleScope::iter() {
            let style = if scope == self.scope {
                config.theme.header()
            } else {
                config.theme.inactive()
            };
            spans.push(" ".into());
            spans.push(Span::styled(scope.to_icon(config).to_string(), style));
        }
        spans.push(" ".into());
        title
    }
}