splashboard 2.10.2

A customizable terminal splash screen with plugin-based data sources
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
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
//! Shared HTTP client + URL helpers for the `wikipedia_*` family. Targets the public
//! Wikipedia REST API at `https://{lang}.wikipedia.org/api/rest_v1/...` — host stays inside
//! `*.wikipedia.org` regardless of which language code the config supplies, so every fetcher
//! in the family is Safety::Safe.

use std::sync::OnceLock;
use std::time::Duration;

use reqwest::Client;
use serde::Deserialize;
use serde::de::DeserializeOwned;

use crate::fetcher::FetchError;
use crate::payload::{Body, LinkedLine, LinkedTextBlockData, TextBlockData, TextData};
use crate::render::Shape;

pub const REST_API_PATH: &str = "/api/rest_v1";
pub const DEFAULT_LANG: &str = "en";
const USER_AGENT: &str = concat!(
    "splashboard/",
    env!("CARGO_PKG_VERSION"),
    " (https://github.com/unhappychoice/splashboard-2)"
);
const REQUEST_TIMEOUT: Duration = Duration::from_secs(10);

pub fn http() -> &'static Client {
    static CLIENT: OnceLock<Client> = OnceLock::new();
    CLIENT.get_or_init(|| {
        Client::builder()
            .user_agent(USER_AGENT)
            .timeout(REQUEST_TIMEOUT)
            .gzip(true)
            .build()
            .expect("reqwest client should build with default config")
    })
}

pub fn rest_api_base(lang: &str) -> String {
    format!("https://{lang}.wikipedia.org{REST_API_PATH}")
}

pub async fn get<T: DeserializeOwned>(url: &str) -> Result<T, FetchError> {
    let res = http()
        .get(url)
        .send()
        .await
        .map_err(|e| FetchError::Failed(format!("wikipedia request failed: {e}")))?;
    let status = res.status();
    if !status.is_success() {
        return Err(FetchError::Failed(format!("wikipedia {status}")));
    }
    res.json()
        .await
        .map_err(|e| FetchError::Failed(format!("wikipedia json parse: {e}")))
}

/// Page summary returned by `/page/random/summary`, embedded inside `feed/featured`'s `tfa`
/// block, and inside each on-this-day event's `pages[]`. Featured / random share the rendering
/// helpers below; on-this-day uses only `url()` to wire up event links.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct PageSummary {
    #[serde(default)]
    pub title: String,
    #[serde(default)]
    pub extract: Option<String>,
    #[serde(default)]
    pub content_urls: Option<ContentUrls>,
    /// REST API `thumbnail.source` — a remote `*.wikipedia.org` image URL. Optional because not
    /// every page has a thumbnail; the JSON omits the whole block in that case.
    #[serde(default)]
    pub thumbnail: Option<PageThumbnail>,
}

#[derive(Debug, Clone, Default, Deserialize)]
pub struct PageThumbnail {
    #[serde(default)]
    pub source: String,
}

#[derive(Debug, Clone, Default, Deserialize)]
pub struct ContentUrls {
    #[serde(default)]
    pub desktop: Option<UrlsByPlatform>,
    #[serde(default)]
    pub mobile: Option<UrlsByPlatform>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct UrlsByPlatform {
    pub page: String,
}

impl PageSummary {
    pub fn url(&self) -> Option<String> {
        self.content_urls
            .as_ref()
            .and_then(|c| c.desktop.as_ref().or(c.mobile.as_ref()))
            .map(|p| p.page.clone())
    }

    /// Remote URL of the page's thumbnail image (typically `upload.wikimedia.org/...`). Returns
    /// `None` when the REST API omits the thumbnail or returns an empty source. Callers feed
    /// this through [`crate::fetcher::thumbnails::download_to_cache`] before handing the local
    /// path to a `list_cards`-style renderer.
    pub fn thumbnail_url(&self) -> Option<&str> {
        self.thumbnail
            .as_ref()
            .map(|t| t.source.as_str())
            .filter(|s| !s.is_empty())
    }

