-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This uses similar design for patch rendering. The cover of a patch is everything before the `---` line that separates the email part of a patch and the actual diff. Currently, only `bat` was configured as an option besides the `default` This also includes a config option for the cover renderer Closes: #71 Signed-off-by: OJarrisonn <j.h.m.t.v.10@gmail.com> Reviewed-by: David Tadokoro <davidbtadokoro@usp.br> Signed-off-by: David Tadokoro <davidbtadokoro@usp.br>
- Loading branch information
1 parent
b73e85a
commit 48d87bc
Showing
6 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
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
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,78 @@ | ||
use std::{ | ||
fmt::Display, | ||
io::Write, | ||
process::{Command, Stdio}, | ||
}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::logging::Logger; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default)] | ||
pub enum CoverRenderer { | ||
#[default] | ||
#[serde(rename = "default")] | ||
Default, | ||
#[serde(rename = "bat")] | ||
Bat, | ||
} | ||
|
||
impl From<String> for CoverRenderer { | ||
fn from(value: String) -> Self { | ||
match value.as_str() { | ||
"bat" => CoverRenderer::Bat, | ||
_ => CoverRenderer::Default, | ||
} | ||
} | ||
} | ||
|
||
impl From<&str> for CoverRenderer { | ||
fn from(value: &str) -> Self { | ||
match value { | ||
"bat" => CoverRenderer::Bat, | ||
_ => CoverRenderer::Default, | ||
} | ||
} | ||
} | ||
|
||
impl Display for CoverRenderer { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
CoverRenderer::Default => write!(f, "default"), | ||
CoverRenderer::Bat => write!(f, "bat"), | ||
} | ||
} | ||
} | ||
|
||
pub fn render_cover(raw: &str, renderer: &CoverRenderer) -> color_eyre::Result<String> { | ||
let text = match renderer { | ||
CoverRenderer::Default => Ok(raw.to_string()), | ||
CoverRenderer::Bat => bat_cover_renderer(raw), | ||
}?; | ||
|
||
Ok(text) | ||
} | ||
|
||
/// Renders a .mbx cover using the `bat` command line tool. | ||
/// | ||
/// # Errors | ||
/// | ||
/// If bat isn't installed or if the command fails, an error will be returned. | ||
fn bat_cover_renderer(patch: &str) -> color_eyre::Result<String> { | ||
let mut bat = Command::new("bat") | ||
.arg("-pp") | ||
.arg("-f") | ||
.arg("-l") | ||
.arg("mbx") | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::piped()) | ||
.spawn() | ||
.map_err(|e| { | ||
Logger::error(format!("Failed to spawn bat for cover preview: {}", e)); | ||
e | ||
})?; | ||
|
||
bat.stdin.as_mut().unwrap().write_all(patch.as_bytes())?; | ||
let output = bat.wait_with_output()?; | ||
Ok(String::from_utf8(output.stdout)?) | ||
} |
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
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