11use crate :: onefetch:: { ascii_art:: AsciiArt , error:: * , info:: Info } ;
22use colored:: Color ;
33use std:: io:: Write ;
4+ use strum:: { EnumIter , EnumString , IntoStaticStr } ;
45
56const CENTER_PAD_LENGTH : usize = 3 ;
67
8+ #[ derive( EnumString , EnumIter , IntoStaticStr ) ]
9+ #[ strum( serialize_all = "lowercase" ) ]
10+ pub enum SerializationFormat {
11+ Json ,
12+ Yaml ,
13+ }
14+
715pub struct Printer < W > {
816 writer : W ,
917 info : Info ,
@@ -15,64 +23,73 @@ impl<W: Write> Printer<W> {
1523 }
1624
1725 pub fn print ( & mut self ) -> Result < ( ) > {
18- let center_pad = " " . repeat ( CENTER_PAD_LENGTH ) ;
19- let info_str = format ! ( "{}" , & self . info) ;
20- let mut info_lines = info_str. lines ( ) ;
21- let colors: Vec < Color > = Vec :: new ( ) ;
22- let mut buf = String :: new ( ) ;
26+ match & self . info . config . output {
27+ Some ( format) => match format {
28+ SerializationFormat :: Json => {
29+ write ! ( self . writer, "{}" , serde_json:: to_string_pretty( & self . info) . unwrap( ) ) ?
30+ }
31+ SerializationFormat :: Yaml => {
32+ write ! ( self . writer, "{}" , serde_yaml:: to_string( & self . info) . unwrap( ) ) ?
33+ }
34+ } ,
35+ None => {
36+ let center_pad = " " . repeat ( CENTER_PAD_LENGTH ) ;
37+ let info_str = format ! ( "{}" , & self . info) ;
38+ let mut info_lines = info_str. lines ( ) ;
39+ let colors: Vec < Color > = Vec :: new ( ) ;
40+ let mut buf = String :: new ( ) ;
2341
24- if self . info . config . art_off {
25- buf. push_str ( & info_str) ;
26- } else if let Some ( custom_image) = & self . info . config . image {
27- buf. push_str (
28- & self
29- . info
30- . config
31- . image_backend
32- . as_ref ( )
33- . unwrap ( )
34- . add_image (
35- info_lines. map ( |s| format ! ( "{}{}" , center_pad, s) ) . collect ( ) ,
36- custom_image,
37- self . info . config . image_color_resolution ,
38- )
39- . chain_err ( || "Error while drawing image" ) ?,
40- ) ;
41- } else {
42- let mut logo_lines = if let Some ( custom_ascii) = & self . info . config . ascii_input {
43- AsciiArt :: new ( custom_ascii, & colors, !self . info . config . no_bold )
44- } else {
45- AsciiArt :: new ( self . get_ascii ( ) , & self . info . ascii_colors , !self . info . config . no_bold )
46- } ;
42+ if self . info . config . art_off {
43+ buf. push_str ( & info_str) ;
44+ } else if let Some ( custom_image) = & self . info . config . image {
45+ buf. push_str (
46+ & self
47+ . info
48+ . config
49+ . image_backend
50+ . as_ref ( )
51+ . unwrap ( )
52+ . add_image (
53+ info_lines. map ( |s| format ! ( "{}{}" , center_pad, s) ) . collect ( ) ,
54+ custom_image,
55+ self . info . config . image_color_resolution ,
56+ )
57+ . chain_err ( || "Error while drawing image" ) ?,
58+ ) ;
59+ } else {
60+ let mut logo_lines = if let Some ( custom_ascii) = & self . info . config . ascii_input {
61+ AsciiArt :: new ( custom_ascii, & colors, !self . info . config . no_bold )
62+ } else {
63+ AsciiArt :: new (
64+ self . get_ascii ( ) ,
65+ & self . info . ascii_colors ,
66+ !self . info . config . no_bold ,
67+ )
68+ } ;
4769
48- loop {
49- match ( logo_lines. next ( ) , info_lines. next ( ) ) {
50- ( Some ( logo_line) , Some ( info_line) ) => {
51- buf. push_str ( & format ! ( "{}{}{:^}\n " , logo_line, center_pad, info_line) )
52- }
53- ( Some ( logo_line) , None ) => buf. push_str ( & format ! ( "{}\n " , logo_line) ) ,
54- ( None , Some ( info_line) ) => buf. push_str ( & format ! (
55- "{:<width$}{}{:^}\n " ,
56- "" ,
57- center_pad,
58- info_line,
59- width = logo_lines. width( )
60- ) ) ,
61- ( None , None ) => {
62- buf. push ( '\n' ) ;
63- break ;
70+ loop {
71+ match ( logo_lines. next ( ) , info_lines. next ( ) ) {
72+ ( Some ( logo_line) , Some ( info_line) ) => buf
73+ . push_str ( & format ! ( "{}{}{:^}\n " , logo_line, center_pad, info_line) ) ,
74+ ( Some ( logo_line) , None ) => buf. push_str ( & format ! ( "{}\n " , logo_line) ) ,
75+ ( None , Some ( info_line) ) => buf. push_str ( & format ! (
76+ "{:<width$}{}{:^}\n " ,
77+ "" ,
78+ center_pad,
79+ info_line,
80+ width = logo_lines. width( )
81+ ) ) ,
82+ ( None , None ) => {
83+ buf. push ( '\n' ) ;
84+ break ;
85+ }
86+ }
6487 }
6588 }
89+
90+ write ! ( self . writer, "{}" , buf) ?;
6691 }
6792 }
68-
69- write ! ( self . writer, "{}" , buf) ?;
70-
71- Ok ( ( ) )
72- }
73-
74- pub fn print_json ( & mut self ) -> Result < ( ) > {
75- write ! ( self . writer, "{}" , serde_json:: to_string_pretty( & self . info) . unwrap( ) ) ?;
7693 Ok ( ( ) )
7794 }
7895
0 commit comments