    /// First sentence of the extract — split at the first `". "` so the Text shape can carry a
    /// preview without the whole multi-paragraph body. Returns `None` when the extract is
    /// missing or empty.
    pub fn first_sentence(&self) -> Option<String> {
        let extract = self.extract.as_deref()?.trim();
        if extract.is_empty() {
            return None;
        }
        let cut = extract.find(". ").map(|i| i + 1).unwrap_or(extract.len());
        Some(extract[..cut].trim().to_string())
    }
}

/// Shape-aware rendering for a single page summary — shared by `wikipedia_featured` and
/// `wikipedia_random` since both fetchers expose the same `{title, extract, url}` triplet.
pub fn render_page_summary(summary: &PageSummary, shape: Shape) -> Body {
    render_page_summary_with_thumbnail(summary, shape, None)
}

/// Render variant that pins a pre-resolved local thumbnail path onto the `ImageLinkedList`
/// shape's single card. Other shapes ignore the thumbnail (Text / TextBlock have no place to
/// surface it). The thumbnail download itself is async, so callers run
/// [`crate::fetcher::thumbnails::download_to_cache`] before calling this and pass the local
/// path in.
pub fn render_page_summary_with_thumbnail(
    summary: &PageSummary,
    shape: Shape,
    thumbnail_path: Option<std::path::PathBuf>,
) -> Body {
    match shape {
        Shape::Text => Body::Text(TextData {
            value: text_line(summary),
        }),
        Shape::TextBlock => Body::TextBlock(TextBlockData {
            lines: text_block_lines(summary),
        }),
        Shape::ImageLinkedList => Body::ImageLinkedList(crate::payload::ImageLinkedListData {
            items: vec![crate::payload::ImageLinkedItem {
                title: summary.title.clone(),
                url: summary.url(),
                thumbnail_path: thumbnail_path.map(|p| p.to_string_lossy().into_owned()),
                subtitle: summary.first_sentence(),
            }],
        }),
        _ => Body::LinkedTextBlock(LinkedTextBlockData {
            items: vec![LinkedLine {
                text: summary.title.clone(),
                url: summary.url(),
            }],
        }),
    }
}

fn text_line(summary: &PageSummary) -> String {
    match summary.first_sentence() {
        Some(sentence) => format!("{}: {sentence}", summary.title),
        None => summary.title.clone(),
    }
}

fn text_block_lines(summary: &PageSummary) -> Vec<String> {
    let mut lines = vec![summary.title.clone()];
    if let Some(extract) = summary.extract.as_deref().filter(|s| !s.is_empty()) {
        for line in extract.lines().filter(|l| !l.trim().is_empty()) {
            lines.push(line.to_string());
        }
    }
    lines
}

#[cfg(test)]
mod tests {
    use std::io::{Read, Write};
    use std::net::TcpListener;
    use std::thread;

    use super::*;

    fn summary_with(extract: Option<&str>, page_url: Option<&str>) -> PageSummary {
        PageSummary {
            title: "Quokka".into(),
            extract: extract.map(String::from),
            content_urls: page_url.map(|p| ContentUrls {
                desktop: Some(UrlsByPlatform { page: p.into() }),
                mobile: None,
            }),
            thumbnail: None,
        }
    }

    fn run_async<T>(fut: impl std::future::Future<Output = T>) -> T {
        tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap()
            .block_on(fut)
    }

    fn serve_once(status: &str, content_type: &str, body: &str) -> String {
        let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();
        let addr = listener.local_addr().unwrap();
        let body = body.to_owned();
        let content_type = content_type.to_owned();
        let status = status.to_owned();
        thread::spawn(move || {
            let (mut stream, _) = listener.accept().unwrap();
            let mut request = [0_u8; 1024];
            let _ = stream.read(&mut request);
            let response = format!(
                "HTTP/1.1 {status}\r\ncontent-type: {content_type}\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{body}",
                body.len()
            );
            stream.write_all(response.as_bytes()).unwrap();
        });
        format!("http://{addr}/")
    }

    #[test]
    fn rest_api_base_uses_lang_subdomain() {
        assert_eq!(rest_api_base("ja"), "https://ja.wikipedia.org/api/rest_v1");
    }

