@@ -3,10 +3,11 @@ use std::fmt::{self, Formatter, Display};
3
3
use ansi_term:: Colour :: { self , RGB } ;
4
4
use ansi_term:: Style ;
5
5
6
+ use formatter:: YDCVFormatter ;
7
+
6
8
#[ derive( Debug , Deserialize ) ]
7
9
struct Basic {
8
10
explains : Vec < String > ,
9
- phonetic : Option < String > ,
10
11
#[ serde( rename="uk-phonetic" ) ]
11
12
uk_phonetic : Option < String > ,
12
13
#[ serde( rename="us-phonetic" ) ]
@@ -28,74 +29,65 @@ pub struct Translation {
28
29
web : Option < Vec < Reference > > ,
29
30
}
30
31
31
- const HEADER_COLOR : Colour = RGB ( 26 , 159 , 160 ) ;
32
- const PHONETIC_COLOR : Colour = RGB ( 220 , 186 , 40 ) ;
33
- const REFERENCE_COLOR : Colour = RGB ( 138 , 88 , 164 ) ;
34
-
35
- impl Display for Reference {
36
- fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
37
- let mut content = String :: new ( ) ;
38
- let sub_str_count = self . contents . len ( ) ;
39
-
40
- for ( index, str) in self . contents . iter ( ) . enumerate ( ) {
41
- content. push_str ( str) ;
42
-
43
- if index < sub_str_count - 1 {
44
- content. push_str ( "; " )
32
+ impl YDCVFormatter for Translation {
33
+ fn translation_description ( & self ) -> String {
34
+ let yellow_star = Colour :: Yellow . paint ( "*" ) ;
35
+ let mut header_str = String :: new ( ) ;
36
+ if let Some ( ref translations) = self . translation {
37
+ header_str. push_str ( & format ! ( " {}\n \t {} " , Colour :: Purple . paint( "Translation:" ) , yellow_star) ) ;
38
+ for ( idx, value) in translations. iter ( ) . enumerate ( ) {
39
+ header_str. push_str ( & value) ;
40
+ if idx == translations. len ( ) - 1 {
41
+ header_str. push_str ( "\n " ) ;
42
+ } else {
43
+ header_str. push_str ( "; " ) ;
44
+ }
45
45
}
46
46
}
47
47
48
- write ! ( f, "\n \t * {}\n \t {}" , self . key, REFERENCE_COLOR . paint( content) )
49
- }
50
- }
51
-
52
- impl Display for Basic {
53
- fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
54
- let mut tmp_str = String :: new ( ) ;
55
-
56
- if let Some ( ref phone) = self . phonetic {
57
- tmp_str = format ! ( "\t [{}]\n " , PHONETIC_COLOR . paint( phone. clone( ) ) ) ;
58
- }
48
+ let mut phonetic_str = String :: new ( ) ;
49
+ if let Some ( ref phonetic_basic) = self . basic {
50
+ phonetic_str. push_str ( & format ! ( " {}\n " , Colour :: Purple . paint( "Word Explanation" ) ) ) ;
51
+ if let Some ( ref uk_phonetic) = phonetic_basic. uk_phonetic {
52
+ phonetic_str. push_str ( & format ! ( "\t UK: [{}]" , Style :: new( ) . underline( ) . paint( uk_phonetic. as_str( ) ) ) ) ;
53
+ if let Some ( ref us_phonetic) = phonetic_basic. us_phonetic {
54
+ phonetic_str. push_str ( & format ! ( " US: [{}]\n " , Style :: new( ) . underline( ) . paint( us_phonetic. as_str( ) ) ) ) ;
55
+ } ;
56
+ } else {
57
+ if let Some ( ref us_phonetic) = phonetic_basic. us_phonetic {
58
+ phonetic_str. push_str ( & format ! ( "\t US: [{}]\n " , Style :: new( ) . underline( ) . paint( us_phonetic. as_str( ) ) ) ) ;
59
+ }
60
+ }
59
61
60
- if let ( Some ( uk) , Some ( us) ) = ( self . uk_phonetic . clone ( ) , self . us_phonetic . clone ( ) ) {
61
- tmp_str = format ! ( "\t UK: [{}] US: [{}]\n " , PHONETIC_COLOR . paint( uk) , PHONETIC_COLOR . paint( us) ) ;
62
+ for explain in & phonetic_basic. explains {
63
+ phonetic_str. push_str ( & format ! ( "\t {} {}\n " , yellow_star, explain) ) ;
64
+ }
62
65
}
63
66
64
- write ! ( f, "{}\n {}:{}" ,
65
- tmp_str, HEADER_COLOR . paint( "Word Explanation" ) , self . explains
66
- . iter( )
67
- . fold( String :: new( ) , |mut acc, ref x| {
68
- acc. push_str( format!( "\n \t * {}" , x) . as_str( ) ) ;
69
- acc
70
- } ) )
71
- }
72
- }
73
-
74
- impl Display for Translation {
75
- fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
76
- let tmp_trans = match self . translation {
77
- Some ( ref trans) => format ! ( "\n {}\n \t * {}" , HEADER_COLOR . paint( "Translation:" ) , trans. first( ) . expect( "" ) ) ,
78
- None => String :: new ( ) ,
79
- } ;
80
-
81
- let tmp_basic = match self . basic {
82
- Some ( ref bsc) => format ! ( "\n {}\n " , bsc) ,
83
- None => String :: new ( ) ,
84
- } ;
85
-
86
- let tmp_web = match self . web {
87
- Some ( ref vecs) => {
88
- let content = vecs. iter ( )
89
- . fold ( String :: new ( ) , |mut acc, ref x| {
90
- acc. push_str ( format ! ( "{}" , x) . as_str ( ) ) ;
91
- acc
92
- } ) ;
93
- format ! ( "\n {}:{}" , HEADER_COLOR . paint( "Web Reference" ) , content)
67
+ let mut reference_str = String :: new ( ) ;
68
+ if let Some ( ref web_ref) = self . web {
69
+ reference_str. push_str ( & format ! ( " {}\n " , Colour :: Purple . paint( "Web Reference:" ) ) ) ;
70
+ for web in web_ref {
71
+ reference_str. push_str ( & format ! ( "\t {} {}\n \t " , yellow_star, web. key) ) ;
72
+ for ( idx, value) in web. contents . iter ( ) . enumerate ( ) {
73
+ reference_str. push_str ( & value) ;
74
+ if idx != web. contents . len ( ) - 1 {
75
+ reference_str. push_str ( "; " ) ;
76
+ } else {
77
+ reference_str. push_str ( "\n " ) ;
78
+ }
79
+ }
94
80
}
95
- None => String :: new ( ) ,
96
- } ;
81
+ }
97
82
98
- write ! ( f, "{}:{}{}{}" ,
99
- Style :: new( ) . underline( ) . paint( self . query. clone( ) ) , tmp_trans, tmp_basic, tmp_web)
83
+ if !header_str. is_empty ( ) {
84
+ header_str. push_str ( "\n " ) ;
85
+ }
86
+ header_str. push_str ( & phonetic_str) ;
87
+ if !reference_str. is_empty ( ) {
88
+ header_str. push_str ( "\n " ) ;
89
+ }
90
+ header_str. push_str ( & reference_str) ;
91
+ header_str
100
92
}
101
93
}
0 commit comments