patchy/commands/
help.rs

1use colored::Colorize as _;
2
3use crate::{
4    commands::{
5        gen_patch::GEN_PATCH_NAME_FLAG,
6        pr_fetch::{PR_FETCH_BRANCH_NAME_FLAG, PR_FETCH_CHECKOUT_FLAG, PR_FETCH_REPO_NAME_FLAG},
7        run::RUN_YES_FLAG,
8    },
9    flags::Flag,
10    APP_NAME,
11};
12
13fn format_subcommand(command: &str, description: &str) -> String {
14    let command = command.bright_yellow();
15    format!("{command}\n    {}", format_description(description))
16}
17
18pub fn format_description(description: &str) -> String {
19    format!("{} {description}", "ยป".bright_black())
20}
21
22pub static HELP_FLAG: Flag<'static> = Flag {
23    short: "-h",
24    long: "--help",
25    description: "Print this message",
26};
27
28pub static VERBOSE_FLAG: Flag<'static> = Flag {
29    short: "-V",
30    long: "--verbose",
31    description: "Increased logging information",
32};
33
34pub static VERSION_FLAG: Flag<'static> = Flag {
35    short: "-v",
36    long: "--version",
37    description: "Get patchy version",
38};
39
40pub fn help(command: Option<&str>) -> anyhow::Result<()> {
41    let author = "Nikita Revenco ".italic();
42    let less_than = "<".bright_black().italic();
43    let email = "pm@nikrev.com".italic();
44    let greater_than = ">".bright_black().italic();
45    let app_name = APP_NAME.bright_blue();
46    let flags_label = "[<flags>]".bright_magenta();
47    let command_str = "<command>".bright_yellow();
48    let args = "[<args>]".bright_green();
49    let version = env!("CARGO_PKG_VERSION");
50    let init = format_subcommand("init", "Create example config file");
51    let pr_fetch = format_subcommand(
52        "pr-fetch",
53        "Fetch pull request for a GitHub repository as a local branch",
54    );
55    let branch_fetch = format_subcommand(
56        "branch-fetch",
57        "Fetch branches for a GitHub repository as a local branch",
58    );
59    let gen_patch = format_subcommand("gen-patch", "Generate a .patch file from commit hashes");
60    let run = format_subcommand("run", &format!("Start {APP_NAME}"));
61    let header = format!(
62        "  {app_name} {version}
63  {author}{less_than}{email}{greater_than}"
64    );
65    match command {
66        Some(cmd_name @ "init") => {
67            let this_command_name = format!("{app_name} {}", cmd_name.bright_yellow());
68
69            let description = format_description("Create example config file");
70
71            println!(
72                "
73{header}
74        
75  Usage:
76
77    {this_command_name}
78    {description}
79
80  Flags:
81
82    {HELP_FLAG}
83",
84            );
85        }
86        Some(cmd_name @ "run") => {
87            let this_command_name = format!("{app_name} {}", cmd_name.bright_yellow());
88
89            let description = format_description("Create example config file");
90
91            println!(
92                "
93{header}
94        
95  Usage:
96
97    {this_command_name}
98    {description}
99
100  Flags:
101
102    {HELP_FLAG}
103
104    {RUN_YES_FLAG}
105",
106            );
107        }
108        Some(cmd_name @ "gen-patch") => {
109            let this_command_name = format!("{app_name} {}", cmd_name.bright_yellow());
110
111            let description = format_description("Generate a .patch file from commit hashes");
112
113            let example_1 = format!(
114                "{}
115    {}",
116                "133cbaae83f710b793c98018cea697a04479bbe4".bright_green(),
117                format_description("Generate a single .patch file from one commit hash")
118            );
119
120            let example_2 = format!(
121                "{}
122    {}",
123                "133cbaae83f710b793c98018cea697a04479bbe4 9ad5aa637ccf363b5d6713f66d0c2830736c35a9 cc75a895f344cf2fe83eaf6d78dfb7aeac8b33a4".bright_green(),
124                format_description("Generate several .patch files from several commit hashes")
125            );
126
127            let example_3 = format!(
128                "{} {} {} {} {}
129    {}",
130                "133cbaae83f710b793c98018cea697a04479bbe4".bright_green(),
131                "--patch-filename=some-patch".bright_magenta(),
132                "9ad5aa637ccf363b5d6713f66d0c2830736c35a9".bright_green(),
133                "--patch-filename=another-patch".bright_magenta(),
134                "cc75a895f344cf2fe83eaf6d78dfb7aeac8b33a4".bright_green(),
135                format_description(
136                    "Generate several .patch files from several commit hashes and give 2 of them custom names"
137                )
138            );
139
140            println!(
141                "
142{header}
143        
144  Usage:
145
146    {this_command_name}
147    {description}
148
149  Examples:
150
151    {this_command_name} {example_1}
152
153    {this_command_name} {example_2}
154
155    {this_command_name} {example_3}
156
157  Flags:
158
159    {GEN_PATCH_NAME_FLAG}
160
161    {HELP_FLAG}
162",
163            );
164        }
165        Some(cmd_name @ "branch-fetch") => {
166            let description = format_description("Fetch remote branches into a local branch");
167
168            let example_1 = format!(
169                "{}
170    {}",
171                "helix-editor/helix/master".bright_green(),
172                format_description("Fetch a single branch")
173            );
174            let example_2 = format!(
175                "{}
176    {}",
177                "'helix-editor/helix/master@6049f20'".bright_green(),
178                format_description("Fetch a single branch at a certain commit")
179            );
180
181            let this_command_name = format!("{app_name} {}", cmd_name.bright_yellow());
182
183            println!(
184                "
185{header}
186        
187  Usage:
188
189    {this_command_name} {args} {flags_label}
190    {description}
191
192  Examples:
193
194    {this_command_name} {example_1}
195
196    {this_command_name} {example_2}
197",
198            );
199        }
200        Some(cmd_name @ "pr-fetch") => {
201            let description = format_description("Fetch pull requests into a local branch");
202
203            let example_1 = format!(
204                "{}
205    {}",
206                "11745".bright_green(),
207                format_description("Fetch a single pull request")
208            );
209
210            let example_2 = format!(
211                "{}
212    {}",
213                "11745 10000 9191 600".bright_green(),
214                format_description("Fetch several pull requests")
215            );
216
217            let example_3 = format!(
218                "{} {} {} {} {}
219    {}",
220                "11745 10000".bright_green(),
221                "--branch-name=some-pr".bright_magenta(),
222                "9191".bright_green(),
223                "--branch-name=another-pr".bright_magenta(),
224                "600".bright_green(),
225                format_description(
226                    "Fetch several pull requests and choose custom branch names for the pull requests #10000 and #9191"
227                )
228            );
229
230            let example_4 = format!(
231                "{} {} {}
232    {}",
233                "--repo-name=helix-editor/helix".bright_magenta(),
234                "11745 10000 9191 600".bright_green(),
235                "--checkout".bright_magenta(),
236                format_description("Fetch several pull requests, checkout the first one and use a custom github repo: https://github.com/helix-editor/helix")
237            );
238
239            let example_5 = format!(
240                "{}
241    {}",
242                "11745 10000@be8f264327f6ae729a0b372ef01f6fde49a78310 9191 600@5d10fa5beb917a0dbe0ef8441d14b3d0dd15227b".bright_green(),
243                format_description("Fetch several pull requests at a certain commit")
244            );
245            let this_command_name = format!("{app_name} {}", cmd_name.bright_yellow());
246
247            println!(
248                "
249{header}
250        
251  Usage:
252
253    {this_command_name} {args} {flags_label}
254    {description}
255
256  Examples:
257
258    {this_command_name} {example_1}
259
260    {this_command_name} {example_2}
261
262    {this_command_name} {example_3}
263
264    {this_command_name} {example_4}
265
266    {this_command_name} {example_5}
267
268  Flags:
269
270    {PR_FETCH_BRANCH_NAME_FLAG}
271
272    {PR_FETCH_CHECKOUT_FLAG}
273
274    {PR_FETCH_REPO_NAME_FLAG}
275
276    {HELP_FLAG}
277",
278            );
279        }
280        _ => {
281            println!(
282                "
283{header}
284        
285  Usage:
286
287    {app_name} {command_str} {args} {flags_label}
288
289  Commands:
290
291    {init} 
292
293    {run}
294
295    {gen_patch} 
296
297    {pr_fetch} 
298
299    {branch_fetch} 
300
301  Flags:
302
303    {HELP_FLAG}
304
305    {VERSION_FLAG}
306"
307            );
308        }
309    }
310
311    Ok(())
312}