    #[test]
    fn url_prefers_desktop_over_mobile() {
        let s = summary_with(None, Some("https://en.wikipedia.org/wiki/Quokka"));
        assert_eq!(
            s.url().as_deref(),
            Some("https://en.wikipedia.org/wiki/Quokka")
        );
    }

    #[test]
    fn url_falls_back_to_mobile_when_desktop_absent() {
        let s = PageSummary {
            title: "x".into(),
            extract: None,
            content_urls: Some(ContentUrls {
                desktop: None,
                mobile: Some(UrlsByPlatform {
                    page: "https://en.m.wikipedia.org/wiki/x".into(),
                }),
            }),
            thumbnail: None,
        };
        assert_eq!(
            s.url().as_deref(),
            Some("https://en.m.wikipedia.org/wiki/x")
        );
    }

    #[test]
    fn url_is_none_when_content_urls_absent() {
        assert!(summary_with(None, None).url().is_none());
    }

    #[test]
    fn first_sentence_splits_on_period_space() {
        let s = summary_with(Some("Apollo 11 was a mission. It landed in 1969."), None);
        assert_eq!(
            s.first_sentence().as_deref(),
            Some("Apollo 11 was a mission.")
        );
    }

    #[test]
    fn first_sentence_returns_full_extract_when_no_period_space() {
        let s = summary_with(Some("Single fragment"), None);
        assert_eq!(s.first_sentence().as_deref(), Some("Single fragment"));
    }

    #[test]
    fn first_sentence_returns_none_when_extract_missing_or_blank() {
        assert!(summary_with(None, None).first_sentence().is_none());
        assert!(summary_with(Some("   "), None).first_sentence().is_none());
    }

    #[test]
    fn render_text_combines_title_and_first_sentence() {
        let s = summary_with(Some("It hops. It is fluffy."), None);
        let body = render_page_summary(&s, Shape::Text);
        assert!(matches!(body, Body::Text(_)));
        if let Body::Text(t) = body {
            assert_eq!(t.value, "Quokka: It hops.");
        }
    }

    #[test]
    fn render_text_falls_back_to_title_when_extract_missing() {
        let body = render_page_summary(&summary_with(None, None), Shape::Text);
        assert!(matches!(body, Body::Text(_)));
        if let Body::Text(t) = body {
            assert_eq!(t.value, "Quokka");
        }
    }

    #[test]
    fn render_text_block_emits_title_then_extract_lines() {
        let s = summary_with(Some("Para one.\n\nPara two."), None);
        let body = render_page_summary(&s, Shape::TextBlock);
        assert!(matches!(body, Body::TextBlock(_)));
        if let Body::TextBlock(t) = body {
            assert_eq!(t.lines, vec!["Quokka", "Para one.", "Para two."]);
        }
    }

    #[test]
    fn render_text_block_emits_title_only_when_extract_empty() {
        let s = summary_with(Some(""), None);
        let body = render_page_summary(&s, Shape::TextBlock);
        assert!(matches!(body, Body::TextBlock(_)));
        if let Body::TextBlock(t) = body {
            assert_eq!(t.lines, vec!["Quokka"]);
        }
    }

    #[test]
    fn render_linked_text_block_uses_summary_url() {
        let s = summary_with(None, Some("https://en.wikipedia.org/wiki/Quokka"));
        let body = render_page_summary(&s, Shape::LinkedTextBlock);
        assert!(matches!(body, Body::LinkedTextBlock(_)));
        if let Body::LinkedTextBlock(b) = body {
            assert_eq!(b.items.len(), 1);
            assert_eq!(b.items[0].text, "Quokka");
            assert_eq!(
                b.items[0].url.as_deref(),
                Some("https://en.wikipedia.org/wiki/Quokka")
            );
        }
    }

    #[test]
    fn render_linked_text_block_drops_url_when_absent() {
        let body = render_page_summary(&summary_with(None, None), Shape::LinkedTextBlock);
        assert!(matches!(body, Body::LinkedTextBlock(_)));
        if let Body::LinkedTextBlock(b) = body {
            assert!(b.items[0].url.is_none());
        }
    }

