@@ -2,11 +2,7 @@ use home::home_dir;
2
2
use std:: { ffi:: OsStr , fs, io:: Error } ;
3
3
4
4
use colored:: * ;
5
- use inquire:: { validator:: Validation , Confirm , Editor , InquireError , Text } ;
6
-
7
- const PR_PREFIX : [ & str ; 9 ] = [
8
- "Add " , "Clean " , "Fix " , "Improve " , "Remove " , "Update " , "Rework " , "Ignore " , "Bump " ,
9
- ] ;
5
+ use inquire:: { validator:: Validation , Confirm , Editor , InquireError , Select , Text } ;
10
6
11
7
pub struct Config {
12
8
pub pr_name : String ,
@@ -99,29 +95,33 @@ impl Config {
99
95
false => Ok ( Validation :: Valid ) ,
100
96
} ;
101
97
102
- let pr_name_validator =
103
- |value : & str | match PR_PREFIX . iter ( ) . any ( |current| value. starts_with ( current) ) {
104
- true => Ok ( Validation :: Valid ) ,
105
- false => {
106
- let mut output = PR_PREFIX
107
- . iter ( )
108
- . map ( |current| format ! ( "- {}..." , current) )
109
- . collect :: < Vec < String > > ( )
110
- . join ( "\n " ) ;
111
- output =
112
- "The name must start with one of the following:\n " . to_owned ( ) + & output;
113
- Ok ( Validation :: Invalid ( output. into ( ) ) )
114
- }
115
- } ;
116
-
117
98
let linear_branch = Text :: new ( "Linear branch name:" )
118
99
. with_validator ( not_empty_validator)
119
100
. prompt ( ) ?;
120
101
121
- let mut pr_name = Text :: new ( "Pull request name:" )
122
- . with_validators ( & [ Box :: new ( not_empty_validator) , Box :: new ( pr_name_validator) ] )
102
+ let type_options: Vec < & str > = vec ! [
103
+ "feat" , "fix" , "refactor" , "perf" , "style" , "test" , "docs" , "build" , "ops" , "chore" ,
104
+ ] ;
105
+
106
+ let _type = Select :: new ( "Type:" , type_options) . prompt ( ) ?;
107
+
108
+ let scope = Text :: new ( "Scope (optional):" ) . prompt_skippable ( ) ?;
109
+
110
+ let name = Text :: new ( "Name:" )
111
+ . with_validators ( & [ Box :: new ( not_empty_validator) ] )
123
112
. prompt ( ) ?;
124
113
114
+ let mut pr_name = match scope {
115
+ Some ( scope) => {
116
+ if scope. is_empty ( ) {
117
+ format ! ( "{}: {}" , _type, name)
118
+ } else {
119
+ format ! ( "{}({}): {}" , _type, scope, name)
120
+ }
121
+ }
122
+ None => format ! ( "{}: {}" , _type, name) ,
123
+ } ;
124
+
125
125
let splited_branch = linear_branch. split ( '-' ) . collect :: < Vec < & str > > ( ) ;
126
126
if splited_branch. len ( ) > 1 {
127
127
pr_name = format ! (
@@ -132,20 +132,7 @@ impl Config {
132
132
)
133
133
}
134
134
135
- let prefix = pr_name. split ( ' ' ) . collect :: < Vec < & str > > ( ) [ 0 ] ;
136
- let branch_prefix = match prefix {
137
- "Add" => "feature" ,
138
- "Clean" => "rework" ,
139
- "Fix" => "fix" ,
140
- "Improve" => "rework" ,
141
- "Remove" => "feature" ,
142
- "Update" => "feature" ,
143
- "Rework" => "rework" ,
144
- "Ignore" => "feature" ,
145
- "Bump" => "core" ,
146
- _ => return Err ( InquireError :: Custom ( "Invalid input" . into ( ) ) ) ,
147
- } ;
148
- let branch = format ! ( "{}/{}" , branch_prefix, & linear_branch) ;
135
+ let branch = format ! ( "{}/{}" , _type, & linear_branch) ;
149
136
150
137
Ok ( Config { pr_name, branch } )
151
138
}
0 commit comments