patchy/commands/
help.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use colored::Colorize;

use crate::{
    commands::{
        gen_patch::GEN_PATCH_NAME_FLAG,
        pr_fetch::{PR_FETCH_BRANCH_NAME_FLAG, PR_FETCH_CHECKOUT_FLAG, PR_FETCH_REPO_NAME_FLAG},
        run::RUN_YES_FLAG,
    },
    flags::Flag,
    APP_NAME,
};

fn format_subcommand(command: &str, description: &str) -> String {
    let command = command.bright_yellow();
    format!("{command}\n    {}", format_description(description))
}

pub fn format_description(description: &str) -> String {
    format!("{} {description}", "ยป".bright_black())
}

pub static HELP_FLAG: Flag<'static> = Flag {
    short: "-h",
    long: "--help",
    description: "Print this message",
};

pub static VERBOSE_FLAG: Flag<'static> = Flag {
    short: "-V",
    long: "--verbose",
    description: "Increased logging information",
};

pub static VERSION_FLAG: Flag<'static> = Flag {
    short: "-v",
    long: "--version",
    description: "Get patchy version",
};

pub fn help(command: Option<&str>) -> anyhow::Result<()> {
    let author = "Nikita Revenco ".italic();
    let less_than = "<".bright_black().italic();
    let email = "pm@nikrev.com".italic();
    let greater_than = ">".bright_black().italic();
    let app_name = APP_NAME.bright_blue();
    let flags_label = "[<flags>]".bright_magenta();
    let command_str = "<command>".bright_yellow();
    let args = "[<args>]".bright_green();
    let version = env!("CARGO_PKG_VERSION");
    let init = format_subcommand("init", "Create example config file");
    let pr_fetch = format_subcommand(
        "pr-fetch",
        "Fetch pull request for a GitHub repository as a local branch",
    );
    let gen_patch = format_subcommand("gen-patch", "Generate a .patch file from commit hashes");
    let run = format_subcommand("run", &format!("Start {APP_NAME}"));
    let header = format!(
        "  {app_name} {version}
  {author}{less_than}{email}{greater_than}"
    );
    match command {
        Some(cmd_name @ "init") => {
            let this_command_name = format!("{app_name} {}", cmd_name.bright_yellow());

            let description = format_description("Create example config file");

            println!(
                "
{header}
        
  Usage:

    {this_command_name}
    {description}

  Flags:

    {HELP_FLAG}
",
            );
        }
        Some(cmd_name @ "run") => {
            let this_command_name = format!("{app_name} {}", cmd_name.bright_yellow());

            let description = format_description("Create example config file");

            println!(
                "
{header}
        
  Usage:

    {this_command_name}
    {description}

  Flags:

    {HELP_FLAG}

    {RUN_YES_FLAG}
",
            );
        }
        Some(cmd_name @ "gen-patch") => {
            let this_command_name = format!("{app_name} {}", cmd_name.bright_yellow());

            let description = format_description("Generate a .patch file from commit hashes");

            let example_1 = format!(
                "{}
    {}",
                "133cbaae83f710b793c98018cea697a04479bbe4".bright_green(),
                format_description("Generate a single .patch file from one commit hash")
            );

            let example_2 = format!(
                "{}
    {}",
                "133cbaae83f710b793c98018cea697a04479bbe4 9ad5aa637ccf363b5d6713f66d0c2830736c35a9 cc75a895f344cf2fe83eaf6d78dfb7aeac8b33a4".bright_green(),
                format_description("Generate several .patch files from several commit hashes")
            );

            let example_3 = format!(
                "{} {} {} {} {}
    {}",
                "133cbaae83f710b793c98018cea697a04479bbe4".bright_green(),
                "--patch-filename=some-patch".bright_magenta(),
                "9ad5aa637ccf363b5d6713f66d0c2830736c35a9".bright_green(),
                "--patch-filename=another-patch".bright_magenta(),
                "cc75a895f344cf2fe83eaf6d78dfb7aeac8b33a4".bright_green(),
                format_description(
                    "Generate several .patch files from several commit hashes and give 2 of them custom names"
                )
            );

            println!(
                "
{header}
        
  Usage:

    {this_command_name}
    {description}

  Examples:

    {this_command_name} {example_1}

    {this_command_name} {example_2}

    {this_command_name} {example_3}

  Flags:

    {GEN_PATCH_NAME_FLAG}

    {HELP_FLAG}
",
            );
        }
        Some(cmd_name @ "pr-fetch") => {
            let description = format_description("Fetch pull requests into a local branch");

            let example_1 = format!(
                "{}
    {}",
                "11745".bright_green(),
                format_description("Fetch a single pull request")
            );

            let example_2 = format!(
                "{}
    {}",
                "11745 10000 9191 600".bright_green(),
                format_description("Fetch several pull requests")
            );

            let example_3 = format!(
                "{} {} {} {} {}
    {}",
                "11745 10000".bright_green(),
                "--branch-name=some-pr".bright_magenta(),
                "9191".bright_green(),
                "--branch-name=another-pr".bright_magenta(),
                "600".bright_green(),
                format_description(
                    "Fetch several pull requests and choose custom branch names for the pull requests #10000 and #9191"
                )
            );

            let example_4 = format!(
                "{} {} {}
    {}",
                "--repo-name=helix-editor/helix".bright_magenta(),
                "11745 10000 9191 600".bright_green(),
                "--checkout".bright_magenta(),
                format_description("Fetch several pull requests, checkout the first one and use a custom github repo: https://github.com/helix-editor/helix")
            );

            let example_5 = format!(
                "{}
    {}",
                "11745 10000@be8f264327f6ae729a0b372ef01f6fde49a78310 9191 600@5d10fa5beb917a0dbe0ef8441d14b3d0dd15227b".bright_green(),
                format_description("Fetch several pull requests at a certain commit")
            );
            let this_command_name = format!("{app_name} {}", cmd_name.bright_yellow());

            println!(
                "
{header}
        
  Usage:

    {this_command_name} {args} {flags_label}
    {description}

  Examples:

    {this_command_name} {example_1}

    {this_command_name} {example_2}

    {this_command_name} {example_3}

    {this_command_name} {example_4}

    {this_command_name} {example_5}

  Flags:

    {PR_FETCH_BRANCH_NAME_FLAG}

    {PR_FETCH_CHECKOUT_FLAG}

    {PR_FETCH_REPO_NAME_FLAG}

    {HELP_FLAG}
",
            );
        }
        _ => {
            println!(
                "
{header}
        
  Usage:

    {app_name} {command_str} {args} {flags_label}

  Commands:

    {init} 

    {run}

    {gen_patch} 

    {pr_fetch} 

  Flags:

    {HELP_FLAG}

    {VERSION_FLAG}
"
            );
        }
    }

    Ok(())
}