    #[test]
    fn page_summary_deserializes_real_api_payload() {
        let raw = r#"{
            "title": "Quokka",
            "extract": "The quokka is a small macropod.",
            "content_urls": {
                "desktop": { "page": "https://en.wikipedia.org/wiki/Quokka" }
            }
        }"#;
        let s: PageSummary = serde_json::from_str(raw).unwrap();
        assert_eq!(s.title, "Quokka");
        assert_eq!(
            s.extract.as_deref(),
            Some("The quokka is a small macropod.")
        );
        assert_eq!(
            s.url().as_deref(),
            Some("https://en.wikipedia.org/wiki/Quokka")
        );
    }

    #[test]
    fn page_summary_deserializes_with_only_title() {
        let raw = r#"{"title":"x"}"#;
        let s: PageSummary = serde_json::from_str(raw).unwrap();
        assert_eq!(s.title, "x");
        assert!(s.extract.is_none());
        assert!(s.url().is_none());
    }

    #[test]
    fn get_deserializes_successful_json_payloads() {
        let url = serve_once(
            "200 OK",
            "application/json",
            r#"{"title":"Quokka","extract":"The quokka is a small macropod."}"#,
        );

        let summary = run_async(get::<PageSummary>(&url)).unwrap();

        assert_eq!(summary.title, "Quokka");
        assert_eq!(
            summary.extract.as_deref(),
            Some("The quokka is a small macropod.")
        );
    }

    #[test]
    fn get_rejects_non_success_statuses() {
        let url = serve_once("503 Service Unavailable", "text/plain", "busy");

        let err = run_async(get::<PageSummary>(&url)).unwrap_err();

        assert!(matches!(
            err,
            FetchError::Failed(message) if message.contains("wikipedia 503")
        ));
    }

    #[test]
    fn get_rejects_invalid_json_payloads() {
        let url = serve_once("200 OK", "application/json", "{\"title\":");

        let err = run_async(get::<PageSummary>(&url)).unwrap_err();

        assert!(matches!(
            err,
            FetchError::Failed(message) if message.contains("wikipedia json parse")
        ));
    }

    #[test]
    fn thumbnail_url_returns_some_for_non_empty_source() {
        let s = PageSummary {
            title: "x".into(),
            extract: None,
            content_urls: None,
            thumbnail: Some(PageThumbnail {
                source: "https://upload.wikimedia.org/x.jpg".into(),
            }),
        };
        assert_eq!(
            s.thumbnail_url(),
            Some("https://upload.wikimedia.org/x.jpg"),
        );
    }

    #[test]
    fn thumbnail_url_returns_none_for_missing_or_empty() {
        // Missing thumbnail block.
        assert!(summary_with(None, None).thumbnail_url().is_none());
        // Empty `source` string — REST sometimes returns the block with a blank URL.
        let s = PageSummary {
            title: "x".into(),
            extract: None,
            content_urls: None,
            thumbnail: Some(PageThumbnail {
                source: String::new(),
            }),
        };
        assert!(s.thumbnail_url().is_none());
    }

    #[test]
    fn render_image_linked_list_pins_thumbnail_path_and_first_sentence_subtitle() {
        let s = summary_with(Some("It hops. Other stuff."), Some("https://example.com/q"));
        let body = render_page_summary_with_thumbnail(
            &s,
            Shape::ImageLinkedList,
            Some(std::path::PathBuf::from("/tmp/quokka.jpg")),
        );
        let Body::ImageLinkedList(b) = body else {
            panic!("expected image_linked_list");
        };
        assert_eq!(b.items.len(), 1);
        assert_eq!(b.items[0].title, "Quokka");
        assert_eq!(b.items[0].url.as_deref(), Some("https://example.com/q"));
        assert_eq!(
            b.items[0].thumbnail_path.as_deref(),
            Some("/tmp/quokka.jpg")
        );
        assert_eq!(b.items[0].subtitle.as_deref(), Some("It hops."));
    }

    #[test]
    fn render_image_linked_list_without_path_leaves_thumbnail_empty() {
        let s = summary_with(None, None);
        let body = render_page_summary_with_thumbnail(&s, Shape::ImageLinkedList, None);
        let Body::ImageLinkedList(b) = body else {
            panic!("expected image_linked_list");
        };
        assert_eq!(b.items[0].thumbnail_path, None);
        assert_eq!(b.items[0].subtitle, None);
    }
}