1
1
use std:: { borrow:: Cow , io:: BufWriter , path:: PathBuf } ;
2
2
3
- use anyhow:: Result ;
3
+ use anyhow:: { Context , Result } ;
4
4
use md5:: { Digest , Md5 } ;
5
5
use yazi_shared:: Xdg ;
6
6
7
7
pub ( crate ) struct Package {
8
- pub ( crate ) repo : String ,
8
+ pub ( crate ) host : String ,
9
+ pub ( crate ) owner : String ,
10
+ pub ( crate ) repo_name : String ,
9
11
pub ( crate ) child : String ,
10
12
pub ( crate ) rev : String ,
11
13
pub ( super ) is_flavor : bool ,
12
14
}
13
15
14
16
impl Package {
15
- pub ( super ) fn new ( url : & str , rev : Option < & str > ) -> Self {
17
+ pub ( super ) fn new ( url : & str , rev : Option < & str > ) -> Result < Self > {
16
18
let mut parts = url. splitn ( 2 , ':' ) ;
17
19
18
- let mut repo = parts. next ( ) . unwrap_or_default ( ) . to_owned ( ) ;
20
+ let mut repo_part = parts. next ( ) . unwrap_or_default ( ) . to_owned ( ) ;
19
21
let child = if let Some ( s) = parts. next ( ) {
20
22
format ! ( "{s}.yazi" )
21
23
} else {
22
- repo . push_str ( ".yazi" ) ;
24
+ repo_part . push_str ( ".yazi" ) ;
23
25
String :: new ( )
24
26
} ;
25
27
26
- Self { repo, child, rev : rev. unwrap_or_default ( ) . to_owned ( ) , is_flavor : false }
28
+ let mut repo = repo_part. rsplit ( '/' ) ;
29
+ let repo_name = repo. next ( ) . context ( "failed to get repo name" ) ?. to_owned ( ) ;
30
+ let owner = repo. next ( ) . context ( "failed to get repo owner" ) ?. to_owned ( ) ;
31
+ let host = repo. next ( ) . unwrap_or ( "github.com" ) . to_owned ( ) ;
32
+
33
+ Ok ( Self {
34
+ repo_name,
35
+ owner,
36
+ host,
37
+ child,
38
+ rev : rev. unwrap_or_default ( ) . to_owned ( ) ,
39
+ is_flavor : false ,
40
+ } )
27
41
}
28
42
29
43
#[ inline]
30
44
pub ( super ) fn use_ ( & self ) -> Cow < str > {
31
45
if self . child . is_empty ( ) {
32
- self . repo . trim_end_matches ( ".yazi" ) . into ( )
46
+ format ! ( "{}/{}/{}" , self . host , self . owner , self . repo_name . trim_end_matches( ".yazi" ) ) . into ( )
33
47
} else {
34
- format ! ( "{}:{}" , self . repo, self . child. trim_end_matches( ".yazi" ) ) . into ( )
48
+ format ! (
49
+ "{}/{}/{}:{}" ,
50
+ self . host,
51
+ self . owner,
52
+ self . repo_name,
53
+ self . child. trim_end_matches( ".yazi" )
54
+ )
55
+ . into ( )
35
56
}
36
57
}
37
58
38
59
#[ inline]
39
- pub ( super ) fn name ( & self ) -> Option < & str > {
40
- let s = if self . child . is_empty ( ) {
41
- self . repo . split ( '/' ) . last ( ) . filter ( |s| !s. is_empty ( ) )
42
- } else {
43
- Some ( self . child . as_str ( ) )
44
- } ;
45
-
46
- s. filter ( |s| s. bytes ( ) . all ( |b| matches ! ( b, b'0' ..=b'9' | b'a' ..=b'z' | b'-' | b'.' ) ) )
60
+ pub ( super ) fn name ( & self ) -> & str {
61
+ if self . child . is_empty ( ) { self . repo_name . as_str ( ) } else { self . child . as_str ( ) }
47
62
}
48
63
49
64
#[ inline]
@@ -55,8 +70,7 @@ impl Package {
55
70
56
71
#[ inline]
57
72
pub ( super ) fn remote ( & self ) -> String {
58
- // Support more Git hosting services in the future
59
- format ! ( "https://github.com/{}.git" , self . repo)
73
+ format ! ( "https://{}/{}/{}.git" , self . host, self . owner, self . repo_name)
60
74
}
61
75
62
76
pub ( super ) fn header ( & self , s : & str ) -> Result < ( ) > {
@@ -68,7 +82,7 @@ impl Package {
68
82
SetAttributes ( Attribute :: Reverse . into( ) ) ,
69
83
SetAttributes ( Attribute :: Bold . into( ) ) ,
70
84
Print ( " " ) ,
71
- Print ( s. replacen( "{name}" , self . name( ) . unwrap_or_default ( ) , 1 ) ) ,
85
+ Print ( s. replacen( "{name}" , self . name( ) , 1 ) ) ,
72
86
Print ( " " ) ,
73
87
SetAttributes ( Attribute :: Reset . into( ) ) ,
74
88
Print ( "\n \n " ) ,
0 commit comments