File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ use clap:: Parser as _;
2+ use std:: ffi:: OsString ;
13use std:: path:: Path ;
24
3- use clap:: Parser as _;
45use rustickers:: cli;
56use rustickers:: storage:: paths:: AppPaths ;
67
78fn main ( ) {
89 let app_paths = AppPaths :: new ( ) . expect ( "App paths should initialize" ) ;
910
10- // when args length is 1 and it is a valid file/url then run view command directly
11- let cli = if std:: env:: args ( ) . len ( ) == 2 {
12- let arg = std:: env:: args ( ) . nth ( 1 ) . unwrap ( ) ;
13- if rustickers:: utils:: url:: is_url ( & arg)
14- || Path :: new ( & arg) . is_dir ( )
15- || Path :: new ( & arg) . is_file ( )
16- {
17- cli:: view:: run ( arg) . unwrap_or_else ( |err| {
18- eprintln ! ( "error: {err:#}" ) ;
19- std:: process:: exit ( 1 ) ;
20- } ) ;
21- return ;
22- } else {
23- cli:: Cli :: parse ( )
24- }
25- } else {
26- cli:: Cli :: parse ( )
27- } ;
11+ let cli = cli:: Cli :: parse_from ( normalize_argv_for_view_alias ( ) ) ;
2812
2913 if let Err ( err) = cli:: run ( cli, & app_paths) {
3014 eprintln ! ( "error: {err:#}" ) ;
3115 std:: process:: exit ( 1 ) ;
3216 }
3317}
18+
19+ fn normalize_argv_for_view_alias ( ) -> Vec < OsString > {
20+ let args: Vec < OsString > = std:: env:: args_os ( ) . collect ( ) ;
21+
22+ if args. len ( ) != 2 {
23+ return args;
24+ }
25+
26+ let source = args[ 1 ] . clone ( ) ;
27+ let source_path = Path :: new ( & source) ;
28+ let is_existing_path = source_path. is_file ( ) || source_path. is_dir ( ) ;
29+ let is_url = source. to_str ( ) . is_some_and ( rustickers:: utils:: url:: is_url) ;
30+
31+ if is_existing_path || is_url {
32+ vec ! [ args[ 0 ] . clone( ) , OsString :: from( "view" ) , source]
33+ } else {
34+ args
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments