splashboard 1.4.0

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
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
//! `hackernews_user` — profile rollup for an HN account via `/user/{login}.json`. Same read,
//! three shapes:
//!
//! - `Text` — `"@login · Npt karma"` for hero / subtitle bands.
//! - `Entries` — login / karma / created / submissions count, key/value rows.
//! - `TextBlock` — bio block (login / about / created / karma) for sidebar use.

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::Deserialize;

use crate::fetcher::github::common::{cache_key, parse_options, payload};
use crate::fetcher::{FetchContext, FetchError, Fetcher, Safety};
use crate::options::OptionSchema;
use crate::payload::{Body, EntriesData, Entry, LinkedLine, LinkedTextBlockData, Payload};
use crate::render::Shape;
use crate::samples;

use super::client::{API_BASE, HN_USER_URL, get};

#[cfg(test)]
use crate::payload::{TextBlockData, TextData};

const SHAPES: &[Shape] = &[
    Shape::Entries,
    Shape::Text,
    Shape::LinkedTextBlock,
    Shape::TextBlock,
];

const OPTION_SCHEMAS: &[OptionSchema] = &[OptionSchema {
    name: "user",
    type_hint: "string (HN login)",
    required: true,
    default: None,
    description: "Hacker News login to fetch (e.g. `\"pg\"`).",
}];

/// Profile rollup for a single Hacker News account.
pub struct HackernewsUserFetcher;

#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Options {
    #[serde(default)]
    pub user: Option<String>,
}

#[async_trait]
impl Fetcher for HackernewsUserFetcher {
    fn name(&self) -> &str {
        "hackernews_user"
    }
    fn safety(&self) -> Safety {
        Safety::Safe
    }
    fn description(&self) -> &'static str {
        "Profile rollup for one Hacker News account — login, karma, join date, submission count, and bio. Use `hackernews_user_submissions` for that user's recent stories or `hackernews_user_comments` for their recent comments."
    }
    fn shapes(&self) -> &[Shape] {
        SHAPES
    }
    fn option_schemas(&self) -> &[OptionSchema] {
        OPTION_SCHEMAS
    }
    fn cache_key(&self, ctx: &FetchContext) -> String {
        let extra = ctx
            .options
            .as_ref()
            .and_then(|v| toml::to_string(v).ok())
            .unwrap_or_default();
        cache_key(self.name(), ctx, &extra)
    }
    fn sample_body(&self, shape: Shape) -> Option<Body> {
        Some(match shape {
            Shape::Text => samples::text("@pg · 156000pt karma"),
            Shape::Entries => samples::entries(&[
                ("login", "pg"),
                ("karma", "156000"),
                ("created", "2006-10-09"),
                ("submissions", "1234"),
            ]),
            Shape::TextBlock => samples::text_block(&[
                "@pg",
                "156000pt karma",
                "joined 2006-10-09",
                "Paul Graham, Y Combinator co-founder.",
            ]),
            Shape::LinkedTextBlock => samples::linked_text_block(&[(
                "@pg · 156000pt karma",
                Some("https://news.ycombinator.com/user?id=pg"),
            )]),
            _ => return None,
        })
    }
    async fn fetch(&self, ctx: &FetchContext) -> Result<Payload, FetchError> {
        let opts: Options = parse_options(ctx.options.as_ref()).map_err(FetchError::Failed)?;
        let login = opts
            .user
            .ok_or_else(|| FetchError::Failed("hackernews_user requires `user` option".into()))?;
        let info: UserInfo = get(&format!("{API_BASE}/user/{login}.json")).await?;
        Ok(payload(render_body(
            &info,
            ctx.shape.unwrap_or(Shape::Entries),
        )))
    }
}

#[derive(Debug, Deserialize)]
struct UserInfo {
    id: String,
    #[serde(default)]
    karma: u64,
    /// Unix seconds.
    #[serde(default)]
    created: i64,
    #[serde(default)]
    about: Option<String>,
    #[serde(default)]
    submitted: Vec<u64>,
}

fn render_body(info: &UserInfo, shape: Shape) -> Body {
    match shape {
        Shape::Text => crate::payload::Body::Text(crate::payload::TextData {
            value: format!("@{} · {}pt karma", info.id, info.karma),
        }),
        Shape::TextBlock => crate::payload::Body::TextBlock(crate::payload::TextBlockData {
            lines: text_block_lines(info),
        }),
        Shape::LinkedTextBlock => Body::LinkedTextBlock(LinkedTextBlockData {
            items: vec![LinkedLine {
                text: format!("@{} · {}pt karma", info.id, info.karma),
                url: Some(format!("{HN_USER_URL}{}", info.id)),
            }],
        }),
        _ => Body::Entries(EntriesData {
            items: entries(info),
        }),
    }
}

