feat(cable): allow custom cable channels to override builtins (#260)

This commit is contained in:
Alex Pasmantier 2025-01-09 14:24:37 +01:00 committed by GitHub
parent b388a56745
commit d9ca7b1f9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 18 deletions

View File

@ -73,22 +73,21 @@ impl RemoteControl {
}
pub fn zap(&self, channel_name: &str) -> Result<TelevisionChannel> {
if let Ok(channel) = UnitChannel::try_from(channel_name) {
Ok(channel.into())
} else {
let maybe_prototype = self
.cable_channels
.as_ref()
.and_then(|channels| channels.get(channel_name));
match maybe_prototype {
Some(prototype) => Ok(TelevisionChannel::Cable(
cable::Channel::from(prototype.clone()),
)),
None => Err(color_eyre::eyre::eyre!(
match self
.cable_channels
.as_ref()
.and_then(|channels| channels.get(channel_name).cloned())
{
Some(prototype) => Ok(TelevisionChannel::Cable(
cable::Channel::from(prototype.clone()),
)),
None => match UnitChannel::try_from(channel_name) {
Ok(channel) => Ok(channel.into()),
Err(_) => Err(color_eyre::eyre::eyre!(
"No channel or cable channel prototype found for {}",
channel_name
)),
}
},
}
}
}
@ -105,10 +104,21 @@ impl Default for RemoteControl {
}
}
pub fn load_builtin_channels() -> Vec<UnitChannel> {
CliTvChannel::value_variants()
pub fn load_builtin_channels(
filter_out_cable_names: Option<&[&String]>,
) -> Vec<UnitChannel> {
let mut value_variants = CliTvChannel::value_variants()
.iter()
.flat_map(|v| UnitChannel::try_from(v.to_string().as_str()))
.map(std::string::ToString::to_string)
.collect::<Vec<_>>();
if let Some(f) = filter_out_cable_names {
value_variants.retain(|v| !f.iter().any(|c| *c == v));
}
value_variants
.iter()
.flat_map(|v| UnitChannel::try_from(v.as_str()))
.collect()
}

View File

@ -68,8 +68,11 @@ impl Television {
}
let previewer = Previewer::new(Some(config.previewers.clone().into()));
let keymap = Keymap::from(&config.keybindings);
let builtin_channels = load_builtin_channels();
let cable_channels = load_cable_channels().unwrap_or_default();
let builtin_channels = load_builtin_channels(Some(
&cable_channels.keys().collect::<Vec<_>>(),
));
let app_metadata = AppMetadata::new(
env!("CARGO_PKG_VERSION").to_string(),
BuildMetadata::new(
@ -115,8 +118,10 @@ impl Television {
}
pub fn init_remote_control(&mut self) {
let builtin_channels = load_builtin_channels();
let cable_channels = load_cable_channels().unwrap_or_default();
let builtin_channels = load_builtin_channels(Some(
&cable_channels.keys().collect::<Vec<_>>(),
));
self.remote_control = TelevisionChannel::RemoteControl(
RemoteControl::new(builtin_channels, Some(cable_channels)),
);