git_helpe_rs/cli/
define.rs

1use clap::{builder::styling, Arg, ArgAction, Command};
2
3pub fn build_cli_commands() -> Command {
4    Command::new("")
5        .arg(Arg::new("config").required(false))
6        .subcommand(
7            Command::new("set-branch-prefix")
8                .arg(Arg::new("key").required(false))
9                .arg(Arg::new("prefix").required(false))
10                .about("Set prefix for checkout using clipboard contents")
11                .after_help(
12                    "Set branch prefix under given key so then you can use it when\n\
13                running \n\
14                git-helpe-rs bp <key> \n\
15                to checkout to a branch name based on contents of your clipboard\n\
16                ",
17                ),
18        )
19        .subcommand(
20            Command::new("set-branch-template")
21                .arg(Arg::new("key").required(true))
22                .arg(Arg::new("template").required(true))
23                .after_help(
24                    "Set branch template under given key so then you can use it when running \n\
25                    git-helpe-rs bf ...args_to_interpolate \n\
26                    branch template will be interpolated where {} occurs. \n\
27                    For example \n\
28                    git-helpe-rs set-branch-template fu feature-{}/utils-{} \n\
29                    git-helpe-rs bf fu 123 'new cli for stuff automation' \n\
30                    will checkout in the following way: \n\
31                    git checkout -b feature-123/utils-new-cli-for-stuff-automation \n\
32                    you try anything funky with args to interpolate on your own \n\
33                    ",
34                )
35                .about("Set template that can be used when switching branches"),
36        )
37        .subcommand(
38            Command::new("bp")
39                .arg(Arg::new("prefix").required(true))
40                .about("Check you out to a branch using your clipboard contents")
41                .after_help(
42                    "Will use contents of your clipboard to checkout you to\n\
43                    branch with prefix that's under the given key. \n\
44                     Valid clipboard contents when calling this command looks like this: \n\
45                    git checkout -b name-of-your-branch \n\
46                    If there is a valid content in your clipboard \n\
47                    after running following commands: \n\n\
48                    git-helpe-rs set-branch-prefix f 'feature/' \n\
49                    git-helpe-rs bp \n\n\
50                    you will be checked out like this \n\
51                    git checkout -b feature/name-of-your-branch
52                    ",
53                )
54                .add_copy_flag()
55                .add_dry_run_flag(),
56        )
57        .subcommand(
58            Command::new("bt")
59                .arg(Arg::new("interpolate-values").required(true).num_args(0..))
60                .arg(Arg::new("key").short('k').required(false).help(
61                    "Specify which template you want to use \n\
62                    if you omit this param default template will be used",
63                ))
64                .about("Check out to a branch based on template")
65                .add_copy_flag()
66                .add_dry_run_flag(),
67        )
68        // ========== COMMIT-RELATED COMMANDS ========== //
69        .subcommand(
70            Command::new("set-commit")
71                .arg(Arg::new("template").required(true).help(
72                    "Template has places to interpolate marked with {}, [], {b} \n\n\
73                    When you provide: \n\
74                    '[{}] - {}' \n\
75                    as your commit template, you will be able to run \n\
76                    commit command with following args: \n\
77                    git-helpe-rs commit 123 'fix gpu issues' \n\
78                    and this commit message will be added: \n\
79                    git commit -m \'[123] - fix gpu issues\' \n\n\
80                    If you provide [] in your template you will have to \n\
81                    set autocomplete value with set-auto-complete \n\
82                    with the same number of args as number of [] \n\
83                    in provided template \n\n\
84                    if you provide {b} in your template you should \n\
85                     - have some number in your branch \n\
86                     - run git-helpers-c with -b flag \n\
87                    ",
88                ))
89                .arg(Arg::new("key").short('k').required(false).help(
90                    "
91                You can provide key to have different templates at hand: \n\
92                git-helpe-rs commit 123 'fix gpu issues' -k sec \n\
93                will use template that was set with \n\
94                git-helpe-rs set-commit '{} - {}' -k sec \n\
95                Otherwise it will be saved as your default \n\
96                ",
97                ))
98                .about("Set template for commit formatting")
99                .after_help(
100                    "Sets commit format. \n\
101                    You can use {{}} for places to interpolate \n\
102                    and {[]} for places to autocomplete. \n\
103                    and {b} as places to autocomplete from number in branch. \n\
104                    ",
105                ),
106        )
107        .subcommand(
108            Command::new("set-auto-complete")
109                .about("set value that will be used to autocomplete commit template")
110                .arg(Arg::new("auto_complete_value").required(true).num_args(0..)),
111        )
112        .subcommand(
113            Command::new("c")
114                .arg(Arg::new("interpolate-values").required(true).num_args(0..))
115                .arg(
116                    Arg::new("auto-complete")
117                        .short('a')
118                        .action(ArgAction::SetTrue)
119                        .help("Should use autocomplete values for commit message"),
120                )
121                .arg(Arg::new("key").short('k').required(false).help(
122                    "
123                    Specify which template you want to use \n\
124                    if you omit this param default template will be used \n\
125                    ",
126                ))
127                .arg(
128                    Arg::new("infer-number-from-branch")
129                        .short('b')
130                        .action(ArgAction::SetTrue)
131                        .help(
132                            "Will try to catch any number from branch name \n\
133                    and use it as a value for {b} in your template \n\
134                    if your template has no {b} it will panic \n\
135                    ",
136                        ),
137                )
138                .about("Commit using one of templates")
139                .add_copy_flag()
140                .add_dry_run_flag(),
141        )
142        // ============== OTHERS ============== //
143        .subcommand(Command::new("show").about("Show current config in plain JSON"))
144        .subcommand(
145            Command::new("set-clipboard-command")
146                .about("[WIP] Set pair of copy&paste command which will be used")
147                .arg(Arg::new("copy-paste-pair").num_args(0..3).help(
148                    "On default it's written to use pbcopy and pbpaste as \
149                command for setting and taking value from clipboard, but if you want \
150                you can use command of your own choice.
151                ",
152                )),
153        )
154        .subcommand(
155            Command::new("generate-autocompletion-script")
156                .about("Generates bash completion script")
157                .arg(
158                    Arg::new("output-directory")
159                        .required(true)
160                        .help("Directory in which git-helpe-rs.bash will be placed"),
161                ),
162        )
163        // ============= COLORS ============== //
164        .color(clap::ColorChoice::Always)
165        .get_styles()
166}
167
168trait AddCopyFlag {
169    fn add_copy_flag(self) -> Self;
170}
171
172impl AddCopyFlag for Command {
173    fn add_copy_flag(self) -> Self {
174        self.arg(
175            Arg::new("copy-flag")
176                .short('c')
177                .action(ArgAction::SetTrue)
178                .help(
179                    "instead of executing command pass it to clipboard \n \n
180        you can always configure command used for passing to clipboard",
181                ),
182        )
183    }
184}
185
186trait AddDryRunFlag {
187    fn add_dry_run_flag(self) -> Self;
188}
189
190impl AddDryRunFlag for Command {
191    fn add_dry_run_flag(self) -> Self {
192        self.arg(
193            Arg::new("dry-run")
194                .long("dr")
195                .action(ArgAction::SetTrue)
196                .help("instead of executing command see what will be ran \n \n"),
197        )
198    }
199}
200
201trait Styles {
202    fn get_styles(self) -> Self;
203}
204impl Styles for Command {
205    fn get_styles(self) -> Self {
206        let styles = styling::Styles::styled()
207            .header(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
208            .usage(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
209            .literal(styling::AnsiColor::Blue.on_default() | styling::Effects::BOLD)
210            .placeholder(styling::AnsiColor::Cyan.on_default());
211
212        self.styles(styles)
213    }
214}