From: Simon Tatham Date: Mon, 25 Dec 2023 20:43:55 +0000 (+0000) Subject: ExtendableIndicator, without the hard part X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=a79c116b4403dab4b27ecd2e34ace9563a63430c;p=mastodonochrome.git ExtendableIndicator, without the hard part --- diff --git a/src/text.rs b/src/text.rs index b61efa9..9b59666 100644 --- a/src/text.rs +++ b/src/text.rs @@ -1,5 +1,5 @@ -use chrono::{DateTime,Utc,Local}; -use core::cmp::max; +use chrono::{DateTime, Utc, Local}; +use core::cmp::{max, min}; use std::collections::{HashMap, BTreeSet}; use super::html; @@ -11,6 +11,8 @@ pub trait TextFragment { // Some means of passing a flag to render() that says which username or // status is to be highlighted fn render(&self, width: usize) -> Vec; + + fn as_extendable_indicator(&self) -> Option<&ExtendableIndicator> { None } } pub struct BlankLine {} @@ -733,8 +735,47 @@ fn test_html() { }); } +pub struct ExtendableIndicator {} + +pub fn extendable_indicator() -> Box { + Box::new(ExtendableIndicator{}) +} + +impl TextFragment for ExtendableIndicator { + fn render(&self, width: usize) -> Vec { + // FIXME: decide how to make this message change when it's primed + let message = ColouredString::general( + "Press [0] at top of file to extend", + "HHHHHHHKHHHHHHHHHHHHHHHHHHHHHHHHHH"); + let msgtrunc = message.truncate(width); + let space = width - min(msgtrunc.width() + 1, width); + let left = space / 2; + let msgpad = ColouredString::plain(" ").repeat(left) + msgtrunc; + vec! { + ColouredString::plain(""), + msgpad, + ColouredString::plain(""), + } + } + + // FIXME: get rid of this silly trait method if I turn out not to need it + fn as_extendable_indicator(&self) -> Option<&ExtendableIndicator> { + Some(self) + } +} + +#[test] +fn test_extendable() { + assert_eq!(extendable_indicator().render(40), vec! { + ColouredString::plain(""), + ColouredString::general( + " Press [0] at top of file to extend", + " HHHHHHHKHHHHHHHHHHHHHHHHHHHHHHHHHH"), + ColouredString::plain(""), + }); +} + // TODO: -// ExtendableIndicator at file header (with an 'active' flag) // InReplyToLine, with first line of included paragraph // NotificationLog, also with included toot // UserListEntry