Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ chrono = "0.4.38"
ansi-to-tui = "6.0.0"
which = "6.0.3"
ureq = { version = "=3.0.0-rc2", features = ["rustls"] }
tokio = { version = "1.43.0", features = ["full"] }

# The profile that 'cargo dist' will build with
[profile.dist]
Expand Down
66 changes: 37 additions & 29 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::{
log_on_error,
logger::Logger,
ui::popup::{info_popup::InfoPopUp, PopUp},
};
use ansi_to_tui::IntoText;
use color_eyre::eyre::bail;
use config::Config;
use cover_renderer::render_cover;
use logging::Logger;
use patch_hub::lore::{
lore_api_client::BlockingLoreAPIClient,
lore_session,
Expand All @@ -28,7 +27,6 @@ use crate::utils;

mod config;
pub mod cover_renderer;
pub mod logging;
pub mod patch_renderer;
pub mod screens;

Expand All @@ -54,6 +52,8 @@ pub struct App {
/// Client to handle Lore API requests and responses
pub lore_api_client: BlockingLoreAPIClient,
pub popup: Option<Box<dyn PopUp>>,
/// The logger actor instance that the application will use when logging
pub logger: Logger,
}

impl App {
Expand All @@ -65,7 +65,7 @@ impl App {
/// # Returns
///
/// `App` instance with loading configurations and app data.
pub fn new() -> color_eyre::Result<Self> {
pub async fn new(logger: Logger) -> color_eyre::Result<Self> {
let config: Config = Config::build();
config.create_dirs();

Expand All @@ -83,9 +83,8 @@ impl App {
let lore_api_client = BlockingLoreAPIClient::default();

// Initialize the logger before the app starts
Logger::init_log_file(&config)?;
Logger::info("patch-hub started");
logging::garbage_collector::collect_garbage(&config);
logger.info("patch-hub started");
logger.collect_garbage().await;

Ok(App {
current_screen: CurrentScreen::MailingListSelection,
Expand All @@ -108,7 +107,8 @@ impl App {
config,
lore_api_client,
popup: None,
})
logger,
}
}

/// Initializes field [App::latest_patchsets], from currently selected
Expand Down Expand Up @@ -163,15 +163,19 @@ impl App {
screen => bail!(format!("Invalid screen passed as argument {screen:?}")),
};

let patchset_path: String = match log_on_error!(lore_session::download_patchset(
self.config.patchsets_cache_dir(),
&representative_patch,
)) {
Ok(result) => result,
Err(io_error) => bail!("{io_error}"),
};
let patchset_path: String =
match self.logger.error_on_error(lore_session::download_patchset(
self.config.patchsets_cache_dir(),
&representative_patch,
)) {
Ok(result) => result,
Err(io_error) => bail!("{io_error}"),
};

match log_on_error!(lore_session::split_patchset(&patchset_path)) {
match self
.logger
.error_on_error(lore_session::split_patchset(&patchset_path))
{
Ok(raw_patches) => {
let mut patches_preview: Vec<Text> = Vec::new();
for raw_patch in &raw_patches {
Expand Down Expand Up @@ -209,19 +213,21 @@ impl App {
let rendered_cover = match render_cover(raw_cover, self.config.cover_renderer())
{
Ok(render) => render,
Err(_) => {
Logger::error("Failed to render cover preview with external program");
Err(e) => {
self.logger
.error("Failed to render cover preview with external program");
self.logger.error(e);
raw_cover.to_string()
}
};

let rendered_patch =
match render_patch_preview(raw_patch, self.config.patch_renderer()) {
Ok(render) => render,
Err(_) => {
Logger::error(
"Failed to render patch preview with external program",
);
Err(e) => {
self.logger
.error("Failed to render patch preview with external program");
self.logger.error(e);
raw_patch.to_string()
}
};
Expand Down Expand Up @@ -395,30 +401,32 @@ impl App {
let mut app_can_run = true;

if !utils::binary_exists("b4") {
Logger::error("b4 is not installed, patchsets cannot be downloaded");
self.logger
.error("b4 is not installed, patchsets cannot be downloaded");
app_can_run = false;
}

if !utils::binary_exists("git") {
Logger::warn("git is not installed, send-email won't work");
self.logger
.warn("git is not installed, send-email won't work");
}

match self.config.patch_renderer() {
PatchRenderer::Bat => {
if !utils::binary_exists("bat") {
Logger::warn("bat is not installed, patch rendering will fallback to default");
self.logger
.warn("bat is not installed, patch rendering will fallback to default");
}
}
PatchRenderer::Delta => {
if !utils::binary_exists("delta") {
Logger::warn(
"delta is not installed, patch rendering will fallback to default",
);
self.logger
.warn("delta is not installed, patch rendering will fallback to default");
}
}
PatchRenderer::DiffSoFancy => {
if !utils::binary_exists("diff-so-fancy") {
Logger::warn(
self.logger.warn(
"diff-so-fancy is not installed, patch rendering will fallback to default",
);
}
Expand Down
8 changes: 2 additions & 6 deletions src/app/cover_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use std::{
process::{Command, Stdio},
};

use color_eyre::eyre::Context;
use serde::{Deserialize, Serialize};

use super::logging::Logger;

#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default)]
pub enum CoverRenderer {
#[default]
Expand Down Expand Up @@ -67,10 +66,7 @@ fn bat_cover_renderer(patch: &str) -> color_eyre::Result<String> {
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.map_err(|e| {
Logger::error(format!("Failed to spawn bat for cover preview: {}", e));
e
})?;
.context("Failed to spawn bat for cover preview")?;

bat.stdin.as_mut().unwrap().write_all(patch.as_bytes())?;
let output = bat.wait_with_output()?;
Expand Down
50 changes: 0 additions & 50 deletions src/app/logging/garbage_collector.rs

This file was deleted.

27 changes: 0 additions & 27 deletions src/app/logging/log_on_error.rs

This file was deleted.

Loading
Loading