diff --git a/television/channels/cable.rs b/television/channels/cable.rs index a64140d..af5b60b 100644 --- a/television/channels/cable.rs +++ b/television/channels/cable.rs @@ -152,6 +152,7 @@ async fn load_candidates( if let Ok(l) = line { if !l.trim().is_empty() { let () = injector.push(l, |e, cols| { + // PERF: maybe we can avoid cloning here by using &Utf32Str cols[0] = e.clone().into(); }); produced_output = true; diff --git a/television/channels/prototypes.rs b/television/channels/prototypes.rs index d636baf..f0ecba2 100644 --- a/television/channels/prototypes.rs +++ b/television/channels/prototypes.rs @@ -95,6 +95,19 @@ impl Display for ChannelPrototype { pub static CMD_RE: &Lazy = regex!(r"\{(\d+)\}"); +/// Formats a prototype string with the given template and source strings. +/// +/// # Example +/// ``` +/// use television::channels::prototypes::format_prototype_string; +/// +/// let template = "cat {} {1}"; +/// let source = "foo:bar:baz"; +/// let delimiter = ":"; +/// +/// let formatted = format_prototype_string(template, source, delimiter); +/// assert_eq!(formatted, "cat 'foo:bar:baz' 'bar'"); +/// ``` pub fn format_prototype_string( template: &str, source: &str, @@ -108,8 +121,9 @@ pub fn format_prototype_string( formatted_string = CMD_RE .replace_all(&formatted_string, |caps: ®ex::Captures| { let index = + // these unwraps are safe because of the regex pattern caps.get(1).unwrap().as_str().parse::().unwrap(); - format!("'{}'", parts[index]) + format!("'{}'", parts.get(index).unwrap_or(&"")) }) .to_string(); diff --git a/television/previewer/mod.rs b/television/previewer/mod.rs index 96c93ae..afbf32c 100644 --- a/television/previewer/mod.rs +++ b/television/previewer/mod.rs @@ -131,10 +131,11 @@ impl Preview { pub struct Previewer { config: Config, + // FIXME: maybe use a bounded channel here with a single slot requests: UnboundedReceiver, last_job_entry: Option, preview_command: PreviewCommand, - previews: UnboundedSender, + results: UnboundedSender, } impl Previewer { @@ -149,7 +150,7 @@ impl Previewer { requests: receiver, last_job_entry: None, preview_command, - previews: sender, + results: sender, } } @@ -166,7 +167,7 @@ impl Previewer { debug!("Preview request is stale, skipping"); continue; } - let notify = self.previews.clone(); + let results_handle = self.results.clone(); let command = self.preview_command.format_with(&ticket.entry); self.last_job_entry = Some(ticket.entry.clone()); @@ -174,7 +175,11 @@ impl Previewer { match timeout( self.config.job_timeout, tokio::spawn(async move { - try_preview(&command, &ticket.entry, ¬ify); + try_preview( + &command, + &ticket.entry, + &results_handle, + ); }), ) .await @@ -203,7 +208,7 @@ impl Previewer { pub fn try_preview( command: &str, entry: &Entry, - notify: &UnboundedSender, + results_handle: &UnboundedSender, ) { debug!("Preview command: {}", command); @@ -241,7 +246,7 @@ pub fn try_preview( ) } }; - notify + results_handle .send(preview) .expect("Unable to send preview result to main thread."); }