fn entries(info: &UserInfo) -> Vec<Entry> {
    let mut items = vec![
        kv("login", info.id.clone()),
        kv("karma", info.karma.to_string()),
        kv("created", iso_date(info.created)),
        kv("submissions", info.submitted.len().to_string()),
    ];
    if let Some(about) = info.about.as_deref().filter(|s| !s.is_empty()) {
        items.push(kv("about", strip_tags(about)));
    }
    items
}

fn text_block_lines(info: &UserInfo) -> Vec<String> {
    let mut lines = vec![
        format!("@{}", info.id),
        format!("{}pt karma", info.karma),
        format!("joined {}", iso_date(info.created)),
    ];
    if let Some(about) = info.about.as_deref().filter(|s| !s.is_empty()) {
        lines.push(strip_tags(about));
    }
    lines
}

fn kv(key: &str, value: String) -> Entry {
    Entry {
        key: key.into(),
        value: Some(value),
        status: None,
    }
}

fn iso_date(unix_seconds: i64) -> String {
    DateTime::<Utc>::from_timestamp(unix_seconds, 0)
        .map(|dt| dt.format("%Y-%m-%d").to_string())
        .unwrap_or_default()
}

/// HN's `about` field embeds raw HTML (`<p>`, `<a>`, `&#x2F;`). Strip the markup conservatively
/// so the rollup stays text-only — replacing `<br>` / `<p>` with spaces and dropping the rest.
/// This isn't a full HTML parser; it's a "best-effort make it readable on a splash" pass.
fn strip_tags(raw: &str) -> String {
    let with_breaks = raw.replace("<p>", " ").replace("<br>", " ");
    let mut out = String::with_capacity(with_breaks.len());
    let mut in_tag = false;
    for ch in with_breaks.chars() {
        match (ch, in_tag) {
            ('<', _) => in_tag = true,
            ('>', _) => in_tag = false,
            (c, false) => out.push(c),
            _ => {}
        }
    }
    decode_entities(&out)
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
}

fn decode_entities(s: &str) -> String {
    s.replace("&amp;", "&")
        .replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
        .replace("&#x27;", "'")
        .replace("&#x2F;", "/")
        .replace("&#39;", "'")
}

#[cfg(test)]
mod tests {
    use super::*;

    fn info(about: Option<&str>) -> UserInfo {
        UserInfo {
            id: "pg".into(),
            karma: 156000,
            created: 1_160_418_092, // 2006-10-09
            about: about.map(String::from),
            submitted: vec![1, 2, 3, 4, 5],
        }
    }

    fn ctx(shape: Shape, options: &str) -> FetchContext {
        FetchContext {
            shape: Some(shape),
            options: Some(toml::from_str(options).unwrap()),
            ..FetchContext::default()
        }
    }

    #[test]
    fn options_require_user() {
        let opts: Options = toml::from_str("user = \"pg\"").unwrap();
        assert_eq!(opts.user.as_deref(), Some("pg"));
    }

    #[test]
    fn options_reject_unknown_keys() {
        let raw: toml::Value = toml::from_str("user = \"pg\"\nbogus = true").unwrap();
        assert!(raw.try_into::<Options>().is_err());
    }

    #[test]
    fn fetcher_catalog_surface_matches_contract() {
        let fetcher = HackernewsUserFetcher;
        assert_eq!(fetcher.name(), "hackernews_user");
        assert_eq!(fetcher.safety(), Safety::Safe);
        assert_eq!(fetcher.default_shape(), Shape::Entries);
        assert_eq!(fetcher.shapes(), SHAPES);
        assert_eq!(fetcher.option_schemas().len(), 1);
        assert_eq!(fetcher.option_schemas()[0].name, "user");
        assert!(fetcher.description().contains("recent stories"));
    }

