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

This commit is contained in:
Alexandre Pasmantier 2025-01-09 14:18:50 +01:00
parent b388a56745
commit dc938dd62b
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> { pub fn zap(&self, channel_name: &str) -> Result<TelevisionChannel> {
if let Ok(channel) = UnitChannel::try_from(channel_name) { match self
Ok(channel.into()) .cable_channels
} else { .as_ref()
let maybe_prototype = self .and_then(|channels| channels.get(channel_name).cloned())
.cable_channels {
.as_ref() Some(prototype) => Ok(TelevisionChannel::Cable(
.and_then(|channels| channels.get(channel_name)); cable::Channel::from(prototype.clone()),
match maybe_prototype { )),
Some(prototype) => Ok(TelevisionChannel::Cable( None => match UnitChannel::try_from(channel_name) {
cable::Channel::from(prototype.clone()), Ok(channel) => Ok(channel.into()),
)), Err(_) => Err(color_eyre::eyre::eyre!(
None => Err(color_eyre::eyre::eyre!(
"No channel or cable channel prototype found for {}", "No channel or cable channel prototype found for {}",
channel_name channel_name
)), )),
} },
} }
} }
} }
@ -105,10 +104,21 @@ impl Default for RemoteControl {
} }
} }
pub fn load_builtin_channels() -> Vec<UnitChannel> { pub fn load_builtin_channels(
CliTvChannel::value_variants() filter_out_cable_names: Option<&[&String]>,
) -> Vec<UnitChannel> {
let mut value_variants = CliTvChannel::value_variants()
.iter() .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() .collect()
} }

View File

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