gitu/cli.rs
1use std::io;
2use std::path::PathBuf;
3
4use clap::{CommandFactory, Parser, Subcommand};
5use clap_complete::Shell;
6
7#[derive(Default, Debug, Parser)]
8#[command(name = "gitu")]
9#[command(flatten_help = true)]
10pub struct Args {
11 #[command(subcommand)]
12 pub command: Option<Commands>,
13
14 /// Send keys on startup (eg: `gitu -k ll`).
15 /// It is possible to send:
16 /// - single char-keys: a, b, c, ...
17 /// - special keys: <backspace>, <enter>, <up>, <tab>, <delete>, <esc>, ...
18 /// - modifiers: <ctrl+a>, <ctrl+shift+alt+a>, <shift+delete>
19 #[clap(short, long, verbatim_doc_comment)]
20 pub keys: Option<String>,
21
22 /// Print one frame and exit. Useful for debugging.
23 #[clap(long, action)]
24 pub print: bool,
25
26 /// Read a patch on stdin and browse it, for use as git's pager:
27 /// GIT_PAGER='gitu --pager' git show
28 #[clap(long, action, verbatim_doc_comment)]
29 pub pager: bool,
30
31 /// With --pager, print what fits on one screen and exit, as less's --quit-if-one-screen.
32 #[clap(long, action, requires = "pager")]
33 pub quit_if_one_screen: bool,
34
35 /// Enable logging to 'gitu.log'
36 #[clap(long, action)]
37 pub log: bool,
38
39 #[clap(long, action)]
40 /// Print version
41 pub version: bool,
42
43 /// Config file to use
44 #[clap(short, long)]
45 pub config: Option<PathBuf>,
46}
47
48#[derive(Debug, Subcommand)]
49pub enum Commands {
50 Show {
51 reference: String,
52 },
53 /// Interactively rebase onto <upstream>, editing the instruction list in Gitu.
54 Rebase {
55 upstream: String,
56 },
57 /// Edit a `git rebase -i` instruction list, for use as GIT_SEQUENCE_EDITOR:
58 /// GIT_SEQUENCE_EDITOR='gitu sequence-editor' git rebase -i <upstream>
59 #[clap(verbatim_doc_comment)]
60 SequenceEditor {
61 file: PathBuf,
62 },
63 Blame {
64 file: String,
65 #[clap(short, long)]
66 rev: Option<String>,
67 },
68 /// Print a shell completion script to stdout.
69 ///
70 /// Example (bash): `gitu completion bash > ~/.local/share/bash-completion/completions/gitu`
71 Completion {
72 /// The shell to generate a completion script for
73 shell: Shell,
74 },
75}
76
77/// Write a shell completion script for `gitu` to the given writer.
78pub fn completions(shell: Shell, out: &mut impl io::Write) {
79 let mut cmd = Args::command();
80 let bin_name = cmd.get_name().to_string();
81 clap_complete::generate(shell, &mut cmd, bin_name, out);
82}