From be6cdf8a3a4dff6bd72037a6b3b98370e84cee0d Mon Sep 17 00:00:00 2001 From: Alex Pasmantier <47638216+alexpasmantier@users.noreply.github.com> Date: Wed, 23 Jul 2025 00:20:52 +0200 Subject: [PATCH] feat(preview): previews can now be cached on a per-channel basis (#667) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #660 ## 📺 PR Description **This PR adds a cache mechanism for previews that can be set on a per-channel basis.** **Typical use case**: your preview is making an expensive call (e.g. API call, heavily computational, etc.) This introduces a new `cache` option inside each channel's preview specification: ```toml [metadata] name = "test" [source] command = "echo 'test1\\ntest2\\ntest3\\ntest4\\ntest5'" [preview] command = "sleep 1 && echo 'Previewing {}'" cached = true ``` ## Checklist - [x] my commits **and PR title** follow the [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/#summary) format - [x] if this is a new feature, I have added tests to consolidate the feature and prevent regressions - [ ] if this is a bug fix, I have added a test that reproduces the bug (if applicable) - [x] I have added a reasonable amount of documentation to the code where appropriate --- television/channels/entry.rs | 7 ++ television/channels/prototypes.rs | 9 +- television/previewer/cache.rs | 149 ++++++++++++++++++++++++++++++ television/previewer/mod.rs | 39 +++++++- television/television.rs | 2 + television/utils/cache.rs | 13 +++ 6 files changed, 215 insertions(+), 4 deletions(-) create mode 100644 television/previewer/cache.rs diff --git a/television/channels/entry.rs b/television/channels/entry.rs index 9c6b274..541ca7b 100644 --- a/television/channels/entry.rs +++ b/television/channels/entry.rs @@ -24,6 +24,9 @@ pub struct Entry { impl Hash for Entry { fn hash(&self, state: &mut H) { self.raw.hash(state); + if let Some(display) = &self.display { + display.hash(state); + } if let Some(line_number) = self.line_number { line_number.hash(state); } @@ -35,6 +38,8 @@ impl PartialEq for &Entry { self.raw == other.raw && (self.line_number.is_none() && other.line_number.is_none() || self.line_number == other.line_number) + && (self.display.is_none() && other.display.is_none() + || self.display == other.display) } } @@ -43,6 +48,8 @@ impl PartialEq for Entry { self.raw == other.raw && (self.line_number.is_none() && other.line_number.is_none() || self.line_number == other.line_number) + && (self.display.is_none() && other.display.is_none() + || self.display == other.display) } } diff --git a/television/channels/prototypes.rs b/television/channels/prototypes.rs index 35ebf34..f44c823 100644 --- a/television/channels/prototypes.rs +++ b/television/channels/prototypes.rs @@ -348,11 +348,17 @@ pub struct PreviewSpec { pub command: CommandSpec, #[serde(default)] pub offset: Option