Skip to content

Commit

Permalink
new things
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Oct 20, 2024
1 parent 10f3025 commit 5556515
Show file tree
Hide file tree
Showing 23 changed files with 521 additions and 280 deletions.
5 changes: 3 additions & 2 deletions .config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ ctrl-d = "ScrollPreviewHalfPageDown"
alt-up = "ScrollPreviewHalfPageUp"
ctrl-u = "ScrollPreviewHalfPageUp"
enter = "SelectEntry"
ctrl-s = "ToChannelSelection"
ctrl-enter = "SendToChannel"
ctrl-s = "ToggleChannelSelection"

[keybindings.ChannelSelection]
esc = "Quit"
Expand All @@ -18,5 +19,5 @@ up = "SelectPrevEntry"
ctrl-n = "SelectNextEntry"
ctrl-p = "SelectPrevEntry"
enter = "SelectEntry"
ctrl-enter = "PipeInto"
ctrl-s = "ToggleChannelSelection"

4 changes: 2 additions & 2 deletions crates/television/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ pub enum Action {
Error(String),
NoOp,
// channel actions
ToChannelSelection,
PipeInto,
ToggleChannelSelection,
SendToChannel,
}
5 changes: 3 additions & 2 deletions crates/television/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@
*/
use std::sync::Arc;


use color_eyre::Result;
use tokio::sync::{mpsc, Mutex};
use tracing::{debug, info};

