-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cable): add support for custom channels (#75)
- Loading branch information
1 parent
fee4ed2
commit a5f5d20
Showing
37 changed files
with
1,490 additions
and
755 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::{ | ||
collections::HashMap, | ||
fmt::{self, Display, Formatter}, | ||
ops::Deref, | ||
}; | ||
|
||
#[derive(Clone, Debug, serde::Deserialize)] | ||
pub struct CableChannelPrototype { | ||
pub name: String, | ||
pub source_command: String, | ||
pub preview_command: String, | ||
#[serde(default = "default_delimiter")] | ||
pub preview_delimiter: String, | ||
} | ||
|
||
const DEFAULT_DELIMITER: &str = " "; | ||
|
||
fn default_delimiter() -> String { | ||
DEFAULT_DELIMITER.to_string() | ||
} | ||
|
||
impl Display for CableChannelPrototype { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
write!(f, "{}", self.name) | ||
} | ||
} | ||
|
||
#[derive(Debug, serde::Deserialize, Default)] | ||
pub struct CableChannels(pub HashMap<String, CableChannelPrototype>); | ||
|
||
impl Deref for CableChannels { | ||
type Target = HashMap<String, CableChannelPrototype>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use crate::cable::CableChannelPrototype; | ||
use crate::channels::OnAir; | ||
use crate::entry::{Entry, PreviewCommand, PreviewType}; | ||
use television_fuzzy::{ | ||
matcher::{config::Config, injector::Injector}, | ||
Matcher, | ||
}; | ||
|
||
#[allow(dead_code)] | ||
pub struct Channel { | ||
name: String, | ||
matcher: Matcher<String>, | ||
entries_command: String, | ||
preview_command: PreviewCommand, | ||
} | ||
|
||
impl Default for Channel { | ||
fn default() -> Self { | ||
Self::new( | ||
"Files", | ||
"find . -type f", | ||
PreviewCommand::new("bat -n --color=always {}", ":"), | ||
) | ||
} | ||
} | ||
|
||
impl From<CableChannelPrototype> for Channel { | ||
fn from(prototype: CableChannelPrototype) -> Self { | ||
Self::new( | ||
&prototype.name, | ||
&prototype.source_command, | ||
PreviewCommand::new( | ||
&prototype.preview_command, | ||
&prototype.preview_delimiter, | ||
), | ||
) | ||
} | ||
} | ||
|
||
impl Channel { | ||
pub fn new( | ||
name: &str, | ||
entries_command: &str, | ||
preview_command: PreviewCommand, | ||
) -> Self { | ||
let matcher = Matcher::new(Config::default()); | ||
let injector = matcher.injector(); | ||
tokio::spawn(load_candidates(entries_command.to_string(), injector)); | ||
Self { | ||
matcher, | ||
entries_command: entries_command.to_string(), | ||
preview_command, | ||
name: name.to_string(), | ||
} | ||
} | ||
} | ||
|
||
#[allow(clippy::unused_async)] | ||
async fn load_candidates(command: String, injector: Injector<String>) { | ||
let output = std::process::Command::new("sh") | ||
.arg("-c") | ||
.arg(command) | ||
.output() | ||
.expect("failed to execute process"); | ||
|
||
let branches = String::from_utf8(output.stdout).unwrap(); | ||
|
||
for line in branches.lines() { | ||
let () = injector.push(line.to_string(), |e, cols| { | ||
cols[0] = e.clone().into(); | ||
}); | ||
} | ||
} | ||
|
||
impl OnAir for Channel { | ||
fn find(&mut self, pattern: &str) { | ||
self.matcher.find(pattern); | ||
} | ||
|
||
fn results(&mut self, num_entries: u32, offset: u32) -> Vec<Entry> { | ||
self.matcher.tick(); | ||
self.matcher | ||
.results(num_entries, offset) | ||
.into_iter() | ||
.map(|item| { | ||
let path = item.matched_string; | ||
Entry::new( | ||
path.clone(), | ||
PreviewType::Command(self.preview_command.clone()), | ||
) | ||
.with_name_match_ranges(item.match_indices) | ||
}) | ||
.collect() | ||
} | ||
|
||
fn get_result(&self, index: u32) -> Option<Entry> { | ||
self.matcher.get_result(index).map(|item| { | ||
let path = item.matched_string; | ||
Entry::new( | ||
path.clone(), | ||
PreviewType::Command(self.preview_command.clone()), | ||
) | ||
}) | ||
} | ||
|
||
fn result_count(&self) -> u32 { | ||
self.matcher.matched_item_count | ||
} | ||
|
||
fn total_count(&self) -> u32 { | ||
self.matcher.total_item_count | ||
} | ||
|
||
fn running(&self) -> bool { | ||
self.matcher.status.running | ||
} | ||
|
||
fn shutdown(&self) {} | ||
} |
Oops, something went wrong.