use colored::Colorize;
use std::io::{self, Write};
use crate::types::Summary;
pub struct Output {
quiet: bool,
json: bool,
}
impl Output {
pub const fn new(quiet: bool, json: bool) -> Self {
Self { quiet, json }
}
pub fn success(&self, msg: &str) {
if self.quiet {
return;
}
println!("{}", msg.green().bold());
}
pub fn warning(&self, msg: &str) {
if self.quiet {
return;
}
eprintln!("{}", msg.yellow().bold());
}
pub fn error(&self, msg: &str) {
let _ = self;
eprintln!("{}", msg.red().bold());
}
pub fn info(&self, msg: &str) {
if self.quiet {
return;
}
println!("{}", msg.cyan());
}
pub fn progress(&self, msg: &str) {
if self.quiet {
return;
}
print!("{}", msg.blue());
io::stdout().flush().ok();
}
pub fn println(&self, msg: &str) {
if self.quiet {
return;
}
println!("{msg}");
}
pub fn command(&self, cmd: &str) {
if self.quiet || self.json {
return;
}
println!("{}", format!("$ {cmd}").bright_magenta().bold());
}
#[allow(dead_code)]
pub const fn is_quiet(&self) -> bool {
self.quiet
}
#[allow(dead_code)]
pub const fn is_json(&self) -> bool {
self.json
}
}
fn show_staged_summary(summary: &Summary, output: &Output) {
if summary.files_staged == 0 {
return;
}
output.success(&format!(" Staged {} file(s)", summary.files_staged));
}
fn show_commits_summary(summary: &Summary, output: &Output) {
if summary.commits_created == 0 {
return;
}
output.success(&format!(
" Committed {} commit(s)",
summary.commits_created
));
}
fn show_pulled_summary(summary: &Summary, output: &Output) {
if summary.commits_pulled == 0 {
return;
}
output.success(&format!(" Pulled {} commit(s)", summary.commits_pulled));
}
fn show_push_summary(summary: &Summary, output: &Output) {
if !summary.pushed {
return;
}
output.success(" Pushed successfully");
}
fn show_stash_summary(summary: &Summary, output: &Output) {
if !summary.stashed {
return;
}
output.info(" Stashed and restored changes");
}
fn show_skipped_summary(summary: &Summary, output: &Output) {
if summary.skipped_stage {
output.warning(" Skipped staging (no changes)");
}
if summary.skipped_commit {
output.warning(" Skipped commit (nothing to commit)");
}
if summary.skipped_pull {
output.warning(" Skipped pull (already up to date)");
}
if summary.skipped_push {
output.warning(" Skipped push (nothing to push)");
}
}
fn output_json_summary(summary: &Summary) {
println!(
r#"{{"files_staged":{},"commits_created":{},"commits_pulled":{},"pushed":{},"stashed":{},"skipped":{{"stage":{},"commit":{},"pull":{},"push":{}}}}}"#,
summary.files_staged,
summary.commits_created,
summary.commits_pulled,
summary.pushed,
summary.stashed,
summary.skipped_stage,
summary.skipped_commit,
summary.skipped_pull,
summary.skipped_push
);
}
pub fn show_summary(summary: &Summary, output: &Output) {
if output.quiet {
return;
}
if output.json {
output_json_summary(summary);
return;
}
output.println("\nSummary:");
show_staged_summary(summary, output);
show_commits_summary(summary, output);
show_pulled_summary(summary, output);
show_push_summary(summary, output);
show_stash_summary(summary, output);
show_skipped_summary(summary, output);
}