Directly write to stdout when rendering jiradoc

This commit is contained in:
Daan Boerlage 2025-01-22 03:53:28 +01:00
parent c2f1fd239b
commit 8ce7882b38
Signed by: daan
GPG key ID: FCE070E1E4956606
2 changed files with 15 additions and 10 deletions

View file

@ -66,7 +66,7 @@ fn pretty_print(config: &JiraConfig, issue: &JiraIssue) -> Result<(), Box<dyn st
println!("\n== Description {:=<65}", "");
match issue.fields.description.clone() {
Some(x) => match x {
Description::Doc(doc) => println!("{}", render_doc(doc)?),
Description::Doc(doc) => render_doc(doc)?,
},
None => println!("(Issue does not have a description)"),
}
@ -78,7 +78,7 @@ fn pretty_print(config: &JiraConfig, issue: &JiraIssue) -> Result<(), Box<dyn st
comment.author.display_name.red(),
comment.created.with_timezone(&chrono::Local)
);
println!("{}", render_doc(comment.body)?);
render_doc(comment.body)?;
}
println!("\n== Actions {:=<69}", "");
println!(

View file

@ -1,8 +1,9 @@
use crossterm::style::Stylize;
use libjirac::entities::doc::{Doc, DocMedia, DocNode, DocTextMarker, DocTextNode};
use std::fmt::Write;
use std::io::Write;
use std::io::{stdout, StdoutLock};
fn render_doc_node(f: &mut String, node: &DocNode) -> Result<(), Box<dyn std::error::Error>> {
fn render_doc_node(f: &mut StdoutLock, node: &DocNode) -> Result<(), Box<dyn std::error::Error>> {
match node {
DocNode::Paragraph(x) => {
for node in x {
@ -66,7 +67,10 @@ fn render_doc_node(f: &mut String, node: &DocNode) -> Result<(), Box<dyn std::er
Ok(())
}
fn render_text_node(f: &mut String, node: &DocTextNode) -> Result<(), Box<dyn std::error::Error>> {
fn render_text_node(
f: &mut StdoutLock,
node: &DocTextNode,
) -> Result<(), Box<dyn std::error::Error>> {
match node {
DocTextNode::Text(x) => {
write!(f, "{}", x.text)?;
@ -86,7 +90,7 @@ fn render_text_node(f: &mut String, node: &DocTextNode) -> Result<(), Box<dyn st
}
fn render_text_markets(
f: &mut String,
f: &mut StdoutLock,
node: &DocTextMarker,
) -> Result<(), Box<dyn std::error::Error>> {
match node {
@ -107,7 +111,7 @@ fn render_text_markets(
Ok(())
}
fn render_media(f: &mut String, node: &DocMedia) -> Result<(), Box<dyn std::error::Error>> {
fn render_media(f: &mut StdoutLock, node: &DocMedia) -> Result<(), Box<dyn std::error::Error>> {
match node {
DocMedia::File(x) => {
writeln!(f, "[file:{}:{}]", x.id, x.alt)?;
@ -117,12 +121,13 @@ fn render_media(f: &mut String, node: &DocMedia) -> Result<(), Box<dyn std::erro
Ok(())
}
pub fn render_doc(doc: Doc) -> Result<String, Box<dyn std::error::Error>> {
let mut f = String::new();
pub fn render_doc(doc: Doc) -> Result<(), Box<dyn std::error::Error>> {
let out = stdout();
let mut f = out.lock();
for node in doc.content {
render_doc_node(&mut f, &node)?;
}
Ok(f)
Ok(())
}