    #[test]
    fn sample_body_supports_each_declared_shape() {
        let fetcher = HackernewsUserFetcher;

        assert_eq!(
            fetcher.sample_body(Shape::Text),
            Some(Body::Text(TextData {
                value: "@pg · 156000pt karma".into(),
            }))
        );

        assert_eq!(
            fetcher.sample_body(Shape::Entries),
            Some(Body::Entries(EntriesData {
                items: vec![
                    Entry {
                        key: "login".into(),
                        value: Some("pg".into()),
                        status: None,
                    },
                    Entry {
                        key: "karma".into(),
                        value: Some("156000".into()),
                        status: None,
                    },
                    Entry {
                        key: "created".into(),
                        value: Some("2006-10-09".into()),
                        status: None,
                    },
                    Entry {
                        key: "submissions".into(),
                        value: Some("1234".into()),
                        status: None,
                    },
                ],
            }))
        );

        assert_eq!(
            fetcher.sample_body(Shape::TextBlock),
            Some(Body::TextBlock(TextBlockData {
                lines: vec![
                    "@pg".into(),
                    "156000pt karma".into(),
                    "joined 2006-10-09".into(),
                    "Paul Graham, Y Combinator co-founder.".into(),
                ],
            }))
        );

        assert_eq!(
            fetcher.sample_body(Shape::LinkedTextBlock),
            Some(Body::LinkedTextBlock(LinkedTextBlockData {
                items: vec![LinkedLine {
                    text: "@pg · 156000pt karma".into(),
                    url: Some("https://news.ycombinator.com/user?id=pg".into()),
                }],
            }))
        );

        assert!(fetcher.sample_body(Shape::Timeline).is_none());
    }

    #[test]
    fn cache_key_changes_with_shape_and_options() {
        let fetcher = HackernewsUserFetcher;
        let base = fetcher.cache_key(&ctx(Shape::Entries, "user = \"pg\""));
        let same = fetcher.cache_key(&ctx(Shape::Entries, "user = \"pg\""));
        let different_shape = fetcher.cache_key(&ctx(Shape::Text, "user = \"pg\""));
        let different_options = fetcher.cache_key(&ctx(Shape::Entries, "user = \"dang\""));

        assert_eq!(base, same);
        assert_ne!(base, different_shape);
        assert_ne!(base, different_options);
    }

    #[test]
    fn text_shape_combines_login_and_karma() {
        assert_eq!(
            render_body(&info(None), Shape::Text),
            Body::Text(TextData {
                value: "@pg · 156000pt karma".into(),
            })
        );
    }

    #[test]
    fn entries_shape_includes_canonical_rows() {
        assert_eq!(
            render_body(&info(None), Shape::Entries),
            Body::Entries(EntriesData {
                items: vec![
                    Entry {
                        key: "login".into(),
                        value: Some("pg".into()),
                        status: None,
                    },
                    Entry {
                        key: "karma".into(),
                        value: Some("156000".into()),
                        status: None,
                    },
                    Entry {
                        key: "created".into(),
                        value: Some("2006-10-09".into()),
                        status: None,
                    },
                    Entry {
                        key: "submissions".into(),
                        value: Some("5".into()),
                        status: None,
                    },
                ],
            })
        );
    }

    #[test]
    fn entries_shape_appends_about_when_present() {
        assert_eq!(
            render_body(&info(Some("Paul.")), Shape::Entries),
            Body::Entries(EntriesData {
                items: vec![
                    Entry {
                        key: "login".into(),
                        value: Some("pg".into()),
                        status: None,
                    },
                    Entry {
                        key: "karma".into(),
                        value: Some("156000".into()),
                        status: None,
                    },
                    Entry {
                        key: "created".into(),
                        value: Some("2006-10-09".into()),
                        status: None,
                    },
                    Entry {
                        key: "submissions".into(),
                        value: Some("5".into()),
                        status: None,
                    },
                    Entry {
                        key: "about".into(),
                        value: Some("Paul.".into()),
                        status: None,
                    },
                ],
            })
        );
    }

    #[test]
    fn linked_text_block_shape_links_to_user_page() {
        assert_eq!(
            render_body(&info(None), Shape::LinkedTextBlock),
            Body::LinkedTextBlock(LinkedTextBlockData {
                items: vec![LinkedLine {
                    text: "@pg · 156000pt karma".into(),
                    url: Some("https://news.ycombinator.com/user?id=pg".into()),
                }],
            })
        );
    }

    #[test]
    fn iso_date_formats_unix_seconds() {
        assert_eq!(iso_date(1_160_418_092), "2006-10-09");
    }

