1
1
#[ macro_use]
2
2
extern crate clap;
3
3
4
- #[ cfg( target_os = "linux" ) ]
5
- use image_backends:: ImageBackend ;
4
+ #[ cfg( unix) ]
6
5
use {
7
6
ascii_art:: AsciiArt ,
8
7
commit_info:: CommitInfo ,
9
8
error:: Error ,
10
9
exit_codes:: ExitCode ,
11
- info:: { Info , InfoFieldOn , InfoFields } ,
10
+ info:: Info ,
12
11
language:: Language ,
13
12
process:: { Command , Stdio } ,
14
13
std:: { convert:: From , env, process, result, str:: FromStr } ,
15
14
strum:: IntoEnumIterator ,
16
15
} ;
17
16
18
- mod app ;
17
+ mod clap_app ;
19
18
mod ascii_art;
20
19
mod commit_info;
21
20
mod error;
@@ -28,90 +27,38 @@ mod options;
28
27
29
28
type Result < T > = result:: Result < T , Error > ;
30
29
31
- fn run ( ) -> Result < ( ) > {
32
- #[ cfg( target_os = "windows" ) ]
33
- let enabled = ansi_term:: enable_ansi_support ( ) . is_ok ( ) ;
34
-
35
- #[ cfg( not( target_os = "windows" ) ) ]
36
- let enabled = true ;
37
-
38
- if enabled {
39
- colored:: control:: set_override ( true ) ;
40
- }
30
+ /// Returns `Err(..)` upon fatal errors. Otherwise, returns `Ok(true)` on full success and
31
+ /// `Ok(false)` if any intermediate errors occurred (were printed).
32
+ fn run ( ) -> Result < bool > {
33
+ #[ cfg( windows) ]
34
+ let _ = ansi_term:: enable_ansi_support ( ) ;
41
35
42
36
if !is_git_installed ( ) {
43
37
return Err ( Error :: GitNotInstalled ) ;
44
38
}
45
39
46
- let matches = app :: build_app ( ) . get_matches_from ( env:: args_os ( ) ) ;
40
+ let matches = clap_app :: build_app ( ) . get_matches_from ( env:: args_os ( ) ) ;
47
41
48
42
if matches. is_present ( "languages" ) {
49
- let iterator = Language :: iter ( ) . filter ( |x| * x != Language :: Unknown ) ;
50
-
51
- for l in iterator {
52
- println ! ( "{}" , l) ;
53
- }
54
- std:: process:: exit ( 0 ) ;
43
+ return list_languages ( ) ;
55
44
}
56
45
57
- let mut disabled_fields = InfoFieldOn {
58
- ..Default :: default ( )
59
- } ;
60
-
61
46
let fields_to_hide: Vec < String > = if let Some ( values) = matches. values_of ( "disable-fields" ) {
62
47
values. map ( String :: from) . collect ( )
63
48
} else {
64
49
Vec :: new ( )
65
50
} ;
66
51
67
- for field in fields_to_hide. iter ( ) {
68
- let item = InfoFields :: from_str ( field. to_lowercase ( ) . as_str ( ) )
69
- . unwrap_or ( InfoFields :: UnrecognizedField ) ;
70
-
71
- match item {
72
- InfoFields :: GitInfo => disabled_fields. git_info = true ,
73
- InfoFields :: Project => disabled_fields. project = true ,
74
- InfoFields :: HEAD => disabled_fields. head = true ,
75
- InfoFields :: Version => disabled_fields. version = true ,
76
- InfoFields :: Created => disabled_fields. created = true ,
77
- InfoFields :: Languages => disabled_fields. languages = true ,
78
- InfoFields :: Authors => disabled_fields. authors = true ,
79
- InfoFields :: LastChange => disabled_fields. last_change = true ,
80
- InfoFields :: Repo => disabled_fields. repo = true ,
81
- InfoFields :: Pending => disabled_fields. pending = true ,
82
- InfoFields :: Commits => disabled_fields. commits = true ,
83
- InfoFields :: LinesOfCode => disabled_fields. lines_of_code = true ,
84
- InfoFields :: Size => disabled_fields. size = true ,
85
- InfoFields :: License => disabled_fields. license = true ,
86
- _ => ( ) ,
87
- }
88
- }
89
-
90
52
let image = if let Some ( image_path) = matches. value_of ( "image" ) {
91
53
Some ( image:: open ( image_path) . map_err ( |_| Error :: ImageLoadError ) ?)
92
54
} else {
93
55
None
94
56
} ;
95
57
96
- let image_backend = if image. is_some ( ) {
97
- if let Some ( backend_name) = matches. value_of ( "image-backend" ) {
98
- #[ cfg( target_os = "linux" ) ]
99
- let backend =
100
- Some ( match backend_name {
101
- "kitty" => Box :: new ( image_backends:: kitty:: KittyBackend :: new ( ) )
102
- as Box < dyn ImageBackend > ,
103
- "sixel" => Box :: new ( image_backends:: sixel:: SixelBackend :: new ( ) )
104
- as Box < dyn ImageBackend > ,
105
- _ => unreachable ! ( ) ,
106
- } ) ;
107
- #[ cfg( not( target_os = "linux" ) ) ]
108
- let backend = None ;
109
- backend
110
- } else {
111
- crate :: image_backends:: get_best_backend ( )
112
- }
58
+ let image_backend = if let Some ( backend_name) = matches. value_of ( "image-backend" ) {
59
+ image_backends:: get_image_backend ( & image, backend_name)
113
60
} else {
114
- None
61
+ image_backends :: get_best_backend ( )
115
62
} ;
116
63
117
64
let config = options:: Options {
@@ -126,9 +73,9 @@ fn run() -> Result<()> {
126
73
} else {
127
74
Vec :: new ( )
128
75
} ,
129
- disabled_fields,
76
+ disabled_fields : info :: get_disabled_fields ( fields_to_hide ) ? ,
130
77
no_bold : !matches. is_present ( "no-bold" ) ,
131
- image : image ,
78
+ image,
132
79
image_backend,
133
80
no_merges : matches. is_present ( "no-merge-commits" ) ,
134
81
no_color_blocks : matches. is_present ( "no-color-blocks" ) ,
@@ -147,15 +94,28 @@ fn run() -> Result<()> {
147
94
let info = Info :: new ( config) ?;
148
95
149
96
print ! ( "{}" , info) ;
150
- Ok ( ( ) )
97
+ Ok ( true )
98
+ }
99
+
100
+ pub fn list_languages ( ) -> Result < bool > {
101
+ let iterator = Language :: iter ( ) . filter ( |x| * x != Language :: Unknown ) ;
102
+
103
+ for l in iterator {
104
+ println ! ( "{}" , l) ;
105
+ }
106
+
107
+ Ok ( true )
151
108
}
152
109
153
110
fn main ( ) {
154
111
let result = run ( ) ;
155
112
match result {
156
- Ok ( _ ) => {
113
+ Ok ( true ) => {
157
114
process:: exit ( ExitCode :: Success . into ( ) ) ;
158
115
}
116
+ Ok ( false ) => {
117
+ process:: exit ( ExitCode :: GeneralError . into ( ) ) ;
118
+ }
159
119
Err ( _) => {
160
120
process:: exit ( ExitCode :: GeneralError . into ( ) ) ;
161
121
}
0 commit comments