Skip to content

Commit

Permalink
feat(cable): allow custom cable channels to override builtins (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier authored Jan 9, 2025
1 parent b388a56 commit d9ca7b1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
42 changes: 26 additions & 16 deletions crates/television-channels/src/channels/remote_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)),
}
},
}
}
}
Expand All @@ -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()
.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.to_string().as_str()))
.flat_map(|v| UnitChannel::try_from(v.as_str()))
.collect()
}

Expand Down
9 changes: 7 additions & 2 deletions crates/television/television.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)),
);
Expand Down

0 comments on commit d9ca7b1

Please sign in to comment.