use crate::channels::{AvailableChannel, CliTvChannel};
use crate::channels::{TelevisionChannel, CliTvChannel};
use crate::television::Television;
use crate::{
action::Action,
Expand Down Expand Up @@ -84,7 +85,7 @@ pub struct App {

impl App {
pub fn new(
channel: AvailableChannel,
channel: TelevisionChannel,
tick_rate: f64,
frame_rate: f64,
) -> Result<Self> {
Expand Down
27 changes: 15 additions & 12 deletions crates/television/channels.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::entry::Entry;
use color_eyre::eyre::Result;
use television_derive::{CliChannel, TvChannel};
use television_derive::{CliChannel, Broadcast, UnitChannel};

mod alias;
pub mod channels;
Expand All @@ -13,7 +13,7 @@ mod text;
/// The interface that all television channels must implement.
///
/// # Important
/// The `TelevisionChannel` requires the `Send` trait to be implemented as
/// The `OnAir` requires the `Send` trait to be implemented as
/// well. This is necessary to allow the channels to be used in a
/// multithreaded environment.
///
Expand Down Expand Up @@ -47,7 +47,7 @@ mod text;
/// fn total_count(&self) -> u32;
/// ```
///
pub trait TelevisionChannel: Send {
pub trait OnAir: Send {
/// Find entries that match the given pattern.
///
/// This method does not return anything and instead typically stores the
Expand All @@ -70,6 +70,9 @@ pub trait TelevisionChannel: Send {

/// Check if the channel is currently running.
fn running(&self) -> bool;

/// Turn off
fn shutdown(&self);
}

/// The available television channels.
Expand All @@ -89,8 +92,8 @@ pub trait TelevisionChannel: Send {
/// instance from the selected CLI enum variant.
///
#[allow(dead_code, clippy::module_name_repetitions)]
#[derive(CliChannel, TvChannel)]
pub enum AvailableChannel {
#[derive(UnitChannel, CliChannel, Broadcast)]
pub enum TelevisionChannel {
Env(env::Channel),
Files(files::Channel),
GitRepos(git_repos::Channel),
Expand All @@ -101,19 +104,19 @@ pub enum AvailableChannel {
}

/// NOTE: this could be generated by a derive macro
impl TryFrom<&Entry> for AvailableChannel {
impl TryFrom<&Entry> for TelevisionChannel {
type Error = String;

fn try_from(entry: &Entry) -> Result<Self, Self::Error> {
match entry.name.to_ascii_lowercase().as_ref() {
"env" => Ok(AvailableChannel::Env(env::Channel::default())),
"files" => Ok(AvailableChannel::Files(files::Channel::default())),
"env" => Ok(TelevisionChannel::Env(env::Channel::default())),
"files" => Ok(TelevisionChannel::Files(files::Channel::default())),
"gitrepos" => {
Ok(AvailableChannel::GitRepos(git_repos::Channel::default()))
Ok(TelevisionChannel::GitRepos(git_repos::Channel::default()))
}
"text" => Ok(AvailableChannel::Text(text::Channel::default())),
"stdin" => Ok(AvailableChannel::Stdin(stdin::Channel::default())),
"alias" => Ok(AvailableChannel::Alias(alias::Channel::default())),
"text" => Ok(TelevisionChannel::Text(text::Channel::default())),
"stdin" => Ok(TelevisionChannel::Stdin(stdin::Channel::default())),
"alias" => Ok(TelevisionChannel::Alias(alias::Channel::default())),
_ => Err(format!("Unknown channel: {}", entry.name)),
}
}
Expand Down
8 changes: 6 additions & 2 deletions crates/television/channels/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use devicons::FileIcon;
use nucleo::{Config, Injector, Nucleo};
use tracing::debug;

use crate::channels::TelevisionChannel;
use crate::channels::OnAir;
use crate::entry::Entry;
use crate::fuzzy::MATCHER;
use crate::previewers::PreviewType;
Expand Down Expand Up @@ -102,7 +102,7 @@ impl Default for Channel {
}
}

impl TelevisionChannel for Channel {
impl OnAir for Channel {
fn find(&mut self, pattern: &str) {
if pattern != self.last_pattern {
self.matcher.pattern.reparse(
Expand Down Expand Up @@ -198,6 +198,10 @@ impl TelevisionChannel for Channel {
fn running(&self) -> bool {
self.running
}

fn shutdown(&self) {
todo!()
}
}

#[allow(clippy::unused_async)]
Expand Down
8 changes: 6 additions & 2 deletions crates/television/channels/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use nucleo::{
};

use crate::{
channels::{AvailableChannel, CliTvChannel, TelevisionChannel},
channels::{CliTvChannel, OnAir},
entry::Entry,
fuzzy::MATCHER,
previewers::PreviewType,
Expand Down Expand Up @@ -61,7 +61,7 @@ const TV_ICON: FileIcon = FileIcon {
color: "#ffffff",
};

impl TelevisionChannel for SelectionChannel {
impl OnAir for SelectionChannel {
fn find(&mut self, pattern: &str) {
if pattern != self.last_pattern {
self.matcher.pattern.reparse(
Expand Down Expand Up @@ -133,4 +133,8 @@ impl TelevisionChannel for SelectionChannel {
fn running(&self) -> bool {
self.running
}

fn shutdown(&self) {
todo!()
}
}
8 changes: 6 additions & 2 deletions crates/television/channels/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nucleo::{
};
use std::sync::Arc;

use super::TelevisionChannel;
use super::OnAir;
use crate::entry::Entry;
use crate::fuzzy::MATCHER;
use crate::previewers::PreviewType;
Expand Down Expand Up @@ -62,7 +62,7 @@ impl Default for Channel {
}
}

impl TelevisionChannel for Channel {
impl OnAir for Channel {
fn find(&mut self, pattern: &str) {
if pattern != self.last_pattern {
self.matcher.pattern.reparse(
Expand Down Expand Up @@ -160,4 +160,8 @@ impl TelevisionChannel for Channel {
fn running(&self) -> bool {
self.running
}

fn shutdown(&self) {
todo!()
}
}
8 changes: 6 additions & 2 deletions crates/television/channels/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{

use ignore::DirEntry;

use super::TelevisionChannel;
use super::OnAir;
use crate::previewers::PreviewType;
use crate::utils::files::{walk_builder, DEFAULT_NUM_THREADS};
use crate::{
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Default for Channel {
}
}

impl TelevisionChannel for Channel {
impl OnAir for Channel {
fn find(&mut self, pattern: &str) {
if pattern != self.last_pattern {
self.matcher.pattern.reparse(
Expand Down Expand Up @@ -131,6 +131,10 @@ impl TelevisionChannel for Channel {
fn running(&self) -> bool {
self.running
}

fn shutdown(&self) {
todo!()
}
}

#[allow(clippy::unused_async)]
Expand Down
37 changes: 26 additions & 11 deletions crates/television/channels/git_repos.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::sync::Arc;

use color_eyre::owo_colors::OwoColorize;
use devicons::FileIcon;
use ignore::{overrides::OverrideBuilder, DirEntry};
use nucleo::{
pattern::{CaseMatching, Normalization},
Config, Nucleo,
};
use tokio::sync::{oneshot, watch};
use tracing::debug;

use crate::{
Expand All @@ -15,7 +16,7 @@ use crate::{
utils::files::{walk_builder, DEFAULT_NUM_THREADS},
};

use crate::channels::TelevisionChannel;
use crate::channels::OnAir;

pub struct Channel {
matcher: Nucleo<DirEntry>,
Expand All @@ -24,6 +25,7 @@ pub struct Channel {
total_count: u32,
running: bool,
icon: FileIcon,
crawl_cancellation_tx: watch::Sender<bool>,
}

impl Channel {
Expand All @@ -35,9 +37,11 @@ impl Channel {
1,
);
// start loading files in the background
let (tx, rx) = watch::channel(false);
tokio::spawn(crawl_for_repos(
std::env::home_dir().expect("Could not get home directory"),
matcher.injector(),
rx,
));
Channel {
matcher,
Expand All @@ -46,6 +50,7 @@ impl Channel {
total_count: 0,
running: false,
icon: FileIcon::from("git"),
crawl_cancellation_tx: tx,
}
}

Expand All @@ -58,7 +63,7 @@ impl Default for Channel {
}
}

impl TelevisionChannel for Channel {
impl OnAir for Channel {
fn find(&mut self, pattern: &str) {
if pattern != self.last_pattern {
self.matcher.pattern.reparse(
Expand All @@ -72,14 +77,6 @@ impl TelevisionChannel for Channel {
}
}

fn result_count(&self) -> u32 {
self.result_count
}

fn total_count(&self) -> u32 {
self.total_count
}

fn results(&mut self, num_entries: u32, offset: u32) -> Vec<Entry> {
let status = self.matcher.tick(Self::MATCHER_TICK_TIMEOUT);
let snapshot = self.matcher.snapshot();
Expand Down Expand Up @@ -127,15 +124,28 @@ impl TelevisionChannel for Channel {
})
}

fn result_count(&self) -> u32 {
self.result_count
}

fn total_count(&self) -> u32 {
self.total_count
}

fn running(&self) -> bool {
self.running
}

fn shutdown(&self) {
self.crawl_cancellation_tx.send(true).unwrap();
}
}

#[allow(clippy::unused_async)]
async fn crawl_for_repos(
starting_point: std::path::PathBuf,
injector: nucleo::Injector<DirEntry>,
cancellation_rx: watch::Receiver<bool>,
) {
let mut walker_overrides_builder = OverrideBuilder::new(&starting_point);
walker_overrides_builder.add(".git").unwrap();
Expand All @@ -148,7 +158,12 @@ async fn crawl_for_repos(

walker.run(|| {
let injector = injector.clone();
let cancellation_rx = cancellation_rx.clone();
Box::new(move |result| {
if let Ok(true) = cancellation_rx.has_changed() {
debug!("Crawling for git repos cancelled");
return ignore::WalkState::Quit;
}
if let Ok(entry) = result {
if entry.file_type().unwrap().is_dir()
&& entry.path().ends_with(".git")
Expand Down
9 changes: 6 additions & 3 deletions crates/television/channels/stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use std::{io::BufRead, sync::Arc};

use devicons::FileIcon;
use nucleo::{Config, Nucleo};
use tracing::debug;

use crate::entry::Entry;
use crate::fuzzy::MATCHER;
use crate::previewers::PreviewType;

use super::TelevisionChannel;
use super::OnAir;

pub struct Channel {
matcher: Nucleo<String>,
Expand Down Expand Up @@ -59,7 +58,7 @@ impl Default for Channel {
}
}

impl TelevisionChannel for Channel {
impl OnAir for Channel {
// maybe this could be sort of automatic with a blanket impl (making Finder generic over
// its matcher type or something)
fn find(&mut self, pattern: &str) {
Expand Down Expand Up @@ -150,4 +149,8 @@ impl TelevisionChannel for Channel {
fn running(&self) -> bool {
self.running
}

fn shutdown(&self) {
todo!()
}
}
Loading

0 comments on commit 5556515

Please sign in to comment.