rcom/src/main.rs

71 lines
2.4 KiB
Rust

use std::sync::Arc;
use clap::Parser;
use directories::UserDirs;
use rcom::cli::Cli;
use rcom::config::{config_path, load_from_path};
use rcom::fs::{BackendRegistry, LocalFs};
use rcom::image_preview::{ImageDetectionGates, ImageEngine, ImageRenderContext};
use rcom::session::{Application, initial_location};
use rcom::ssh::{ConnectionManager, DiscoveryResult, OpenSshConnector, discover};
use rcom::tui::run_event_loop;
use rcom::ui::terminal::TerminalGuard;
use rcom::ui::{Theme, ThemeId, render_with_context};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
if cli.list_themes {
for id in ThemeId::ALL {
println!(
"{}\t{}\t{}",
id.as_str(),
id.display_name(),
id.mode().as_str()
);
}
return Ok(());
}
let config = load_from_path(&config_path())?;
let images = config.images_enabled(cli.no_images);
let theme = Theme::from_env_with_appearance(
config.selected_icon_mode(cli.icons),
config.selected_theme(cli.theme),
);
let initial = initial_location(cli.path).await?;
let discovery = ssh_discovery();
let registry = BackendRegistry::new(Arc::new(LocalFs::new()));
let connections = ConnectionManager::new(Arc::new(OpenSshConnector::default()), registry);
let (mut application, mut results) = Application::new(initial, connections, discovery);
application.configure_theme(theme, config_path());
let mut terminal = TerminalGuard::enter()?;
application.configure_images(ImageEngine::detect(ImageDetectionGates::from_environment(
images,
theme.colors_enabled(),
theme.is_ascii(),
)));
let image_cache = application.image_cache();
let image_factory = application.image_factory();
run_event_loop(
&mut application,
&mut results,
&mut terminal,
|frame, state, picker| {
let cache = image_cache.lock().unwrap();
let context = image_factory.as_ref().map(|factory| ImageRenderContext {
cache: &cache,
factory,
});
render_with_context(frame, state, picker, &state.theme, context);
},
)
.await
}
fn ssh_discovery() -> DiscoveryResult {
UserDirs::new().map_or_else(DiscoveryResult::default, |directories| {
let home = directories.home_dir();
discover(&home.join(".ssh").join("config"), home)
})
}