fix(preview): move preview config logic out of the channel

This commit is contained in:
lalvarezt 2025-07-28 13:09:53 +02:00
parent e5303a3052
commit acba4b5f11
2 changed files with 41 additions and 18 deletions

View File

@ -256,20 +256,10 @@ impl Channel {
let item = self.matcher.get_result(index);
if let Some(item) = item {
let mut entry = Entry::new(item.inner.clone())
let entry = Entry::new(item.inner.clone())
.with_display(item.matched_string)
.with_match_indices(&item.match_indices);
if let Some(p) = &self.prototype.preview {
// FIXME: this should be done by the previewer instead
if let Some(offset_expr) = &p.offset {
let offset_str =
offset_expr.format(&item.inner).unwrap_or_default();
entry = entry.with_line_number(
offset_str.parse::<usize>().unwrap_or(0),
);
}
}
.with_match_indices(&item.match_indices)
.ansi(self.prototype.source.ansi);
Some(entry)
} else {
None

View File

@ -364,12 +364,45 @@ impl Television {
}
pub fn get_selected_entry(&mut self) -> Option<Entry> {
if self.channel.result_count() == 0 {
return None;
match self.mode {
Mode::Channel => {
let entry = self
.results_picker
.selected()
.and_then(|idx| self.results_picker.entries.get(idx))
.cloned()?;
Some(self.apply_preview_offset(entry))
}
Mode::RemoteControl => {
if self
.remote_control
.as_ref()
.map_or(0, RemoteControl::result_count)
== 0
{
return None;
}
let entry = self
.selected_index()
.map(|idx| self.channel.get_result(idx))
.and_then(|entry| entry)?;
Some(self.apply_preview_offset(entry))
}
}
self.selected_index()
.map(|idx| self.channel.get_result(idx))
.and_then(|entry| entry)
}
/// Apply preview offset logic to an entry
fn apply_preview_offset(&self, mut entry: Entry) -> Entry {
if let Some(p) = &self.channel.prototype.preview {
if let Some(offset_expr) = &p.offset {
let offset_str =
offset_expr.format(&entry.raw).unwrap_or_default();
entry = entry.with_line_number(
offset_str.parse::<usize>().unwrap_or(0),
);
}
}
entry
}
pub fn get_selected_cable_entry(&mut self) -> Option<CableEntry> {