    #[test]
    fn iso_date_falls_back_to_empty_for_invalid_timestamp() {
        assert_eq!(iso_date(i64::MAX), "");
    }

    #[test]
    fn strip_tags_drops_html_and_decodes_entities() {
        let out = strip_tags("<p>Hello &amp; <a href=\"/x\">world</a></p>");
        assert_eq!(out, "Hello & world");
    }

    #[test]
    fn strip_tags_collapses_whitespace_runs() {
        // <p> becomes a single space, multiple internal spaces get folded by split_whitespace.
        assert_eq!(strip_tags("a<p><p><p>b"), "a b");
        assert_eq!(strip_tags("a    b\n\nc"), "a b c");
    }

    #[test]
    fn decode_entities_handles_canonical_set() {
        assert_eq!(
            decode_entities("&amp;&lt;&gt;&quot;&#x27;&#x2F;&#39;"),
            "&<>\"'/'"
        );
    }

    #[test]
    fn text_block_lines_omit_about_when_blank() {
        let lines = text_block_lines(&info(None));
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0], "@pg");
        assert!(lines[1].contains("156000pt"));
        assert!(lines[2].starts_with("joined "));
    }

    #[test]
    fn text_block_lines_append_about_when_present() {
        let lines = text_block_lines(&info(Some("<p>Founder</p>")));
        assert_eq!(lines.len(), 4);
        assert_eq!(lines[3], "Founder");
    }

    #[test]
    fn entries_uses_submission_count_not_full_array() {
        assert_eq!(
            render_body(&info(None), Shape::Entries),
            Body::Entries(EntriesData {
                items: vec![
                    Entry {
                        key: "login".into(),
                        value: Some("pg".into()),
                        status: None,
                    },
                    Entry {
                        key: "karma".into(),
                        value: Some("156000".into()),
                        status: None,
                    },
                    Entry {
                        key: "created".into(),
                        value: Some("2006-10-09".into()),
                        status: None,
                    },
                    Entry {
                        key: "submissions".into(),
                        value: Some("5".into()),
                        status: None,
                    },
                ],
            })
        );
    }

    #[test]
    fn empty_about_is_treated_as_missing() {
        assert_eq!(
            render_body(&info(Some("")), Shape::Entries),
            Body::Entries(EntriesData {
                items: vec![
                    Entry {
                        key: "login".into(),
                        value: Some("pg".into()),
                        status: None,
                    },
                    Entry {
                        key: "karma".into(),
                        value: Some("156000".into()),
                        status: None,
                    },
                    Entry {
                        key: "created".into(),
                        value: Some("2006-10-09".into()),
                        status: None,
                    },
                    Entry {
                        key: "submissions".into(),
                        value: Some("5".into()),
                        status: None,
                    },
                ],
            })
        );
    }

    #[test]
    fn user_info_deserializes_minimal_payload() {
        let raw = r#"{"id":"pg"}"#;
        let info: UserInfo = serde_json::from_str(raw).unwrap();
        assert_eq!(info.id, "pg");
        assert_eq!(info.karma, 0);
        assert_eq!(info.created, 0);
        assert!(info.about.is_none());
        assert!(info.submitted.is_empty());
    }

    #[tokio::test]
    async fn fetch_rejects_unknown_options() {
        let fetcher = HackernewsUserFetcher;
        let err = fetcher
            .fetch(&ctx(Shape::Entries, "user = \"pg\"\nbogus = true"))
            .await
            .unwrap_err();
        assert!(matches!(
            err,
            FetchError::Failed(msg) if msg.contains("unknown field `bogus`")
        ));
    }

    #[tokio::test]
    async fn fetch_requires_user_option() {
        let fetcher = HackernewsUserFetcher;
        let err = fetcher.fetch(&ctx(Shape::Entries, "")).await.unwrap_err();
        assert!(matches!(
            err,
            FetchError::Failed(msg) if msg == "hackernews_user requires `user` option"
        ));
    }

    #[tokio::test]
    async fn fetch_with_bad_login_surfaces_hn_failure() {
        let fetcher = HackernewsUserFetcher;
        let err = fetcher
            .fetch(&ctx(Shape::LinkedTextBlock, "user = \"bad login\""))
            .await
            .unwrap_err();
        assert!(matches!(
            err,
            FetchError::Failed(msg)
                if msg.contains("hn request failed")
                    || msg.contains("hn json parse")
                    || msg.starts_with("hn ")
        ));
    }
}